use of org.openmuc.framework.data.IntValue in project OpenMUC by isc-konstanz.
the class ChannelImpl method write.
@Override
public Flag write(Value value) {
if (config.deviceParent.driverParent.getId().equals("virtual")) {
Record record = new Record(value, System.currentTimeMillis());
setLatestRecord(record);
List<ChannelRecordContainer> recordContainers = new ArrayList<>();
ChannelRecordContainer recordContainer = new ChannelRecordContainerImpl(this);
recordContainer.setRecord(record);
recordContainers.add(recordContainer);
dataManager.newRecords(recordContainers);
dataManager.interrupt();
return record.getFlag();
}
CountDownLatch writeTaskFinishedSignal = new CountDownLatch(1);
WriteValueContainerImpl writeValueContainer = new WriteValueContainerImpl(this);
Value adjustedValue = value;
Double valueOffset = config.getValueOffset();
Double scalingFactor = config.getScalingFactor();
if (valueOffset != null) {
Double adjustedDouble = adjustedValue.asDouble() - valueOffset;
switch(config.getValueType()) {
case FLOAT:
adjustedValue = new FloatValue(adjustedDouble.floatValue());
break;
case SHORT:
adjustedValue = new ShortValue(adjustedDouble.shortValue());
break;
case INTEGER:
adjustedValue = new IntValue(adjustedDouble.intValue());
break;
case LONG:
adjustedValue = new LongValue(adjustedDouble.longValue());
break;
default:
adjustedValue = new DoubleValue(adjustedDouble);
break;
}
}
if (scalingFactor != null) {
Double adjustedDouble = adjustedValue.asDouble() / scalingFactor;
switch(config.getValueType()) {
case FLOAT:
adjustedValue = new FloatValue(adjustedDouble.floatValue());
break;
case SHORT:
adjustedValue = new ShortValue(adjustedDouble.shortValue());
break;
case INTEGER:
adjustedValue = new IntValue(adjustedDouble.intValue());
break;
case LONG:
adjustedValue = new LongValue(adjustedDouble.longValue());
break;
default:
adjustedValue = new DoubleValue(adjustedDouble);
break;
}
}
writeValueContainer.setValue(adjustedValue);
List<WriteValueContainerImpl> writeValueContainerList = Arrays.asList(writeValueContainer);
WriteTask writeTask = new WriteTask(dataManager, config.deviceParent.device, writeValueContainerList, writeTaskFinishedSignal);
synchronized (dataManager.newWriteTasks) {
dataManager.newWriteTasks.add(writeTask);
}
dataManager.interrupt();
try {
writeTaskFinishedSignal.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
long timestamp = System.currentTimeMillis();
latestRecord = new Record(value, timestamp, writeValueContainer.getFlag());
notifyListeners();
return writeValueContainer.getFlag();
}
use of org.openmuc.framework.data.IntValue in project OpenMUC by isc-konstanz.
the class Options method parseValue.
public static Value parseValue(ValueType type, String valueStr) throws ArgumentSyntaxException {
Value value;
switch(type) {
case DOUBLE:
value = new DoubleValue(Double.valueOf(valueStr));
break;
case FLOAT:
value = new FloatValue(Float.valueOf(valueStr));
break;
case INTEGER:
value = new IntValue(Integer.valueOf(valueStr));
break;
case LONG:
value = new LongValue(Long.valueOf(valueStr));
break;
case SHORT:
value = new ShortValue(Short.valueOf(valueStr));
break;
case BYTE:
value = new ByteValue(Byte.valueOf(valueStr));
break;
case BOOLEAN:
value = new BooleanValue(Boolean.valueOf(valueStr));
break;
case BYTE_ARRAY:
byte[] arr;
if (!valueStr.startsWith("0x")) {
arr = valueStr.getBytes(StandardCharsets.US_ASCII);
} else {
try {
arr = OptionValue.hexToBytes(valueStr.substring(2).trim());
} catch (IllegalArgumentException e) {
throw new ArgumentSyntaxException("Unable to parse value as byte array: " + valueStr);
}
}
value = new ByteArrayValue(arr);
break;
case STRING:
value = new StringValue(valueStr);
break;
default:
throw new ArgumentSyntaxException("Parameter value type not configured: " + type.name().toLowerCase());
}
return value;
}
use of org.openmuc.framework.data.IntValue in project OpenMUC by isc-konstanz.
the class ModbusDriverUtil method getValueFromByteArray.
@SuppressWarnings("deprecation")
public static Value getValueFromByteArray(byte[] registerAsByteArray, DataType datatype) {
Value registerValue = null;
switch(datatype) {
// break;
case SHORT:
case INT16:
registerValue = new ShortValue(ModbusUtil.registerToShort(registerAsByteArray));
break;
case INT32:
registerValue = new IntValue(ModbusUtil.registersToInt(registerAsByteArray));
break;
// break;
case UINT16:
// TODO might need both: big/little endian support in settings
// int uint16 = DatatypeConversion.bytes_To_UnsignedInt16(registerAsByteArray,
// EndianInput.BYTES_ARE_BIG_ENDIAN);
// registerValue = new IntValue(uint16);
registerValue = new IntValue(ModbusUtil.registerToUnsignedShort(registerAsByteArray));
break;
case UINT32:
// TODO might need both: big/little endian support in settings
long uint32 = DataTypeConverter.bytesToUnsignedInt32(registerAsByteArray, EndianInput.BYTES_ARE_BIG_ENDIAN);
registerValue = new LongValue(uint32);
break;
case FLOAT:
registerValue = new FloatValue(ModbusUtil.registersToFloat(registerAsByteArray));
break;
case DOUBLE:
registerValue = new DoubleValue(ModbusUtil.registersToDouble(registerAsByteArray));
break;
case LONG:
registerValue = new LongValue(ModbusUtil.registersToLong(registerAsByteArray));
break;
case BYTEARRAY:
registerValue = new ByteArrayValue(registerAsByteArray);
break;
case BYTEARRAYLONG:
registerValue = new LongValue(DataTypeConverter.bytesToSignedInt64(registerAsByteArray, EndianInput.BYTES_ARE_LITTLE_ENDIAN));
break;
// break;
default:
throw new RuntimeException("Datatype " + datatype.toString() + " not supported yet");
}
return registerValue;
}
Aggregations