Search in sources :

Example 1 with SerialPortConnection

use of net.sf.mbus4j.SerialPortConnection in project ma-modules-public by infiniteautomation.

the class MBusConnectionSerializer method serialize.

/* (non-Javadoc)
	 * @see com.fasterxml.jackson.databind.JsonSerializer#serialize(java.lang.Object, com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.databind.SerializerProvider)
	 */
@Override
public void serialize(Connection value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
    gen.writeStartObject();
    gen.writeNumberField("bitPerSecond", value.getBitPerSecond());
    gen.writeNumberField("responseTimeoutOffset", value.getResponseTimeOutOffset());
    if (value instanceof TcpIpConnection) {
        TcpIpConnection conn = (TcpIpConnection) value;
        gen.writeStringField("host", conn.getHost());
        gen.writeNumberField("port", conn.getPort());
        gen.writeStringField("modelType", "mbusTcpIp");
    } else if (value instanceof SerialPortConnection) {
        SerialPortConnection conn = (SerialPortConnection) value;
        gen.writeStringField("portName", conn.getPortName());
        gen.writeStringField("modelType", "mbusSerial");
    }
    gen.writeEndObject();
}
Also used : TcpIpConnection(net.sf.mbus4j.TcpIpConnection) SerialPortConnection(net.sf.mbus4j.SerialPortConnection)

Example 2 with SerialPortConnection

use of net.sf.mbus4j.SerialPortConnection in project ma-modules-public by infiniteautomation.

the class MBusDataSourceVO method jsonWrite.

/* (non-Javadoc)
     * @see com.serotonin.m2m2.vo.dataSource.DataSourceVO#jsonWrite(com.serotonin.json.ObjectWriter)
     */
@Override
public void jsonWrite(ObjectWriter writer) throws IOException, JsonException {
    super.jsonWrite(writer);
    writeUpdatePeriodType(writer, this.updatePeriodType);
    if (connection instanceof SerialPortConnection) {
        writer.writeEntry("connectionType", "mbusSerial");
        SerialPortConnection conn = (SerialPortConnection) connection;
        writer.writeEntry("bitPerSecond", conn.getBitPerSecond());
        writer.writeEntry("responseTimeoutOffset", conn.getResponseTimeOutOffset());
        writer.writeEntry("portName", conn.getPortName());
    } else if (connection instanceof TcpIpConnection) {
        writer.writeEntry("connectionType", "mbusTcpIp");
        TcpIpConnection conn = (TcpIpConnection) connection;
        writer.writeEntry("bitPerSecond", conn.getBitPerSecond());
        writer.writeEntry("responseTimeoutOffset", conn.getResponseTimeOutOffset());
        writer.writeEntry("host", conn.getHost());
        writer.writeEntry("port", conn.getPort());
    }
}
Also used : TcpIpConnection(net.sf.mbus4j.TcpIpConnection) SerialPortConnection(net.sf.mbus4j.SerialPortConnection)

Example 3 with SerialPortConnection

use of net.sf.mbus4j.SerialPortConnection in project ma-modules-public by infiniteautomation.

the class MBusDataSourceVO method readObject.

