Search in sources :

Example 1 with MqttWriter

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

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();
    }
}
Also used : MqttConnection(org.openmuc.framework.lib.mqtt.MqttConnection) MqttWriter(org.openmuc.framework.lib.mqtt.MqttWriter) MqttSettings(org.openmuc.framework.lib.mqtt.MqttSettings)

Example 3 with MqttWriter

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();
}
Also used : MqttConnection(org.openmuc.framework.lib.mqtt.MqttConnection) MqttWriter(org.openmuc.framework.lib.mqtt.MqttWriter) PropertyHandler(org.openmuc.framework.lib.osgi.config.PropertyHandler) MqttSettings(org.openmuc.framework.lib.mqtt.MqttSettings)

Aggregations

MqttConnection (org.openmuc.framework.lib.mqtt.MqttConnection)3 MqttSettings (org.openmuc.framework.lib.mqtt.MqttSettings)3 MqttWriter (org.openmuc.framework.lib.mqtt.MqttWriter)3 PropertyHandler (org.openmuc.framework.lib.osgi.config.PropertyHandler)2 IOException (java.io.IOException)1 Dictionary (java.util.Dictionary)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Record (org.openmuc.framework.data.Record)1 MqttLogChannel (org.openmuc.framework.datalogger.mqtt.dto.MqttLogChannel)1 MqttLogMsg (org.openmuc.framework.datalogger.mqtt.dto.MqttLogMsg)1 MqttLogMsgBuilder (org.openmuc.framework.datalogger.mqtt.util.MqttLogMsgBuilder)1 DataLoggerService (org.openmuc.framework.datalogger.spi.DataLoggerService)1 LogChannel (org.openmuc.framework.datalogger.spi.LogChannel)1 LoggingRecord (org.openmuc.framework.datalogger.spi.LoggingRecord)1 DictionaryPreprocessor (org.openmuc.framework.lib.osgi.config.DictionaryPreprocessor)1 ServicePropertyException (org.openmuc.framework.lib.osgi.config.ServicePropertyException)1 ParserService (org.openmuc.framework.parser.spi.ParserService)1 SslManagerInterface (org.openmuc.framework.security.SslManagerInterface)1