2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.touchwand.internal;
15 import static org.openhab.binding.touchwand.internal.TouchWandBindingConstants.*;
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;
34 import com.google.gson.JsonObject;
35 import com.google.gson.JsonParseException;
36 import com.google.gson.JsonParser;
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.
43 * @author Roie Geron - Initial contribution
47 public abstract class TouchWandBaseUnitHandler extends BaseThingHandler implements TouchWandUnitUpdateListener {
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 = "";
54 protected @Nullable TouchWandBridgeHandler bridgeHandler;
56 public TouchWandBaseUnitHandler(Thing thing) {
61 public void handleCommand(ChannelUID channelUID, Command command) {
62 if (command instanceof RefreshType) {
63 // updateTouchWandUnitState(getUnitState(unitId));
65 touchWandUnitHandleCommand(command);
70 public void dispose() {
71 TouchWandBridgeHandler myTmpBridgeHandler = bridgeHandler;
72 if (myTmpBridgeHandler != null) {
73 myTmpBridgeHandler.unregisterUpdateListener(this);
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());
86 bridgeHandler = (TouchWandBridgeHandler) bridge.getHandler();
88 String unitId = getThing().getProperties().get(HANDLER_PROPERTIES_ID); // TouchWand unit id
90 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "unitID missing");
95 TouchWandBridgeHandler myTmpBridgeHandler = bridgeHandler;
96 if (myTmpBridgeHandler != null) {
97 myTmpBridgeHandler.registerUpdateListener(this);
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);
109 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
115 @SuppressWarnings("unused") // not used at the moment till touchWand state in hub will be fixed
116 private int getUnitState(String unitId) {
119 TouchWandBridgeHandler touchWandBridgeHandler = bridgeHandler;
120 if (touchWandBridgeHandler == null) {
124 String response = touchWandBridgeHandler.touchWandClient.cmdGetUnitById(unitId);
125 if (!response.isEmpty()) {
129 JsonParser jsonParser = new JsonParser();
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);
137 } catch (JsonParseException | IllegalStateException e) {
138 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
139 "Could not parse cmdGetUnitById:" + e.getMessage());
144 abstract void touchWandUnitHandleCommand(Command command);
146 abstract void updateTouchWandUnitState(TouchWandUnitData unitData);
149 public void onItemStatusUpdate(TouchWandUnitData unitData) {
150 if (unitData.getStatus().equals("ALIVE")) {
151 updateStatus(ThingStatus.ONLINE);
153 // updateStatus(ThingStatus.OFFLINE); // comment - OFFLINE status is not accurate at the moment
155 updateTouchWandUnitState(unitData);
159 public String getId() {