]> git.basschouten.com Git - openhab-addons.git/blob
61703e5362e9ce3ff918267c9e9fea2702f6c9b4
[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         String unitId = getThing().getProperties().get(HANDLER_PROPERTIES_ID); // TouchWand unit id
89         if (unitId == null) {
90             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "unitID missing");
91             return;
92         }
93         this.unitId = unitId;
94
95         TouchWandBridgeHandler myTmpBridgeHandler = bridgeHandler;
96         if (myTmpBridgeHandler != null) {
97             myTmpBridgeHandler.registerUpdateListener(this);
98         }
99
100         updateStatus(ThingStatus.UNKNOWN);
101         scheduler.execute(() -> {
102             boolean thingReachable = false;
103             if (myTmpBridgeHandler != null) {
104                 String response = myTmpBridgeHandler.touchWandClient.cmdGetUnitById(unitId);
105                 thingReachable = !response.isEmpty();
106                 if (thingReachable) {
107                     updateStatus(ThingStatus.ONLINE);
108                 } else {
109                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
110                 }
111             }
112         });
113     }
114
115     @SuppressWarnings("unused") // not used at the moment till touchWand state in hub will be fixed
116     private int getUnitState(String unitId) {
117         int status = 0;
118
119         TouchWandBridgeHandler touchWandBridgeHandler = bridgeHandler;
120         if (touchWandBridgeHandler == null) {
121             return status;
122         }
123
124         String response = touchWandBridgeHandler.touchWandClient.cmdGetUnitById(unitId);
125         if (!response.isEmpty()) {
126             return status;
127         }
128
129         JsonParser jsonParser = new JsonParser();
130
131         try {
132             JsonObject unitObj = jsonParser.parse(response).getAsJsonObject();
133             status = unitObj.get("currStatus").getAsInt();
134             if (!this.getThing().getStatusInfo().getStatus().equals(ThingStatus.ONLINE)) {
135                 updateStatus(ThingStatus.ONLINE);
136             }
137         } catch (JsonParseException | IllegalStateException e) {
138             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
139                     "Could not parse cmdGetUnitById:" + e.getMessage());
140         }
141         return status;
142     }
143
144     abstract void touchWandUnitHandleCommand(Command command);
145
146     abstract void updateTouchWandUnitState(TouchWandUnitData unitData);
147
148     @Override
149     public void onItemStatusUpdate(TouchWandUnitData unitData) {
150         if (unitData.getStatus().equals("ALIVE")) {
151             updateStatus(ThingStatus.ONLINE);
152         } else {
153             // updateStatus(ThingStatus.OFFLINE); // comment - OFFLINE status is not accurate at the moment
154         }
155         updateTouchWandUnitState(unitData);
156     }
157
158     @Override
159     public String getId() {
160         return unitId;
161     }
162 }