Search in sources :

Example 1 with MqttSettings

use of org.openmuc.framework.lib.mqtt.MqttSettings in project OpenMUC by isc-konstanz.

the class MqttDriverConnection method getMqttSettings.

private MqttSettings getMqttSettings(String host, String settings) throws ArgumentSyntaxException {
    settings = settings.replaceAll(";", "\n");
    try {
        this.settings.load(new StringReader(settings));
    } catch (IOException e) {
        throw new ArgumentSyntaxException("Could not read settings string");
    }
    int port = Integer.parseInt(this.settings.getProperty("port"));
    String username = this.settings.getProperty("username");
    String password = this.settings.getProperty("password");
    boolean ssl = Boolean.parseBoolean(this.settings.getProperty("ssl"));
    long maxBufferSize = Long.parseLong(this.settings.getProperty("maxBufferSize", "0"));
    long maxFileSize = Long.parseLong(this.settings.getProperty("maxFileSize", "0"));
    int maxFileCount = Integer.parseInt(this.settings.getProperty("maxFileCount", "1"));
    int connectionRetryInterval = Integer.parseInt(this.settings.getProperty("connectionRetryInterval", "10"));
    int connectionAliveInterval = Integer.parseInt(this.settings.getProperty("connectionAliveInterval", "10"));
    String persistenceDirectory = this.settings.getProperty("persistenceDirectory", "data/driver/mqtt");
    String lastWillTopic = this.settings.getProperty("lastWillTopic", "");
    byte[] lastWillPayload = this.settings.getProperty("lastWillPayload", "").getBytes();
    boolean lastWillAlways = Boolean.parseBoolean(this.settings.getProperty("lastWillAlways", "false"));
    String firstWillTopic = this.settings.getProperty("firstWillTopic", "");
    byte[] firstWillPayload = this.settings.getProperty("firstWillPayload", "").getBytes();
    boolean webSocket = Boolean.parseBoolean(this.settings.getProperty("webSocket", "false"));
    return new MqttSettings(host, port, username, password, ssl, maxBufferSize, maxFileSize, maxFileCount, connectionRetryInterval, connectionAliveInterval, persistenceDirectory, lastWillTopic, lastWillPayload, lastWillAlways, firstWillTopic, firstWillPayload, webSocket);
}
Also used : StringReader(java.io.StringReader) IOException(java.io.IOException) ArgumentSyntaxException(org.openmuc.framework.config.ArgumentSyntaxException) MqttSettings(org.openmuc.framework.lib.mqtt.MqttSettings)

Example 2 with MqttSettings

use of org.openmuc.framework.lib.mqtt.MqttSettings 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 MqttSettings

use of org.openmuc.framework.lib.mqtt.MqttSettings 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

MqttSettings (org.openmuc.framework.lib.mqtt.MqttSettings)3 MqttConnection (org.openmuc.framework.lib.mqtt.MqttConnection)2 MqttWriter (org.openmuc.framework.lib.mqtt.MqttWriter)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 ArgumentSyntaxException (org.openmuc.framework.config.ArgumentSyntaxException)1 PropertyHandler (org.openmuc.framework.lib.osgi.config.PropertyHandler)1