Search in sources :

Example 1 with MqttLogMsgBuilder

use of org.openmuc.framework.datalogger.mqtt.util.MqttLogMsgBuilder in project OpenMUC by isc-konstanz.

the class MqttLogger method log.

@Override
public void log(List<LoggingRecord> loggingRecordList, long timestamp) {
    if (!isLoggerReady()) {
        logger.warn("Skipped logging values, still loading");
        return;
    }
    // logger.info("============================");
    // loggingRecordList.stream().map(LoggingRecord::getChannelId).forEach(id -> logger.info(id));
    // FIXME refactor OpenMUC core - actually the datamanager should only call logger.log()
    // with channels configured for this logger. If this is the case the containsKey check could be ignored
    // The filter serves as WORKAROUND to process only channels which were configured for mqtt logger
    List<LoggingRecord> logRecordsForMqttLogger = loggingRecordList.stream().filter(record -> channelsToLog.containsKey(record.getChannelId())).collect(Collectors.toList());
    // channelsToLog.values().stream().map(channel -> channel.topic).distinct().count();
    // Concept of the MqttLogMsgBuilder:
    // 1. cleaner code
    // 2. better testability: MqttLogMsgBuilder can be easily created in a test and the output of
    // MqttLogMsgBuilder.build() can be verified. It takes the input from logger.log() method, processes it
    // and creates ready to use messages for the mqttWriter
    MqttLogMsgBuilder logMsgBuilder = new MqttLogMsgBuilder(channelsToLog, availableParsers.get(parser));
    List<MqttLogMsg> logMessages = logMsgBuilder.buildLogMsg(logRecordsForMqttLogger, isLogMultiple);
    for (MqttLogMsg msg : logMessages) {
        logTraceMqttMessage(msg);
        mqttWriter.write(msg.topic, msg.message);
    }
}
Also used : ServicePropertyException(org.openmuc.framework.lib.osgi.config.ServicePropertyException) MqttLogMsgBuilder(org.openmuc.framework.datalogger.mqtt.util.MqttLogMsgBuilder) MqttWriter(org.openmuc.framework.lib.mqtt.MqttWriter) Logger(org.slf4j.Logger) MqttLogChannel(org.openmuc.framework.datalogger.mqtt.dto.MqttLogChannel) MqttConnection(org.openmuc.framework.lib.mqtt.MqttConnection) MqttSettings(org.openmuc.framework.lib.mqtt.MqttSettings) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) MqttLogMsg(org.openmuc.framework.datalogger.mqtt.dto.MqttLogMsg) List(java.util.List) SslManagerInterface(org.openmuc.framework.security.SslManagerInterface) PropertyHandler(org.openmuc.framework.lib.osgi.config.PropertyHandler) Record(org.openmuc.framework.data.Record) LoggingRecord(org.openmuc.framework.datalogger.spi.LoggingRecord) DataLoggerService(org.openmuc.framework.datalogger.spi.DataLoggerService) DictionaryPreprocessor(org.openmuc.framework.lib.osgi.config.DictionaryPreprocessor) ManagedService(org.osgi.service.cm.ManagedService) Dictionary(java.util.Dictionary) ParserService(org.openmuc.framework.parser.spi.ParserService) LogChannel(org.openmuc.framework.datalogger.spi.LogChannel) MqttLogMsg(org.openmuc.framework.datalogger.mqtt.dto.MqttLogMsg) MqttLogMsgBuilder(org.openmuc.framework.datalogger.mqtt.util.MqttLogMsgBuilder) LoggingRecord(org.openmuc.framework.datalogger.spi.LoggingRecord)

Example 2 with MqttLogMsgBuilder

use of org.openmuc.framework.datalogger.mqtt.util.MqttLogMsgBuilder in project OpenMUC by isc-konstanz.

the class MqttLogMsgBuilderTest method test_logTwoChannels_sameTopic_multipleTrue.

