]> git.basschouten.com Git - openhab-addons.git/blob
b70dd77383ff9ed74108be314cc098027bc00fb6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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         properties.put(PROPERTY_RETAIN, String.valueOf(connection.isRetain()));
73         final MqttWillAndTestament lastWill = connection.getLastWill();
74         if (lastWill != null) {
75             properties.put(PROPERTY_LAST_WILL, lastWill.toString());
76         } else {
77             properties.put(PROPERTY_LAST_WILL, "");
78         }
79         if (connection.getReconnectStrategy() instanceof PeriodicReconnectStrategy) {
80             final PeriodicReconnectStrategy strategy = (PeriodicReconnectStrategy) connection.getReconnectStrategy();
81             if (strategy != null) {
82                 properties.put(PROPERTY_RECONNECT_TIME, String.valueOf(strategy.getReconnectFrequency()));
83             }
84         }
85         properties.put(PROPERTY_KEEP_ALIVE_TIME, String.valueOf(connection.getKeepAliveInterval()));
86
87         updateProperties(properties);
88         super.connectionStateChanged(state, error);
89     }
90
91     /**
92      * The base implementation will set the connection variable to the given broker
93      * if it matches the brokerID and will start to connect to the broker if there
94      * is no connection established yet.
95      */
96     @Override
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 }