Search in sources :

Example 6 with SerialPortConnection

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

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