Search in sources :

Example 1 with TcpIpConnection

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

the class MBusDataSourceVO method createNewDataSource.

// private final static Log LOG = LogFactory.getLog(MBusDataSourceVO.class);
public static MBusDataSourceVO createNewDataSource() {
    MBusDataSourceVO result = new MBusDataSourceVO();
    result.setConnection(new TcpIpConnection("192.168.1.210", 10001, Connection.DEFAULT_BAUDRATE, TcpIpConnection.DEFAULT_RESPONSE_TIMEOUT_OFFSET));
    return result;
}
Also used : TcpIpConnection(net.sf.mbus4j.TcpIpConnection)

Example 2 with TcpIpConnection

use of net.sf.mbus4j.TcpIpConnection 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 3 with TcpIpConnection

use of net.sf.mbus4j.TcpIpConnection 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 4 with TcpIpConnection

use of net.sf.mbus4j.TcpIpConnection 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 TcpIpConnection

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

the class MBusConnectionDeserializer method deserialize.

/* (non-Javadoc)
	 * @see com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext)
	 */
@Override
public Connection deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    JsonNode n = p.readValueAsTree();
    JsonNode t = n.get("modelType");
    int bitPerSecond = n.get("bitPerSecond").asInt();
    int responseTimeoutOffset = n.get("responseTimeoutOffset").asInt();
    String type = null;
    if (t != null)
        type = t.asText();
    else
        throw new IOException("No connection model type provided.");
    switch(type) {
        case "mbusSerial":
            SerialPortConnection serialModel = new SerialPortConnection(n.get("portName").asText(), bitPerSecond, responseTimeoutOffset);
            return serialModel;
        case "mbusTcpIp":
            TcpIpConnection tcpModel = new TcpIpConnection(n.get("host").asText(), n.get("port").asInt(), bitPerSecond, responseTimeoutOffset);
            return tcpModel;
        default:
            throw new IOException("Invalid model type: " + type);
    }
}
Also used : TcpIpConnection(net.sf.mbus4j.TcpIpConnection) SerialPortConnection(net.sf.mbus4j.SerialPortConnection) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Aggregations

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