use of org.openmuc.framework.data.Value in project OpenMUC by isc-konstanz.
the class OpenmucParserServiceImplTest method serializeByteArrayValue.
@Test
void serializeByteArrayValue() throws SerializationException {
String controlString = "{\"timestamp\":1582722316,\"flag\":\"VALID\",\"value\":\"dGVzdA==\"}";
Value byteArrayValue = new ByteArrayValue("test".getBytes());
long timestamp = 1582722316;
Flag flag = Flag.VALID;
Record record = new Record(byteArrayValue, 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 OpcChannel method encode.
public DataValue encode() {
Value value = getRecord().getValue();
Variant variant;
switch(getValueType()) {
case BOOLEAN:
variant = new Variant(value.asBoolean());
case BYTE:
variant = new Variant(value.asByte());
case SHORT:
variant = new Variant(value.asShort());
case INTEGER:
variant = new Variant(value.asInt());
case LONG:
variant = new Variant(value.asLong());
case FLOAT:
variant = new Variant(value.asFloat());
case DOUBLE:
variant = new Variant(value.asDouble());
default:
variant = new Variant(value.asString());
}
// FIXME: verify necessity of timestamp
return new DataValue(variant, StatusCode.GOOD, null);
}
use of org.openmuc.framework.data.Value in project OpenMUC by isc-konstanz.
the class ModbusChannelGroup method setChannelValues.
public void setChannelValues(InputRegister[] inputRegisters, List<ChannelRecordContainer> containers) {
for (ModbusChannel channel : channels) {
// determine start index of the registers which contain the values of the channel
int registerIndex = channel.getStartAddress() - getStartAddress();
// create a temporary register array
InputRegister[] registers = new InputRegister[channel.getCount()];
// copy relevant registers for the channel
System.arraycopy(inputRegisters, registerIndex, registers, 0, channel.getCount());
// now we have a register array which contains the value of the channel
ChannelRecordContainer container = searchContainer(channel.getChannelAddress(), containers);
long receiveTime = System.currentTimeMillis();
Value value = ModbusDriverUtil.getRegistersValue(registers, channel.getDatatype(), channel.getSwap());
if (logger.isTraceEnabled()) {
logger.trace("response value channel " + channel.getChannelAddress() + ": " + value.toString());
}
container.setRecord(new Record(value, receiveTime));
}
}
use of org.openmuc.framework.data.Value in project OpenMUC by isc-konstanz.
the class ModbusTCPConnection method read.
@Override
public Object read(List<ChannelRecordContainer> containers, Object containerListHandle, String samplingGroup) throws UnsupportedOperationException, ConnectionException {
// reads channels one by one
if (samplingGroup.isEmpty()) {
for (ChannelRecordContainer container : containers) {
// TODO consider retries in sampling timeout (e.g. one time 12000 ms or three times 4000 ms)
// FIXME quite inconvenient/complex to get the timeout from config, since the driver doesn't know the
// device id!
long receiveTime = System.currentTimeMillis();
ModbusChannel channel = getModbusChannel(container.getChannelAddress(), EAccess.READ);
Value value;
try {
value = readChannel(channel);
if (logger.isTraceEnabled()) {
logger.trace("Value of response: {}", value.toString());
}
container.setRecord(new Record(value, receiveTime));
} catch (ModbusIOException e) {
logger.error("ModbusIOException while reading channel:" + channel.getChannelAddress() + " used timeout: " + timeoutMs + " ms", e);
disconnect();
throw new ConnectionException("Try to solve issue with reconnect.");
} catch (ModbusException e) {
logger.error("ModbusException while reading channel: " + channel.getChannelAddress(), e);
container.setRecord(new Record(Flag.DRIVER_ERROR_CHANNEL_NOT_ACCESSIBLE));
} catch (Exception e) {
// catch all possible exceptions and provide info about the channel
logger.error("Exception while reading channel: " + channel.getChannelAddress(), e);
container.setRecord(new Record(Flag.UNKNOWN_ERROR));
}
if (!connection.isConnected()) {
throw new ConnectionException("Lost connection.");
}
}
} else // reads whole samplingGroup at once
{
readChannelGroupHighLevel(containers, containerListHandle, samplingGroup);
if (!connection.isConnected()) {
throw new ConnectionException("Lost connection.");
}
}
return null;
}
use of org.openmuc.framework.data.Value in project OpenMUC by isc-konstanz.
the class InputPin method read.
@Read
public void read(List<GpioChannel> channels, String samplingGroup) throws ConnectionException {
long samplingTime = System.currentTimeMillis();
for (GpioChannel channel : channels) {
PinState state = pin.getState();
Value value;
if (!channel.isInverted()) {
value = new BooleanValue(state.isHigh());
} else {
value = new BooleanValue(state.isLow());
}
channel.setRecord(new Record(value, samplingTime, Flag.VALID));
}
}
Aggregations