Search in sources :

Example 6 with FloatValue

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;
}
Also used : ShortValue(org.openmuc.framework.data.ShortValue) DoubleValue(org.openmuc.framework.data.DoubleValue) LongValue(org.openmuc.framework.data.LongValue) Record(org.openmuc.framework.data.Record) FloatValue(org.openmuc.framework.data.FloatValue) IntValue(org.openmuc.framework.data.IntValue)

Example 7 with FloatValue

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;
}
Also used : ShortValue(org.openmuc.framework.data.ShortValue) DoubleValue(org.openmuc.framework.data.DoubleValue) DoubleValue(org.openmuc.framework.data.DoubleValue) ShortValue(org.openmuc.framework.data.ShortValue) Value(org.openmuc.framework.data.Value) LongValue(org.openmuc.framework.data.LongValue) FloatValue(org.openmuc.framework.data.FloatValue) IntValue(org.openmuc.framework.data.IntValue) LongValue(org.openmuc.framework.data.LongValue) Record(org.openmuc.framework.data.Record) FloatValue(org.openmuc.framework.data.FloatValue) IntValue(org.openmuc.framework.data.IntValue)

Example 8 with FloatValue

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);
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) ByteValue(org.openmuc.framework.data.ByteValue) ShortValue(org.openmuc.framework.data.ShortValue) DoubleValue(org.openmuc.framework.data.DoubleValue) BooleanValue(org.openmuc.framework.data.BooleanValue) LongValue(org.openmuc.framework.data.LongValue) Record(org.openmuc.framework.data.Record) FloatValue(org.openmuc.framework.data.FloatValue) StringValue(org.openmuc.framework.data.StringValue) IntValue(org.openmuc.framework.data.IntValue)

Example 9 with FloatValue

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());
}
Also used : ArrayList(java.util.ArrayList) ChannelScanInfo(org.openmuc.framework.config.ChannelScanInfo) ChannelRecordContainer(org.openmuc.framework.driver.spi.ChannelRecordContainer) ServerModel(com.beanit.iec61850bean.ServerModel) BooleanValue(org.openmuc.framework.data.BooleanValue) ByteArrayValue(org.openmuc.framework.data.ByteArrayValue) FloatValue(org.openmuc.framework.data.FloatValue) ChannelValueContainer(org.openmuc.framework.driver.spi.ChannelValueContainer) Test(org.junit.jupiter.api.Test)

Example 10 with FloatValue

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();
}
Also used : ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) ChannelRecordContainer(org.openmuc.framework.driver.spi.ChannelRecordContainer) ShortValue(org.openmuc.framework.data.ShortValue) DoubleValue(org.openmuc.framework.data.DoubleValue) ByteArrayValue(org.openmuc.framework.data.ByteArrayValue) StringValue(org.openmuc.framework.data.StringValue) Value(org.openmuc.framework.data.Value) FloatValue(org.openmuc.framework.data.FloatValue) DoubleValue(org.openmuc.framework.data.DoubleValue) ByteValue(org.openmuc.framework.data.ByteValue) ShortValue(org.openmuc.framework.data.ShortValue) FutureValue(org.openmuc.framework.data.FutureValue) BooleanValue(org.openmuc.framework.data.BooleanValue) LongValue(org.openmuc.framework.data.LongValue) IntValue(org.openmuc.framework.data.IntValue) LongValue(org.openmuc.framework.data.LongValue) Record(org.openmuc.framework.data.Record) FloatValue(org.openmuc.framework.data.FloatValue) IntValue(org.openmuc.framework.data.IntValue)

Aggregations

FloatValue (org.openmuc.framework.data.FloatValue)12 DoubleValue (org.openmuc.framework.data.DoubleValue)11 IntValue (org.openmuc.framework.data.IntValue)11 LongValue (org.openmuc.framework.data.LongValue)11 ShortValue (org.openmuc.framework.data.ShortValue)11 BooleanValue (org.openmuc.framework.data.BooleanValue)10 ByteArrayValue (org.openmuc.framework.data.ByteArrayValue)8 ByteValue (org.openmuc.framework.data.ByteValue)8 StringValue (org.openmuc.framework.data.StringValue)8 Record (org.openmuc.framework.data.Record)7 Value (org.openmuc.framework.data.Value)5 ArrayList (java.util.ArrayList)2 ChannelRecordContainer (org.openmuc.framework.driver.spi.ChannelRecordContainer)2 ServerModel (com.beanit.iec61850bean.ServerModel)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Variant (org.eclipse.milo.opcua.stack.core.types.builtin.Variant)1 Test (org.junit.jupiter.api.Test)1 ArgumentSyntaxException (org.openmuc.framework.config.ArgumentSyntaxException)1 ChannelScanInfo (org.openmuc.framework.config.ChannelScanInfo)1 ParseException (org.openmuc.framework.config.ParseException)1