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.discovery;
15 import java.util.Collections;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.caddx.internal.CaddxBindingConstants;
21 import org.openhab.binding.caddx.internal.CaddxEvent;
22 import org.openhab.binding.caddx.internal.config.CaddxKeypadConfiguration;
23 import org.openhab.binding.caddx.internal.config.CaddxPartitionConfiguration;
24 import org.openhab.binding.caddx.internal.config.CaddxZoneConfiguration;
25 import org.openhab.binding.caddx.internal.handler.CaddxBridgeHandler;
26 import org.openhab.binding.caddx.internal.handler.CaddxThingType;
27 import org.openhab.core.config.discovery.AbstractDiscoveryService;
28 import org.openhab.core.config.discovery.DiscoveryResult;
29 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
30 import org.openhab.core.config.discovery.DiscoveryService;
31 import org.openhab.core.thing.Bridge;
32 import org.openhab.core.thing.ThingUID;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.openhab.core.thing.binding.ThingHandlerService;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * This class is responsible for discovering the supported Things.
41 * @author Georgios Moutsos - Initial contribution
44 public class CaddxDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService, DiscoveryService {
45 private final Logger logger = LoggerFactory.getLogger(CaddxDiscoveryService.class);
47 private @Nullable CaddxBridgeHandler caddxBridgeHandler = null;
49 public CaddxDiscoveryService() {
50 super(CaddxBindingConstants.SUPPORTED_THING_TYPES_UIDS, 15, false);
54 protected void startScan() {
55 // Discovery is performed implicitly via the CadxBridgeHandler
59 * Method to add a Thing to the Inbox.
62 * @param caddxThingType
65 public void addThing(Bridge bridge, CaddxThingType caddxThingType, CaddxEvent event) {
66 ThingUID thingUID = null;
68 String thingLabel = "";
69 Map<String, Object> properties = null;
71 Integer partition = event.getPartition();
72 Integer zone = event.getZone();
73 Integer keypad = event.getKeypad();
74 String representationProperty = null;
76 switch (caddxThingType) {
80 thingUID = new ThingUID(CaddxBindingConstants.PANEL_THING_TYPE, bridge.getUID(), thingID);
83 thingID = "partition" + partition;
84 thingLabel = "Partition " + partition;
85 thingUID = new ThingUID(CaddxBindingConstants.PARTITION_THING_TYPE, bridge.getUID(), thingID);
87 if (partition != null) {
88 properties = Collections.singletonMap(CaddxPartitionConfiguration.PARTITION_NUMBER, partition);
89 representationProperty = CaddxPartitionConfiguration.PARTITION_NUMBER;
94 thingID = "zone" + zone;
95 thingLabel = "Zone " + zone;
96 thingUID = new ThingUID(CaddxBindingConstants.ZONE_THING_TYPE, bridge.getUID(), thingID);
99 properties = Collections.singletonMap(CaddxZoneConfiguration.ZONE_NUMBER, zone);
100 representationProperty = CaddxZoneConfiguration.ZONE_NUMBER;
105 thingLabel = "Keypad";
106 thingUID = new ThingUID(CaddxBindingConstants.KEYPAD_THING_TYPE, bridge.getUID(), thingID);
108 if (keypad != null) {
109 properties = Collections.singletonMap(CaddxKeypadConfiguration.KEYPAD_ADDRESS, keypad);
110 representationProperty = CaddxKeypadConfiguration.KEYPAD_ADDRESS;
115 if (thingUID != null) {
116 DiscoveryResult discoveryResult;
118 if (properties != null && representationProperty != null) {
119 discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
120 .withRepresentationProperty(representationProperty).withBridge(bridge.getUID())
121 .withLabel(thingLabel).build();
123 discoveryResult = DiscoveryResultBuilder.create(thingUID).withBridge(bridge.getUID())
124 .withLabel(thingLabel).build();
127 thingDiscovered(discoveryResult);
129 logger.warn("addThing(): Unable to Add Caddx Alarm Thing to Inbox!");
134 * Activates the Discovery Service.
137 public void activate() {
138 CaddxBridgeHandler handler = caddxBridgeHandler;
139 if (handler != null) {
140 handler.registerDiscoveryService(this);
145 * Deactivates the Discovery Service.
148 public void deactivate() {
149 CaddxBridgeHandler handler = caddxBridgeHandler;
150 if (handler != null) {
151 handler.unregisterDiscoveryService();
156 public void setThingHandler(@Nullable ThingHandler handler) {
157 if (handler instanceof CaddxBridgeHandler) {
158 caddxBridgeHandler = (CaddxBridgeHandler) handler;
163 public @Nullable ThingHandler getThingHandler() {
164 return caddxBridgeHandler;