2 * Copyright (c) 2010-2023 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, THING_TYPE_BSENSOR,
51 THING_TYPE_THERMOSTAT);
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 TouchWandUnitData myUnitData = getUnitState(unitId);
64 if (myUnitData != null) {
65 updateTouchWandUnitState(myUnitData);
68 touchWandUnitHandleCommand(command);
73 public void dispose() {
74 TouchWandBridgeHandler myTmpBridgeHandler = bridgeHandler;
75 if (myTmpBridgeHandler != null) {
76 myTmpBridgeHandler.unregisterUpdateListener(this);
81 public void initialize() {
82 Bridge bridge = getBridge();
83 if (bridge == null || !(bridge.getHandler() instanceof TouchWandBridgeHandler)) {
84 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
85 logger.warn("Trying to initialize {} without a bridge", getThing().getUID());
89 bridgeHandler = (TouchWandBridgeHandler) bridge.getHandler();
91 String unitId = getThing().getProperties().get(HANDLER_PROPERTIES_ID); // TouchWand unit id
93 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "unitID missing");
98 TouchWandBridgeHandler myTmpBridgeHandler = bridgeHandler;
99 if (myTmpBridgeHandler != null) {
100 myTmpBridgeHandler.registerUpdateListener(this);
103 updateStatus(ThingStatus.UNKNOWN);
104 scheduler.schedule(() -> {
105 boolean thingReachable = false;
106 if (myTmpBridgeHandler != null) {
107 String response = myTmpBridgeHandler.touchWandClient.cmdGetUnitById(unitId);
108 thingReachable = !response.isEmpty();
109 if (thingReachable) {
110 updateStatus(ThingStatus.ONLINE);
111 updateTouchWandUnitState(TouchWandUnitFromJson.parseResponse(response));
113 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
116 }, UNITS_STATUS_UPDATE_DELAY_SEC, TimeUnit.SECONDS);
119 private @Nullable TouchWandUnitData getUnitState(String unitId) {
120 TouchWandBridgeHandler touchWandBridgeHandler = bridgeHandler;
122 if (touchWandBridgeHandler == null) {
126 String response = touchWandBridgeHandler.touchWandClient.cmdGetUnitById(unitId);
127 if (response.isEmpty()) {
131 return TouchWandUnitFromJson.parseResponse(response);
134 abstract void touchWandUnitHandleCommand(Command command);
136 abstract void updateTouchWandUnitState(TouchWandUnitData unitData);
139 public void onItemStatusUpdate(TouchWandUnitData unitData) {
140 if (unitData.getStatus().equals("ALIVE")) {
141 updateStatus(ThingStatus.ONLINE);
143 // updateStatus(ThingStatus.OFFLINE); // comment - OFFLINE status is not accurate at the moment
145 updateTouchWandUnitState(unitData);
149 public String getId() {