Search in sources :

Example 6 with Value

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);
}
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) ByteArrayValue(org.openmuc.framework.data.ByteArrayValue) Flag(org.openmuc.framework.data.Flag) Test(org.junit.jupiter.api.Test)

Example 7 with Value

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);
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) DoubleValue(org.openmuc.framework.data.DoubleValue) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) 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)

Example 8 with Value

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));
    }
}
Also used : ChannelRecordContainer(org.openmuc.framework.driver.spi.ChannelRecordContainer) InputRegister(com.ghgande.j2mod.modbus.procimg.InputRegister) Value(org.openmuc.framework.data.Value) BooleanValue(org.openmuc.framework.data.BooleanValue) Record(org.openmuc.framework.data.Record)

Example 9 with Value

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;
}
Also used : ModbusChannel(org.openmuc.framework.driver.modbus.ModbusChannel) ChannelRecordContainer(org.openmuc.framework.driver.spi.ChannelRecordContainer) Value(org.openmuc.framework.data.Value) Record(org.openmuc.framework.data.Record) ModbusIOException(com.ghgande.j2mod.modbus.ModbusIOException) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) ArgumentSyntaxException(org.openmuc.framework.config.ArgumentSyntaxException) ScanException(org.openmuc.framework.config.ScanException) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) ModbusIOException(com.ghgande.j2mod.modbus.ModbusIOException) ModbusException(com.ghgande.j2mod.modbus.ModbusException) ModbusException(com.ghgande.j2mod.modbus.ModbusException)

Example 10 with Value

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));
    }
}
Also used : PinState(com.pi4j.io.gpio.PinState) BooleanValue(org.openmuc.framework.data.BooleanValue) Value(org.openmuc.framework.data.Value) BooleanValue(org.openmuc.framework.data.BooleanValue) Record(org.openmuc.framework.data.Record) Read(org.openmuc.framework.driver.annotation.Read)

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