Search in sources :

Example 1 with SerializationException

use of org.openmuc.framework.parser.spi.SerializationException in project OpenMUC by isc-konstanz.

the class MqttLogMsgBuilder method logSingle.

private List<MqttLogMsg> logSingle(List<LoggingRecord> loggingRecords) {
    List<MqttLogMsg> logMessages = new ArrayList<>();
    for (LoggingRecord loggingRecord : loggingRecords) {
        try {
            String topic = channelsToLog.get(loggingRecord.getChannelId()).topic;
            byte[] message = parserService.serialize(loggingRecord);
            logMessages.add(new MqttLogMsg(loggingRecord.getChannelId(), message, topic));
        } catch (SerializationException e) {
            logger.error("failed to parse records {}", e.getMessage());
        }
    }
    return logMessages;
}
Also used : MqttLogMsg(org.openmuc.framework.datalogger.mqtt.dto.MqttLogMsg) SerializationException(org.openmuc.framework.parser.spi.SerializationException) ArrayList(java.util.ArrayList) LoggingRecord(org.openmuc.framework.datalogger.spi.LoggingRecord)

Example 2 with SerializationException

use of org.openmuc.framework.parser.spi.SerializationException in project OpenMUC by isc-konstanz.

the class AmqpDriverConnection method write.

@Override
public Object write(List<ChannelValueContainer> containers, Object containerListHandle) throws UnsupportedOperationException {
    for (ChannelValueContainer container : containers) {
        Record record = new Record(container.getValue(), System.currentTimeMillis());
        if (parsers.containsKey(setting.parser)) {
            byte[] message = new byte[0];
            try {
                message = parsers.get(setting.parser).serialize(record, container);
            } catch (SerializationException e) {
                logger.error(e.getMessage());
            }
            writer.write(container.getChannelAddress(), message);
            container.setFlag(Flag.VALID);
        } else {
            throw new UnsupportedOperationException("A parser is needed to write messages");
        }
    }
    return null;
}
Also used : SerializationException(org.openmuc.framework.parser.spi.SerializationException) Record(org.openmuc.framework.data.Record) ChannelValueContainer(org.openmuc.framework.driver.spi.ChannelValueContainer)

Example 3 with SerializationException

use of org.openmuc.framework.parser.spi.SerializationException in project OpenMUC by isc-konstanz.

the class MqttLogMsgBuilder method logMultiple.

private List<MqttLogMsg> logMultiple(List<LoggingRecord> loggingRecords) {
    List<MqttLogMsg> logMessages = new ArrayList<>();
    if (hasDifferentTopics()) {
        throw new UnsupportedOperationException("logMultiple feature is an experimental feature: logMultiple=true is not possible with " + "different topics in logSettings. Set logMultiple=false OR leave it true " + "and assign same topic to all channels.");
    // TODO make improvement: check only for given channels
    // TODO make improvement:
    // CASE A - OK
    // ch1, ch2, ch3 = 5 s - topic1
    // CASE B - NOT SUPPORTED YET
    // ch1, ch2 logInterval = 5 s - topic1
    // ch3, ch3 logInterval = 10 s - topic2
    // ch4 logInterval 20 s - topic 3
    // if isLogMultiple=true, then group channels per topic
    // or default: log warning and use logSingle instead
    } else {
        try {
            // since all topics are the same, get the topic of
            String topic = channelsToLog.get(loggingRecords.get(0).getChannelId()).topic;
            byte[] message = parserService.serialize(loggingRecords);
            String channelIds = loggingRecords.stream().map(record -> record.getChannelId()).collect(Collectors.toList()).toString();
            logMessages.add(new MqttLogMsg(channelIds, message, topic));
        } catch (SerializationException e) {
            logger.error("failed to parse records {}", e.getMessage());
        }
    }
    return logMessages;
}
Also used : MqttLogMsg(org.openmuc.framework.datalogger.mqtt.dto.MqttLogMsg) SerializationException(org.openmuc.framework.parser.spi.SerializationException) ArrayList(java.util.ArrayList)

Example 4 with SerializationException

use of org.openmuc.framework.parser.spi.SerializationException in project OpenMUC by isc-konstanz.

the class MqttDriverConnection method write.

@Override
public Object write(List<ChannelValueContainer> containers, Object containerListHandle) throws UnsupportedOperationException, ConnectionException {
    for (ChannelValueContainer container : containers) {
        Record record = new Record(container.getValue(), System.currentTimeMillis());
        if (parsers.containsKey(settings.getProperty("parser"))) {
            byte[] message;
            try {
                message = parsers.get(settings.getProperty("parser")).serialize(record, container);
            } catch (SerializationException e) {
                logger.error(e.getMessage());
                continue;
            }
            String topic = Stream.of(container.getChannelAddress().split(";")).findFirst().orElse(ChannelConfig.ADDRESS_DEFAULT);
            mqttWriter.write(topic, message);
            container.setFlag(Flag.VALID);
        } else {
            logger.error("A parser is needed to write messages and none have been registered.");
            throw new UnsupportedOperationException();
        }
    }
    return null;
}
Also used : SerializationException(org.openmuc.framework.parser.spi.SerializationException) Record(org.openmuc.framework.data.Record) ChannelValueContainer(org.openmuc.framework.driver.spi.ChannelValueContainer)

Aggregations

SerializationException (org.openmuc.framework.parser.spi.SerializationException)4 ArrayList (java.util.ArrayList)2 Record (org.openmuc.framework.data.Record)2 MqttLogMsg (org.openmuc.framework.datalogger.mqtt.dto.MqttLogMsg)2 ChannelValueContainer (org.openmuc.framework.driver.spi.ChannelValueContainer)2 LoggingRecord (org.openmuc.framework.datalogger.spi.LoggingRecord)1