]> git.basschouten.com Git - openhab-addons.git/blob
01fddec086fc45cabaea926d21c1cdc9710ccae5
[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 data
106      */
107     public abstract void updateChannel(ChannelUID channel, String data);
108
109     /**
110      * Receives Events from the bridge.
111      *
112      * @param event
113      * @param thing
114      */
115     public abstract void caddxEventReceived(CaddxEvent event, Thing thing);
116
117     @Override
118     public void handleCommand(ChannelUID channelUID, Command command) {
119     }
120
121     /**
122      * Get the thing configuration.
123      *
124      * @param caddxThingType The Thing type
125      */
126     private void getConfiguration(CaddxThingType caddxThingType) {
127         switch (caddxThingType) {
128             case PARTITION:
129                 CaddxPartitionConfiguration partitionConfiguration = getConfigAs(CaddxPartitionConfiguration.class);
130                 setPartitionNumber(partitionConfiguration.getPartitionNumber());
131                 break;
132             case ZONE:
133                 CaddxZoneConfiguration zoneConfiguration = getConfigAs(CaddxZoneConfiguration.class);
134                 setZoneNumber(zoneConfiguration.getZoneNumber());
135                 break;
136             case KEYPAD:
137                 CaddxKeypadConfiguration keypadConfiguration = getConfigAs(CaddxKeypadConfiguration.class);
138                 setKeypadAddress(keypadConfiguration.getKeypadAddress());
139                 setTerminalModeSeconds(keypadConfiguration.getTerminalModeSeconds());
140             default:
141                 break;
142         }
143     }
144
145     /**
146      * Get the Thing type.
147      *
148      * @return caddxThingType
149      */
150     public CaddxThingType getCaddxThingType() {
151         return caddxThingType;
152     }
153
154     /**
155      * Get Partition Number.
156      *
157      * @return partitionNumber
158      */
159     public int getPartitionNumber() {
160         return partitionNumber;
161     }
162
163     /**
164      * Set Partition Number.
165      *
166      * @param partitionNumber
167      */
168     public void setPartitionNumber(int partitionNumber) {
169         this.partitionNumber = partitionNumber;
170     }
171
172     /**
173      * Get Zone Number.
174      *
175      * @return zoneNumber
176      */
177     public int getZoneNumber() {
178         return zoneNumber;
179     }
180
181     /**
182      * Set Zone Number.
183      *
184      * @param zoneNumber
185      */
186     public void setZoneNumber(int zoneNumber) {
187         this.zoneNumber = zoneNumber;
188     }
189
190     /**
191      * Get Keypad Address.
192      *
193      * @return keypadAddress
194      */
195     public int getKeypadAddress() {
196         return keypadAddress;
197     }
198
199     /**
200      * Set Keypad Address.
201      *
202      * @param keypadAddress
203      */
204     public void setKeypadAddress(int keypadAddress) {
205         this.keypadAddress = keypadAddress;
206     }
207
208     /**
209      * Get Keypad Terminal Mode Seconds.
210      *
211      * @return terminalModeSeconds
212      */
213     public int getTerminalModeSeconds() {
214         return terminalModeSeconds;
215     }
216
217     /**
218      * Set Keypad Terminal Mode Seconds.
219      *
220      * @param terminalModeSeconds
221      */
222     public void setTerminalModeSeconds(int terminalModeSeconds) {
223         this.terminalModeSeconds = terminalModeSeconds;
224     }
225
226     /**
227      * Get Channel by ChannelUID.
228      *
229      * @param channelUID
230      */
231     public @Nullable Channel getChannel(ChannelUID channelUID) {
232         Channel channel = null;
233
234         List<Channel> channels = getThing().getChannels();
235
236         for (Channel ch : channels) {
237             if (channelUID.equals(ch.getUID())) {
238                 channel = ch;
239                 break;
240             }
241         }
242
243         return channel;
244     }
245 }