Search in sources :

Example 1 with Value

use of org.openmuc.framework.data.Value in project OpenMUC by isc-konstanz.

the class OptionValue method getFromDomNode.

static OptionValue getFromDomNode(String id, Node node) throws ParseException {
    OptionValue option = new OptionValue(id);
    Node valueDefaultNode = null;
    Node valueSelectionNode = null;
    NodeList childNodes = node.getChildNodes();
    try {
        for (int j = 0; j < childNodes.getLength(); j++) {
            Node childNode = childNodes.item(j);
            String childNodeName = childNode.getNodeName();
            if (childNodeName.equals("#text")) {
                continue;
            } else if (childNodeName.equals("name")) {
                option.name = childNode.getTextContent().trim();
            } else if (childNodeName.equals("description")) {
                option.description = Options.trimDomNodeText(childNode);
            } else if (childNodeName.equals("mandatory")) {
                String mandatoryString = childNode.getTextContent().trim().toLowerCase();
                if (mandatoryString.equals("true")) {
                    option.mandatory = true;
                } else if (mandatoryString.equals("false")) {
                    option.mandatory = false;
                } else {
                    throw new ParseException("Option \"mandatory\" contains neither \"true\" nor \"false\"");
                }
            } else if (childNodeName.equals("type")) {
                String valueTypeString = childNode.getTextContent().trim().toUpperCase();
                try {
                    option.type = ValueType.valueOf(valueTypeString);
                } catch (IllegalArgumentException e) {
                    throw new ParseException("Unknown option value type found:" + valueTypeString);
                }
            } else if (childNodeName.equals("default")) {
                valueDefaultNode = childNode;
            } else if (childNodeName.equals("selection")) {
                valueSelectionNode = childNode;
            } else {
                throw new ParseException("Unknown tag found:" + childNodeName);
            }
        }
        if (option.name == null) {
            option.name = id;
        }
        if (valueDefaultNode != null) {
            Value valueDefault = new StringValue(valueDefaultNode.getTextContent().trim());
            // Verify default values to be of the specified value type
            switch(option.type) {
                case FLOAT:
                    option.valueDefault = new FloatValue(valueDefault.asFloat());
                    break;
                case DOUBLE:
                    option.valueDefault = new DoubleValue(valueDefault.asDouble());
                    break;
                case SHORT:
                    option.valueDefault = new ShortValue(valueDefault.asShort());
                    break;
                case INTEGER:
                    option.valueDefault = new IntValue(valueDefault.asInt());
                    break;
                case LONG:
                    option.valueDefault = new LongValue(valueDefault.asLong());
                    break;
                case BYTE:
                    option.valueDefault = new ByteValue(valueDefault.asByte());
                    break;
                case BYTE_ARRAY:
                    byte[] arr;
                    if (!valueDefault.asString().startsWith("0x")) {
                        arr = valueDefault.asByteArray();
                    } else {
                        try {
                            arr = OptionValue.hexToBytes(valueDefault.asString().substring(2).trim());
                        } catch (IllegalArgumentException e) {
                            throw new ParseException(e);
                        }
                    }
                    option.valueDefault = new ByteArrayValue(arr);
                    break;
                case BOOLEAN:
                    option.valueDefault = new BooleanValue(valueDefault.asBoolean());
                    break;
                case STRING:
                    option.valueDefault = valueDefault;
                    break;
                default:
                    break;
            }
        }
        if (valueSelectionNode != null) {
            option.valueSelection = OptionSelection.getFromDomNode(valueSelectionNode, option.type);
        }
    } catch (IllegalArgumentException e) {
        throw new ParseException(e);
    }
    return option;
}
Also used : ByteValue(org.openmuc.framework.data.ByteValue) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) ShortValue(org.openmuc.framework.data.ShortValue) DoubleValue(org.openmuc.framework.data.DoubleValue) BooleanValue(org.openmuc.framework.data.BooleanValue) ByteArrayValue(org.openmuc.framework.data.ByteArrayValue) DoubleValue(org.openmuc.framework.data.DoubleValue) ByteValue(org.openmuc.framework.data.ByteValue) ShortValue(org.openmuc.framework.data.ShortValue) StringValue(org.openmuc.framework.data.StringValue) Value(org.openmuc.framework.data.Value) BooleanValue(org.openmuc.framework.data.BooleanValue) LongValue(org.openmuc.framework.data.LongValue) FloatValue(org.openmuc.framework.data.FloatValue) IntValue(org.openmuc.framework.data.IntValue) LongValue(org.openmuc.framework.data.LongValue) ParseException(org.openmuc.framework.config.ParseException) FloatValue(org.openmuc.framework.data.FloatValue) ByteArrayValue(org.openmuc.framework.data.ByteArrayValue) StringValue(org.openmuc.framework.data.StringValue) IntValue(org.openmuc.framework.data.IntValue)

