]> git.basschouten.com Git - openhab-addons.git/blob
ba009faefa89ea80da3ea8c6136d76b2b2502af1
[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.touchwand.internal;
14
15 import static org.openhab.binding.touchwand.internal.TouchWandBindingConstants.*;
16
17 import java.util.Set;
18 import java.util.concurrent.TimeUnit;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.touchwand.internal.dto.TouchWandUnitData;
23 import org.openhab.binding.touchwand.internal.dto.TouchWandUnitFromJson;
24 import org.openhab.core.thing.Bridge;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.thing.ThingStatusDetail;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.binding.BaseThingHandler;
31 import org.openhab.core.types.Command;
32 import org.openhab.core.types.RefreshType;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * The {@link TouchWandBaseUnitHandler} is responsible for handling commands and status updates
38  * for TouchWand units. This is an abstract class , units should implement the specific command
39  * handling and status updates.
40  *
41  * @author Roie Geron - Initial contribution
42  *
43  */
44 @NonNullByDefault
45 public abstract class TouchWandBaseUnitHandler extends BaseThingHandler implements TouchWandUnitUpdateListener {
46
47     private static final int UNITS_STATUS_UPDATE_DELAY_SEC = 1;
48     protected final Logger logger = LoggerFactory.getLogger(TouchWandBaseUnitHandler.class);
49     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_SHUTTER, THING_TYPE_SWITCH,
50             THING_TYPE_WALLCONTROLLER, THING_TYPE_DIMMER, THING_TYPE_ALARMSENSOR, THING_TYPE_BSENSOR,
51             THING_TYPE_THERMOSTAT);
52     protected String unitId = "";
53
54     protected @Nullable TouchWandBridgeHandler bridgeHandler;
55
56     public TouchWandBaseUnitHandler(Thing thing) {
57         super(thing);
58     }
59
60     @Override
61     public void handleCommand(ChannelUID channelUID, Command command) {
62         if (command instanceof RefreshType) {
63             TouchWandUnitData myUnitData = getUnitState(unitId);
64             if (myUnitData != null) {
65                 updateTouchWandUnitState(myUnitData);
66             }
67         } else {
68             touchWandUnitHandleCommand(command);
69         }
70     }
71
72     @Override
73     public void dispose() {
74         TouchWandBridgeHandler myTmpBridgeHandler = bridgeHandler;
75         if (myTmpBridgeHandler != null) {
76             myTmpBridgeHandler.unregisterUpdateListener(this);
77         }
78     }
79
80     @Override
81     public void initialize() {
82         Bridge bridge = getBridge();
83         if (bridge == null || !(bridge.getHandler() instanceof TouchWandBridgeHandler)) {
84             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
85             logger.warn("Trying to initialize {} without a bridge", getThing().getUID());
86             return;
87         }
88
89         bridgeHandler = (TouchWandBridgeHandler) bridge.getHandler();
90
91         String unitId = getThing().getProperties().get(HANDLER_PROPERTIES_ID); // TouchWand unit id
92         if (unitId == null) {
93             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "unitID missing");
94             return;
95         }
96         this.unitId = unitId;
97
98         TouchWandBridgeHandler myTmpBridgeHandler = bridgeHandler;
99         if (myTmpBridgeHandler != null) {
100             myTmpBridgeHandler.registerUpdateListener(this);
101         }
102
103         updateStatus(ThingStatus.UNKNOWN);
104         scheduler.schedule(() -> {
105             boolean thingReachable = false;
106             if (myTmpBridgeHandler != null) {
107                 String response = myTmpBridgeHandler.touchWandClient.cmdGetUnitById(unitId);
108                 thingReachable = !response.isEmpty();
109                 if (thingReachable) {
110                     updateStatus(ThingStatus.ONLINE);
111                     updateTouchWandUnitState(TouchWandUnitFromJson.parseResponse(response));
112                 } else {
113                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
114                 }
115             }
116         }, UNITS_STATUS_UPDATE_DELAY_SEC, TimeUnit.SECONDS);
117     }
118
119     private @Nullable TouchWandUnitData getUnitState(String unitId) {
120         TouchWandBridgeHandler touchWandBridgeHandler = bridgeHandler;
121
122         if (touchWandBridgeHandler == null) {
123             return null;
124         }
125
126         String response = touchWandBridgeHandler.touchWandClient.cmdGetUnitById(unitId);
127         if (response.isEmpty()) {
128             return null;
129         }
130
131         return TouchWandUnitFromJson.parseResponse(response);
132     }
133
134     abstract void touchWandUnitHandleCommand(Command command);
135
136     abstract void updateTouchWandUnitState(TouchWandUnitData unitData);
137
138     @Override
139     public void onItemStatusUpdate(TouchWandUnitData unitData) {
140         if (unitData.getStatus().equals("ALIVE")) {
141             updateStatus(ThingStatus.ONLINE);
142         } else {
143             // updateStatus(ThingStatus.OFFLINE); // comment - OFFLINE status is not accurate at the moment
144         }
145         updateTouchWandUnitState(unitData);
146     }
147
148     @Override
149     public String getId() {
150         return unitId;
151     }
152 }