]> git.basschouten.com Git - openhab-addons.git/blob
f7775c5d2a045ede7f2b179326495aa281a35b57
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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     private int userNumber;
53
54     /** Zone */
55     private int zoneNumber;
56
57     /** Keypad */
58     private int keypadAddress;
59     private int terminalModeSeconds;
60
61     public CaddxBaseThingHandler(Thing thing, CaddxThingType caddxThingType) {
62         super(thing);
63         this.caddxThingType = caddxThingType;
64     }
65
66     @Override
67     public void initialize() {
68         getConfiguration(caddxThingType);
69
70         // set the Thing offline for now
71         updateStatus(ThingStatus.OFFLINE);
72     }
73
74     /**
75      * Get the Bridge Handler for the Caddx system.
76      *
77      * @return CaddxBridgeHandler
78      */
79     public @Nullable CaddxBridgeHandler getCaddxBridgeHandler() {
80         if (this.caddxBridgeHandler == null) {
81             Bridge bridge = getBridge();
82
83             if (bridge == null) {
84                 logger.debug("getCaddxBridgeHandler(): Unable to get bridge!");
85                 return null;
86             }
87
88             logger.trace("getCaddxBridgeHandler(): Bridge for '{}' - '{}'", getThing().getUID(), bridge.getUID());
89
90             ThingHandler handler = bridge.getHandler();
91
92             if (handler instanceof CaddxBridgeHandler) {
93                 this.caddxBridgeHandler = (CaddxBridgeHandler) handler;
94             } else {
95                 logger.debug("getCaddxBridgeHandler(): Unable to get bridge handler!");
96             }
97         }
98
99         return this.caddxBridgeHandler;
100     }
101
102     /**
103      * Method to Update a Channel
104      *
105      * @param channel
106      * @param state
107      * @param description
108      */
109     public abstract void updateChannel(ChannelUID channel, String data);
110
111     /**
112      * Receives Events from the bridge.
113      *
114      * @param event.
115      * @param thing
116      */
117     public abstract void caddxEventReceived(CaddxEvent event, Thing thing);
118
119     @Override
120     public void handleCommand(ChannelUID channelUID, Command command) {
121     }
122
123     /**
124      * Get the thing configuration.
125      *
126      * @param caddxThingType The Thing type
127      */
128     private void getConfiguration(CaddxThingType caddxThingType) {
129         switch (caddxThingType) {
130             case PARTITION:
131                 CaddxPartitionConfiguration partitionConfiguration = getConfigAs(CaddxPartitionConfiguration.class);
132                 setPartitionNumber(partitionConfiguration.getPartitionNumber());
133                 setUserNumber(partitionConfiguration.getUserNumber());
134                 break;
135             case ZONE:
136                 CaddxZoneConfiguration zoneConfiguration = getConfigAs(CaddxZoneConfiguration.class);
137                 setZoneNumber(zoneConfiguration.getZoneNumber());
138                 break;
139             case KEYPAD:
140                 CaddxKeypadConfiguration keypadConfiguration = getConfigAs(CaddxKeypadConfiguration.class);
141                 setKeypadAddress(keypadConfiguration.getKeypadAddress());
142             default:
143                 break;
144         }
145     }
146
147     /**
148      * Get the Thing type.
149      *
150      * @return caddxThingType
151      */
152     public CaddxThingType getCaddxThingType() {
153         return caddxThingType;
154     }
155
156     /**
157      * Get Partition Number.
158      *
159      * @return partitionNumber
160      */
161     public int getPartitionNumber() {
162         return partitionNumber;
163     }
164
165     /**
166      * Set Partition Number.
167      *
168      * @param partitionNumber
169      */
170     public void setPartitionNumber(int partitionNumber) {
171         this.partitionNumber = partitionNumber;
172     }
173
174     /**
175      * Get User Number.
176      *
177      * @return userNumber
178      */
179     public int getUserNumber() {
180         return userNumber;
181     }
182
183     /**
184      * Set User Number.
185      *
186      * @param userNumber
187      */
188     public void setUserNumber(int userNumber) {
189         this.userNumber = userNumber;
190     }
191
192     /**
193      * Get Zone Number.
194      *
195      * @return zoneNumber
196      */
197     public int getZoneNumber() {
198         return zoneNumber;
199     }
200
201     /**
202      * Set Zone Number.
203      *
204      * @param zoneNumber
205      */
206     public void setZoneNumber(int zoneNumber) {
207         this.zoneNumber = zoneNumber;
208     }
209
210     /**
211      * Get Keypad Address.
212      *
213      * @return keypadAddress
214      */
215     public int getKeypadAddress() {
216         return keypadAddress;
217     }
218
219     /**
220      * Set Keypad Address.
221      *
222      * @param keypadAddress
223      */
224     public void setKeypadAddress(int keypadAddress) {
225         this.keypadAddress = keypadAddress;
226     }
227
228     /**
229      * Get Keypad Terminal Mode Seconds.
230      *
231      * @return keypadAddress
232      */
233     public int getTerminalModeSeconds() {
234         return terminalModeSeconds;
235     }
236
237     /**
238      * Set Keypad Address.
239      *
240      * @param keypadAddress
241      */
242     public void setTerminalModeSeconds(int terminalModeSeconds) {
243         this.terminalModeSeconds = terminalModeSeconds;
244     }
245
246     /**
247      * Get Channel by ChannelUID.
248      *
249      * @param channelUID
250      */
251     public @Nullable Channel getChannel(ChannelUID channelUID) {
252         Channel channel = null;
253
254         List<Channel> channels = getThing().getChannels();
255
256         for (Channel ch : channels) {
257             if (channelUID == ch.getUID()) {
258                 channel = ch;
259                 break;
260             }
261         }
262
263         return channel;
264     }
265 }