2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.alarmdecoder.internal.handler;
15 import java.util.concurrent.atomic.AtomicBoolean;
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;
30 * {@link ADThingHandler} is the abstract base class for all AD thing handlers.
32 * @author Bob Adair - Initial contribution
35 public abstract class ADThingHandler extends BaseThingHandler {
37 private final Logger logger = LoggerFactory.getLogger(ADThingHandler.class);
38 protected final AtomicBoolean firstUpdateReceived = new AtomicBoolean(false);
40 public ADThingHandler(Thing thing) {
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.
49 protected void initDeviceState() {
50 logger.trace("Initializing device state");
51 Bridge bridge = getBridge();
53 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No bridge configured");
54 } else if (bridge.getStatus() == ThingStatus.ONLINE) {
56 updateStatus(ThingStatus.ONLINE);
58 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
63 * Initialize channel states if necessary
65 public abstract void initChannelState();
68 * Notify handler that panel is in ready state so that any un-updated contact channels can be set to default
71 public abstract void notifyPanelReady();
74 * Notify handler of a message from the AD via the bridge
76 * @param msg The ADMessage to handle
78 public abstract void handleUpdate(ADMessage msg);
81 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
82 ThingStatus bridgeStatus = bridgeStatusInfo.getStatus();
83 logger.debug("Bridge status changed to {} for AD handler", bridgeStatus);
85 if (bridgeStatus == ThingStatus.ONLINE
86 && getThing().getStatusInfo().getStatusDetail() == ThingStatusDetail.BRIDGE_OFFLINE) {
89 } else if (bridgeStatus == ThingStatus.OFFLINE) {
90 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
95 * Send a command via the bridge
97 * @param command command to send
99 protected void sendCommand(ADCommand command) {
100 Bridge bridge = getBridge();
101 ADBridgeHandler bridgeHandler = bridge == null ? null : (ADBridgeHandler) bridge.getHandler();
103 if (bridgeHandler == null) {
104 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_MISSING_ERROR, "No bridge associated");
106 bridgeHandler.sendADCommand(command);