Search in sources :

Example 1 with Read

use of org.openmuc.framework.driver.annotation.Read 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)

Example 2 with Read

use of org.openmuc.framework.driver.annotation.Read in project OpenMUC by isc-konstanz.

the class EdgeCounter method read.

@Read
public void read(List<GpioChannel> channels, String samplingGroup) throws ConnectionException {
    long samplingTime = System.currentTimeMillis();
    int newVal = counter.getValue();
    for (GpioChannel channel : channels) {
        Value value = null;
        if (channel.isDerivative() || channel.isIntervalCount()) {
            String channelId = channel.getId();
            Record lastRecord = null;
            int lastVal;
            if (counters.containsKey(channelId)) {
                lastRecord = counters.get(channelId);
                lastVal = lastRecord.getValue().asInt();
            } else {
                lastVal = 0;
            }
            double counterDelta = (newVal - lastVal) / channel.getImpulses();
            if (channel.isDerivative()) {
                if (lastRecord != null) {
                    double timeDelta = (samplingTime - lastRecord.getTimestamp()) / channel.getDerivativeTime();
                    if (timeDelta > 0) {
                        value = new DoubleValue(counterDelta / timeDelta);
                    }
                }
            } else {
                value = new DoubleValue(counterDelta);
            }
            counters.put(channelId, new Record(new IntValue(newVal), samplingTime));
        } else {
            value = new DoubleValue(newVal / channel.getImpulses());
        }
        if (value != null) {
            channel.setRecord(new Record(value, samplingTime, Flag.VALID));
        } else {
            channel.setRecord(new Record(null, samplingTime, Flag.DRIVER_ERROR_CHANNEL_TEMPORARILY_NOT_ACCESSIBLE));
        }
    }
}
Also used : GpioChannel(org.openmuc.framework.driver.rpi.gpio.GpioChannel) DoubleValue(org.openmuc.framework.data.DoubleValue) DoubleValue(org.openmuc.framework.data.DoubleValue) Value(org.openmuc.framework.data.Value) IntValue(org.openmuc.framework.data.IntValue) Record(org.openmuc.framework.data.Record) IntValue(org.openmuc.framework.data.IntValue) Read(org.openmuc.framework.driver.annotation.Read)

Example 3 with Read

use of org.openmuc.framework.driver.annotation.Read in project OpenMUC by isc-konstanz.

the class SqlClient method read.

@Read
public void read(List<SqlChannel> channels, String samplingGroup) throws ConnectionException {
    try (Connection connection = source.getConnection()) {
        if (union) {
            readTables();
            readUnion(channels, connection);
        } else {
            read(channels, connection);
        }
    } catch (SQLException e) {
        throw new ConnectionException(e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) Read(org.openmuc.framework.driver.annotation.Read)

Example 4 with Read

use of org.openmuc.framework.driver.annotation.Read in project OpenMUC by isc-konstanz.

the class OpcConnection method read.

@Read
public void read(List<OpcChannel> channels, String samplingGroup) throws ConnectionException {
    try {
        List<NodeId> nodeIds = channels.stream().map(c -> c.getNodeId()).collect(Collectors.toList());
        List<DataValue> values = client.readValues(0.0, TimestampsToReturn.Both, nodeIds).get();
        for (OpcChannel channel : channels) {
            try {
                int index = nodeIds.indexOf(channel.getNodeId());
                channel.setRecord(channel.decode(values.get(index)));
            } catch (NullPointerException e) {
                channel.setRecord(new Record(new DoubleValue(Double.NaN), System.currentTimeMillis(), Flag.DRIVER_ERROR_READ_FAILURE));
            }
        }
    } catch (InterruptedException e) {
        for (OpcChannel channel : channels) {
            channel.setRecord(new Record(new DoubleValue(Double.NaN), System.currentTimeMillis(), Flag.DRIVER_ERROR_TIMEOUT));
        }
    } catch (ExecutionException | NullPointerException e) {
        logger.warn("Reading data from OPC server failed. {}", e);
        throw new ConnectionException(e);
    }
}
Also used : Device(org.openmuc.framework.driver.annotation.Device) Arrays(java.util.Arrays) SETTING(org.openmuc.framework.config.option.annotation.OptionType.SETTING) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) OpcUaClient(org.eclipse.milo.opcua.sdk.client.OpcUaClient) LoggerFactory(org.slf4j.LoggerFactory) Disconnect(org.openmuc.framework.driver.annotation.Disconnect) OpcUaClientConfigBuilder(org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfigBuilder) Connect(org.openmuc.framework.driver.annotation.Connect) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) ServerTypeNode(org.eclipse.milo.opcua.sdk.client.model.nodes.objects.ServerTypeNode) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) Path(java.nio.file.Path) AnonymousProvider(org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider) StatusCodes(org.eclipse.milo.opcua.stack.core.StatusCodes) TimestampsToReturn(org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) DoubleValue(org.openmuc.framework.data.DoubleValue) Logger(org.slf4j.Logger) Files(java.nio.file.Files) LocalizedText(org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText) Collectors(java.util.stream.Collectors) Flag(org.openmuc.framework.data.Flag) Read(org.openmuc.framework.driver.annotation.Read) ExecutionException(java.util.concurrent.ExecutionException) ADDRESS(org.openmuc.framework.config.option.annotation.OptionType.ADDRESS) List(java.util.List) DriverDevice(org.openmuc.framework.driver.DriverDevice) Paths(java.nio.file.Paths) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) Record(org.openmuc.framework.data.Record) Write(org.openmuc.framework.driver.annotation.Write) UaException(org.eclipse.milo.opcua.stack.core.UaException) DiscoveryClient(org.eclipse.milo.opcua.stack.client.DiscoveryClient) EndpointUtil(org.eclipse.milo.opcua.stack.core.util.EndpointUtil) Identifiers(org.eclipse.milo.opcua.stack.core.Identifiers) Option(org.openmuc.framework.config.option.annotation.Option) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) DoubleValue(org.openmuc.framework.data.DoubleValue) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) Record(org.openmuc.framework.data.Record) ExecutionException(java.util.concurrent.ExecutionException) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) Read(org.openmuc.framework.driver.annotation.Read)