Example 2 with Value

use of org.openmuc.framework.data.Value in project OpenMUC by isc-konstanz.

the class FromJson method convertRecord.

public static Record convertRecord(RestRecord rrc, ValueType type) throws ClassCastException {
    Object value = rrc.getValue();
    Flag flag = rrc.getFlag();
    Value retValue = null;
    if (value != null) {
        retValue = convertValue(value, type);
    }
    if (flag == null) {
        return new Record(retValue, rrc.getTimestamp());
    } else {
        return new Record(retValue, rrc.getTimestamp(), rrc.getFlag());
    }
}
Also used : ByteArrayValue(org.openmuc.framework.data.ByteArrayValue) StringValue(org.openmuc.framework.data.StringValue) Value(org.openmuc.framework.data.Value) FloatValue(org.openmuc.framework.data.FloatValue) DoubleValue(org.openmuc.framework.data.DoubleValue) ByteValue(org.openmuc.framework.data.ByteValue) ShortValue(org.openmuc.framework.data.ShortValue) RestValue(org.openmuc.framework.lib.rest.objects.RestValue) BooleanValue(org.openmuc.framework.data.BooleanValue) LongValue(org.openmuc.framework.data.LongValue) IntValue(org.openmuc.framework.data.IntValue) JsonObject(com.google.gson.JsonObject) RestRecord(org.openmuc.framework.lib.rest.objects.RestRecord) Record(org.openmuc.framework.data.Record) Flag(org.openmuc.framework.data.Flag)

Example 3 with Value

use of org.openmuc.framework.data.Value in project OpenMUC by isc-konstanz.

the class FlotteladenParser method deserialize.

@Override
public synchronized Record deserialize(byte[] byteArray, SerializationContainer container) {
    JsonObject json = gson.fromJson(new String(byteArray), JsonObject.class);
    JsonObject units = json.getAsJsonObject("units");
    String unit = Stream.of(container.getChannelAddress().split(";")).skip(1).collect(Collectors.joining(";"));
    logger.debug("Received unit \"{}\" from register \"{}\" source \"{}\": {}", unit, json.get("register").getAsString(), json.get("source").getAsString(), units.get(unit).getAsString());
    Value value = deserializeValue(units.get(unit), container.getValueType());
    return new Record(value, System.currentTimeMillis());
}
Also used : DoubleValue(org.openmuc.framework.data.DoubleValue) ByteValue(org.openmuc.framework.data.ByteValue) ShortValue(org.openmuc.framework.data.ShortValue) StringValue(org.openmuc.framework.data.StringValue) Value(org.openmuc.framework.data.Value) BooleanValue(org.openmuc.framework.data.BooleanValue) LongValue(org.openmuc.framework.data.LongValue) FloatValue(org.openmuc.framework.data.FloatValue) IntValue(org.openmuc.framework.data.IntValue) JsonObject(com.google.gson.JsonObject) Record(org.openmuc.framework.data.Record) LoggingRecord(org.openmuc.framework.datalogger.spi.LoggingRecord)

