use of org.openmuc.framework.data.FloatValue in project OpenMUC by isc-konstanz.
the class LoggingChannel method updateRecord.
void updateRecord(Record record) {
long timestamp = record.getTimestamp();
if (isLoggingDynamic() && getRecord().getTimestamp() != null && System.currentTimeMillis() - getRecord().getTimestamp() >= getLoggingDelayMaximum()) {
timestamp = System.currentTimeMillis();
if (!isAveraging()) {
record = new Record(record.getValue(), timestamp);
}
}
if (isAveraging() && records.size() > 0) {
double average = records.stream().mapToDouble(c -> c.getValue().asDouble()).average().getAsDouble();
switch(getValueType()) {
case SHORT:
record = new Record(new ShortValue((short) Math.round(average)), timestamp);
break;
case INTEGER:
record = new Record(new IntValue((int) Math.round(average)), timestamp);
break;
case LONG:
record = new Record(new LongValue((long) Math.round(average)), timestamp);
break;
case FLOAT:
record = new Record(new FloatValue((float) average), timestamp);
break;
case DOUBLE:
record = new Record(new DoubleValue(average), timestamp);
break;
default:
break;
}
logger.trace("Average of {} values for channel \"{}\": {}", records.size(), channel.getId(), average);
records.clear();
}
this.record = record;
}
use of org.openmuc.framework.data.FloatValue in project OpenMUC by isc-konstanz.
the class ChannelInputRegister method getChannelValue.
private Value getChannelValue() throws NullPointerException {
Record record = channel.getLatestRecord();
if (!record.isValid()) {
throw new NullPointerException("Channel record is invalid: " + record);
}
Value value = channel.getLatestRecord().getValue();
if (useUnscaledValues) {
switch(dataType) {
case SHORT:
case INT16:
return new ShortValue((short) (value.asShort() / (short) channel.getScalingFactor()));
case UINT16:
case INTEGER:
case INT32:
return new IntValue(value.asInt() / (int) channel.getScalingFactor());
case UINT32:
case LONG:
return new LongValue(value.asLong() / (long) channel.getScalingFactor());
case FLOAT:
return new FloatValue(value.asFloat() / (float) channel.getScalingFactor());
case DOUBLE:
return new DoubleValue(value.asDouble() / channel.getScalingFactor());
default:
}
}
return value;
}
use of org.openmuc.framework.data.FloatValue in project OpenMUC by isc-konstanz.
the class OpcChannel method setValue.
@Override
public void setValue(AttributeContext context, VariableNode node, DataValue value) throws UaException {
Variant variant = value.getValue();
Record record = null;
switch(getValueType()) {
case BOOLEAN:
record = new Record(new BooleanValue((Boolean) variant.getValue()), value.getServerTime().getUtcTime());
break;
case BYTE:
record = new Record(new ByteValue((Byte) variant.getValue()), value.getServerTime().getUtcTime());
break;
case SHORT:
record = new Record(new ShortValue((Short) variant.getValue()), value.getServerTime().getUtcTime());
break;
case INTEGER:
record = new Record(new IntValue((Integer) variant.getValue()), value.getServerTime().getUtcTime());
break;
case LONG:
record = new Record(new LongValue((Long) variant.getValue()), value.getServerTime().getUtcTime());
break;
case FLOAT:
record = new Record(new FloatValue((Float) variant.getValue()), value.getServerTime().getUtcTime());
break;
case DOUBLE:
record = new Record(new DoubleValue((Double) variant.getValue()), value.getServerTime().getUtcTime());
break;
default:
record = new Record(new StringValue((String) variant.getValue()), value.getServerTime().getUtcTime());
break;
}
setRecord(record);
}
use of org.openmuc.framework.data.FloatValue in project OpenMUC by isc-konstanz.
the class Iec61850ConnectionTest method testWrite.
@Test
public void testWrite() throws IOException, ServiceError, ConfigurationException, javax.naming.ConfigurationException, SclParseException, InterruptedException, UnsupportedOperationException, ConnectionException {
System.out.println("Attempting to connect to server " + host + " on port " + port);
clientAssociation = clientSap.associate(InetAddress.getByName(host), port, null, this);
ServerModel serverModel = SclParser.parse("src/test/resources/testOpenmuc.icd").get(0);
clientAssociation.setServerModel(serverModel);
getAllBdas(serverModel, clientAssociation);
// ------------SCAN FOR CHANNELS-------------------
Iec61850Connection testIec61850Connection = new Iec61850Connection(clientAssociation, serverModel);
List<ChannelScanInfo> testChannelScanList = testIec61850Connection.scanForChannels("");
// ----------WRITE-----------------
List<ChannelValueContainer> testChannelValueContainers = new ArrayList<>();
byte[] newValue = { 0x44 };
testChannelValueContainers.add(new ChannelValueContainerImpl(testChannelScanList.get(14).getChannelAddress(), new ByteArrayValue(newValue)));
testChannelValueContainers.add(new ChannelValueContainerImpl(testChannelScanList.get(25).getChannelAddress(), new FloatValue((float) 12.5)));
testChannelValueContainers.add(new ChannelValueContainerImpl(testChannelScanList.get(24).getChannelAddress(), new BooleanValue(true)));
testIec61850Connection.write(testChannelValueContainers, null);
// Create record container to read the changes made by "write"
List<ChannelRecordContainer> testRecordContainers = new ArrayList<>();
for (int i = 0; i < 34; i++) {
testRecordContainers.add(new ChannelRecordContainerImpl(testChannelScanList.get(i).getChannelAddress()));
}
testIec61850Connection.read(testRecordContainers, null, "");
Assert.assertEquals("[68]", testRecordContainers.get(14).getRecord().getValue().toString());
Assert.assertEquals("12.5", testRecordContainers.get(25).getRecord().getValue().toString());
Assert.assertEquals("true", testRecordContainers.get(24).getRecord().getValue().toString());
}
use of org.openmuc.framework.data.FloatValue 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();
}
Aggregations