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);
}
}
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);
}
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));
}
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));
}
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);
});
}
Aggregations