Search in sources :

Example 11 with IntValue

use of org.openmuc.framework.data.IntValue 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)

Example 12 with IntValue

use of org.openmuc.framework.data.IntValue in project OpenMUC by isc-konstanz.

the class Options method parseValue.

public static Value parseValue(ValueType type, String valueStr) throws ArgumentSyntaxException {
    Value value;
    switch(type) {
        case DOUBLE:
            value = new DoubleValue(Double.valueOf(valueStr));
            break;
        case FLOAT:
            value = new FloatValue(Float.valueOf(valueStr));
            break;
        case INTEGER:
            value = new IntValue(Integer.valueOf(valueStr));
            break;
        case LONG:
            value = new LongValue(Long.valueOf(valueStr));
            break;
        case SHORT:
            value = new ShortValue(Short.valueOf(valueStr));
            break;
        case BYTE:
            value = new ByteValue(Byte.valueOf(valueStr));
            break;
        case BOOLEAN:
            value = new BooleanValue(Boolean.valueOf(valueStr));
            break;
        case BYTE_ARRAY:
            byte[] arr;
            if (!valueStr.startsWith("0x")) {
                arr = valueStr.getBytes(StandardCharsets.US_ASCII);
            } else {
                try {
                    arr = OptionValue.hexToBytes(valueStr.substring(2).trim());
                } catch (IllegalArgumentException e) {
                    throw new ArgumentSyntaxException("Unable to parse value as byte array: " + valueStr);
                }
            }
            value = new ByteArrayValue(arr);
            break;
        case STRING:
            value = new StringValue(valueStr);
            break;
        default:
            throw new ArgumentSyntaxException("Parameter value type not configured: " + type.name().toLowerCase());
    }
    return value;
}
Also used : ByteValue(org.openmuc.framework.data.ByteValue) ShortValue(org.openmuc.framework.data.ShortValue) DoubleValue(org.openmuc.framework.data.DoubleValue) BooleanValue(org.openmuc.framework.data.BooleanValue) 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) BooleanValue(org.openmuc.framework.data.BooleanValue) LongValue(org.openmuc.framework.data.LongValue) IntValue(org.openmuc.framework.data.IntValue) LongValue(org.openmuc.framework.data.LongValue) FloatValue(org.openmuc.framework.data.FloatValue) ByteArrayValue(org.openmuc.framework.data.ByteArrayValue) StringValue(org.openmuc.framework.data.StringValue) IntValue(org.openmuc.framework.data.IntValue) ArgumentSyntaxException(org.openmuc.framework.config.ArgumentSyntaxException)

Example 13 with IntValue

use of org.openmuc.framework.data.IntValue in project OpenMUC by isc-konstanz.

the class ModbusDriverUtil method getValueFromByteArray.

@SuppressWarnings("deprecation")
public static Value getValueFromByteArray(byte[] registerAsByteArray, DataType datatype) {
    Value registerValue = null;
    switch(datatype) {
        // break;
        case SHORT:
        case INT16:
            registerValue = new ShortValue(ModbusUtil.registerToShort(registerAsByteArray));
            break;
        case INT32:
            registerValue = new IntValue(ModbusUtil.registersToInt(registerAsByteArray));
            break;
        // break;
        case UINT16:
            // TODO might need both: big/little endian support in settings
            // int uint16 = DatatypeConversion.bytes_To_UnsignedInt16(registerAsByteArray,
            // EndianInput.BYTES_ARE_BIG_ENDIAN);
            // registerValue = new IntValue(uint16);
            registerValue = new IntValue(ModbusUtil.registerToUnsignedShort(registerAsByteArray));
            break;
        case UINT32:
            // TODO might need both: big/little endian support in settings
            long uint32 = DataTypeConverter.bytesToUnsignedInt32(registerAsByteArray, EndianInput.BYTES_ARE_BIG_ENDIAN);
            registerValue = new LongValue(uint32);
            break;
        case FLOAT:
            registerValue = new FloatValue(ModbusUtil.registersToFloat(registerAsByteArray));
            break;
        case DOUBLE:
            registerValue = new DoubleValue(ModbusUtil.registersToDouble(registerAsByteArray));
            break;
        case LONG:
            registerValue = new LongValue(ModbusUtil.registersToLong(registerAsByteArray));
            break;
        case BYTEARRAY:
            registerValue = new ByteArrayValue(registerAsByteArray);
            break;
        case BYTEARRAYLONG:
            registerValue = new LongValue(DataTypeConverter.bytesToSignedInt64(registerAsByteArray, EndianInput.BYTES_ARE_LITTLE_ENDIAN));
            break;
        // break;
        default:
            throw new RuntimeException("Datatype " + datatype.toString() + " not supported yet");
    }
    return registerValue;
}
Also used : ShortValue(org.openmuc.framework.data.ShortValue) DoubleValue(org.openmuc.framework.data.DoubleValue) ByteArrayValue(org.openmuc.framework.data.ByteArrayValue) DoubleValue(org.openmuc.framework.data.DoubleValue) ShortValue(org.openmuc.framework.data.ShortValue) 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) LongValue(org.openmuc.framework.data.LongValue) FloatValue(org.openmuc.framework.data.FloatValue) ByteArrayValue(org.openmuc.framework.data.ByteArrayValue) IntValue(org.openmuc.framework.data.IntValue)

Aggregations

IntValue (org.openmuc.framework.data.IntValue)13 DoubleValue (org.openmuc.framework.data.DoubleValue)12 FloatValue (org.openmuc.framework.data.FloatValue)11 LongValue (org.openmuc.framework.data.LongValue)11 ShortValue (org.openmuc.framework.data.ShortValue)11 BooleanValue (org.openmuc.framework.data.BooleanValue)9 ByteValue (org.openmuc.framework.data.ByteValue)8 Record (org.openmuc.framework.data.Record)8 StringValue (org.openmuc.framework.data.StringValue)8 ByteArrayValue (org.openmuc.framework.data.ByteArrayValue)7 Value (org.openmuc.framework.data.Value)6 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Variant (org.eclipse.milo.opcua.stack.core.types.builtin.Variant)1 ArgumentSyntaxException (org.openmuc.framework.config.ArgumentSyntaxException)1 ParseException (org.openmuc.framework.config.ParseException)1 FutureValue (org.openmuc.framework.data.FutureValue)1 TypeConversionException (org.openmuc.framework.data.TypeConversionException)1 LogIntervalContainerGroup (org.openmuc.framework.datalogger.ascii.LogIntervalContainerGroup)1