Search in sources :

Example 1 with FloatValue

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

the class OptionValue method getFromDomNode.

static OptionValue getFromDomNode(String id, Node node) throws ParseException {
    OptionValue option = new OptionValue(id);
    Node valueDefaultNode = null;
    Node valueSelectionNode = null;
    NodeList childNodes = node.getChildNodes();
    try {
        for (int j = 0; j < childNodes.getLength(); j++) {
            Node childNode = childNodes.item(j);
            String childNodeName = childNode.getNodeName();
            if (childNodeName.equals("#text")) {
                continue;
            } else if (childNodeName.equals("name")) {
                option.name = childNode.getTextContent().trim();
            } else if (childNodeName.equals("description")) {
                option.description = Options.trimDomNodeText(childNode);
            } else if (childNodeName.equals("mandatory")) {
                String mandatoryString = childNode.getTextContent().trim().toLowerCase();
                if (mandatoryString.equals("true")) {
                    option.mandatory = true;
                } else if (mandatoryString.equals("false")) {
                    option.mandatory = false;
                } else {
                    throw new ParseException("Option \"mandatory\" contains neither \"true\" nor \"false\"");
                }
            } else if (childNodeName.equals("type")) {
                String valueTypeString = childNode.getTextContent().trim().toUpperCase();
                try {
                    option.type = ValueType.valueOf(valueTypeString);
                } catch (IllegalArgumentException e) {
                    throw new ParseException("Unknown option value type found:" + valueTypeString);
                }
            } else if (childNodeName.equals("default")) {
                valueDefaultNode = childNode;
            } else if (childNodeName.equals("selection")) {
                valueSelectionNode = childNode;
            } else {
                throw new ParseException("Unknown tag found:" + childNodeName);
            }
        }
        if (option.name == null) {
            option.name = id;
        }
        if (valueDefaultNode != null) {
            Value valueDefault = new StringValue(valueDefaultNode.getTextContent().trim());
            // Verify default values to be of the specified value type
            switch(option.type) {
                case FLOAT:
                    option.valueDefault = new FloatValue(valueDefault.asFloat());
                    break;
                case DOUBLE:
                    option.valueDefault = new DoubleValue(valueDefault.asDouble());
                    break;
                case SHORT:
                    option.valueDefault = new ShortValue(valueDefault.asShort());
                    break;
                case INTEGER:
                    option.valueDefault = new IntValue(valueDefault.asInt());
                    break;
                case LONG:
                    option.valueDefault = new LongValue(valueDefault.asLong());
                    break;
                case BYTE:
                    option.valueDefault = new ByteValue(valueDefault.asByte());
                    break;
                case BYTE_ARRAY:
                    byte[] arr;
                    if (!valueDefault.asString().startsWith("0x")) {
                        arr = valueDefault.asByteArray();
                    } else {
                        try {
                            arr = OptionValue.hexToBytes(valueDefault.asString().substring(2).trim());
                        } catch (IllegalArgumentException e) {
                            throw new ParseException(e);
                        }
                    }
                    option.valueDefault = new ByteArrayValue(arr);
                    break;
                case BOOLEAN:
                    option.valueDefault = new BooleanValue(valueDefault.asBoolean());
                    break;
                case STRING:
                    option.valueDefault = valueDefault;
                    break;
                default:
                    break;
            }
        }
        if (valueSelectionNode != null) {
            option.valueSelection = OptionSelection.getFromDomNode(valueSelectionNode, option.type);
        }
    } catch (IllegalArgumentException e) {
        throw new ParseException(e);
    }
    return option;
}
Also used : ByteValue(org.openmuc.framework.data.ByteValue) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) ShortValue(org.openmuc.framework.data.ShortValue) DoubleValue(org.openmuc.framework.data.DoubleValue) BooleanValue(org.openmuc.framework.data.BooleanValue) ByteArrayValue(org.openmuc.framework.data.ByteArrayValue) DoubleValue(org.openmuc.framework.data.DoubleValue) ByteValue(org.openmuc.framework.data.ByteValue) ShortValue(org.openmuc.framework.data.ShortValue) StringValue(org.openmuc.framework.data.StringValue) 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) ParseException(org.openmuc.framework.config.ParseException) FloatValue(org.openmuc.framework.data.FloatValue) ByteArrayValue(org.openmuc.framework.data.ByteArrayValue) StringValue(org.openmuc.framework.data.StringValue) IntValue(org.openmuc.framework.data.IntValue)

