]> git.basschouten.com Git - openhab-addons.git/blob
4fcf2404e2233f06cfc8f75b7942311ef9725bc0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mqtt.handler;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
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;
29
30 /**
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.
34  *
35  * @author David Graeff - Initial contribution
36  */
37 @NonNullByDefault
38 public class SystemBrokerHandler extends AbstractBrokerHandler implements MqttServiceObserver {
39     // Properties
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";
49
50     protected final MqttService service;
51
52     protected String brokerID = "";
53     protected boolean discoveryEnabled = true;
54
55     public SystemBrokerHandler(Bridge thing, MqttService service) {
56         super(thing);
57         this.service = service;
58     }
59
60     @Override
61     public void connectionStateChanged(MqttConnectionState state, @Nullable Throwable error) {
62         Map<String, String> properties = new HashMap<>();
63
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);
70         }
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());
75         } else {
76             properties.put(PROPERTY_LAST_WILL, "");
77         }
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()));
82             }
83         }
84         properties.put(PROPERTY_KEEP_ALIVE_TIME, String.valueOf(connection.getKeepAliveInterval()));
85
86         updateProperties(properties);
87         super.connectionStateChanged(state, error);
88     }
89
90     /**
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.
94      */
95     @Override
96     @SuppressWarnings("PMD.CompareObjectsWithEquals")
97     public void brokerAdded(String connectionName, MqttBrokerConnection addedConnection) {
98         if (!connectionName.equals(brokerID) || connection == addedConnection) {
99             return;
100         }
101
102         this.connection = addedConnection;
103         super.initialize();
104     }
105
106     @Override
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");
113             return;
114         }
115     }
116
117     @Override
118     public void initialize() {
119         this.brokerID = getThing().getConfiguration().get("brokerid").toString();
120         this.discoveryEnabled = (Boolean) getThing().getConfiguration().get("enableDiscovery");
121
122         service.addBrokersListener(this);
123
124         connection = service.getBrokerConnection(brokerID);
125         if (connection == null) {
126             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
127                     "@text/offline.notextualconfig [\"" + brokerID + "\"");
128             return;
129         }
130         super.initialize();
131     }
132
133     @Override
134     public void dispose() {
135         service.removeBrokersListener(this);
136         super.dispose();
137     }
138
139     @Override
140     public boolean discoveryEnabled() {
141         return discoveryEnabled;
142     }
143 }