]> git.basschouten.com Git - openhab-addons.git/blob
d1b1de3dcc0e3b7a9e388b5af2f479570920075b
[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
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.touchwand.internal.dto.TouchWandUnitData;
22 import org.openhab.core.thing.Bridge;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingStatus;
26 import org.openhab.core.thing.ThingStatusDetail;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.thing.binding.BaseThingHandler;
29 import org.openhab.core.types.Command;
30 import org.openhab.core.types.RefreshType;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import com.google.gson.JsonObject;
35 import com.google.gson.JsonParseException;
36 import com.google.gson.JsonParser;
37
38 /**
39  * The {@link TouchWandBaseUnitHandler} is responsible for handling commands and status updates
40  * for TouchWand units. This is an abstract class , units should implement the specific command
41  * handling and status updates.
42  *
43  * @author Roie Geron - Initial contribution
44  *
45  */
46 @NonNullByDefault
47 public abstract class TouchWandBaseUnitHandler extends BaseThingHandler implements TouchWandUnitUpdateListener {
48
49     protected final Logger logger = LoggerFactory.getLogger(TouchWandBaseUnitHandler.class);
50     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_SHUTTER, THING_TYPE_SWITCH,
51             THING_TYPE_WALLCONTROLLER, THING_TYPE_DIMMER);
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             // updateTouchWandUnitState(getUnitState(unitId));
64         } else {
65             touchWandUnitHandleCommand(command);
66         }
67     }
68
69     @Override
70     public void dispose() {
71         TouchWandBridgeHandler myTmpBridgeHandler = bridgeHandler;
72         if (myTmpBridgeHandler != null) {
73             myTmpBridgeHandler.unregisterUpdateListener(this);
74         }
75     }
76
77     @Override
78     public void initialize() {
79         Bridge bridge = getBridge();
80         if (bridge == null || !(bridge.getHandler() instanceof TouchWandBridgeHandler)) {
81             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
82             logger.warn("Trying to initialize {} without a bridge", getThing().getUID());
83             return;
84         }
85
86         bridgeHandler = (TouchWandBridgeHandler) bridge.getHandler();
87
88         unitId = getThing().getProperties().get(HANDLER_PROPERTIES_ID); // TouchWand unit id
89
90         TouchWandBridgeHandler myTmpBridgeHandler = bridgeHandler;
91         if (myTmpBridgeHandler != null) {
92             myTmpBridgeHandler.registerUpdateListener(this);
93         }
94
95         updateStatus(ThingStatus.UNKNOWN);
96         scheduler.execute(() -> {
97             boolean thingReachable = false;
98             if (myTmpBridgeHandler != null) {
99                 String response = myTmpBridgeHandler.touchWandClient.cmdGetUnitById(unitId);
100                 thingReachable = !response.isEmpty();
101                 if (thingReachable) {
102                     updateStatus(ThingStatus.ONLINE);
103                 } else {
104                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
105                 }
106             }
107         });
108     }
109
110     @SuppressWarnings("unused") // not used at the moment till touchWand state in hub will be fixed
111     private int getUnitState(String unitId) {
112         int status = 0;
113
114         TouchWandBridgeHandler touchWandBridgeHandler = bridgeHandler;
115         if (touchWandBridgeHandler == null) {
116             return status;
117         }
118
119         String response = touchWandBridgeHandler.touchWandClient.cmdGetUnitById(unitId);
120         if (!response.isEmpty()) {
121             return status;
122         }
123
124         JsonParser jsonParser = new JsonParser();
125
126         try {
127             JsonObject unitObj = jsonParser.parse(response).getAsJsonObject();
128             status = unitObj.get("currStatus").getAsInt();
129             if (!this.getThing().getStatusInfo().getStatus().equals(ThingStatus.ONLINE)) {
130                 updateStatus(ThingStatus.ONLINE);
131             }
132         } catch (JsonParseException | IllegalStateException e) {
133             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
134                     "Could not parse cmdGetUnitById:" + e.getMessage());
135         }
136         return status;
137     }
138
139     abstract void touchWandUnitHandleCommand(Command command);
140
141     abstract void updateTouchWandUnitState(TouchWandUnitData unitData);
142
143     @Override
144     public void onItemStatusUpdate(TouchWandUnitData unitData) {
145         if (unitData.getStatus().equals("ALIVE")) {
146             updateStatus(ThingStatus.ONLINE);
147         } else {
148             // updateStatus(ThingStatus.OFFLINE); // comment - OFFLINE status is not accurate at the moment
149         }
150         updateTouchWandUnitState(unitData);
151     }
152
153     @Override
154     public String getId() {
155         return unitId;
156     }
157 }