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));
}
}
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));
}
}
}
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);
}
}
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);
}
}
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));
}
}
}
Aggregations