2 * Copyright (c) 2010-2021 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.*;
18 import java.util.concurrent.TimeUnit;
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;
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.
41 * @author Roie Geron - Initial contribution
45 public abstract class TouchWandBaseUnitHandler extends BaseThingHandler implements TouchWandUnitUpdateListener {
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 = "";
53 protected @Nullable TouchWandBridgeHandler bridgeHandler;
55 public TouchWandBaseUnitHandler(Thing thing) {
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);
67 touchWandUnitHandleCommand(command);
72 public void dispose() {
73 TouchWandBridgeHandler myTmpBridgeHandler = bridgeHandler;
74 if (myTmpBridgeHandler != null) {
75 myTmpBridgeHandler.unregisterUpdateListener(this);
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());
88 bridgeHandler = (TouchWandBridgeHandler) bridge.getHandler();
90 String unitId = getThing().getProperties().get(HANDLER_PROPERTIES_ID); // TouchWand unit id
92 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "unitID missing");
97 TouchWandBridgeHandler myTmpBridgeHandler = bridgeHandler;
98 if (myTmpBridgeHandler != null) {
99 myTmpBridgeHandler.registerUpdateListener(this);
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));
112 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
115 }, UNITS_STATUS_UPDATE_DELAY_SEC, TimeUnit.SECONDS);
118 private @Nullable TouchWandUnitData getUnitState(String unitId) {
119 TouchWandBridgeHandler touchWandBridgeHandler = bridgeHandler;
121 if (touchWandBridgeHandler == null) {
125 String response = touchWandBridgeHandler.touchWandClient.cmdGetUnitById(unitId);
126 if (response.isEmpty()) {
130 return TouchWandUnitFromJson.parseResponse(response);
133 abstract void touchWandUnitHandleCommand(Command command);
135 abstract void updateTouchWandUnitState(TouchWandUnitData unitData);
138 public void onItemStatusUpdate(TouchWandUnitData unitData) {
139 if (unitData.getStatus().equals("ALIVE")) {
140 updateStatus(ThingStatus.ONLINE);
142 // updateStatus(ThingStatus.OFFLINE); // comment - OFFLINE status is not accurate at the moment
144 updateTouchWandUnitState(unitData);
148 public String getId() {