]> git.basschouten.com Git - openhab-addons.git/blob
85277eb398be81825f4154f8a1788384a55d4fff
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.tradfri.internal.handler;
14
15 import static org.openhab.core.thing.Thing.*;
16
17 import java.net.URI;
18 import java.net.URISyntaxException;
19 import java.util.concurrent.TimeUnit;
20
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;
36
37 /**
38  * The {@link TradfriThingHandler} is the abstract base class for individual device handlers.
39  *
40  * @author Kai Kreuzer - Initial contribution
41  * @author Christoph Weitkamp - Restructuring and refactoring of the binding
42  */
43 @NonNullByDefault
44 public abstract class TradfriThingHandler extends BaseThingHandler implements CoapCallback {
45
46     private final Logger logger = LoggerFactory.getLogger(TradfriThingHandler.class);
47
48     // the unique instance id of the device
49     protected @Nullable Integer id;
50
51     // used to check whether we have already been disposed when receiving data asynchronously
52     protected volatile boolean active;
53
54     protected @Nullable TradfriCoapClient coapClient;
55
56     private @Nullable CoapObserveRelation observeRelation;
57
58     public TradfriThingHandler(Thing thing) {
59         super(thing);
60     }
61
62     @Override
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();
68
69         active = true;
70         updateStatus(ThingStatus.UNKNOWN);
71         switch (tradfriGateway.getStatus()) {
72             case ONLINE:
73                 String uriString = handler.getGatewayURI() + "/" + id;
74                 try {
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());
81                     return;
82                 }
83
84                 scheduler.schedule(() -> {
85                     observeRelation = coapClient.startObserve(this);
86                 }, 3, TimeUnit.SECONDS);
87                 break;
88             case OFFLINE:
89             default:
90                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE,
91                         String.format("Gateway offline '%s'", tradfriGateway.getStatusInfo()));
92                 break;
93         }
94     }
95
96     @Override
97     public synchronized void dispose() {
98         active = false;
99         if (observeRelation != null) {
100             observeRelation.reactiveCancel();
101             observeRelation = null;
102         }
103         if (coapClient != null) {
104             coapClient.shutdown();
105         }
106         super.dispose();
107     }
108
109     @Override
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;
119                 }
120                 observeRelation = coapClient.startObserve(this);
121             }, 10, TimeUnit.SECONDS);
122         }
123     }
124
125     @Override
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) {
131             dispose();
132         } else if (bridgeStatusInfo.getStatus() == ThingStatus.ONLINE) {
133             initialize();
134         }
135     }
136
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);
142         } else {
143             logger.debug("coapClient is null!");
144         }
145     }
146
147     protected void updateDeviceProperties(TradfriDeviceData state) {
148         String firmwareVersion = state.getFirmwareVersion();
149         if (firmwareVersion != null) {
150             getThing().setProperty(PROPERTY_FIRMWARE_VERSION, firmwareVersion);
151         }
152
153         String modelId = state.getModelId();
154         if (modelId != null) {
155             getThing().setProperty(PROPERTY_MODEL_ID, modelId);
156         }
157
158         String vendor = state.getVendor();
159         if (vendor != null) {
160             getThing().setProperty(PROPERTY_VENDOR, vendor);
161         }
162     }
163 }