]> git.basschouten.com Git - openhab-addons.git/blob
9c9ca20df9315f4e14fa9506424cda6e55eab654
[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.alarmdecoder.internal.handler;
14
15 import java.util.concurrent.atomic.AtomicBoolean;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.alarmdecoder.internal.protocol.ADCommand;
19 import org.openhab.binding.alarmdecoder.internal.protocol.ADMessage;
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  * {@link ADThingHandler} is the abstract base class for all AD thing handlers.
31  *
32  * @author Bob Adair - Initial contribution
33  */
34 @NonNullByDefault
35 public abstract class ADThingHandler extends BaseThingHandler {
36
37     private final Logger logger = LoggerFactory.getLogger(ADThingHandler.class);
38     protected final AtomicBoolean firstUpdateReceived = new AtomicBoolean(false);
39
40     public ADThingHandler(Thing thing) {
41         super(thing);
42     }
43
44     /**
45      * Initialize device state and set status for handler. Should be called at the end of initialize(). Also called by
46      * bridgeStatusChanged() when bridge status changes from OFFLINE to ONLINE. Calls initChannelState() to initialize
47      * channels if setting status to ONLINE.
48      */
49     protected void initDeviceState() {
50         logger.trace("Initializing device state");
51         Bridge bridge = getBridge();
52         if (bridge == null) {
53             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No bridge configured");
54         } else if (bridge.getStatus() == ThingStatus.ONLINE) {
55             initChannelState();
56             updateStatus(ThingStatus.ONLINE);
57         } else {
58             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
59         }
60     }
61
62     /**
63      * Initialize channel states if necessary
64      */
65     public abstract void initChannelState();
66
67     /**
68      * Notify handler that panel is in ready state so that any un-updated contact channels can be set to default
69      * (closed).
70      */
71     public abstract void notifyPanelReady();
72
73     /**
74      * Notify handler of a message from the AD via the bridge
75      *
76      * @param msg The ADMessage to handle
77      */
78     public abstract void handleUpdate(ADMessage msg);
79
80     @Override
81     public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
82         ThingStatus bridgeStatus = bridgeStatusInfo.getStatus();
83         logger.debug("Bridge status changed to {} for AD handler", bridgeStatus);
84
85         if (bridgeStatus == ThingStatus.ONLINE
86                 && getThing().getStatusInfo().getStatusDetail() == ThingStatusDetail.BRIDGE_OFFLINE) {
87             initDeviceState();
88         } else if (bridgeStatus == ThingStatus.OFFLINE) {
89             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
90         }
91     }
92
93     /**
94      * Send a command via the bridge
95      *
96      * @param command command to send
97      */
98     protected void sendCommand(ADCommand command) {
99         Bridge bridge = getBridge();
100         ADBridgeHandler bridgeHandler = bridge == null ? null : (ADBridgeHandler) bridge.getHandler();
101
102         if (bridgeHandler == null) {
103             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_MISSING_ERROR, "No bridge associated");
104         } else {
105             bridgeHandler.sendADCommand(command);
106         }
107     }
108 }