@Test
public void test_logTwoChannels_sameTopic_multipleTrue() {
    // 1. prepare channels to log - equal to logger.setChannelsToLog(...) call
    HashMap<String, MqttLogChannel> channelsToLog = new HashMap<>();
    channelsToLog.put(logChannelMockA.getId(), new MqttLogChannel(logChannelMockA));
    channelsToLog.put(logChannelMockB.getId(), new MqttLogChannel(logChannelMockB));
    // 2. apply settings to logger
    boolean isLogMultiple = true;
    // 3. prepare records which should be logged
    List<LoggingRecord> records = new ArrayList<>();
    records.add(new LoggingRecord(logChannelMockA.getId(), record3));
    records.add(new LoggingRecord(logChannelMockB.getId(), record5));
    // 4. equal to calling logger.log(..) method
    MqttLogMsgBuilder builder = new MqttLogMsgBuilder(channelsToLog, parser);
    List<MqttLogMsg> messages = builder.buildLogMsg(records, isLogMultiple);
    printDebug(isDebugEnabled, messages);
    // expected size = 1 since isLogMultiple = true;
    assertEquals(1, messages.size());
    // check content of the messages
    StringBuilder sbRef = new StringBuilder();
    sbRef.append(TOPIC_1 + ": {\"timestamp\":" + TIMESTAMP + ",\"flag\":\"VALID\",\"value\":3.0}");
    sbRef.append("\n");
    sbRef.append("{\"timestamp\":" + TIMESTAMP + ",\"flag\":\"VALID\",\"value\":5.0}");
    sbRef.append("\n");
    String referenceString = sbRef.toString();
    StringBuilder sbTest = new StringBuilder();
    sbTest.append(TOPIC_1 + ": ").append(new String(messages.get(0).message));
    String testString = sbTest.toString();
    assertEquals(referenceString, testString);
}
Also used : MqttLogChannel(org.openmuc.framework.datalogger.mqtt.dto.MqttLogChannel) MqttLogMsg(org.openmuc.framework.datalogger.mqtt.dto.MqttLogMsg) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MqttLogMsgBuilder(org.openmuc.framework.datalogger.mqtt.util.MqttLogMsgBuilder) LoggingRecord(org.openmuc.framework.datalogger.spi.LoggingRecord) Test(org.junit.jupiter.api.Test)

Example 3 with MqttLogMsgBuilder

use of org.openmuc.framework.datalogger.mqtt.util.MqttLogMsgBuilder in project OpenMUC by isc-konstanz.

the class MqttLogMsgBuilderTest method test_logSingleChannel_multipleTrue.

@Test
public void test_logSingleChannel_multipleTrue() {
    // 1. prepare channels to log - equal to logger.setChannelsToLog(...) call
    HashMap<String, MqttLogChannel> channelsToLog = new HashMap<>();
    channelsToLog.put(logChannelMockA.getId(), new MqttLogChannel(logChannelMockA));
    // 2. apply settings to logger
    boolean isLogMultiple = true;
    // 3. prepare records which should be logged
    List<LoggingRecord> records = new ArrayList<>();
    records.add(new LoggingRecord(logChannelMockA.getId(), record3));
    // 4. equal to calling logger.log(..) method
    MqttLogMsgBuilder builder = new MqttLogMsgBuilder(channelsToLog, parser);
    List<MqttLogMsg> messages = builder.buildLogMsg(records, isLogMultiple);
    printDebug(isDebugEnabled, messages);
    String controlString = TOPIC_1 + ": {\"timestamp\":" + TIMESTAMP + ",\"flag\":\"VALID\",\"value\":3.0}" + "\n";
    assertEquals(controlString, TOPIC_1 + ": " + new String(messages.get(0).message));
}
Also used : MqttLogChannel(org.openmuc.framework.datalogger.mqtt.dto.MqttLogChannel) MqttLogMsg(org.openmuc.framework.datalogger.mqtt.dto.MqttLogMsg) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MqttLogMsgBuilder(org.openmuc.framework.datalogger.mqtt.util.MqttLogMsgBuilder) LoggingRecord(org.openmuc.framework.datalogger.spi.LoggingRecord) Test(org.junit.jupiter.api.Test)

Example 4 with MqttLogMsgBuilder

use of org.openmuc.framework.datalogger.mqtt.util.MqttLogMsgBuilder in project OpenMUC by isc-konstanz.

the class MqttLogMsgBuilderTest method test_logTwoChannels_sameTopic_multipleFalse.

