]> git.basschouten.com Git - openhab-addons.git/blob
34a39acff2b4a83b5ec242216b74133f22fe8efa
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.lutron.internal.handler;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.lutron.internal.protocol.LutronCommand;
18 import org.openhab.binding.lutron.internal.protocol.LutronCommandType;
19 import org.openhab.binding.lutron.internal.protocol.LutronOperation;
20 import org.openhab.core.thing.Bridge;
21 import org.openhab.core.thing.Thing;
22 import org.openhab.core.thing.ThingStatus;
23 import org.openhab.core.thing.ThingStatusDetail;
24 import org.openhab.core.thing.ThingStatusInfo;
25 import org.openhab.core.thing.binding.BaseThingHandler;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Base type for all Lutron thing handlers.
31  *
32  * @author Allan Tong - Initial contribution
33  * @author Bob Adair - Added additional commands and methods for status and state management
34  *
35  */
36 @NonNullByDefault
37 public abstract class LutronHandler extends BaseThingHandler {
38     private final Logger logger = LoggerFactory.getLogger(LutronHandler.class);
39
40     public LutronHandler(Thing thing) {
41         super(thing);
42     }
43
44     public abstract int getIntegrationId();
45
46     public abstract void handleUpdate(LutronCommandType type, String... parameters);
47
48     /**
49      * Queries for any device state needed at initialization time or after losing connectivity to the bridge, and
50      * updates device status. Will be called when bridge status changes to ONLINE and thing has status
51      * OFFLINE:BRIDGE_OFFLINE.
52      */
53     protected abstract void initDeviceState();
54
55     /**
56      * Called when changing thing status to offline. Subclasses may override to take any needed actions.
57      */
58     protected void thingOfflineNotify() {
59     }
60
61     protected @Nullable IPBridgeHandler getBridgeHandler() {
62         Bridge bridge = getBridge();
63
64         return bridge == null ? null : (IPBridgeHandler) bridge.getHandler();
65     }
66
67     @Override
68     public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
69         logger.debug("Bridge status changed to {} for lutron device handler {}", bridgeStatusInfo.getStatus(),
70                 getIntegrationId());
71
72         if (bridgeStatusInfo.getStatus() == ThingStatus.ONLINE
73                 && getThing().getStatusInfo().getStatusDetail() == ThingStatusDetail.BRIDGE_OFFLINE) {
74             initDeviceState();
75
76         } else if (bridgeStatusInfo.getStatus() == ThingStatus.OFFLINE) {
77             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
78             thingOfflineNotify();
79         }
80     }
81
82     private void sendCommand(LutronCommand command) {
83         IPBridgeHandler bridgeHandler = getBridgeHandler();
84
85         if (bridgeHandler == null) {
86             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_MISSING_ERROR, "No bridge associated");
87             thingOfflineNotify();
88         } else {
89             bridgeHandler.sendCommand(command);
90         }
91     }
92
93     protected void output(Object... parameters) {
94         sendCommand(
95                 new LutronCommand(LutronOperation.EXECUTE, LutronCommandType.OUTPUT, getIntegrationId(), parameters));
96     }
97
98     protected void device(Object... parameters) {
99         sendCommand(
100                 new LutronCommand(LutronOperation.EXECUTE, LutronCommandType.DEVICE, getIntegrationId(), parameters));
101     }
102
103     protected void timeclock(Object... parameters) {
104         sendCommand(new LutronCommand(LutronOperation.EXECUTE, LutronCommandType.TIMECLOCK, getIntegrationId(),
105                 parameters));
106     }
107
108     protected void greenMode(Object... parameters) {
109         sendCommand(new LutronCommand(LutronOperation.EXECUTE, LutronCommandType.MODE, getIntegrationId(), parameters));
110     }
111
112     protected void sysvar(Object... parameters) {
113         sendCommand(
114                 new LutronCommand(LutronOperation.EXECUTE, LutronCommandType.SYSVAR, getIntegrationId(), parameters));
115     }
116
117     protected void shadegrp(Object... parameters) {
118         sendCommand(
119                 new LutronCommand(LutronOperation.EXECUTE, LutronCommandType.SHADEGRP, getIntegrationId(), parameters));
120     }
121
122     protected void queryOutput(Object... parameters) {
123         sendCommand(new LutronCommand(LutronOperation.QUERY, LutronCommandType.OUTPUT, getIntegrationId(), parameters));
124     }
125
126     protected void queryDevice(Object... parameters) {
127         sendCommand(new LutronCommand(LutronOperation.QUERY, LutronCommandType.DEVICE, getIntegrationId(), parameters));
128     }
129
130     protected void queryTimeclock(Object... parameters) {
131         sendCommand(
132                 new LutronCommand(LutronOperation.QUERY, LutronCommandType.TIMECLOCK, getIntegrationId(), parameters));
133     }
134
135     protected void queryGreenMode(Object... parameters) {
136         sendCommand(new LutronCommand(LutronOperation.QUERY, LutronCommandType.MODE, getIntegrationId(), parameters));
137     }
138
139     protected void querySysvar(Object... parameters) {
140         sendCommand(new LutronCommand(LutronOperation.QUERY, LutronCommandType.SYSVAR, getIntegrationId(), parameters));
141     }
142
143     protected void queryShadegrp(Object... parameters) {
144         sendCommand(
145                 new LutronCommand(LutronOperation.QUERY, LutronCommandType.SHADEGRP, getIntegrationId(), parameters));
146     }
147 }