]> git.basschouten.com Git - openhab-addons.git/blob
83a960670cef33ffd448b76c05f3f38faf88e5d1
[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.dscalarm.internal.handler;
14
15 import static org.openhab.binding.dscalarm.internal.DSCAlarmBindingConstants.PANEL_MESSAGE;
16
17 import java.util.EventObject;
18 import java.util.List;
19
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;
38
39 /**
40  * Abstract class for a DSC Alarm Thing Handler.
41  *
42  * @author Russell Stephens - Initial Contribution
43  */
44 public abstract class DSCAlarmBaseThingHandler extends BaseThingHandler {
45
46     private final Logger logger = LoggerFactory.getLogger(DSCAlarmBaseThingHandler.class);
47
48     /** Bridge Handler for the Thing. */
49     public DSCAlarmBaseBridgeHandler dscAlarmBridgeHandler = null;
50
51     /** DSC Alarm Thing type. */
52     private DSCAlarmThingType dscAlarmThingType = null;
53
54     /** DSC Alarm Properties. */
55
56     private boolean thingHandlerInitialized = false;
57
58     /** User Code for some DSC Alarm commands. */
59     private String userCode = null;
60
61     /** Suppress Acknowledge messages when received. */
62     private boolean suppressAcknowledgementMsgs = false;
63
64     /** Partition Number. */
65     private int partitionNumber;
66
67     /** Zone Number. */
68     private int zoneNumber;
69
70     /**
71      * Constructor.
72      *
73      * @param thing
74      */
75     public DSCAlarmBaseThingHandler(Thing thing) {
76         super(thing);
77     }
78
79     @Override
80     public void initialize() {
81         logger.debug("Initializing DSC Alarm Thing handler - Thing Type: {}; Thing ID: {}.", dscAlarmThingType,
82                 this.getThing().getUID());
83
84         getConfiguration(dscAlarmThingType);
85
86         Bridge bridge = getBridge();
87         initializeThingHandler(bridge != null ? bridge.getStatus() : null);
88         this.setThingHandlerInitialized(true);
89     }
90
91     @Override
92     public void dispose() {
93         logger.debug("Thing {} disposed.", getThing().getUID());
94         this.setThingHandlerInitialized(false);
95         super.dispose();
96     }
97
98     /**
99      * Method to Initialize Thing Handler.
100      */
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());
107
108                 for (Channel channel : channels) {
109                     if ("DateTime".equals(channel.getAcceptedItemType())) {
110                         updateChannel(channel.getUID(), 0, "0000010100");
111                     } else {
112                         updateChannel(channel.getUID(), 0, "");
113                     }
114                 }
115
116                 if (dscAlarmThingType.equals(DSCAlarmThingType.PANEL)) {
117                     dscAlarmBridgeHandler.setUserCode(getUserCode());
118                 }
119
120                 updateStatus(ThingStatus.ONLINE);
121
122                 logger.debug("initializeThingHandler(): Thing Handler Initialized - {}", thing.getUID());
123             } else {
124                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
125                 logger.debug("initializeThingHandler(): Thing '{}' is set to OFFLINE because bridge is OFFLINE",
126                         thing.getUID());
127             }
128         } else {
129             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED);
130             logger.debug("initializeThingHandler(): Thing '{}' is set to OFFLINE because bridge is uninitialized",
131                     thing.getUID());
132         }
133     }
134
135     /**
136      * Get the Bridge Handler for the DSC Alarm.
137      *
138      * @return dscAlarmBridgeHandler
139      */
140     public synchronized DSCAlarmBaseBridgeHandler getDSCAlarmBridgeHandler() {
141         if (this.dscAlarmBridgeHandler == null) {
142             Bridge bridge = getBridge();
143
144             if (bridge == null) {
145                 logger.debug("getDSCAlarmBridgeHandler(): Unable to get bridge!");
146                 return null;
147             }
148
149             logger.debug("getDSCAlarmBridgeHandler(): Bridge for '{}' - '{}'", getThing().getUID(), bridge.getUID());
150
151             ThingHandler handler = bridge.getHandler();
152
153             if (handler instanceof DSCAlarmBaseBridgeHandler dscAlarmBridgeHandler) {
154                 this.dscAlarmBridgeHandler = dscAlarmBridgeHandler;
155             } else {
156                 logger.debug("getDSCAlarmBridgeHandler(): Unable to get bridge handler!");
157             }
158         }
159
160         return this.dscAlarmBridgeHandler;
161     }
162
163     /**
164      * Method to Update a Channel
165      *
166      * @param channel
167      * @param state
168      * @param description
169      */
170     public abstract void updateChannel(ChannelUID channel, int state, String description);
171
172     /**
173      * Receives DSC Alarm Events from the bridge.
174      *
175      * @param event.
176      * @param thing
177      */
178     public abstract void dscAlarmEventReceived(EventObject event, Thing thing);
179
180     @Override
181     public void handleCommand(ChannelUID channelUID, Command command) {
182     }
183
184     @Override
185     public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
186         logger.debug("bridgeStatusChanged(): Bridge Status: '{}' - Thing '{}' Status: '{}'!", bridgeStatusInfo,
187                 getThing().getUID(), getThing().getStatus());
188         initializeThingHandler(bridgeStatusInfo.getStatus());
189     }
190
191     /**
192      * Get the thing configuration.
193      *
194      * @param dscAlarmDeviceType
195      */
196     private void getConfiguration(DSCAlarmThingType dscAlarmDeviceType) {
197         switch (dscAlarmDeviceType) {
198             case PANEL:
199                 DSCAlarmPanelConfiguration panelConfiguration = getConfigAs(DSCAlarmPanelConfiguration.class);
200                 setUserCode(panelConfiguration.userCode);
201                 setSuppressAcknowledgementMsgs(panelConfiguration.suppressAcknowledgementMsgs);
202                 break;
203             case PARTITION:
204                 DSCAlarmPartitionConfiguration partitionConfiguration = getConfigAs(
205                         DSCAlarmPartitionConfiguration.class);
206                 setPartitionNumber(partitionConfiguration.partitionNumber.intValue());
207                 break;
208             case ZONE:
209                 DSCAlarmZoneConfiguration zoneConfiguration = getConfigAs(DSCAlarmZoneConfiguration.class);
210                 setPartitionNumber(zoneConfiguration.partitionNumber.intValue());
211                 setZoneNumber(zoneConfiguration.zoneNumber.intValue());
212                 break;
213             case KEYPAD:
214             default:
215                 break;
216         }
217     }
218
219     /**
220      * Get the DSC Alarm Thing type.
221      *
222      * @return dscAlarmThingType
223      */
224     public DSCAlarmThingType getDSCAlarmThingType() {
225         return dscAlarmThingType;
226     }
227
228     /**
229      * Set the DSC Alarm Thing type.
230      *
231      * @param dscAlarmDeviceType
232      */
233     public void setDSCAlarmThingType(DSCAlarmThingType dscAlarmDeviceType) {
234         if (dscAlarmDeviceType == null) {
235             String thingType = getThing().getThingTypeUID().toString().split(":")[1];
236             this.dscAlarmThingType = DSCAlarmThingType.getDSCAlarmThingType(thingType);
237         } else {
238             this.dscAlarmThingType = dscAlarmDeviceType;
239         }
240     }
241
242     /**
243      * Get suppressAcknowledgementMsgs.
244      *
245      * @return suppressAcknowledgementMsgs
246      */
247     public boolean getSuppressAcknowledgementMsgs() {
248         return suppressAcknowledgementMsgs;
249     }
250
251     /**
252      * Set suppressAcknowledgementMsgs.
253      *
254      * @param suppressAckMsgs
255      */
256     public void setSuppressAcknowledgementMsgs(boolean suppressAckMsgs) {
257         this.suppressAcknowledgementMsgs = suppressAckMsgs;
258     }
259
260     /**
261      * Get Partition Number.
262      *
263      * @return partitionNumber
264      */
265     public int getPartitionNumber() {
266         return partitionNumber;
267     }
268
269     /**
270      * Set Partition Number.
271      *
272      * @param partitionNumber
273      */
274     public void setPartitionNumber(int partitionNumber) {
275         this.partitionNumber = partitionNumber;
276     }
277
278     /**
279      * Get Zone Number.
280      *
281      * @return zoneNumber
282      */
283     public int getZoneNumber() {
284         return zoneNumber;
285     }
286
287     /**
288      * Set Zone Number.
289      *
290      * @param zoneNumber
291      */
292     public void setZoneNumber(int zoneNumber) {
293         this.zoneNumber = zoneNumber;
294     }
295
296     /**
297      * Get User Code.
298      *
299      * @return userCode
300      */
301     public String getUserCode() {
302         return userCode;
303     }
304
305     /**
306      * Set User Code.
307      *
308      * @param userCode
309      */
310     public void setUserCode(String userCode) {
311         this.userCode = userCode;
312     }
313
314     /**
315      * Get Channel by ChannelUID.
316      *
317      * @param channelUID
318      */
319     public Channel getChannel(ChannelUID channelUID) {
320         Channel channel = null;
321
322         List<Channel> channels = getThing().getChannels();
323
324         for (Channel ch : channels) {
325             if (ch.getUID().equals(channelUID)) {
326                 channel = ch;
327                 break;
328             }
329         }
330
331         return channel;
332     }
333
334     /**
335      * Get Thing Handler refresh status.
336      *
337      * @return thingRefresh
338      */
339     public boolean isThingHandlerInitialized() {
340         return thingHandlerInitialized;
341     }
342
343     /**
344      * Set Thing Handler refresh status.
345      *
346      * @param deviceInitialized
347      */
348     public void setThingHandlerInitialized(boolean refreshed) {
349         this.thingHandlerInitialized = refreshed;
350     }
351
352     /**
353      * Method to set the panel message.
354      *
355      * @param dscAlarmMessage
356      */
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));
362
363         if ((dscAlarmCode == DSCAlarmCode.CommandAcknowledge || dscAlarmCode == DSCAlarmCode.TimeDateBroadcast)
364                 && getSuppressAcknowledgementMsgs()) {
365             return;
366         } else {
367             updateChannel(channelUID, 0, message);
368             logger.debug("setPanelMessage(): Panel Message Set to - {}", message);
369         }
370     }
371 }