2 * Copyright (c) 2010-2023 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;
54 private int zoneNumber;
57 private int keypadAddress;
58 private int terminalModeSeconds;
60 public CaddxBaseThingHandler(Thing thing, CaddxThingType caddxThingType) {
62 this.caddxThingType = caddxThingType;
66 public void initialize() {
67 getConfiguration(caddxThingType);
69 // set the Thing offline for now
70 updateStatus(ThingStatus.OFFLINE);
74 * Get the Bridge Handler for the Caddx system.
76 * @return CaddxBridgeHandler
78 public @Nullable CaddxBridgeHandler getCaddxBridgeHandler() {
79 if (this.caddxBridgeHandler == null) {
80 Bridge bridge = getBridge();
83 logger.debug("getCaddxBridgeHandler(): Unable to get bridge!");
87 logger.trace("getCaddxBridgeHandler(): Bridge for '{}' - '{}'", getThing().getUID(), bridge.getUID());
89 ThingHandler handler = bridge.getHandler();
91 if (handler instanceof CaddxBridgeHandler) {
92 this.caddxBridgeHandler = (CaddxBridgeHandler) handler;
94 logger.debug("getCaddxBridgeHandler(): Unable to get bridge handler!");
98 return this.caddxBridgeHandler;
102 * Method to Update a Channel
108 public abstract void updateChannel(ChannelUID channel, String data);
111 * Receives Events from the bridge.
116 public abstract void caddxEventReceived(CaddxEvent event, Thing thing);
119 public void handleCommand(ChannelUID channelUID, Command command) {
123 * Get the thing configuration.
125 * @param caddxThingType The Thing type
127 private void getConfiguration(CaddxThingType caddxThingType) {
128 switch (caddxThingType) {
130 CaddxPartitionConfiguration partitionConfiguration = getConfigAs(CaddxPartitionConfiguration.class);
131 setPartitionNumber(partitionConfiguration.getPartitionNumber());
134 CaddxZoneConfiguration zoneConfiguration = getConfigAs(CaddxZoneConfiguration.class);
135 setZoneNumber(zoneConfiguration.getZoneNumber());
138 CaddxKeypadConfiguration keypadConfiguration = getConfigAs(CaddxKeypadConfiguration.class);
139 setKeypadAddress(keypadConfiguration.getKeypadAddress());
140 setTerminalModeSeconds(keypadConfiguration.getTerminalModeSeconds());
147 * Get the Thing type.
149 * @return caddxThingType
151 public CaddxThingType getCaddxThingType() {
152 return caddxThingType;
156 * Get Partition Number.
158 * @return partitionNumber
160 public int getPartitionNumber() {
161 return partitionNumber;
165 * Set Partition Number.
167 * @param partitionNumber
169 public void setPartitionNumber(int partitionNumber) {
170 this.partitionNumber = partitionNumber;
178 public int getZoneNumber() {
187 public void setZoneNumber(int zoneNumber) {
188 this.zoneNumber = zoneNumber;
192 * Get Keypad Address.
194 * @return keypadAddress
196 public int getKeypadAddress() {
197 return keypadAddress;
201 * Set Keypad Address.
203 * @param keypadAddress
205 public void setKeypadAddress(int keypadAddress) {
206 this.keypadAddress = keypadAddress;
210 * Get Keypad Terminal Mode Seconds.
212 * @return terminalModeSeconds
214 public int getTerminalModeSeconds() {
215 return terminalModeSeconds;
219 * Set Keypad Terminal Mode Seconds.
221 * @param terminalModeSeconds
223 public void setTerminalModeSeconds(int terminalModeSeconds) {
224 this.terminalModeSeconds = terminalModeSeconds;
228 * Get Channel by ChannelUID.
232 public @Nullable Channel getChannel(ChannelUID channelUID) {
233 Channel channel = null;
235 List<Channel> channels = getThing().getChannels();
237 for (Channel ch : channels) {
238 if (channelUID.equals(ch.getUID())) {