2 * Copyright (c) 2010-2023 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.deconz.internal.handler;
15 import static org.openhab.binding.deconz.internal.BindingConstants.*;
16 import static org.openhab.binding.deconz.internal.Util.buildUrl;
18 import java.net.SocketTimeoutException;
19 import java.util.Collection;
20 import java.util.Collections;
22 import java.util.Objects;
23 import java.util.Optional;
25 import java.util.concurrent.CompletableFuture;
26 import java.util.concurrent.CompletionException;
27 import java.util.concurrent.ScheduledFuture;
28 import java.util.concurrent.TimeUnit;
29 import java.util.concurrent.TimeoutException;
31 import org.eclipse.jdt.annotation.NonNullByDefault;
32 import org.eclipse.jdt.annotation.Nullable;
33 import org.openhab.binding.deconz.internal.discovery.ThingDiscoveryService;
34 import org.openhab.binding.deconz.internal.dto.ApiKeyMessage;
35 import org.openhab.binding.deconz.internal.dto.BridgeFullState;
36 import org.openhab.binding.deconz.internal.netutils.AsyncHttpClient;
37 import org.openhab.binding.deconz.internal.netutils.WebSocketConnection;
38 import org.openhab.binding.deconz.internal.netutils.WebSocketConnectionListener;
39 import org.openhab.core.cache.ExpiringCacheAsync;
40 import org.openhab.core.config.core.Configuration;
41 import org.openhab.core.io.net.http.WebSocketFactory;
42 import org.openhab.core.thing.Bridge;
43 import org.openhab.core.thing.ChannelUID;
44 import org.openhab.core.thing.ThingStatus;
45 import org.openhab.core.thing.ThingStatusDetail;
46 import org.openhab.core.thing.ThingTypeUID;
47 import org.openhab.core.thing.binding.BaseBridgeHandler;
48 import org.openhab.core.thing.binding.ThingHandlerService;
49 import org.openhab.core.thing.util.ThingWebClientUtil;
50 import org.openhab.core.types.Command;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 import com.google.gson.Gson;
57 * The bridge Thing is responsible for requesting all available sensors and switches and propagate
58 * them to the discovery service.
60 * It performs the authorization process if necessary.
62 * A websocket connection is established to the deCONZ software and kept alive.
64 * @author David Graeff - Initial contribution
67 public class DeconzBridgeHandler extends BaseBridgeHandler implements WebSocketConnectionListener {
68 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(BRIDGE_TYPE);
70 private final Logger logger = LoggerFactory.getLogger(DeconzBridgeHandler.class);
71 private final WebSocketConnection websocket;
72 private final AsyncHttpClient http;
73 private DeconzBridgeConfig config = new DeconzBridgeConfig();
74 private final Gson gson;
75 private @Nullable ScheduledFuture<?> scheduledFuture;
76 private int websocketPort = 0;
77 /** Prevent a dispose/init cycle while this flag is set. Use for property updates */
78 private boolean ignoreConfigurationUpdate;
79 private boolean thingDisposing = false;
81 private final ExpiringCacheAsync<Optional<BridgeFullState>> fullStateCache = new ExpiringCacheAsync<>(1000);
83 /** The poll frequency for the API Key verification */
84 private static final int POLL_FREQUENCY_SEC = 10;
86 public DeconzBridgeHandler(Bridge thing, WebSocketFactory webSocketFactory, AsyncHttpClient http, Gson gson) {
90 String websocketID = ThingWebClientUtil.buildWebClientConsumerName(thing.getUID(), null);
91 this.websocket = new WebSocketConnection(this, webSocketFactory.createWebSocketClient(websocketID), gson);
95 public Collection<Class<? extends ThingHandlerService>> getServices() {
96 return Set.of(ThingDiscoveryService.class);
100 public void handleConfigurationUpdate(Map<String, Object> configurationParameters) {
101 if (!ignoreConfigurationUpdate) {
102 super.handleConfigurationUpdate(configurationParameters);
107 public void handleCommand(ChannelUID channelUID, Command command) {
111 * Stops the API request or websocket reconnect timer
113 private void stopTimer() {
114 ScheduledFuture<?> future = scheduledFuture;
115 if (future != null) {
117 scheduledFuture = null;
122 * Parses the response message to the API key generation REST API.
124 * @param r The response
126 private void parseAPIKeyResponse(AsyncHttpClient.Result r) {
127 if (thingDisposing) {
128 // discard response if thing handler is already disposing
131 if (r.getResponseCode() == 403) {
132 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING,
133 "Allow authentication for 3rd party apps. Trying again in " + POLL_FREQUENCY_SEC + " seconds");
135 scheduledFuture = scheduler.schedule(this::requestApiKey, POLL_FREQUENCY_SEC, TimeUnit.SECONDS);
136 } else if (r.getResponseCode() == 200) {
137 ApiKeyMessage[] response = Objects.requireNonNull(gson.fromJson(r.getBody(), ApiKeyMessage[].class));
138 if (response.length == 0) {
139 throw new IllegalStateException("Authorisation request response is empty");
141 config.apikey = response[0].success.username;
142 Configuration configuration = editConfiguration();
143 configuration.put(CONFIG_APIKEY, config.apikey);
144 updateConfiguration(configuration);
145 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING, "Waiting for configuration");
146 initializeBridgeState();
148 throw new IllegalStateException("Unknown status code for authorisation request");
153 * get the full state of the bridge from the cache
155 * @return a CompletableFuture that returns an Optional of the bridge full state
157 public CompletableFuture<Optional<BridgeFullState>> getBridgeFullState() {
158 return fullStateCache.getValue(this::refreshFullStateCache);
162 * refresh the full bridge state (used for initial processing and state-lookup)
164 * @return Completable future with an Optional of the BridgeFullState
166 private CompletableFuture<Optional<BridgeFullState>> refreshFullStateCache() {
167 logger.trace("{} starts refreshing the fullStateCache", thing.getUID());
168 if (config.apikey == null || thingDisposing) {
169 return CompletableFuture.completedFuture(Optional.empty());
171 String url = buildUrl(config.getHostWithoutPort(), config.httpPort, config.apikey);
172 return http.get(url, config.timeout).thenApply(r -> {
173 if (r.getResponseCode() == 403) {
174 return Optional.<BridgeFullState> empty();
175 } else if (r.getResponseCode() == 200) {
176 return Optional.ofNullable(gson.fromJson(r.getBody(), BridgeFullState.class));
178 throw new IllegalStateException("Unknown status code for full state request");
180 }).handle((v, t) -> {
183 } else if (t instanceof SocketTimeoutException || t instanceof TimeoutException
184 || t instanceof CompletionException) {
185 logger.debug("Get full state failed", t);
187 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, t.getMessage());
189 return Optional.empty();
194 * Perform a request to the REST API for retrieving the full bridge state with all sensors and switches
197 public void initializeBridgeState() {
198 getBridgeFullState().thenAccept(fullState -> fullState.ifPresentOrElse(state -> {
199 if (thingDisposing) {
200 // discard response if thing handler is already disposing
203 if (state.config.name.isEmpty()) {
204 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE,
205 "You are connected to a HUE bridge, not a deCONZ software!");
208 if (state.config.websocketport == 0) {
209 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE,
210 "deCONZ software too old. No websocket support!");
214 // Add some information about the bridge
215 Map<String, String> editProperties = editProperties();
216 editProperties.put("apiversion", state.config.apiversion);
217 editProperties.put("swversion", state.config.swversion);
218 editProperties.put("fwversion", state.config.fwversion);
219 editProperties.put("uuid", state.config.uuid);
220 editProperties.put("zigbeechannel", String.valueOf(state.config.zigbeechannel));
221 editProperties.put("ipaddress", state.config.ipaddress);
222 ignoreConfigurationUpdate = true;
223 updateProperties(editProperties);
224 ignoreConfigurationUpdate = false;
226 // Use requested websocket port if no specific port is given
227 websocketPort = config.port == 0 ? state.config.websocketport : config.port;
230 // initial response was empty, re-trying in POLL_FREQUENCY_SEC seconds
231 if (!thingDisposing) {
232 scheduledFuture = scheduler.schedule(this::initializeBridgeState, POLL_FREQUENCY_SEC, TimeUnit.SECONDS);
234 })).exceptionally(e -> {
236 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, e.getMessage());
238 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE);
240 logger.warn("Initial full state request or result parsing failed", e);
241 if (!thingDisposing) {
242 scheduledFuture = scheduler.schedule(this::initializeBridgeState, POLL_FREQUENCY_SEC, TimeUnit.SECONDS);
249 * Starts the websocket connection.
250 * {@link #initializeBridgeState} need to be called first to obtain the websocket port.
252 private void startWebsocket() {
253 if (websocket.isConnected() || websocketPort == 0 || thingDisposing) {
258 scheduledFuture = scheduler.schedule(this::startWebsocket, POLL_FREQUENCY_SEC, TimeUnit.SECONDS);
260 websocket.start(config.getHostWithoutPort() + ":" + websocketPort);
264 * Perform a request to the REST API for generating an API key.
267 private void requestApiKey() {
268 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING, "Requesting API Key");
270 String url = buildUrl(config.getHostWithoutPort(), config.httpPort);
271 http.post(url, "{\"devicetype\":\"openHAB\"}", config.timeout).thenAccept(this::parseAPIKeyResponse)
272 .exceptionally(e -> {
273 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
274 logger.warn("Authorisation failed", e);
280 public void initialize() {
281 logger.debug("Start initializing bridge {}", thing.getUID());
282 thingDisposing = false;
283 config = getConfigAs(DeconzBridgeConfig.class);
284 if (config.apikey == null) {
287 initializeBridgeState();
292 public void dispose() {
293 thingDisposing = true;
299 public void connectionEstablished() {
301 updateStatus(ThingStatus.ONLINE);
305 public void connectionLost(String reason) {
306 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, reason);
309 // Wait for POLL_FREQUENCY_SEC after a connection was closed before trying again
310 scheduledFuture = scheduler.schedule(this::startWebsocket, POLL_FREQUENCY_SEC, TimeUnit.SECONDS);
314 * Return the websocket connection.
316 public WebSocketConnection getWebsocketConnection() {
321 * Send an object to the gateway
323 * @param endPoint the endpoint (e.g. "lights/2/state")
324 * @param object the object (or null if no object)
325 * @return CompletableFuture of the result
327 public CompletableFuture<AsyncHttpClient.Result> sendObject(String endPoint, @Nullable Object object) {
328 String json = object == null ? null : gson.toJson(object);
329 String url = buildUrl(config.host, config.httpPort, config.apikey, endPoint);
330 logger.trace("Sending {} via {}", json, url);
332 return http.put(url, json, config.timeout);