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 bridgeHandler) {
92 this.caddxBridgeHandler = bridgeHandler;
94 logger.debug("getCaddxBridgeHandler(): Unable to get bridge handler!");
98 return this.caddxBridgeHandler;
102 * Method to Update a Channel
107 public abstract void updateChannel(ChannelUID channel, String data);
110 * Receives Events from the bridge.
115 public abstract void caddxEventReceived(CaddxEvent event, Thing thing);
118 public void handleCommand(ChannelUID channelUID, Command command) {
122 * Get the thing configuration.
124 * @param caddxThingType The Thing type
126 private void getConfiguration(CaddxThingType caddxThingType) {
127 switch (caddxThingType) {
129 CaddxPartitionConfiguration partitionConfiguration = getConfigAs(CaddxPartitionConfiguration.class);
130 setPartitionNumber(partitionConfiguration.getPartitionNumber());
133 CaddxZoneConfiguration zoneConfiguration = getConfigAs(CaddxZoneConfiguration.class);
134 setZoneNumber(zoneConfiguration.getZoneNumber());
137 CaddxKeypadConfiguration keypadConfiguration = getConfigAs(CaddxKeypadConfiguration.class);
138 setKeypadAddress(keypadConfiguration.getKeypadAddress());
139 setTerminalModeSeconds(keypadConfiguration.getTerminalModeSeconds());
146 * Get the Thing type.
148 * @return caddxThingType
150 public CaddxThingType getCaddxThingType() {
151 return caddxThingType;
155 * Get Partition Number.
157 * @return partitionNumber
159 public int getPartitionNumber() {
160 return partitionNumber;
164 * Set Partition Number.
166 * @param partitionNumber
168 public void setPartitionNumber(int partitionNumber) {
169 this.partitionNumber = partitionNumber;
177 public int getZoneNumber() {
186 public void setZoneNumber(int zoneNumber) {
187 this.zoneNumber = zoneNumber;
191 * Get Keypad Address.
193 * @return keypadAddress
195 public int getKeypadAddress() {
196 return keypadAddress;
200 * Set Keypad Address.
202 * @param keypadAddress
204 public void setKeypadAddress(int keypadAddress) {
205 this.keypadAddress = keypadAddress;
209 * Get Keypad Terminal Mode Seconds.
211 * @return terminalModeSeconds
213 public int getTerminalModeSeconds() {
214 return terminalModeSeconds;
218 * Set Keypad Terminal Mode Seconds.
220 * @param terminalModeSeconds
222 public void setTerminalModeSeconds(int terminalModeSeconds) {
223 this.terminalModeSeconds = terminalModeSeconds;
227 * Get Channel by ChannelUID.
231 public @Nullable Channel getChannel(ChannelUID channelUID) {
232 Channel channel = null;
234 List<Channel> channels = getThing().getChannels();
236 for (Channel ch : channels) {
237 if (channelUID.equals(ch.getUID())) {