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;
}
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());
}
}
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());
}
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);
}
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);
}
Aggregations