Search in sources :

Example 1 with DoubleValue

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

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

the class LogFileReaderTestSingleFile method setup.

@BeforeAll
public static void setup() {
    System.out.println("### Setup() LogFileReaderTestSingleFile");
    TestUtils.createTestFolder();
    // File file = new File(TestUtils.TESTFOLDERPATH + fileDate0 + "_" + loggingInterval + ext);
    // if (file.exists()) {
    // Do nothing, file exists.
    // }
    // else {
    // eine Datei
    channelIds = new String[] { "power" };
    // Logs 1 channel in second interval from 1 to 3 o'clock
    HashMap<String, LogChannel> logChannelList = new HashMap<>();
    LogChannelTestImpl ch1 = new LogChannelTestImpl(Channel0Name, "", "dummy description", "kW", ValueType.DOUBLE, 0.0, 0.0, false, 1000, 0, "", loggingInterval, loggingTimeOffset, false, false);
    logChannelList.put(Channel0Name, ch1);
    Calendar calendar = TestUtils.stringToDate(dateFormat, fileDate0 + " 01:00:00");
    for (int i = 0; i < ((60 * 60 * 2) * (1000d / loggingInterval)); i++) {
        LoggingRecord container1 = new LoggingRecord(Channel0Name, new Record(new DoubleValue(i), calendar.getTimeInMillis()));
        LogIntervalContainerGroup group = new LogIntervalContainerGroup();
        group.add(container1);
        LogFileWriter lfw = new LogFileWriter(TestUtils.TESTFOLDERPATH, false);
        lfw.log(group, loggingInterval, 0, calendar, logChannelList);
        AsciiLogger.setLastLoggedLineTimeStamp(loggingInterval, 0, calendar.getTimeInMillis());
        calendar.add(Calendar.MILLISECOND, loggingInterval);
    }
// }
}
Also used : HashMap(java.util.HashMap) DoubleValue(org.openmuc.framework.data.DoubleValue) Calendar(java.util.Calendar) LogChannel(org.openmuc.framework.datalogger.spi.LogChannel) LogIntervalContainerGroup(org.openmuc.framework.datalogger.ascii.LogIntervalContainerGroup) Record(org.openmuc.framework.data.Record) LoggingRecord(org.openmuc.framework.datalogger.spi.LoggingRecord) LogFileWriter(org.openmuc.framework.datalogger.ascii.LogFileWriter) LoggingRecord(org.openmuc.framework.datalogger.spi.LoggingRecord) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 3 with DoubleValue

use of org.openmuc.framework.data.DoubleValue 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 4 with DoubleValue

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

the class LoggerUtilsTest method tc_505_test_findLatestValue.

@Test
public void tc_505_test_findLatestValue() {
    Map<String, List<Record>> recordsMap = new HashMap<>();
    for (int j = 0; j < 5; j++) {
        List<Record> records = new LinkedList<>();
        for (int i = 0; i < 20; i++) {
            long timestamp = i;
            DoubleValue value = new DoubleValue(i + j);
            Record record = new Record(value, timestamp);
            records.add(record);
        }
        recordsMap.put("channel" + j, records);
    }
    Map<String, Record> latestValue = LoggerUtils.findLatestValue(recordsMap);
    for (int j = 0; j < 5; j++) {
        Double actual = latestValue.get("channel" + j).getValue().asDouble();
        Double expected = 19.0 + j;
        assertEquals(expected, actual);
    }
}
Also used : HashMap(java.util.HashMap) DoubleValue(org.openmuc.framework.data.DoubleValue) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Record(org.openmuc.framework.data.Record) LoggingRecord(org.openmuc.framework.datalogger.spi.LoggingRecord) LinkedList(java.util.LinkedList) Test(org.junit.jupiter.api.Test)

Example 5 with DoubleValue

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

Aggregations

DoubleValue (org.openmuc.framework.data.DoubleValue)31 Record (org.openmuc.framework.data.Record)26 StringValue (org.openmuc.framework.data.StringValue)14 IntValue (org.openmuc.framework.data.IntValue)12 Value (org.openmuc.framework.data.Value)12 ByteArrayValue (org.openmuc.framework.data.ByteArrayValue)11 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)10 LoggingRecord (org.openmuc.framework.datalogger.spi.LoggingRecord)10 ByteValue (org.openmuc.framework.data.ByteValue)8 ArrayList (java.util.ArrayList)5 Flag (org.openmuc.framework.data.Flag)4 HashMap (java.util.HashMap)3 Test (org.junit.jupiter.api.Test)3 LogIntervalContainerGroup (org.openmuc.framework.datalogger.ascii.LogIntervalContainerGroup)3 Read (org.openmuc.framework.driver.annotation.Read)3 Calendar (java.util.Calendar)2 LinkedList (java.util.LinkedList)2