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.openwebnet.internal.handler;
15 import static org.openhab.binding.openwebnet.internal.OpenWebNetBindingConstants.*;
18 import java.util.concurrent.ScheduledFuture;
19 import java.util.concurrent.TimeUnit;
21 import javax.measure.Quantity;
22 import javax.measure.Unit;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.core.library.types.QuantityType;
27 import org.openhab.core.thing.Bridge;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.ThingStatusDetail;
32 import org.openhab.core.thing.ThingStatusInfo;
33 import org.openhab.core.thing.binding.BaseThingHandler;
34 import org.openhab.core.types.Command;
35 import org.openhab.core.types.RefreshType;
36 import org.openhab.core.types.State;
37 import org.openhab.core.types.UnDefType;
38 import org.openwebnet4j.OpenGateway;
39 import org.openwebnet4j.communication.OWNException;
40 import org.openwebnet4j.communication.Response;
41 import org.openwebnet4j.message.BaseOpenMessage;
42 import org.openwebnet4j.message.OpenMessage;
43 import org.openwebnet4j.message.Where;
44 import org.openwebnet4j.message.WhereZigBee;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * The {@link OpenWebNetThingHandler} is responsible for handling commands for an OpenWebNet device.
50 * It's the abstract class for all OpenWebNet things. It should be extended by each specific OpenWebNet category of
53 * @author Massimo Valla - Initial contribution
56 public abstract class OpenWebNetThingHandler extends BaseThingHandler {
58 private final Logger logger = LoggerFactory.getLogger(OpenWebNetThingHandler.class);
60 protected @Nullable OpenWebNetBridgeHandler bridgeHandler;
61 protected @Nullable String ownId; // OpenWebNet identifier for this device: WHO.WHERE
62 protected @Nullable Where deviceWhere; // this device WHERE address
64 protected @Nullable ScheduledFuture<?> requestChannelStateTimeout;
65 protected @Nullable ScheduledFuture<?> refreshTimeout;
67 private static final int ALL_DEVICES_REFRESH_INTERVAL_MSEC = 60_000; // interval before sending another
68 // refreshAllDevices request
70 public OpenWebNetThingHandler(Thing thing) {
75 public void initialize() {
76 Bridge bridge = getBridge();
78 OpenWebNetBridgeHandler brH = (OpenWebNetBridgeHandler) bridge.getHandler();
82 final String configDeviceWhere = (String) getConfig().get(CONFIG_PROPERTY_WHERE);
83 if (configDeviceWhere == null) {
84 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
85 "@text/offline.conf-error-where");
89 if (brH.isBusGateway()) {
90 w = buildBusWhere(configDeviceWhere);
92 w = new WhereZigBee(configDeviceWhere);
94 } catch (IllegalArgumentException ia) {
95 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
96 "@text/offline.conf-error-where");
100 final String oid = brH.ownIdFromDeviceWhere(w, this);
102 Map<String, String> properties = editProperties();
103 properties.put(PROPERTY_OWNID, oid);
104 updateProperties(properties);
105 brH.registerDevice(oid, this);
106 logger.debug("associated thing to bridge with ownId={}", ownId);
107 updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, "@text/unknown.waiting-state");
111 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
112 "@text/offline.conf-error-no-bridge");
117 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
118 if (bridgeStatusInfo.getStatus() == ThingStatus.ONLINE) {
119 updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, "@text/unknown.waiting-state");
120 } else if (bridgeStatusInfo.getStatus() == ThingStatus.OFFLINE) {
121 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
126 public void handleCommand(ChannelUID channel, Command command) {
127 logger.debug("handleCommand() (command={} - channel={})", command, channel);
128 OpenWebNetBridgeHandler handler = bridgeHandler;
129 if (handler != null) {
130 OpenGateway gw = handler.gateway;
131 if (gw != null && !gw.isConnected()) {
132 logger.info("Cannot handle {} command for {}: gateway is not connected", command, thing.getUID());
135 if (deviceWhere == null) {
136 logger.info("Cannot handle {} command for {}: 'where' parameter is not configured or is invalid",
137 command, thing.getUID());
140 if (command instanceof RefreshType) {
141 requestChannelState(channel);
143 handleChannelCommand(channel, command);
146 logger.debug("Thing {} is not associated to any gateway, skipping command", getThing().getUID());
151 * Handles a command for the specific channel for this thing.
152 * It must be further implemented by each specific device handler.
154 * @param channel the {@link ChannelUID}
155 * @param command the Command to be executed
157 protected abstract void handleChannelCommand(ChannelUID channel, Command command);
160 * Handle incoming message from OWN network via bridge Thing, directed to this device.
161 * It should be further implemented by each specific device handler.
163 * @param msg the message to handle
165 protected void handleMessage(BaseOpenMessage msg) {
166 ThingStatus ts = getThing().getStatus();
167 if (ThingStatus.ONLINE != ts && ThingStatus.REMOVING != ts && ThingStatus.REMOVED != ts) {
168 updateStatus(ThingStatus.ONLINE);
173 * Helper method to send OWN messages from handler.
175 * @param msg the OpenMessage to be sent
177 public @Nullable Response send(OpenMessage msg) throws OWNException {
178 OpenWebNetBridgeHandler bh = bridgeHandler;
180 OpenGateway gw = bh.gateway;
185 logger.warn("Couldn't send message {}: handler or gateway is null", msg);
190 * Helper method to send with high priority OWN messages from handler.
192 * @param msg the OpenMessage to be sent
194 protected @Nullable Response sendHighPriority(OpenMessage msg) throws OWNException {
195 OpenWebNetBridgeHandler handler = bridgeHandler;
196 if (handler != null) {
197 OpenGateway gw = handler.gateway;
199 return gw.sendHighPriority(msg);
206 * Request the state for the specified channel. If no answer is received within THING_STATE_REQ_TIMEOUT_SEC, it is
208 * The method must be further implemented by each specific handler.
210 * @param channel the {@link ChannelUID} to request the state for
212 protected void requestChannelState(ChannelUID channel) {
213 logger.debug("requestChannelState() {}", channel);
214 Where w = deviceWhere;
216 logger.warn("Could not requestChannelState(): deviceWhere is null for thing {}", thing.getUID());
217 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "@text/offline.conf-error-where");
220 // set a schedule to put device OFFLINE if no answer is received after THING_STATE_REQ_TIMEOUT_SEC
221 requestChannelStateTimeout = scheduler.schedule(() -> {
222 if (thing.getStatus().equals(ThingStatus.UNKNOWN)) {
223 logger.debug("requestChannelState() TIMEOUT for thing {}", thing.getUID());
224 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
225 "@text/offline.comm-error-state");
227 }, THING_STATE_REQ_TIMEOUT_SEC, TimeUnit.SECONDS);
231 * Refresh a device, possibly using a single OWN command if refreshAll=true and if supported.
232 * The method must be further implemented by each specific handler.
234 * @param refreshAll true if all devices for this handler must be refreshed with a single OWN command, if supported,
235 * otherwise just refresh the single device.
237 protected abstract void refreshDevice(boolean refreshAll);
240 * If the subclass supports refreshing all devices with a single OWN command, returns the last TS when a refreshAll
241 * was requested, or 0 if not requested yet. If not supported return -1 (default).
242 * It must be implemented by each subclass that supports all devices refresh.
244 * @return timestamp when last refreshAll command was sent, 0 if not requested yet, or -1 if it's not supported by
247 protected long getRefreshAllLastTS() {
252 * Refresh all devices for this handler
254 protected void refreshAllDevices() {
255 logger.debug("--- refreshAllDevices() for device {}", thing.getUID());
256 OpenWebNetBridgeHandler brH = bridgeHandler;
258 long refAllTS = getRefreshAllLastTS();
259 logger.debug("{} support = {}", thing.getUID(), refAllTS >= 0);
260 if (brH.isBusGateway() && refAllTS >= 0) {
261 long now = System.currentTimeMillis();
262 if (now - refAllTS > ALL_DEVICES_REFRESH_INTERVAL_MSEC) {
263 logger.debug("--- refreshAllDevices() : refreshing ALL devices... ({})", thing.getUID());
266 logger.debug("--- refreshAllDevices() : refresh all devices sent {}msec ago, skipping... ({})",
267 ALL_DEVICES_REFRESH_INTERVAL_MSEC, thing.getUID());
269 // sometimes GENERAL (e.g. #*1*0##) refresh requests do not return state for all devices, so let's
270 // schedule another single refresh device, just in case
271 refreshTimeout = scheduler.schedule(() -> {
272 if (thing.getStatus().equals(ThingStatus.UNKNOWN)) {
274 "--- refreshAllDevices() : schedule expired: --UNKNOWN-- status for {}. Refreshing it...",
276 refreshDevice(false);
278 logger.debug("--- refreshAllDevices() : schedule expired: ONLINE status for {}",
281 }, THING_STATE_REQ_TIMEOUT_SEC, TimeUnit.SECONDS);
282 } else { // USB device or AllDevicesRefresh not supported
283 refreshDevice(false);
289 * Abstract builder for device Where address, to be implemented by each subclass to choose the right Where subclass
290 * (the method is used only if the Thing is associated to a BUS gateway).
292 * @param wStr the WHERE string
294 protected abstract Where buildBusWhere(String wStr) throws IllegalArgumentException;
297 public void dispose() {
298 OpenWebNetBridgeHandler bh = bridgeHandler;
300 if (bh != null && oid != null) {
301 bh.unregisterDevice(oid);
303 ScheduledFuture<?> rcst = requestChannelStateTimeout;
307 ScheduledFuture<?> rt = refreshTimeout;
315 * Helper method to return a Quantity from a Number value or UnDefType.NULL if value is null
317 * @param value to be used
318 * @param unit to be used
321 protected <U extends Quantity<U>> State getAsQuantityTypeOrNull(@Nullable Number value, Unit<U> unit) {
322 return value == null ? UnDefType.NULL : new QuantityType<>(value, unit);
326 * Returns a prefix String for ownId specific for each handler. To be implemented by sub-classes.
330 protected abstract String ownIdPrefix();