use of org.openmuc.framework.lib.mqtt.MqttWriter 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.lib.mqtt.MqttWriter in project OpenMUC by isc-konstanz.
the class MqttLogger method connect.
/**
* Connect to MQTT broker
*/
private void connect() {
MqttSettings settings = createMqttSettings();
MqttConnection connection = new MqttConnection(settings);
connection.setSslManager(sslManager);
mqttWriter = new MqttWriter(connection, getId());
if (settings.isSsl()) {
if (isLoggerReady()) {
logger.info("Connecting to MQTT Broker");
mqttWriter.getConnection().connect();
} else {
logger.info("Writer is not ready yet");
}
} else {
logger.info("Connecting to MQTT Broker");
mqttWriter.getConnection().connect();
}
}
use of org.openmuc.framework.lib.mqtt.MqttWriter in project OpenMUC by isc-konstanz.
the class MqttLoggerTest method connect.
// @BeforeAll
static void connect() {
String packageName = MqttLogger.class.getPackage().getName().toLowerCase();
System.setProperty(packageName + ".host", "localhost");
System.setProperty(packageName + ".port", "1883");
System.setProperty(packageName + ".username", "guest");
System.setProperty(packageName + ".password", "guest");
System.setProperty(packageName + ".topic", "device/data");
System.setProperty(packageName + ".maxFileCount", "2");
System.setProperty(packageName + ".maxFileSize", "1");
System.setProperty(packageName + ".maxBufferSize", "1");
String pid = MqttLogger.class.getName();
MqttLoggerSettings settings = new MqttLoggerSettings();
PropertyHandler propertyHandler = new PropertyHandler(settings, pid);
MqttSettings Mqttsettings = new MqttSettings(propertyHandler.getString(MqttLoggerSettings.HOST), propertyHandler.getInt(MqttLoggerSettings.PORT), propertyHandler.getString(MqttLoggerSettings.USERNAME), propertyHandler.getString(MqttLoggerSettings.PASSWORD), propertyHandler.getBoolean(MqttLoggerSettings.SSL), propertyHandler.getInt(MqttLoggerSettings.MAX_BUFFER_SIZE), propertyHandler.getInt(MqttLoggerSettings.MAX_FILE_SIZE), propertyHandler.getInt(MqttLoggerSettings.MAX_FILE_COUNT), propertyHandler.getInt(MqttLoggerSettings.CONNECTION_RETRY_INTERVAL), propertyHandler.getInt(MqttLoggerSettings.CONNECTION_ALIVE_INTERVAL), propertyHandler.getString(MqttLoggerSettings.PERSISTENCE_DIRECTORY));
MqttConnection connection = new MqttConnection(Mqttsettings);
MqttWriter mqttWriter = new MqttWriter(connection, "mqttlogger");
mqttWriter.getConnection().connect();
}
Aggregations