]> git.basschouten.com Git - openhab-addons.git/blob
3c8b252bb809a101c7a76808dd2ec2dbfab05c1d
[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.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);
51     protected String unitId = "";
52
53     protected @Nullable TouchWandBridgeHandler bridgeHandler;
54
55     public TouchWandBaseUnitHandler(Thing thing) {
56         super(thing);
57     }
58
59     @Override
60     public void handleCommand(ChannelUID channelUID, Command command) {
61         if (command instanceof RefreshType) {
62             TouchWandUnitData myUnitData = getUnitState(unitId);
63             if (myUnitData != null) {
64                 updateTouchWandUnitState(myUnitData);
65             }
66         } else {
67             touchWandUnitHandleCommand(command);
68         }
69     }
70
71     @Override
72     public void dispose() {
73         TouchWandBridgeHandler myTmpBridgeHandler = bridgeHandler;
74         if (myTmpBridgeHandler != null) {
75             myTmpBridgeHandler.unregisterUpdateListener(this);
76         }
77     }
78
79     @Override
80     public void initialize() {
81         Bridge bridge = getBridge();
82         if (bridge == null || !(bridge.getHandler() instanceof TouchWandBridgeHandler)) {
83             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
84             logger.warn("Trying to initialize {} without a bridge", getThing().getUID());
85             return;
86         }
87
88         bridgeHandler = (TouchWandBridgeHandler) bridge.getHandler();
89
90         String unitId = getThing().getProperties().get(HANDLER_PROPERTIES_ID); // TouchWand unit id
91         if (unitId == null) {
92             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "unitID missing");
93             return;
94         }
95         this.unitId = unitId;
96
97         TouchWandBridgeHandler myTmpBridgeHandler = bridgeHandler;
98         if (myTmpBridgeHandler != null) {
99             myTmpBridgeHandler.registerUpdateListener(this);
100         }
101
102         updateStatus(ThingStatus.UNKNOWN);
103         scheduler.schedule(() -> {
104             boolean thingReachable = false;
105             if (myTmpBridgeHandler != null) {
106                 String response = myTmpBridgeHandler.touchWandClient.cmdGetUnitById(unitId);
107                 thingReachable = !response.isEmpty();
108                 if (thingReachable) {
109                     updateStatus(ThingStatus.ONLINE);
110                     updateTouchWandUnitState(TouchWandUnitFromJson.parseResponse(response));
111                 } else {
112                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
113                 }
114             }
115         }, UNITS_STATUS_UPDATE_DELAY_SEC, TimeUnit.SECONDS);
116     }
117
118     private @Nullable TouchWandUnitData getUnitState(String unitId) {
119         TouchWandBridgeHandler touchWandBridgeHandler = bridgeHandler;
120
121         if (touchWandBridgeHandler == null) {
122             return null;
123         }
124
125         String response = touchWandBridgeHandler.touchWandClient.cmdGetUnitById(unitId);
126         if (response.isEmpty()) {
127             return null;
128         }
129
130         return TouchWandUnitFromJson.parseResponse(response);
131     }
132
133     abstract void touchWandUnitHandleCommand(Command command);
134
135     abstract void updateTouchWandUnitState(TouchWandUnitData unitData);
136
137     @Override
138     public void onItemStatusUpdate(TouchWandUnitData unitData) {
139         if (unitData.getStatus().equals("ALIVE")) {
140             updateStatus(ThingStatus.ONLINE);
141         } else {
142             // updateStatus(ThingStatus.OFFLINE); // comment - OFFLINE status is not accurate at the moment
143         }
144         updateTouchWandUnitState(unitData);
145     }
146
147     @Override
148     public String getId() {
149         return unitId;
150     }
151 }