@Test
public void test_logTwoChannels_sameTopic_multipleFalse() {
    // 1. prepare channels to log - equal to logger.setChannelsToLog(...) call
    HashMap<String, MqttLogChannel> channelsToLog = new HashMap<>();
    channelsToLog.put(logChannelMockA.getId(), new MqttLogChannel(logChannelMockA));
    channelsToLog.put(logChannelMockB.getId(), new MqttLogChannel(logChannelMockB));
    // 2. apply settings to logger
    boolean isLogMultiple = false;
    // 3. prepare records which should be logged
    List<LoggingRecord> records = new ArrayList<>();
    records.add(new LoggingRecord(logChannelMockA.getId(), record3));
    records.add(new LoggingRecord(logChannelMockB.getId(), record5));
    // 4. equal to calling logger.log(..) method
    MqttLogMsgBuilder builder = new MqttLogMsgBuilder(channelsToLog, parser);
    List<MqttLogMsg> messages = builder.buildLogMsg(records, isLogMultiple);
    printDebug(isDebugEnabled, messages);
    // expected size = 2 since isLogMultiple = false;
    assertEquals(2, messages.size());
    // check content of the messages
    String referenceString1 = TOPIC_1 + ": {\"timestamp\":" + TIMESTAMP + ",\"flag\":\"VALID\",\"value\":3.0}";
    String referenceString2 = TOPIC_1 + ": {\"timestamp\":" + TIMESTAMP + ",\"flag\":\"VALID\",\"value\":5.0}";
    assertEquals(referenceString1, TOPIC_1 + ": " + new String(messages.get(0).message));
    assertEquals(referenceString2, TOPIC_1 + ": " + new String(messages.get(1).message));
}
Also used : MqttLogChannel(org.openmuc.framework.datalogger.mqtt.dto.MqttLogChannel) MqttLogMsg(org.openmuc.framework.datalogger.mqtt.dto.MqttLogMsg) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MqttLogMsgBuilder(org.openmuc.framework.datalogger.mqtt.util.MqttLogMsgBuilder) LoggingRecord(org.openmuc.framework.datalogger.spi.LoggingRecord) Test(org.junit.jupiter.api.Test)

Example 5 with MqttLogMsgBuilder

use of org.openmuc.framework.datalogger.mqtt.util.MqttLogMsgBuilder in project OpenMUC by isc-konstanz.

the class MqttLogMsgBuilderTest method test_logTwoChannels_differentTopic_multipleTrue.

@Test
public void test_logTwoChannels_differentTopic_multipleTrue() {
    // 1. prepare channels to log - equal to logger.setChannelsToLog(...) call
    HashMap<String, MqttLogChannel> channelsToLog = new HashMap<>();
    channelsToLog.put(logChannelMockA.getId(), new MqttLogChannel(logChannelMockA));
    channelsToLog.put(logChannelMockC.getId(), new MqttLogChannel(logChannelMockC));
    // 2. apply settings to logger
    boolean isLogMultiple = true;
    // 3. prepare records which should be logged
    List<LoggingRecord> records = new ArrayList<>();
    records.add(new LoggingRecord(logChannelMockA.getId(), record3));
    records.add(new LoggingRecord(logChannelMockC.getId(), record7));
    // 4. equal to calling logger.log(..) method
    MqttLogMsgBuilder builder = new MqttLogMsgBuilder(channelsToLog, parser);
    Assertions.assertThrows(UnsupportedOperationException.class, () -> {
        List<MqttLogMsg> messages = builder.buildLogMsg(records, isLogMultiple);
    });
}
Also used : MqttLogChannel(org.openmuc.framework.datalogger.mqtt.dto.MqttLogChannel) MqttLogMsg(org.openmuc.framework.datalogger.mqtt.dto.MqttLogMsg) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MqttLogMsgBuilder(org.openmuc.framework.datalogger.mqtt.util.MqttLogMsgBuilder) LoggingRecord(org.openmuc.framework.datalogger.spi.LoggingRecord) Test(org.junit.jupiter.api.Test)

Aggregations

HashMap (java.util.HashMap)6 MqttLogChannel (org.openmuc.framework.datalogger.mqtt.dto.MqttLogChannel)6 MqttLogMsg (org.openmuc.framework.datalogger.mqtt.dto.MqttLogMsg)6 MqttLogMsgBuilder (org.openmuc.framework.datalogger.mqtt.util.MqttLogMsgBuilder)6 LoggingRecord (org.openmuc.framework.datalogger.spi.LoggingRecord)6 ArrayList (java.util.ArrayList)5 Test (org.junit.jupiter.api.Test)5 IOException (java.io.IOException)1 Dictionary (java.util.Dictionary)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Record (org.openmuc.framework.data.Record)1 DataLoggerService (org.openmuc.framework.datalogger.spi.DataLoggerService)1 LogChannel (org.openmuc.framework.datalogger.spi.LogChannel)1 MqttConnection (org.openmuc.framework.lib.mqtt.MqttConnection)1 MqttSettings (org.openmuc.framework.lib.mqtt.MqttSettings)1 MqttWriter (org.openmuc.framework.lib.mqtt.MqttWriter)1 DictionaryPreprocessor (org.openmuc.framework.lib.osgi.config.DictionaryPreprocessor)1 PropertyHandler (org.openmuc.framework.lib.osgi.config.PropertyHandler)1 ServicePropertyException (org.openmuc.framework.lib.osgi.config.ServicePropertyException)1