2 * Copyright (c) 2010-2023 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.dscalarm.internal.handler;
15 import static org.openhab.binding.dscalarm.internal.DSCAlarmBindingConstants.PANEL_MESSAGE;
17 import java.util.EventObject;
18 import java.util.List;
20 import org.openhab.binding.dscalarm.internal.DSCAlarmCode;
21 import org.openhab.binding.dscalarm.internal.DSCAlarmMessage;
22 import org.openhab.binding.dscalarm.internal.DSCAlarmMessage.DSCAlarmMessageInfoType;
23 import org.openhab.binding.dscalarm.internal.config.DSCAlarmPanelConfiguration;
24 import org.openhab.binding.dscalarm.internal.config.DSCAlarmPartitionConfiguration;
25 import org.openhab.binding.dscalarm.internal.config.DSCAlarmZoneConfiguration;
26 import org.openhab.core.thing.Bridge;
27 import org.openhab.core.thing.Channel;
28 import org.openhab.core.thing.ChannelUID;
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.openhab.core.thing.binding.ThingHandler;
35 import org.openhab.core.types.Command;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * Abstract class for a DSC Alarm Thing Handler.
42 * @author Russell Stephens - Initial Contribution
44 public abstract class DSCAlarmBaseThingHandler extends BaseThingHandler {
46 private final Logger logger = LoggerFactory.getLogger(DSCAlarmBaseThingHandler.class);
48 /** Bridge Handler for the Thing. */
49 public DSCAlarmBaseBridgeHandler dscAlarmBridgeHandler = null;
51 /** DSC Alarm Thing type. */
52 private DSCAlarmThingType dscAlarmThingType = null;
54 /** DSC Alarm Properties. */
56 private boolean thingHandlerInitialized = false;
58 /** User Code for some DSC Alarm commands. */
59 private String userCode = null;
61 /** Suppress Acknowledge messages when received. */
62 private boolean suppressAcknowledgementMsgs = false;
64 /** Partition Number. */
65 private int partitionNumber;
68 private int zoneNumber;
75 public DSCAlarmBaseThingHandler(Thing thing) {
80 public void initialize() {
81 logger.debug("Initializing DSC Alarm Thing handler - Thing Type: {}; Thing ID: {}.", dscAlarmThingType,
82 this.getThing().getUID());
84 getConfiguration(dscAlarmThingType);
86 Bridge bridge = getBridge();
87 initializeThingHandler(bridge != null ? bridge.getStatus() : null);
88 this.setThingHandlerInitialized(true);
92 public void dispose() {
93 logger.debug("Thing {} disposed.", getThing().getUID());
94 this.setThingHandlerInitialized(false);
99 * Method to Initialize Thing Handler.
101 private void initializeThingHandler(ThingStatus bridgeStatus) {
102 if (getDSCAlarmBridgeHandler() != null && bridgeStatus != null) {
103 if (bridgeStatus == ThingStatus.ONLINE) {
104 Thing thing = getThing();
105 List<Channel> channels = thing.getChannels();
106 logger.debug("initializeThingHandler(): Initialize Thing Handler - {}", thing.getUID());
108 for (Channel channel : channels) {
109 if ("DateTime".equals(channel.getAcceptedItemType())) {
110 updateChannel(channel.getUID(), 0, "0000010100");
112 updateChannel(channel.getUID(), 0, "");
116 if (dscAlarmThingType.equals(DSCAlarmThingType.PANEL)) {
117 dscAlarmBridgeHandler.setUserCode(getUserCode());
120 updateStatus(ThingStatus.ONLINE);
122 logger.debug("initializeThingHandler(): Thing Handler Initialized - {}", thing.getUID());
124 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
125 logger.debug("initializeThingHandler(): Thing '{}' is set to OFFLINE because bridge is OFFLINE",
129 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED);
130 logger.debug("initializeThingHandler(): Thing '{}' is set to OFFLINE because bridge is uninitialized",
136 * Get the Bridge Handler for the DSC Alarm.
138 * @return dscAlarmBridgeHandler
140 public synchronized DSCAlarmBaseBridgeHandler getDSCAlarmBridgeHandler() {
141 if (this.dscAlarmBridgeHandler == null) {
142 Bridge bridge = getBridge();
144 if (bridge == null) {
145 logger.debug("getDSCAlarmBridgeHandler(): Unable to get bridge!");
149 logger.debug("getDSCAlarmBridgeHandler(): Bridge for '{}' - '{}'", getThing().getUID(), bridge.getUID());
151 ThingHandler handler = bridge.getHandler();
153 if (handler instanceof DSCAlarmBaseBridgeHandler dscAlarmBridgeHandler) {
154 this.dscAlarmBridgeHandler = dscAlarmBridgeHandler;
156 logger.debug("getDSCAlarmBridgeHandler(): Unable to get bridge handler!");
160 return this.dscAlarmBridgeHandler;
164 * Method to Update a Channel
170 public abstract void updateChannel(ChannelUID channel, int state, String description);
173 * Receives DSC Alarm Events from the bridge.
178 public abstract void dscAlarmEventReceived(EventObject event, Thing thing);
181 public void handleCommand(ChannelUID channelUID, Command command) {
185 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
186 logger.debug("bridgeStatusChanged(): Bridge Status: '{}' - Thing '{}' Status: '{}'!", bridgeStatusInfo,
187 getThing().getUID(), getThing().getStatus());
188 initializeThingHandler(bridgeStatusInfo.getStatus());
192 * Get the thing configuration.
194 * @param dscAlarmDeviceType
196 private void getConfiguration(DSCAlarmThingType dscAlarmDeviceType) {
197 switch (dscAlarmDeviceType) {
199 DSCAlarmPanelConfiguration panelConfiguration = getConfigAs(DSCAlarmPanelConfiguration.class);
200 setUserCode(panelConfiguration.userCode);
201 setSuppressAcknowledgementMsgs(panelConfiguration.suppressAcknowledgementMsgs);
204 DSCAlarmPartitionConfiguration partitionConfiguration = getConfigAs(
205 DSCAlarmPartitionConfiguration.class);
206 setPartitionNumber(partitionConfiguration.partitionNumber.intValue());
209 DSCAlarmZoneConfiguration zoneConfiguration = getConfigAs(DSCAlarmZoneConfiguration.class);
210 setPartitionNumber(zoneConfiguration.partitionNumber.intValue());
211 setZoneNumber(zoneConfiguration.zoneNumber.intValue());
220 * Get the DSC Alarm Thing type.
222 * @return dscAlarmThingType
224 public DSCAlarmThingType getDSCAlarmThingType() {
225 return dscAlarmThingType;
229 * Set the DSC Alarm Thing type.
231 * @param dscAlarmDeviceType
233 public void setDSCAlarmThingType(DSCAlarmThingType dscAlarmDeviceType) {
234 if (dscAlarmDeviceType == null) {
235 String thingType = getThing().getThingTypeUID().toString().split(":")[1];
236 this.dscAlarmThingType = DSCAlarmThingType.getDSCAlarmThingType(thingType);
238 this.dscAlarmThingType = dscAlarmDeviceType;
243 * Get suppressAcknowledgementMsgs.
245 * @return suppressAcknowledgementMsgs
247 public boolean getSuppressAcknowledgementMsgs() {
248 return suppressAcknowledgementMsgs;
252 * Set suppressAcknowledgementMsgs.
254 * @param suppressAckMsgs
256 public void setSuppressAcknowledgementMsgs(boolean suppressAckMsgs) {
257 this.suppressAcknowledgementMsgs = suppressAckMsgs;
261 * Get Partition Number.
263 * @return partitionNumber
265 public int getPartitionNumber() {
266 return partitionNumber;
270 * Set Partition Number.
272 * @param partitionNumber
274 public void setPartitionNumber(int partitionNumber) {
275 this.partitionNumber = partitionNumber;
283 public int getZoneNumber() {
292 public void setZoneNumber(int zoneNumber) {
293 this.zoneNumber = zoneNumber;
301 public String getUserCode() {
310 public void setUserCode(String userCode) {
311 this.userCode = userCode;
315 * Get Channel by ChannelUID.
319 public Channel getChannel(ChannelUID channelUID) {
320 Channel channel = null;
322 List<Channel> channels = getThing().getChannels();
324 for (Channel ch : channels) {
325 if (ch.getUID().equals(channelUID)) {
335 * Get Thing Handler refresh status.
337 * @return thingRefresh
339 public boolean isThingHandlerInitialized() {
340 return thingHandlerInitialized;
344 * Set Thing Handler refresh status.
348 public void setThingHandlerInitialized(boolean refreshed) {
349 this.thingHandlerInitialized = refreshed;
353 * Method to set the panel message.
355 * @param dscAlarmMessage
357 public void setPanelMessage(DSCAlarmMessage dscAlarmMessage) {
358 ChannelUID channelUID = new ChannelUID(getThing().getUID(), PANEL_MESSAGE);
359 String message = dscAlarmMessage.getMessageInfo(DSCAlarmMessageInfoType.DESCRIPTION);
360 DSCAlarmCode dscAlarmCode = DSCAlarmCode
361 .getDSCAlarmCodeValue(dscAlarmMessage.getMessageInfo(DSCAlarmMessageInfoType.CODE));
363 if ((dscAlarmCode == DSCAlarmCode.CommandAcknowledge || dscAlarmCode == DSCAlarmCode.TimeDateBroadcast)
364 && getSuppressAcknowledgementMsgs()) {
367 updateChannel(channelUID, 0, message);
368 logger.debug("setPanelMessage(): Panel Message Set to - {}", message);