2 * Copyright (c) 2010-2021 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;
51 private int partitionNumber;
52 private int userNumber;
55 private int zoneNumber;
58 private int keypadAddress;
59 private int terminalModeSeconds;
61 public CaddxBaseThingHandler(Thing thing, CaddxThingType caddxThingType) {
63 this.caddxThingType = caddxThingType;
67 public void initialize() {
68 getConfiguration(caddxThingType);
70 // set the Thing offline for now
71 updateStatus(ThingStatus.OFFLINE);
75 * Get the Bridge Handler for the Caddx system.
77 * @return CaddxBridgeHandler
79 public @Nullable CaddxBridgeHandler getCaddxBridgeHandler() {
80 if (this.caddxBridgeHandler == null) {
81 Bridge bridge = getBridge();
84 logger.debug("getCaddxBridgeHandler(): Unable to get bridge!");
88 logger.trace("getCaddxBridgeHandler(): Bridge for '{}' - '{}'", getThing().getUID(), bridge.getUID());
90 ThingHandler handler = bridge.getHandler();
92 if (handler instanceof CaddxBridgeHandler) {
93 this.caddxBridgeHandler = (CaddxBridgeHandler) handler;
95 logger.debug("getCaddxBridgeHandler(): Unable to get bridge handler!");
99 return this.caddxBridgeHandler;
103 * Method to Update a Channel
109 public abstract void updateChannel(ChannelUID channel, String data);
112 * Receives Events from the bridge.
117 public abstract void caddxEventReceived(CaddxEvent event, Thing thing);
120 public void handleCommand(ChannelUID channelUID, Command command) {
124 * Get the thing configuration.
126 * @param caddxThingType The Thing type
128 private void getConfiguration(CaddxThingType caddxThingType) {
129 switch (caddxThingType) {
131 CaddxPartitionConfiguration partitionConfiguration = getConfigAs(CaddxPartitionConfiguration.class);
132 setPartitionNumber(partitionConfiguration.getPartitionNumber());
133 setUserNumber(partitionConfiguration.getUserNumber());
136 CaddxZoneConfiguration zoneConfiguration = getConfigAs(CaddxZoneConfiguration.class);
137 setZoneNumber(zoneConfiguration.getZoneNumber());
140 CaddxKeypadConfiguration keypadConfiguration = getConfigAs(CaddxKeypadConfiguration.class);
141 setKeypadAddress(keypadConfiguration.getKeypadAddress());
148 * Get the Thing type.
150 * @return caddxThingType
152 public CaddxThingType getCaddxThingType() {
153 return caddxThingType;
157 * Get Partition Number.
159 * @return partitionNumber
161 public int getPartitionNumber() {
162 return partitionNumber;
166 * Set Partition Number.
168 * @param partitionNumber
170 public void setPartitionNumber(int partitionNumber) {
171 this.partitionNumber = partitionNumber;
179 public int getUserNumber() {
188 public void setUserNumber(int userNumber) {
189 this.userNumber = userNumber;
197 public int getZoneNumber() {
206 public void setZoneNumber(int zoneNumber) {
207 this.zoneNumber = zoneNumber;
211 * Get Keypad Address.
213 * @return keypadAddress
215 public int getKeypadAddress() {
216 return keypadAddress;
220 * Set Keypad Address.
222 * @param keypadAddress
224 public void setKeypadAddress(int keypadAddress) {
225 this.keypadAddress = keypadAddress;
229 * Get Keypad Terminal Mode Seconds.
231 * @return keypadAddress
233 public int getTerminalModeSeconds() {
234 return terminalModeSeconds;
238 * Set Keypad Address.
240 * @param keypadAddress
242 public void setTerminalModeSeconds(int terminalModeSeconds) {
243 this.terminalModeSeconds = terminalModeSeconds;
247 * Get Channel by ChannelUID.
251 public @Nullable Channel getChannel(ChannelUID channelUID) {
252 Channel channel = null;
254 List<Channel> channels = getThing().getChannels();
256 for (Channel ch : channels) {
257 if (channelUID == ch.getUID()) {