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.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.WhereLightAutom;
38 import org.openwebnet4j.message.WhereZigBee;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * The {@link OpenWebNetThingHandler} is responsible for handling commands for a OpenWebNet device.
44 * It's the abstract class for all OpenWebNet things. It should be extended by each specific OpenWebNet category of
47 * @author Massimo Valla - Initial contribution
50 public abstract class OpenWebNetThingHandler extends BaseThingHandler {
52 private final Logger logger = LoggerFactory.getLogger(OpenWebNetThingHandler.class);
54 protected @Nullable OpenWebNetBridgeHandler bridgeHandler;
55 protected @Nullable String ownId; // OpenWebNet identifier for this device: WHO.WHERE
56 protected @Nullable Where deviceWhere; // this device Where address
58 protected @Nullable ScheduledFuture<?> refreshTimeout;
60 public OpenWebNetThingHandler(Thing thing) {
65 public void initialize() {
66 Bridge bridge = getBridge();
68 OpenWebNetBridgeHandler brH = (OpenWebNetBridgeHandler) bridge.getHandler();
71 Object deviceWhereConfig = getConfig().get(CONFIG_PROPERTY_WHERE);
72 if (!(deviceWhereConfig instanceof String)) {
73 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
74 "WHERE parameter in configuration is null or invalid");
77 String deviceWhereStr = (String) getConfig().get(CONFIG_PROPERTY_WHERE);
79 if (brH.isBusGateway()) {
80 w = new WhereLightAutom(deviceWhereStr);
82 w = new WhereZigBee(deviceWhereStr);
85 final String oid = brH.ownIdFromDeviceWhere(w.value(), this);
87 Map<String, String> properties = editProperties();
88 properties.put(PROPERTY_OWNID, oid);
89 updateProperties(properties);
90 brH.registerDevice(oid, this);
91 logger.debug("associated thing to bridge with ownId={}", ownId);
92 updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, "waiting state update...");
97 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
98 "No bridge associated, please assign a bridge in thing configuration.");
102 public void handleCommand(ChannelUID channel, Command command) {
103 logger.debug("handleCommand() (command={} - channel={})", command, channel);
104 OpenWebNetBridgeHandler handler = bridgeHandler;
105 if (handler != null) {
106 OpenGateway gw = handler.gateway;
107 if (gw != null && !gw.isConnected()) {
108 logger.info("Cannot handle command {}:{} for {}: gateway is not connected", channel, command,
109 getThing().getUID());
112 if (command instanceof RefreshType) {
113 requestChannelState(channel);
114 // set a schedule to put device OFFLINE if no answer is received after THING_STATE_REQ_TIMEOUT_SEC
115 refreshTimeout = scheduler.schedule(() -> {
116 if (thing.getStatus().equals(ThingStatus.UNKNOWN)) {
117 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
118 "Could not get channel state (timer expired)");
120 }, THING_STATE_REQ_TIMEOUT_SEC, TimeUnit.SECONDS);
122 handleChannelCommand(channel, command);
125 logger.debug("Thing {} is not associated to any gateway, skipping command", getThing().getUID());
130 * Handles a command for the specific channel for this thing.
131 * It must be implemented by each specific OpenWebNet category of device (WHO), based on channel
133 * @param channel specific ChannleUID
134 * @param command the Command to be executed
136 protected abstract void handleChannelCommand(ChannelUID channel, Command command);
139 * Handle incoming message from OWN network via bridge Thing, directed to this device. It should be further
140 * implemented by each specific device handler.
142 * @param msg the message to handle
144 protected void handleMessage(BaseOpenMessage msg) {
145 ThingStatus ts = getThing().getStatus();
146 if (ThingStatus.ONLINE != ts && ThingStatus.REMOVING != ts && ThingStatus.REMOVED != ts) {
147 updateStatus(ThingStatus.ONLINE);
152 * Helper method to send OWN messages from ThingsHandlers
154 protected @Nullable Response send(OpenMessage msg) throws OWNException {
155 OpenWebNetBridgeHandler handler = bridgeHandler;
156 if (handler != null) {
157 OpenGateway gw = handler.gateway;
166 * Helper method to send with high priority OWN messages from ThingsHandlers
168 protected @Nullable Response sendHighPriority(OpenMessage msg) throws OWNException {
169 OpenWebNetBridgeHandler handler = bridgeHandler;
170 if (handler != null) {
171 OpenGateway gw = handler.gateway;
173 return gw.sendHighPriority(msg);
180 * Request to gateway state for thing channel. It must be implemented by each specific device handler.
182 * @param channel the channel to request the state for
184 protected abstract void requestChannelState(ChannelUID channel);
187 public void dispose() {
188 OpenWebNetBridgeHandler bh = bridgeHandler;
190 if (bh != null && oid != null) {
191 bh.unregisterDevice(oid);
193 ScheduledFuture<?> sc = refreshTimeout;
201 * Returns a prefix String for ownId specific for each handler. To be implemented by sub-classes.
205 protected abstract String ownIdPrefix();