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.openwebnet.handler;
15 import static org.openhab.binding.openwebnet.OpenWebNetBindingConstants.*;
18 import java.util.concurrent.ScheduledFuture;
19 import java.util.concurrent.TimeUnit;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.core.thing.Bridge;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.thing.ThingStatusDetail;
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.openwebnet4j.OpenGateway;
32 import org.openwebnet4j.communication.OWNException;
33 import org.openwebnet4j.communication.Response;
34 import org.openwebnet4j.message.BaseOpenMessage;
35 import org.openwebnet4j.message.OpenMessage;
36 import org.openwebnet4j.message.Where;
37 import org.openwebnet4j.message.WhereZigBee;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * The {@link OpenWebNetThingHandler} is responsible for handling commands for a OpenWebNet device.
43 * It's the abstract class for all OpenWebNet things. It should be extended by each specific OpenWebNet category of
46 * @author Massimo Valla - Initial contribution
49 public abstract class OpenWebNetThingHandler extends BaseThingHandler {
51 private final Logger logger = LoggerFactory.getLogger(OpenWebNetThingHandler.class);
53 protected @Nullable OpenWebNetBridgeHandler bridgeHandler;
54 protected @Nullable String ownId; // OpenWebNet identifier for this device: WHO.WHERE
55 protected @Nullable Where deviceWhere; // this device Where address
57 protected @Nullable ScheduledFuture<?> refreshTimeout;
59 public OpenWebNetThingHandler(Thing thing) {
64 public void initialize() {
65 Bridge bridge = getBridge();
67 OpenWebNetBridgeHandler brH = (OpenWebNetBridgeHandler) bridge.getHandler();
70 Object deviceWhereConfig = getConfig().get(CONFIG_PROPERTY_WHERE);
71 if (!(deviceWhereConfig instanceof String)) {
72 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
73 "WHERE parameter in configuration is null or invalid");
75 String deviceWhereStr = (String) getConfig().get(CONFIG_PROPERTY_WHERE);
78 if (brH.isBusGateway()) {
79 w = buildBusWhere(deviceWhereStr);
81 w = new WhereZigBee(deviceWhereStr);
83 } catch (IllegalArgumentException ia) {
84 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
85 "WHERE parameter in configuration is invalid");
89 final String oid = brH.ownIdFromDeviceWhere(w, this);
91 Map<String, String> properties = editProperties();
92 properties.put(PROPERTY_OWNID, oid);
93 updateProperties(properties);
94 brH.registerDevice(oid, this);
95 logger.debug("associated thing to bridge with ownId={}", ownId);
96 updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, "waiting state update...");
100 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
101 "No bridge associated, please assign a bridge in thing configuration.");
106 public void handleCommand(ChannelUID channel, Command command) {
107 logger.debug("handleCommand() (command={} - channel={})", command, channel);
108 OpenWebNetBridgeHandler handler = bridgeHandler;
109 if (handler != null) {
110 OpenGateway gw = handler.gateway;
111 if (gw != null && !gw.isConnected()) {
112 logger.info("Cannot handle {} command for {}: gateway is not connected", command, getThing().getUID());
115 if (deviceWhere == null) {
116 logger.info("Cannot handle {} command for {}: 'where' parameter is not configured or is invalid",
117 command, getThing().getUID());
120 if (command instanceof RefreshType) {
121 requestChannelState(channel);
122 // set a schedule to put device OFFLINE if no answer is received after THING_STATE_REQ_TIMEOUT_SEC
123 refreshTimeout = scheduler.schedule(() -> {
124 if (thing.getStatus().equals(ThingStatus.UNKNOWN)) {
125 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
126 "Could not get channel state (timer expired)");
128 }, THING_STATE_REQ_TIMEOUT_SEC, TimeUnit.SECONDS);
130 handleChannelCommand(channel, command);
133 logger.debug("Thing {} is not associated to any gateway, skipping command", getThing().getUID());
138 * Handles a command for the specific channel for this thing.
139 * It must be implemented by each specific OpenWebNet category of device (WHO), based on channel
141 * @param channel specific ChannleUID
142 * @param command the Command to be executed
144 protected abstract void handleChannelCommand(ChannelUID channel, Command command);
147 * Handle incoming message from OWN network via bridge Thing, directed to this device. It should be further
148 * implemented by each specific device handler.
150 * @param msg the message to handle
152 protected void handleMessage(BaseOpenMessage msg) {
153 ThingStatus ts = getThing().getStatus();
154 if (ThingStatus.ONLINE != ts && ThingStatus.REMOVING != ts && ThingStatus.REMOVED != ts) {
155 updateStatus(ThingStatus.ONLINE);
160 * Helper method to send OWN messages from ThingsHandlers
162 protected @Nullable Response send(OpenMessage msg) throws OWNException {
163 OpenWebNetBridgeHandler handler = bridgeHandler;
164 if (handler != null) {
165 OpenGateway gw = handler.gateway;
174 * Helper method to send with high priority OWN messages from ThingsHandlers
176 protected @Nullable Response sendHighPriority(OpenMessage msg) throws OWNException {
177 OpenWebNetBridgeHandler handler = bridgeHandler;
178 if (handler != null) {
179 OpenGateway gw = handler.gateway;
181 return gw.sendHighPriority(msg);
188 * Request to gateway state for thing channel. It must be implemented by each specific device handler.
190 * @param channel the channel to request the state for
192 protected abstract void requestChannelState(ChannelUID channel);
195 * Abstract builder for device Where address, to be implemented by each subclass to choose the right Where subclass
196 * (the method is used only if the Thing is associated to a BUS gateway).
198 * @param wStr the WHERE string
200 protected abstract Where buildBusWhere(String wStr) throws IllegalArgumentException;
203 public void dispose() {
204 OpenWebNetBridgeHandler bh = bridgeHandler;
206 if (bh != null && oid != null) {
207 bh.unregisterDevice(oid);
209 ScheduledFuture<?> sc = refreshTimeout;
217 * Returns a prefix String for ownId specific for each handler. To be implemented by sub-classes.
221 protected abstract String ownIdPrefix();