2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mqtt.handler;
15 import java.util.HashMap;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
21 import org.openhab.core.io.transport.mqtt.MqttConnectionState;
22 import org.openhab.core.io.transport.mqtt.MqttService;
23 import org.openhab.core.io.transport.mqtt.MqttServiceObserver;
24 import org.openhab.core.io.transport.mqtt.MqttWillAndTestament;
25 import org.openhab.core.io.transport.mqtt.reconnect.PeriodicReconnectStrategy;
26 import org.openhab.core.thing.Bridge;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.thing.ThingStatusDetail;
31 * This handler does not much except providing all information from a
32 * {@link MqttBrokerConnection} via Thing properties and put the Thing
33 * offline or online depending on the connection.
35 * @author David Graeff - Initial contribution
38 public class SystemBrokerHandler extends AbstractBrokerHandler implements MqttServiceObserver {
40 public static final String PROPERTY_URL = "url";
41 public static final String PROPERTY_USERNAME = "username";
42 public static final String PROPERTY_PASSWORD = "password";
43 public static final String PROPERTY_QOS = "qos";
44 public static final String PROPERTY_RETAIN = "retain";
45 public static final String PROPERTY_LAST_WILL = "lastwill";
46 public static final String PROPERTY_RECONNECT_TIME = "reconnect_time_ms";
47 public static final String PROPERTY_KEEP_ALIVE_TIME = "keep_alive_time_ms";
48 public static final String PROPERTY_CONNECT_TIMEOUT = "connect_timeout_ms";
50 protected final MqttService service;
52 protected String brokerID = "";
53 protected boolean discoveryEnabled = true;
55 public SystemBrokerHandler(Bridge thing, MqttService service) {
57 this.service = service;
61 public void connectionStateChanged(MqttConnectionState state, @Nullable Throwable error) {
62 Map<String, String> properties = new HashMap<>();
64 properties.put(PROPERTY_URL, connection.getHost() + ":" + String.valueOf(connection.getPort()));
65 final String username = connection.getUser();
66 final String password = connection.getPassword();
67 if (username != null && password != null) {
68 properties.put(PROPERTY_USERNAME, username);
69 properties.put(PROPERTY_PASSWORD, password);
71 properties.put(PROPERTY_QOS, String.valueOf(connection.getQos()));
72 final MqttWillAndTestament lastWill = connection.getLastWill();
73 if (lastWill != null) {
74 properties.put(PROPERTY_LAST_WILL, lastWill.toString());
76 properties.put(PROPERTY_LAST_WILL, "");
78 if (connection.getReconnectStrategy() instanceof PeriodicReconnectStrategy) {
79 final PeriodicReconnectStrategy strategy = (PeriodicReconnectStrategy) connection.getReconnectStrategy();
80 if (strategy != null) {
81 properties.put(PROPERTY_RECONNECT_TIME, String.valueOf(strategy.getReconnectFrequency()));
84 properties.put(PROPERTY_KEEP_ALIVE_TIME, String.valueOf(connection.getKeepAliveInterval()));
86 updateProperties(properties);
87 super.connectionStateChanged(state, error);
91 * The base implementation will set the connection variable to the given broker
92 * if it matches the brokerID and will start to connect to the broker if there
93 * is no connection established yet.
96 @SuppressWarnings("PMD.CompareObjectsWithEquals")
97 public void brokerAdded(String connectionName, MqttBrokerConnection addedConnection) {
98 if (!connectionName.equals(brokerID) || connection == addedConnection) {
102 this.connection = addedConnection;
107 public void brokerRemoved(String connectionName, MqttBrokerConnection removedConnection) {
108 final MqttBrokerConnection connection = this.connection;
109 if (removedConnection.equals(connection)) {
110 connection.removeConnectionObserver(this);
111 this.connection = null;
112 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "@text/offline.sharedremoved");
118 public void initialize() {
119 this.brokerID = getThing().getConfiguration().get("brokerid").toString();
120 this.discoveryEnabled = (Boolean) getThing().getConfiguration().get("enableDiscovery");
122 service.addBrokersListener(this);
124 connection = service.getBrokerConnection(brokerID);
125 if (connection == null) {
126 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
127 "@text/offline.notextualconfig [\"" + brokerID + "\"");
134 public void dispose() {
135 service.removeBrokersListener(this);
140 public boolean discoveryEnabled() {
141 return discoveryEnabled;