private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException {
    int ver = in.readInt();
    // Switch on the version of the class so that version changes can be elegantly handled.
    if ((ver == 2) || (ver == 1)) {
        String connectionType = in.readUTF();
        switch(connectionType) {
            case "SERIAL_DIRECT":
                connection = new SerialPortConnection(SerializationHelper.readSafeUTF(in));
                break;
            default:
            case "SERIAL_AT_MODEM":
                // TODO modem stuff goes here
                throw new RuntimeException("AT Modem is not supported");
        }
        updatePeriodType = in.readInt();
        updatePeriods = in.readInt();
        connection.setBitPerSecond(in.readInt());
        // flowControlIn =
        in.readInt();
        // flowControlOut =
        in.readInt();
        // dataBits =
        in.readInt();
        // stopBits =
        in.readInt();
        // parity =
        in.readInt();
    } else if (ver == 3) {
        String connectionType = in.readUTF();
        switch(connectionType) {
            case "SERIAL_DIRECT":
                connection = new SerialPortConnection(SerializationHelper.readSafeUTF(in));
                break;
            default:
            case "SERIAL_AT_MODEM":
                // TODO modem stuff goes here
                throw new RuntimeException("AT Modem is not supported");
        }
        updatePeriodType = in.readInt();
        updatePeriods = in.readInt();
        connection.setBitPerSecond(in.readInt());
        // flowControlIn =
        in.readInt();
        // flowControlOut =
        in.readInt();
        // dataBits =
        in.readInt();
        // stopBits =
        in.readInt();
        // parity =
        in.readInt();
        connection.setResponseTimeOutOffset(in.readInt());
    } else if (ver == 4) {
        readObjectVer4(in);
    }
}
Also used : SerialPortConnection(net.sf.mbus4j.SerialPortConnection)

Example 4 with SerialPortConnection

use of net.sf.mbus4j.SerialPortConnection in project ma-modules-public by infiniteautomation.

the class MBusDataSourceVO method jsonRead.

/* (non-Javadoc)
     * @see com.serotonin.m2m2.vo.dataSource.DataSourceVO#jsonRead(com.serotonin.json.JsonReader, com.serotonin.json.type.JsonObject)
     */
@Override
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException {
    super.jsonRead(reader, jsonObject);
    Integer value = readUpdatePeriodType(jsonObject);
    if (value != null)
        updatePeriodType = value;
    String s = jsonObject.getString("connectionType");
    if (s == null) {
        List<String> codes = new ArrayList<String>();
        codes.add("mbusSerial");
        codes.add("mbusTcpIp");
        throw new TranslatableJsonException("emport.error.missing", "connectionType", codes);
    } else {
        int bitPerSecond = jsonObject.getInt("bitPerSecond");
        int responseTimeoutOffset = jsonObject.getInt("responseTimeoutOffset");
        switch(s) {
            case "mbusSerial":
                String portName = jsonObject.getString("portName");
                connection = new SerialPortConnection(portName, bitPerSecond, responseTimeoutOffset);
                break;
            case "mbusTcpIp":
                String host = jsonObject.getString("host");
                int port = jsonObject.getInt("port");
                connection = new TcpIpConnection(host, port, bitPerSecond, responseTimeoutOffset);
                break;
        }
    }
}
Also used : TcpIpConnection(net.sf.mbus4j.TcpIpConnection) ArrayList(java.util.ArrayList) SerialPortConnection(net.sf.mbus4j.SerialPortConnection) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException)

Example 5 with SerialPortConnection

use of net.sf.mbus4j.SerialPortConnection in project ma-modules-public by infiniteautomation.

the class MBusDataSourceRT method openConnection.

private boolean openConnection() {
    try {
        LOG.debug("MBus Try open serial port");
        if (vo.getConnection() instanceof SerialPortConnection) {
            // replace with buggy jssc
            SerialPortConnection spc = (SerialPortConnection) vo.getConnection();
            master.setConnection(new MangoMBusSerialConnection("MBus " + this.vo.getXid(), spc.getPortName(), spc.getBitPerSecond(), 1000));
        } else {
            master.setConnection(vo.getConnection());
        }
        master.open();
        return true;
    } catch (IOException ex) {
        LOG.fatal("MBus Open serial port exception", ex);
        master.setConnection(null);
        raiseEvent(DATA_SOURCE_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.exception2", "openConnection() Failed", ex.getMessage()));
        return false;
    }
}
Also used : SerialPortConnection(net.sf.mbus4j.SerialPortConnection) IOException(java.io.IOException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Aggregations

SerialPortConnection (net.sf.mbus4j.SerialPortConnection)6 TcpIpConnection (net.sf.mbus4j.TcpIpConnection)4 IOException (java.io.IOException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 ArrayList (java.util.ArrayList)1