Example 2 with FloatValue

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

the class LogFileWriterTest method getGroup.

private static LogIntervalContainerGroup getGroup(long timeStamp, int i, boolean boolValue, byte byteValue, String testString) {
    LogIntervalContainerGroup group = new LogIntervalContainerGroup();
    LoggingRecord container1 = new LoggingRecord(ch01, new Record(new FloatValue(i * -7 - 0.555F), timeStamp));
    LoggingRecord container2 = new LoggingRecord(ch02, new Record(new DoubleValue(i * +7 - 0.555), timeStamp));
    LoggingRecord container3 = new LoggingRecord(ch03, new Record(new BooleanValue(boolValue), timeStamp));
    LoggingRecord container4 = new LoggingRecord(ch04, new Record(new ShortValue((short) i), timeStamp));
    LoggingRecord container5 = new LoggingRecord(ch05, new Record(new IntValue(i), timeStamp));
    LoggingRecord container6 = new LoggingRecord(ch06, new Record(new LongValue(i * 1000000), timeStamp));
    LoggingRecord container7 = new LoggingRecord(ch07, new Record(new ByteValue(byteValue), timeStamp));
    LoggingRecord container8 = new LoggingRecord(ch08, new Record(new StringValue(testString), timeStamp));
    LoggingRecord container9 = new LoggingRecord(ch09, new Record(new ByteArrayValue(testByteArray), timeStamp));
    group.add(container1);
    group.add(container2);
    group.add(container3);
    group.add(container4);
    group.add(container5);
    group.add(container6);
    group.add(container7);
    group.add(container8);
    group.add(container9);
    return group;
}
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) LongValue(org.openmuc.framework.data.LongValue) LogIntervalContainerGroup(org.openmuc.framework.datalogger.ascii.LogIntervalContainerGroup) Record(org.openmuc.framework.data.Record) LoggingRecord(org.openmuc.framework.datalogger.spi.LoggingRecord) FloatValue(org.openmuc.framework.data.FloatValue) ByteArrayValue(org.openmuc.framework.data.ByteArrayValue) StringValue(org.openmuc.framework.data.StringValue) LoggingRecord(org.openmuc.framework.datalogger.spi.LoggingRecord) IntValue(org.openmuc.framework.data.IntValue)

Example 3 with FloatValue

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

the class ChannelImpl method convertValidRecord.

