]> git.basschouten.com Git - openhab-addons.git/blob
83b1012d9ec58637bc1428327b2447a9b91e4d63
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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     public void brokerAdded(String connectionName, MqttBrokerConnection addedConnection) {
97         if (!connectionName.equals(brokerID) || connection == addedConnection) {
98             return;
99         }
100
101         this.connection = addedConnection;
102         super.initialize();
103     }
104
105     @Override
106     public void brokerRemoved(String connectionName, MqttBrokerConnection removedConnection) {
107         final MqttBrokerConnection connection = this.connection;
108         if (removedConnection.equals(connection)) {
109             connection.removeConnectionObserver(this);
110             this.connection = null;
111             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "@text/offline.sharedremoved");
112             return;
113         }
114     }
115
116     @Override
117     public void initialize() {
118         this.brokerID = getThing().getConfiguration().get("brokerid").toString();
119         this.discoveryEnabled = (Boolean) getThing().getConfiguration().get("enableDiscovery");
120
121         service.addBrokersListener(this);
122
123         connection = service.getBrokerConnection(brokerID);
124         if (connection == null) {
125             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
126                     "@text/offline.notextualconfig [\"" + brokerID + "\"");
127             return;
128         }
129         super.initialize();
130     }
131
132     @Override
133     public void dispose() {
134         service.removeBrokersListener(this);
135         super.dispose();
136     }
137
138     @Override
139     public boolean discoveryEnabled() {
140         return discoveryEnabled;
141     }
142 }