use of org.openmuc.framework.data.ShortValue 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.ShortValue in project OpenMUC by isc-konstanz.
the class LogFileWriterTest method getGroup.
private static LogIntervalContainerGroup getGroup(long timeStamp, int i, boolean boolValue, byte byteValue, String testString) {
LogIntervalContainerGroup group = new LogIntervalContainerGroup();
LoggingRecord container1 = new LoggingRecord(ch01, new Record(new FloatValue(i * -7 - 0.555F), timeStamp));
LoggingRecord container2 = new LoggingRecord(ch02, new Record(new DoubleValue(i * +7 - 0.555), timeStamp));
LoggingRecord container3 = new LoggingRecord(ch03, new Record(new BooleanValue(boolValue), timeStamp));
LoggingRecord container4 = new LoggingRecord(ch04, new Record(new ShortValue((short) i), timeStamp));
LoggingRecord container5 = new LoggingRecord(ch05, new Record(new IntValue(i), timeStamp));
LoggingRecord container6 = new LoggingRecord(ch06, new Record(new LongValue(i * 1000000), timeStamp));
LoggingRecord container7 = new LoggingRecord(ch07, new Record(new ByteValue(byteValue), timeStamp));
LoggingRecord container8 = new LoggingRecord(ch08, new Record(new StringValue(testString), timeStamp));
LoggingRecord container9 = new LoggingRecord(ch09, new Record(new ByteArrayValue(testByteArray), timeStamp));
group.add(container1);
group.add(container2);
group.add(container3);
group.add(container4);
group.add(container5);
group.add(container6);
group.add(container7);
group.add(container8);
group.add(container9);
return group;
}
use of org.openmuc.framework.data.ShortValue in project OpenMUC by isc-konstanz.
the class ChannelImpl method convertValidRecord.
private Record convertValidRecord(Record record) {
Double scalingFactor = config.getScalingFactor();
Double scalingOffset = config.getValueOffset();
if (scalingFactor != null) {
try {
record = new Record(new DoubleValue(record.getValue().asDouble() * scalingFactor), record.getTimestamp(), record.getFlag());
} catch (TypeConversionException e) {
String msg = "Unable to apply scaling factor to channel " + config.getId() + " because a TypeConversionError occurred.";
logger.error(msg, e);
}
}
if (scalingOffset != null) {
try {
record = new Record(new DoubleValue(record.getValue().asDouble() + scalingOffset), record.getTimestamp(), record.getFlag());
} catch (TypeConversionException e) {
String msg = "Unable to apply scaling offset to channel " + config.getId() + " because a TypeConversionError occurred.";
logger.error(msg, e);
}
}
try {
switch(config.getValueType()) {
case BOOLEAN:
return new Record(new BooleanValue(record.getValue().asBoolean()), record.getTimestamp(), record.getFlag());
case BYTE:
return new Record(new ByteValue(record.getValue().asByte()), record.getTimestamp(), record.getFlag());
case SHORT:
return new Record(new ShortValue(record.getValue().asShort()), record.getTimestamp(), record.getFlag());
case INTEGER:
return new Record(new IntValue(record.getValue().asInt()), record.getTimestamp(), record.getFlag());
case LONG:
return new Record(new LongValue(record.getValue().asLong()), record.getTimestamp(), record.getFlag());
case FLOAT:
return new Record(new FloatValue(record.getValue().asFloat()), record.getTimestamp(), record.getFlag());
case DOUBLE:
return new Record(new DoubleValue(record.getValue().asDouble()), record.getTimestamp(), record.getFlag());
case BYTE_ARRAY:
return new Record(new ByteArrayValue(record.getValue().asByteArray()), record.getTimestamp(), record.getFlag());
case STRING:
default:
return new Record(new StringValue(record.getValue().toString()), record.getTimestamp(), record.getFlag());
}
} catch (TypeConversionException e) {
logger.error("Unable to convert value to configured value type because a TypeConversionError occured.", e);
return new Record(Flag.DRIVER_ERROR_CHANNEL_VALUE_TYPE_CONVERSION_EXCEPTION);
}
}
use of org.openmuc.framework.data.ShortValue in project OpenMUC by isc-konstanz.
the class FromJson method convertValue.
public static Value convertValue(Object value, ValueType type) throws ClassCastException {
if (value.getClass().isInstance(new RestValue())) {
value = ((RestValue) value).getValue();
}
switch(type) {
case FLOAT:
return new FloatValue(((Double) value).floatValue());
case DOUBLE:
return new DoubleValue((Double) value);
case SHORT:
return new ShortValue(((Double) value).shortValue());
case INTEGER:
return new IntValue(((Double) value).intValue());
case LONG:
return new LongValue(((Double) value).longValue());
case BYTE:
return new ByteValue(((Double) value).byteValue());
case BOOLEAN:
return new BooleanValue((Boolean) value);
case BYTE_ARRAY:
@SuppressWarnings("unchecked") List<Double> arrayList = ((ArrayList<Double>) value);
byte[] byteArray = new byte[arrayList.size()];
for (int i = 0; i < arrayList.size(); ++i) {
byteArray[i] = arrayList.get(i).byteValue();
}
return new ByteArrayValue(byteArray);
case STRING:
return new StringValue((String) value);
default:
// should not occur
return new StringValue(value.toString());
}
}
use of org.openmuc.framework.data.ShortValue in project OpenMUC by isc-konstanz.
the class OpcChannel method decode.
public Record decode(DataValue data) {
long timestamp = data.getServerTime().getJavaTime();
Object value = data.getValue().getValue();
switch(getValueType()) {
case BOOLEAN:
return new Record(new BooleanValue((Boolean) value), timestamp);
case BYTE:
return new Record(new ByteValue((Byte) value), timestamp);
case SHORT:
return new Record(new ShortValue((Short) value), timestamp);
case INTEGER:
return new Record(new IntValue((Integer) value), timestamp);
case LONG:
return new Record(new LongValue((Long) value), timestamp);
case FLOAT:
return new Record(new FloatValue((Float) value), timestamp);
case DOUBLE:
return new Record(new DoubleValue((Double) value), timestamp);
default:
return new Record(new StringValue((String) value), timestamp);
}
}
Aggregations