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