Example 4 with Value

use of org.openmuc.framework.data.Value in project OpenMUC by isc-konstanz.

the class OpenmucParserServiceImplTest method serializeDoubleValue.

@Test
void serializeDoubleValue() throws SerializationException {
    String controlString = "{\"timestamp\":1582722316,\"flag\":\"VALID\",\"value\":3.0}";
    Value doubleValue = new DoubleValue(3.0);
    long timestamp = 1582722316;
    Flag flag = Flag.VALID;
    Record record = new Record(doubleValue, timestamp, flag);
    byte[] serializedRecord = parserService.serialize(record, new SerializationContainerTest());
    String serializedJson = new String(serializedRecord);
    assertEquals(controlString, serializedJson);
}
Also used : DoubleValue(org.openmuc.framework.data.DoubleValue) ByteArrayValue(org.openmuc.framework.data.ByteArrayValue) DoubleValue(org.openmuc.framework.data.DoubleValue) StringValue(org.openmuc.framework.data.StringValue) Value(org.openmuc.framework.data.Value) Record(org.openmuc.framework.data.Record) LoggingRecord(org.openmuc.framework.datalogger.spi.LoggingRecord) Flag(org.openmuc.framework.data.Flag) Test(org.junit.jupiter.api.Test)

Example 5 with Value

use of org.openmuc.framework.data.Value in project OpenMUC by isc-konstanz.

the class OpenmucParserServiceImplTest method serializeStringValue.

@Test
void serializeStringValue() throws SerializationException {
    String controlString = "{\"timestamp\":1582722316,\"flag\":\"VALID\",\"value\":\"test\"}";
    Value doubleValue = new StringValue("test");
    long timestamp = 1582722316;
    Flag flag = Flag.VALID;
    Record record = new Record(doubleValue, timestamp, flag);
    byte[] serializedRecord = parserService.serialize(record, new SerializationContainerTest());
    String serializedJson = new String(serializedRecord);
    assertEquals(controlString, serializedJson);
}
Also used : ByteArrayValue(org.openmuc.framework.data.ByteArrayValue) DoubleValue(org.openmuc.framework.data.DoubleValue) StringValue(org.openmuc.framework.data.StringValue) Value(org.openmuc.framework.data.Value) Record(org.openmuc.framework.data.Record) LoggingRecord(org.openmuc.framework.datalogger.spi.LoggingRecord) StringValue(org.openmuc.framework.data.StringValue) Flag(org.openmuc.framework.data.Flag) Test(org.junit.jupiter.api.Test)

Aggregations

Value (org.openmuc.framework.data.Value)41 Record (org.openmuc.framework.data.Record)26 DoubleValue (org.openmuc.framework.data.DoubleValue)25 StringValue (org.openmuc.framework.data.StringValue)20 BooleanValue (org.openmuc.framework.data.BooleanValue)15 ByteArrayValue (org.openmuc.framework.data.ByteArrayValue)15 IntValue (org.openmuc.framework.data.IntValue)15 FloatValue (org.openmuc.framework.data.FloatValue)12 LongValue (org.openmuc.framework.data.LongValue)12 ShortValue (org.openmuc.framework.data.ShortValue)12 Flag (org.openmuc.framework.data.Flag)11 ByteValue (org.openmuc.framework.data.ByteValue)10 LoggingRecord (org.openmuc.framework.datalogger.spi.LoggingRecord)7 ChannelRecordContainer (org.openmuc.framework.driver.spi.ChannelRecordContainer)7 ArgumentSyntaxException (org.openmuc.framework.config.ArgumentSyntaxException)4 ConnectionException (org.openmuc.framework.driver.spi.ConnectionException)4 ModbusException (com.ghgande.j2mod.modbus.ModbusException)3 ModbusIOException (com.ghgande.j2mod.modbus.ModbusIOException)3 LinkedHashMap (java.util.LinkedHashMap)3 Test (org.junit.jupiter.api.Test)3