Search in sources :

Example 1 with TranslatableMessageParseException

use of com.serotonin.m2m2.i18n.TranslatableMessageParseException in project ma-core-public by MangoAutomation.

the class PointValueTimeDeserializer method deserialize.

@Override
public PointValueTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    ObjectCodec codec = jsonParser.getCodec();
    JsonNode node = codec.readTree(jsonParser);
    JsonNode dataTypeNode = node.get("dataType");
    if (dataTypeNode == null) {
        throw JsonMappingException.from(jsonParser, "Missing dataType");
    }
    JsonNode valueNode = node.get("value");
    if (valueNode == null) {
        throw JsonMappingException.from(jsonParser, "Missing value");
    }
    JsonNode timestampNode = node.get("timestamp");
    if (timestampNode == null) {
        throw JsonMappingException.from(jsonParser, "Missing timestamp");
    }
    long timestamp = timestampNode.asLong();
    String dataTypeStr = dataTypeNode.asText();
    DataType dataType = DataType.fromName(dataTypeStr);
    if (dataType == null) {
        throw JsonMappingException.from(jsonParser, "Unknown dataType: " + dataTypeStr);
    }
    DataValue dataValue;
    switch(dataType) {
        case ALPHANUMERIC:
            dataValue = new AlphanumericValue(valueNode.asText());
            break;
        case BINARY:
            dataValue = new BinaryValue(valueNode.asBoolean());
            break;
        case MULTISTATE:
            dataValue = new MultistateValue(valueNode.asInt());
            break;
        case NUMERIC:
            dataValue = new NumericValue(valueNode.asDouble());
            break;
        default:
            throw JsonMappingException.from(jsonParser, "Unsupported dataType " + dataType);
    }
    JsonNode annotationNode = node.get("serializedAnnotation");
    if (annotationNode != null && !annotationNode.isNull()) {
        try {
            return new AnnotatedPointValueTime(dataValue, timestamp, TranslatableMessage.deserialize(annotationNode.asText()));
        } catch (TranslatableMessageParseException e) {
            throw JsonMappingException.from(jsonParser, "Can't deserialize annotation", e);
        }
    }
    return new PointValueTime(dataValue, timestamp);
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) BinaryValue(com.serotonin.m2m2.rt.dataImage.types.BinaryValue) JsonNode(com.fasterxml.jackson.databind.JsonNode) TranslatableMessageParseException(com.serotonin.m2m2.i18n.TranslatableMessageParseException) ObjectCodec(com.fasterxml.jackson.core.ObjectCodec) MultistateValue(com.serotonin.m2m2.rt.dataImage.types.MultistateValue) AlphanumericValue(com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) DataType(com.serotonin.m2m2.DataType) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue)

Example 2 with TranslatableMessageParseException

use of com.serotonin.m2m2.i18n.TranslatableMessageParseException in project ma-core-public by infiniteautomation.

the class PointValueTimeDeserializer method deserialize.

@Override
public PointValueTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    ObjectCodec codec = jsonParser.getCodec();
    JsonNode node = codec.readTree(jsonParser);
    JsonNode dataTypeNode = node.get("dataType");
    if (dataTypeNode == null) {
        throw JsonMappingException.from(jsonParser, "Missing dataType");
    }
    JsonNode valueNode = node.get("value");
    if (valueNode == null) {
        throw JsonMappingException.from(jsonParser, "Missing value");
    }
    JsonNode timestampNode = node.get("timestamp");
    if (timestampNode == null) {
        throw JsonMappingException.from(jsonParser, "Missing timestamp");
    }
    long timestamp = timestampNode.asLong();
    String dataTypeStr = dataTypeNode.asText();
    DataType dataType = DataType.fromName(dataTypeStr);
    if (dataType == null) {
        throw JsonMappingException.from(jsonParser, "Unknown dataType: " + dataTypeStr);
    }
    DataValue dataValue;
    switch(dataType) {
        case ALPHANUMERIC:
            dataValue = new AlphanumericValue(valueNode.asText());
            break;
        case BINARY:
            dataValue = new BinaryValue(valueNode.asBoolean());
            break;
        case MULTISTATE:
            dataValue = new MultistateValue(valueNode.asInt());
            break;
        case NUMERIC:
            dataValue = new NumericValue(valueNode.asDouble());
            break;
        default:
            throw JsonMappingException.from(jsonParser, "Unsupported dataType " + dataType);
    }
    JsonNode annotationNode = node.get("serializedAnnotation");
    if (annotationNode != null && !annotationNode.isNull()) {
        try {
            return new AnnotatedPointValueTime(dataValue, timestamp, TranslatableMessage.deserialize(annotationNode.asText()));
        } catch (TranslatableMessageParseException e) {
            throw JsonMappingException.from(jsonParser, "Can't deserialize annotation", e);
        }
    }
    return new PointValueTime(dataValue, timestamp);
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) BinaryValue(com.serotonin.m2m2.rt.dataImage.types.BinaryValue) JsonNode(com.fasterxml.jackson.databind.JsonNode) TranslatableMessageParseException(com.serotonin.m2m2.i18n.TranslatableMessageParseException) ObjectCodec(com.fasterxml.jackson.core.ObjectCodec) MultistateValue(com.serotonin.m2m2.rt.dataImage.types.MultistateValue) AlphanumericValue(com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) DataType(com.serotonin.m2m2.DataType) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue)

Aggregations

ObjectCodec (com.fasterxml.jackson.core.ObjectCodec)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 DataType (com.serotonin.m2m2.DataType)2 TranslatableMessageParseException (com.serotonin.m2m2.i18n.TranslatableMessageParseException)2 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)2 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)2 AlphanumericValue (com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue)2 BinaryValue (com.serotonin.m2m2.rt.dataImage.types.BinaryValue)2 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)2 MultistateValue (com.serotonin.m2m2.rt.dataImage.types.MultistateValue)2 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)2