Search in sources :

Example 1 with MqttService

use of org.eclipse.smarthome.io.transport.mqtt.MqttService in project smarthome by eclipse.

the class MqttBrokerConnectionServiceInstance method modified.

/**
 * Create broker connections based on the service configuration. This will disconnect and
 * discard all existing textual configured brokers.
 */
@Modified
public void modified(@Nullable Map<String, Object> configMap) {
    if (connection != null) {
        connection.stop();
    }
    if (configMap == null || configMap.isEmpty() || mqttService == null) {
        return;
    }
    @NonNull final MqttService service = (@NonNull MqttService) mqttService;
    // Parse configuration
    MqttBrokerConnectionConfig config = new Configuration(configMap).as(MqttBrokerConnectionConfig.class);
    try {
        // Compute brokerID and make sure it is not empty
        String brokerID = config.getBrokerID();
        if (StringUtils.isBlank(brokerID) || brokerID == null) {
            logger.warn("Ignore invalid broker connection configuration: {}", config);
            return;
        }
        // Add connection and make sure it succeeded
        MqttBrokerConnection c = service.addBrokerConnection(brokerID, config);
        connection = c;
        if (c == null) {
            logger.warn("Ignore existing broker connection configuration for: {}", brokerID);
            return;
        }
        // Start connection
        c.start();
    } catch (ConfigurationException | IllegalArgumentException e) {
        logger.warn("MqttBroker connection configuration faulty: {}", e.getMessage());
    } catch (MqttException e) {
        logger.warn("MqttBroker start failed: {}", e.getMessage(), e);
    }
}
Also used : MqttBrokerConnection(org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection) MqttBrokerConnectionConfig(org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnectionConfig) Configuration(org.eclipse.smarthome.config.core.Configuration) ConfigurationException(org.osgi.service.cm.ConfigurationException) MqttException(org.eclipse.smarthome.io.transport.mqtt.MqttException) NonNull(org.eclipse.jdt.annotation.NonNull) MqttService(org.eclipse.smarthome.io.transport.mqtt.MqttService) Modified(org.osgi.service.component.annotations.Modified)

Aggregations

NonNull (org.eclipse.jdt.annotation.NonNull)1 Configuration (org.eclipse.smarthome.config.core.Configuration)1 MqttBrokerConnection (org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection)1 MqttBrokerConnectionConfig (org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnectionConfig)1 MqttException (org.eclipse.smarthome.io.transport.mqtt.MqttException)1 MqttService (org.eclipse.smarthome.io.transport.mqtt.MqttService)1 ConfigurationException (org.osgi.service.cm.ConfigurationException)1 Modified (org.osgi.service.component.annotations.Modified)1