Example 5 with Read

use of org.openmuc.framework.driver.annotation.Read in project OpenMUC by isc-konstanz.

the class TemperatureDevice method read.

@Read
public void read(List<W1Channel> channels, String samplingGroup) throws ConnectionException {
    long samplingTime = System.currentTimeMillis();
    for (W1Channel channel : channels) {
        Value value = null;
        Double temperature = sensor.getTemperature(channel.getScale());
        if (temperature != null) {
            // Skip temperature readings of exactly 85, as they are commonly missreadings
            if (temperature < maximum) {
                value = new DoubleValue(temperature);
            } else {
                // Don't skip the reading, if the latest value read was longer than 15 minutes ago
                // or above 90% of the maximum configured value of the sensor
                Record lastRecord = channel.getRecord();
                if (lastRecord != null && lastRecord.getFlag() == Flag.VALID) {
                    if (samplingTime - lastRecord.getTimestamp() >= 900000 || lastRecord.getValue().asDouble() >= maximum * 0.9) {
                        value = new DoubleValue(temperature);
                    }
                }
            }
        }
        if (value != null) {
            channel.setRecord(new Record(value, samplingTime, Flag.VALID));
        } else {
            logger.warn("Unknown error occurred while reading temperature sensor: {}", sensor.getName());
            channel.setRecord(new Record(null, samplingTime, Flag.DRIVER_ERROR_READ_FAILURE));
        }
    }
}
Also used : DoubleValue(org.openmuc.framework.data.DoubleValue) W1Channel(org.openmuc.framework.driver.rpi.w1.W1Channel) DoubleValue(org.openmuc.framework.data.DoubleValue) Value(org.openmuc.framework.data.Value) Record(org.openmuc.framework.data.Record) Read(org.openmuc.framework.driver.annotation.Read)

Aggregations

Read (org.openmuc.framework.driver.annotation.Read)5 Record (org.openmuc.framework.data.Record)4 DoubleValue (org.openmuc.framework.data.DoubleValue)3 Value (org.openmuc.framework.data.Value)3 ConnectionException (org.openmuc.framework.driver.spi.ConnectionException)2 PinState (com.pi4j.io.gpio.PinState)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Arrays (java.util.Arrays)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 Collectors (java.util.stream.Collectors)1 OpcUaClient (org.eclipse.milo.opcua.sdk.client.OpcUaClient)1 OpcUaClientConfigBuilder (org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfigBuilder)1 AnonymousProvider (org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider)1 ServerTypeNode (org.eclipse.milo.opcua.sdk.client.model.nodes.objects.ServerTypeNode)1 DiscoveryClient (org.eclipse.milo.opcua.stack.client.DiscoveryClient)1