2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.caddx.internal.handler;
15 import java.util.List;
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;
35 * Abstract class for a Caddx Thing Handler.
37 * @author Georgios Moutsos - Initial contribution
40 public abstract class CaddxBaseThingHandler extends BaseThingHandler {
42 private final Logger logger = LoggerFactory.getLogger(CaddxBaseThingHandler.class);
44 /** Bridge Handler for the Thing. */
45 private @Nullable CaddxBridgeHandler caddxBridgeHandler = null;
47 /** Caddx Alarm Thing type. */
48 private CaddxThingType caddxThingType;
50 /** Partition Number. */
51 private int partitionNumber;
54 private int userNumber;
57 private int zoneNumber;
59 /** Keypad Address. */
60 private int keypadAddress;
62 public CaddxBaseThingHandler(Thing thing, CaddxThingType caddxThingType) {
64 this.caddxThingType = caddxThingType;
68 public void initialize() {
69 getConfiguration(caddxThingType);
71 // set the Thing offline for now
72 updateStatus(ThingStatus.OFFLINE);
76 * Get the Bridge Handler for the Caddx system.
78 * @return CaddxBridgeHandler
80 public @Nullable CaddxBridgeHandler getCaddxBridgeHandler() {
81 if (this.caddxBridgeHandler == null) {
82 Bridge bridge = getBridge();
85 logger.debug("getCaddxBridgeHandler(): Unable to get bridge!");
89 logger.trace("getCaddxBridgeHandler(): Bridge for '{}' - '{}'", getThing().getUID(), bridge.getUID());
91 ThingHandler handler = bridge.getHandler();
93 if (handler instanceof CaddxBridgeHandler) {
94 this.caddxBridgeHandler = (CaddxBridgeHandler) handler;
96 logger.debug("getCaddxBridgeHandler(): Unable to get bridge handler!");
100 return this.caddxBridgeHandler;
104 * Method to Update a Channel
110 public abstract void updateChannel(ChannelUID channel, String data);
113 * Receives Events from the bridge.
118 public abstract void caddxEventReceived(CaddxEvent event, Thing thing);
121 public void handleCommand(ChannelUID channelUID, Command command) {
125 * Get the thing configuration.
127 * @param caddxThingType The Thing type
129 private void getConfiguration(CaddxThingType caddxThingType) {
130 switch (caddxThingType) {
132 CaddxPartitionConfiguration partitionConfiguration = getConfigAs(CaddxPartitionConfiguration.class);
133 setPartitionNumber(partitionConfiguration.getPartitionNumber());
134 setUserNumber(partitionConfiguration.getUserNumber());
137 CaddxZoneConfiguration zoneConfiguration = getConfigAs(CaddxZoneConfiguration.class);
138 setZoneNumber(zoneConfiguration.getZoneNumber());
141 CaddxKeypadConfiguration keypadConfiguration = getConfigAs(CaddxKeypadConfiguration.class);
142 setKeypadAddress(keypadConfiguration.getKeypadAddress());
149 * Get the Thing type.
151 * @return caddxThingType
153 public CaddxThingType getCaddxThingType() {
154 return caddxThingType;
158 * Get Partition Number.
160 * @return partitionNumber
162 public int getPartitionNumber() {
163 return partitionNumber;
167 * Set Partition Number.
169 * @param partitionNumber
171 public void setPartitionNumber(int partitionNumber) {
172 this.partitionNumber = partitionNumber;
180 public int getUserNumber() {
189 public void setUserNumber(int userNumber) {
190 this.userNumber = userNumber;
198 public int getZoneNumber() {
207 public void setZoneNumber(int zoneNumber) {
208 this.zoneNumber = zoneNumber;
212 * Get Keypad Address.
214 * @return keypadAddress
216 public int getKeypadAddress() {
217 return keypadAddress;
221 * Set Keypad Address.
223 * @param keypadAddress
225 public void setKeypadAddress(int keypadAddress) {
226 this.keypadAddress = keypadAddress;
230 * Get Channel by ChannelUID.
234 public @Nullable Channel getChannel(ChannelUID channelUID) {
235 Channel channel = null;
237 List<Channel> channels = getThing().getChannels();
239 for (Channel ch : channels) {
240 if (channelUID == ch.getUID()) {