]> git.basschouten.com Git - openhab-addons.git/blob
2aa13ddf926a979b321bb33697c45cb4ff4c53a7
[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.luxom.internal.handler;
14
15 import java.util.List;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.luxom.internal.protocol.LuxomAction;
20 import org.openhab.binding.luxom.internal.protocol.LuxomCommand;
21 import org.openhab.core.thing.Bridge;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingStatus;
24 import org.openhab.core.thing.ThingStatusDetail;
25 import org.openhab.core.thing.ThingStatusInfo;
26 import org.openhab.core.thing.binding.BaseThingHandler;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Base type for all Luxom thing handlers.
32  *
33  * @author Kris Jespers - Initial contribution
34  */
35 @NonNullByDefault
36 public abstract class LuxomThingHandler extends BaseThingHandler {
37     private final Logger logger = LoggerFactory.getLogger(LuxomThingHandler.class);
38
39     private String address = "";
40
41     @Override
42     public void initialize() {
43         String id = (String) getConfig().get("address");
44         if (id == null) {
45             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
46                     "@text/status.thing-address-missing");
47             address = "noaddress";
48             return;
49         }
50         address = id;
51     }
52
53     public LuxomThingHandler(Thing thing) {
54         super(thing);
55     }
56
57     public abstract void handleCommandComingFromBridge(LuxomCommand command);
58
59     public final String getAddress() {
60         return address;
61     }
62
63     /**
64      * Queries for any device state needed at initialization time or after losing connectivity to the bridge, and
65      * updates device status. Will be called when bridge status changes to ONLINE and thing has status
66      * OFFLINE:BRIDGE_OFFLINE.
67      */
68     protected abstract void initDeviceState();
69
70     /**
71      * Called when changing thing status to offline. Subclasses may override to take any needed actions.
72      */
73     protected void thingOfflineNotify() {
74     }
75
76     protected @Nullable LuxomBridgeHandler getBridgeHandler() {
77         Bridge bridge = getBridge();
78
79         return bridge == null ? null : (LuxomBridgeHandler) bridge.getHandler();
80     }
81
82     @Override
83     public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
84         logger.debug("Bridge status changed to {} for luxom device handler {}", bridgeStatusInfo.getStatus(),
85                 getAddress());
86
87         if (bridgeStatusInfo.getStatus() == ThingStatus.ONLINE
88                 && getThing().getStatusInfo().getStatusDetail() == ThingStatusDetail.BRIDGE_OFFLINE) {
89             initDeviceState();
90
91         } else if (bridgeStatusInfo.getStatus() == ThingStatus.OFFLINE) {
92             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
93             thingOfflineNotify();
94         }
95     }
96
97     protected void sendCommands(List<CommandExecutionSpecification> commands) {
98         @Nullable
99         LuxomBridgeHandler bridgeHandler = getBridgeHandler();
100
101         if (bridgeHandler == null) {
102             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_MISSING_ERROR,
103                     "@text/status.bridge-handler-missing");
104             thingOfflineNotify();
105         } else {
106             bridgeHandler.sendCommands(commands);
107         }
108     }
109
110     /**
111      * example : *P,0,1,21;
112      */
113     protected void ping() {
114         sendCommands(List.of(new CommandExecutionSpecification(LuxomAction.PING.getCommand() + ",0," + getAddress())));
115     }
116
117     /**
118      * example : *S,0,1,21;
119      */
120     protected void set() {
121         sendCommands(List.of(new CommandExecutionSpecification(LuxomAction.SET.getCommand() + ",0," + getAddress())));
122     }
123
124     /**
125      * example : *C,0,1,21;
126      */
127     protected void clear() {
128         sendCommands(List.of(new CommandExecutionSpecification(LuxomAction.CLEAR.getCommand() + ",0," + getAddress())));
129     }
130 }