]> git.basschouten.com Git - openhab-addons.git/blob
ec4963c01318020db058a25185fcfad199d1baa0
[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.caddx.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.caddx.internal.CaddxEvent;
20 import org.openhab.binding.caddx.internal.config.CaddxKeypadConfiguration;
21 import org.openhab.binding.caddx.internal.config.CaddxPartitionConfiguration;
22 import org.openhab.binding.caddx.internal.config.CaddxZoneConfiguration;
23 import org.openhab.core.thing.Bridge;
24 import org.openhab.core.thing.Channel;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.thing.binding.BaseThingHandler;
29 import org.openhab.core.thing.binding.ThingHandler;
30 import org.openhab.core.types.Command;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * Abstract class for a Caddx Thing Handler.
36  *
37  * @author Georgios Moutsos - Initial contribution
38  */
39 @NonNullByDefault
40 public abstract class CaddxBaseThingHandler extends BaseThingHandler {
41
42     private final Logger logger = LoggerFactory.getLogger(CaddxBaseThingHandler.class);
43
44     /** Bridge Handler for the Thing. */
45     private @Nullable CaddxBridgeHandler caddxBridgeHandler = null;
46
47     /** Caddx Alarm Thing type. */
48     private CaddxThingType caddxThingType;
49
50     /** Partition */
51     private int partitionNumber;
52
53     /** Zone */
54     private int zoneNumber;
55
56     /** Keypad */
57     private int keypadAddress;
58     private int terminalModeSeconds;
59
60     public CaddxBaseThingHandler(Thing thing, CaddxThingType caddxThingType) {
61         super(thing);
62         this.caddxThingType = caddxThingType;
63     }
64
65     @Override
66     public void initialize() {
67         getConfiguration(caddxThingType);
68
69         // set the Thing offline for now
70         updateStatus(ThingStatus.OFFLINE);
71     }
72
73     /**
74      * Get the Bridge Handler for the Caddx system.
75      *
76      * @return CaddxBridgeHandler
77      */
78     public @Nullable CaddxBridgeHandler getCaddxBridgeHandler() {
79         if (this.caddxBridgeHandler == null) {
80             Bridge bridge = getBridge();
81
82             if (bridge == null) {
83                 logger.debug("getCaddxBridgeHandler(): Unable to get bridge!");
84                 return null;
85             }
86
87             logger.trace("getCaddxBridgeHandler(): Bridge for '{}' - '{}'", getThing().getUID(), bridge.getUID());
88
89             ThingHandler handler = bridge.getHandler();
90
91             if (handler instanceof CaddxBridgeHandler bridgeHandler) {
92                 this.caddxBridgeHandler = bridgeHandler;
93             } else {
94                 logger.debug("getCaddxBridgeHandler(): Unable to get bridge handler!");
95             }
96         }
97
98         return this.caddxBridgeHandler;
99     }
100
101     /**
102      * Method to Update a Channel
103      *
104      * @param channel
105      * @param state
106      * @param description
107      */
108     public abstract void updateChannel(ChannelUID channel, String data);
109
110     /**
111      * Receives Events from the bridge.
112      *
113      * @param event.
114      * @param thing
115      */
116     public abstract void caddxEventReceived(CaddxEvent event, Thing thing);
117
118     @Override
119     public void handleCommand(ChannelUID channelUID, Command command) {
120     }
121
122     /**
123      * Get the thing configuration.
124      *
125      * @param caddxThingType The Thing type
126      */
127     private void getConfiguration(CaddxThingType caddxThingType) {
128         switch (caddxThingType) {
129             case PARTITION:
130                 CaddxPartitionConfiguration partitionConfiguration = getConfigAs(CaddxPartitionConfiguration.class);
131                 setPartitionNumber(partitionConfiguration.getPartitionNumber());
132                 break;
133             case ZONE:
134                 CaddxZoneConfiguration zoneConfiguration = getConfigAs(CaddxZoneConfiguration.class);
135                 setZoneNumber(zoneConfiguration.getZoneNumber());
136                 break;
137             case KEYPAD:
138                 CaddxKeypadConfiguration keypadConfiguration = getConfigAs(CaddxKeypadConfiguration.class);
139                 setKeypadAddress(keypadConfiguration.getKeypadAddress());
140                 setTerminalModeSeconds(keypadConfiguration.getTerminalModeSeconds());
141             default:
142                 break;
143         }
144     }
145
146     /**
147      * Get the Thing type.
148      *
149      * @return caddxThingType
150      */
151     public CaddxThingType getCaddxThingType() {
152         return caddxThingType;
153     }
154
155     /**
156      * Get Partition Number.
157      *
158      * @return partitionNumber
159      */
160     public int getPartitionNumber() {
161         return partitionNumber;
162     }
163
164     /**
165      * Set Partition Number.
166      *
167      * @param partitionNumber
168      */
169     public void setPartitionNumber(int partitionNumber) {
170         this.partitionNumber = partitionNumber;
171     }
172
173     /**
174      * Get Zone Number.
175      *
176      * @return zoneNumber
177      */
178     public int getZoneNumber() {
179         return zoneNumber;
180     }
181
182     /**
183      * Set Zone Number.
184      *
185      * @param zoneNumber
186      */
187     public void setZoneNumber(int zoneNumber) {
188         this.zoneNumber = zoneNumber;
189     }
190
191     /**
192      * Get Keypad Address.
193      *
194      * @return keypadAddress
195      */
196     public int getKeypadAddress() {
197         return keypadAddress;
198     }
199
200     /**
201      * Set Keypad Address.
202      *
203      * @param keypadAddress
204      */
205     public void setKeypadAddress(int keypadAddress) {
206         this.keypadAddress = keypadAddress;
207     }
208
209     /**
210      * Get Keypad Terminal Mode Seconds.
211      *
212      * @return terminalModeSeconds
213      */
214     public int getTerminalModeSeconds() {
215         return terminalModeSeconds;
216     }
217
218     /**
219      * Set Keypad Terminal Mode Seconds.
220      *
221      * @param terminalModeSeconds
222      */
223     public void setTerminalModeSeconds(int terminalModeSeconds) {
224         this.terminalModeSeconds = terminalModeSeconds;
225     }
226
227     /**
228      * Get Channel by ChannelUID.
229      *
230      * @param channelUID
231      */
232     public @Nullable Channel getChannel(ChannelUID channelUID) {
233         Channel channel = null;
234
235         List<Channel> channels = getThing().getChannels();
236
237         for (Channel ch : channels) {
238             if (channelUID.equals(ch.getUID())) {
239                 channel = ch;
240                 break;
241             }
242         }
243
244         return channel;
245     }
246 }