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.tradfri.internal.handler;
15 import static org.openhab.core.thing.Thing.*;
18 import java.net.URISyntaxException;
19 import java.util.concurrent.TimeUnit;
21 import org.eclipse.californium.core.CoapObserveRelation;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.tradfri.internal.CoapCallback;
25 import org.openhab.binding.tradfri.internal.TradfriCoapClient;
26 import org.openhab.binding.tradfri.internal.config.TradfriDeviceConfig;
27 import org.openhab.binding.tradfri.internal.model.TradfriDeviceData;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.ThingStatusDetail;
32 import org.openhab.core.thing.ThingStatusInfo;
33 import org.openhab.core.thing.binding.BaseThingHandler;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * The {@link TradfriThingHandler} is the abstract base class for individual device handlers.
40 * @author Kai Kreuzer - Initial contribution
41 * @author Christoph Weitkamp - Restructuring and refactoring of the binding
44 public abstract class TradfriThingHandler extends BaseThingHandler implements CoapCallback {
46 private final Logger logger = LoggerFactory.getLogger(TradfriThingHandler.class);
48 // the unique instance id of the device
49 protected @Nullable Integer id;
51 // used to check whether we have already been disposed when receiving data asynchronously
52 protected volatile boolean active;
54 protected @Nullable TradfriCoapClient coapClient;
56 private @Nullable CoapObserveRelation observeRelation;
58 public TradfriThingHandler(Thing thing) {
63 @SuppressWarnings("null")
64 public synchronized void initialize() {
65 Bridge tradfriGateway = getBridge();
66 this.id = getConfigAs(TradfriDeviceConfig.class).id;
67 TradfriGatewayHandler handler = (TradfriGatewayHandler) tradfriGateway.getHandler();
70 updateStatus(ThingStatus.UNKNOWN);
71 switch (tradfriGateway.getStatus()) {
73 String uriString = handler.getGatewayURI() + "/" + id;
75 URI uri = new URI(uriString);
76 coapClient = new TradfriCoapClient(uri);
77 coapClient.setEndpoint(handler.getEndpoint());
78 } catch (URISyntaxException e) {
79 logger.debug("Illegal device URI `{}`: {}", uriString, e.getMessage());
80 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
84 scheduler.schedule(() -> {
85 observeRelation = coapClient.startObserve(this);
86 }, 3, TimeUnit.SECONDS);
90 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE,
91 String.format("Gateway offline '%s'", tradfriGateway.getStatusInfo()));
97 public synchronized void dispose() {
99 if (observeRelation != null) {
100 observeRelation.reactiveCancel();
101 observeRelation = null;
103 if (coapClient != null) {
104 coapClient.shutdown();
110 @SuppressWarnings("null")
111 public void setStatus(ThingStatus status, ThingStatusDetail statusDetail) {
112 if (active && getBridge().getStatus() != ThingStatus.OFFLINE && status != ThingStatus.ONLINE) {
113 updateStatus(status, statusDetail);
114 // we are offline and lost our observe relation - let's try to establish the connection in 10 seconds again
115 scheduler.schedule(() -> {
116 if (observeRelation != null) {
117 observeRelation.reactiveCancel();
118 observeRelation = null;
120 observeRelation = coapClient.startObserve(this);
121 }, 10, TimeUnit.SECONDS);
126 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
127 super.bridgeStatusChanged(bridgeStatusInfo);
128 // the status might have changed because the bridge is completely reconfigured - so we need to re-establish
129 // our CoAP connection as well
130 if (bridgeStatusInfo.getStatus() == ThingStatus.OFFLINE) {
132 } else if (bridgeStatusInfo.getStatus() == ThingStatus.ONLINE) {
137 protected void set(String payload) {
138 TradfriCoapClient coapClient = this.coapClient;
139 if (coapClient != null) {
140 logger.debug("Sending payload: {}", payload);
141 coapClient.asyncPut(payload, this, scheduler);
143 logger.debug("coapClient is null!");
147 protected void updateDeviceProperties(TradfriDeviceData state) {
148 String firmwareVersion = state.getFirmwareVersion();
149 if (firmwareVersion != null) {
150 getThing().setProperty(PROPERTY_FIRMWARE_VERSION, firmwareVersion);
153 String modelId = state.getModelId();
154 if (modelId != null) {
155 getThing().setProperty(PROPERTY_MODEL_ID, modelId);
158 String vendor = state.getVendor();
159 if (vendor != null) {
160 getThing().setProperty(PROPERTY_VENDOR, vendor);