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