]> git.basschouten.com Git - openhab-addons.git/blob
c211b4e78f3fb6230ff81f059821f6e186c9c27e
[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         } else if (bridgeStatusInfo.getStatus() == ThingStatus.OFFLINE) {
91             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
92             thingOfflineNotify();
93         }
94     }
95
96     protected void sendCommands(List<CommandExecutionSpecification> commands) {
97         @Nullable
98         LuxomBridgeHandler bridgeHandler = getBridgeHandler();
99
100         if (bridgeHandler == null) {
101             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_MISSING_ERROR,
102                     "@text/status.bridge-handler-missing");
103             thingOfflineNotify();
104         } else {
105             bridgeHandler.sendCommands(commands);
106         }
107     }
108
109     /**
110      * example : *P,0,1,21;
111      */
112     protected void ping() {
113         sendCommands(List.of(new CommandExecutionSpecification(LuxomAction.PING.getCommand() + ",0," + getAddress())));
114     }
115
116     /**
117      * example : *S,0,1,21;
118      */
119     protected void set() {
120         sendCommands(List.of(new CommandExecutionSpecification(LuxomAction.SET.getCommand() + ",0," + getAddress())));
121     }
122
123     /**
124      * example : *C,0,1,21;
125      */
126     protected void clear() {
127         sendCommands(List.of(new CommandExecutionSpecification(LuxomAction.CLEAR.getCommand() + ",0," + getAddress())));
128     }
129 }