private Record convertValidRecord(Record record) {
    Double scalingFactor = config.getScalingFactor();
    Double scalingOffset = config.getValueOffset();
    if (scalingFactor != null) {
        try {
            record = new Record(new DoubleValue(record.getValue().asDouble() * scalingFactor), record.getTimestamp(), record.getFlag());
        } catch (TypeConversionException e) {
            String msg = "Unable to apply scaling factor to channel " + config.getId() + " because a TypeConversionError occurred.";
            logger.error(msg, e);
        }
    }
    if (scalingOffset != null) {
        try {
            record = new Record(new DoubleValue(record.getValue().asDouble() + scalingOffset), record.getTimestamp(), record.getFlag());
        } catch (TypeConversionException e) {
            String msg = "Unable to apply scaling offset to channel " + config.getId() + " because a TypeConversionError occurred.";
            logger.error(msg, e);
        }
    }
    try {
        switch(config.getValueType()) {
            case BOOLEAN:
                return new Record(new BooleanValue(record.getValue().asBoolean()), record.getTimestamp(), record.getFlag());
            case BYTE:
                return new Record(new ByteValue(record.getValue().asByte()), record.getTimestamp(), record.getFlag());
            case SHORT:
                return new Record(new ShortValue(record.getValue().asShort()), record.getTimestamp(), record.getFlag());
            case INTEGER:
                return new Record(new IntValue(record.getValue().asInt()), record.getTimestamp(), record.getFlag());
            case LONG:
                return new Record(new LongValue(record.getValue().asLong()), record.getTimestamp(), record.getFlag());
            case FLOAT:
                return new Record(new FloatValue(record.getValue().asFloat()), record.getTimestamp(), record.getFlag());
            case DOUBLE:
                return new Record(new DoubleValue(record.getValue().asDouble()), record.getTimestamp(), record.getFlag());
            case BYTE_ARRAY:
                return new Record(new ByteArrayValue(record.getValue().asByteArray()), record.getTimestamp(), record.getFlag());
            case STRING:
            default:
                return new Record(new StringValue(record.getValue().toString()), record.getTimestamp(), record.getFlag());
        }
    } catch (TypeConversionException e) {
        logger.error("Unable to convert value to configured value type because a TypeConversionError occured.", e);
        return new Record(Flag.DRIVER_ERROR_CHANNEL_VALUE_TYPE_CONVERSION_EXCEPTION);
    }
}
Also used : ByteValue(org.openmuc.framework.data.ByteValue) TypeConversionException(org.openmuc.framework.data.TypeConversionException) 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) ByteArrayValue(org.openmuc.framework.data.ByteArrayValue) StringValue(org.openmuc.framework.data.StringValue) IntValue(org.openmuc.framework.data.IntValue)

Example 4 with FloatValue

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

the class FromJson method convertValue.

public static Value convertValue(Object value, ValueType type) throws ClassCastException {
    if (value.getClass().isInstance(new RestValue())) {
        value = ((RestValue) value).getValue();
    }
    switch(type) {
        case FLOAT:
            return new FloatValue(((Double) value).floatValue());
        case DOUBLE:
            return new DoubleValue((Double) value);
        case SHORT:
            return new ShortValue(((Double) value).shortValue());
        case INTEGER:
            return new IntValue(((Double) value).intValue());
        case LONG:
            return new LongValue(((Double) value).longValue());
        case BYTE:
            return new ByteValue(((Double) value).byteValue());
        case BOOLEAN:
            return new BooleanValue((Boolean) value);
        case BYTE_ARRAY:
            @SuppressWarnings("unchecked") List<Double> arrayList = ((ArrayList<Double>) value);
            byte[] byteArray = new byte[arrayList.size()];
            for (int i = 0; i < arrayList.size(); ++i) {
                byteArray[i] = arrayList.get(i).byteValue();
            }
            return new ByteArrayValue(byteArray);
        case STRING:
            return new StringValue((String) value);
        default:
            // should not occur
            return new StringValue(value.toString());
    }
}
Also used : ByteValue(org.openmuc.framework.data.ByteValue) ShortValue(org.openmuc.framework.data.ShortValue) DoubleValue(org.openmuc.framework.data.DoubleValue) RestValue(org.openmuc.framework.lib.rest.objects.RestValue) BooleanValue(org.openmuc.framework.data.BooleanValue) 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)

Example 5 with FloatValue

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

the class OpcChannel method decode.

public Record decode(DataValue data) {
    long timestamp = data.getServerTime().getJavaTime();
    Object value = data.getValue().getValue();
    switch(getValueType()) {
        case BOOLEAN:
            return new Record(new BooleanValue((Boolean) value), timestamp);
        case BYTE:
            return new Record(new ByteValue((Byte) value), timestamp);
        case SHORT:
            return new Record(new ShortValue((Short) value), timestamp);
        case INTEGER:
            return new Record(new IntValue((Integer) value), timestamp);
        case LONG:
            return new Record(new LongValue((Long) value), timestamp);
        case FLOAT:
            return new Record(new FloatValue((Float) value), timestamp);
        case DOUBLE:
            return new Record(new DoubleValue((Double) value), timestamp);
        default:
            return new Record(new StringValue((String) value), timestamp);
    }
}
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) 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)

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