]> git.basschouten.com Git - openhab-addons.git/blob
6c934cdc0f5512d3cf41f24da134dcea48ffc955
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.DeviceCommand;
18 import org.openhab.binding.lutron.internal.protocol.GroupCommand;
19 import org.openhab.binding.lutron.internal.protocol.LutronCommandNew;
20 import org.openhab.binding.lutron.internal.protocol.LutronDuration;
21 import org.openhab.binding.lutron.internal.protocol.ModeCommand;
22 import org.openhab.binding.lutron.internal.protocol.OutputCommand;
23 import org.openhab.binding.lutron.internal.protocol.SysvarCommand;
24 import org.openhab.binding.lutron.internal.protocol.TimeclockCommand;
25 import org.openhab.binding.lutron.internal.protocol.lip.LutronCommandType;
26 import org.openhab.binding.lutron.internal.protocol.lip.LutronOperation;
27 import org.openhab.binding.lutron.internal.protocol.lip.TargetType;
28 import org.openhab.core.thing.Bridge;
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.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * Base type for all Lutron thing handlers.
39  *
40  * @author Allan Tong - Initial contribution
41  * @author Bob Adair - Added additional commands and methods for status and state management. Added TargetType to
42  *         LutronCommand for LEAP bridge.
43  */
44 @NonNullByDefault
45 public abstract class LutronHandler extends BaseThingHandler {
46     private final Logger logger = LoggerFactory.getLogger(LutronHandler.class);
47
48     public LutronHandler(Thing thing) {
49         super(thing);
50     }
51
52     public abstract int getIntegrationId();
53
54     public abstract void handleUpdate(LutronCommandType type, String... parameters);
55
56     /**
57      * Queries for any device state needed at initialization time or after losing connectivity to the bridge, and
58      * updates device status. Will be called when bridge status changes to ONLINE and thing has status
59      * OFFLINE:BRIDGE_OFFLINE.
60      */
61     protected abstract void initDeviceState();
62
63     /**
64      * Called when changing thing status to offline. Subclasses may override to take any needed actions.
65      */
66     protected void thingOfflineNotify() {
67     }
68
69     protected @Nullable LutronBridgeHandler getBridgeHandler() {
70         Bridge bridge = getBridge();
71
72         return bridge == null ? null : (LutronBridgeHandler) bridge.getHandler();
73     }
74
75     @Override
76     public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
77         logger.debug("Bridge status changed to {} for lutron device handler {}", bridgeStatusInfo.getStatus(),
78                 getIntegrationId());
79
80         if (bridgeStatusInfo.getStatus() == ThingStatus.ONLINE
81                 && getThing().getStatusInfo().getStatusDetail() == ThingStatusDetail.BRIDGE_OFFLINE) {
82             initDeviceState();
83
84         } else if (bridgeStatusInfo.getStatus() == ThingStatus.OFFLINE) {
85             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
86             thingOfflineNotify();
87         }
88     }
89
90     protected void sendCommand(LutronCommandNew command) {
91         LutronBridgeHandler bridgeHandler = getBridgeHandler();
92
93         if (bridgeHandler == null) {
94             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_MISSING_ERROR, "No bridge associated");
95             thingOfflineNotify();
96         } else {
97             bridgeHandler.sendCommand(command);
98         }
99     }
100
101     protected void output(TargetType type, int action, @Nullable Number parameter, @Nullable LutronDuration fade,
102             @Nullable LutronDuration delay) {
103         sendCommand(
104                 new OutputCommand(type, LutronOperation.EXECUTE, getIntegrationId(), action, parameter, fade, delay));
105     }
106
107     protected void queryOutput(TargetType type, int action) {
108         sendCommand(
109                 new OutputCommand(type, LutronOperation.QUERY, getIntegrationId(), action, (Integer) null, null, null));
110     }
111
112     protected void device(TargetType type, Integer component, @Nullable Integer leapComponent, Integer action,
113             @Nullable Object parameter) {
114         sendCommand(new DeviceCommand(type, LutronOperation.EXECUTE, getIntegrationId(), component, leapComponent,
115                 action, parameter));
116     }
117
118     protected void queryDevice(TargetType type, Integer component, Integer action) {
119         sendCommand(new DeviceCommand(type, LutronOperation.QUERY, getIntegrationId(), component, null, action, null));
120     }
121
122     protected void timeclock(Integer action, @Nullable Object parameter, @Nullable Boolean enable) {
123         sendCommand(new TimeclockCommand(LutronOperation.EXECUTE, getIntegrationId(), action, parameter, enable));
124     }
125
126     protected void queryTimeclock(Integer action) {
127         sendCommand(new TimeclockCommand(LutronOperation.QUERY, getIntegrationId(), action, null, null));
128     }
129
130     protected void sysvar(Integer action, Object parameter) {
131         sendCommand(new SysvarCommand(LutronOperation.EXECUTE, getIntegrationId(), action, parameter));
132     }
133
134     protected void querySysvar(Integer action) {
135         sendCommand(new SysvarCommand(LutronOperation.QUERY, getIntegrationId(), action, null));
136     }
137
138     protected void greenMode(Integer action, @Nullable Integer parameter) {
139         sendCommand(new ModeCommand(LutronOperation.EXECUTE, getIntegrationId(), action, parameter));
140     }
141
142     protected void queryGreenMode(Integer action) {
143         sendCommand(new ModeCommand(LutronOperation.QUERY, getIntegrationId(), action, null));
144     }
145
146     protected void queryGroup(Integer action) {
147         sendCommand(new GroupCommand(LutronOperation.QUERY, getIntegrationId(), action, null));
148     }
149 }