]> git.basschouten.com Git - openhab-addons.git/blob
cbc83a009f279513fcb1e1fb161dbd4e5d28fc3e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.caddx.internal.discovery;
14
15 import java.util.Map;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.caddx.internal.CaddxBindingConstants;
20 import org.openhab.binding.caddx.internal.CaddxEvent;
21 import org.openhab.binding.caddx.internal.config.CaddxKeypadConfiguration;
22 import org.openhab.binding.caddx.internal.config.CaddxPartitionConfiguration;
23 import org.openhab.binding.caddx.internal.config.CaddxZoneConfiguration;
24 import org.openhab.binding.caddx.internal.handler.CaddxBridgeHandler;
25 import org.openhab.binding.caddx.internal.handler.CaddxThingType;
26 import org.openhab.core.config.discovery.AbstractDiscoveryService;
27 import org.openhab.core.config.discovery.DiscoveryResult;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.config.discovery.DiscoveryService;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.ThingUID;
32 import org.openhab.core.thing.binding.ThingHandler;
33 import org.openhab.core.thing.binding.ThingHandlerService;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * This class is responsible for discovering the supported Things.
39  *
40  * @author Georgios Moutsos - Initial contribution
41  */
42 @NonNullByDefault
43 public class CaddxDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService, DiscoveryService {
44     private final Logger logger = LoggerFactory.getLogger(CaddxDiscoveryService.class);
45
46     private @Nullable CaddxBridgeHandler caddxBridgeHandler = null;
47
48     public CaddxDiscoveryService() {
49         super(CaddxBindingConstants.SUPPORTED_THING_TYPES_UIDS, 15, false);
50     }
51
52     @Override
53     protected void startScan() {
54         // Discovery is performed implicitly via the CadxBridgeHandler
55     }
56
57     /**
58      * Method to add a Thing to the Inbox.
59      *
60      * @param bridge
61      * @param caddxThingType
62      * @param event
63      */
64     public void addThing(Bridge bridge, CaddxThingType caddxThingType, CaddxEvent event) {
65         ThingUID thingUID = null;
66         String thingID = "";
67         String thingLabel = "";
68         Map<String, Object> properties = null;
69
70         Integer partition = event.getPartition();
71         Integer zone = event.getZone();
72         Integer keypad = event.getKeypad();
73         String representationProperty = null;
74
75         switch (caddxThingType) {
76             case PANEL:
77                 thingID = "panel";
78                 thingLabel = "Panel";
79                 thingUID = new ThingUID(CaddxBindingConstants.PANEL_THING_TYPE, bridge.getUID(), thingID);
80                 break;
81             case PARTITION:
82                 thingID = "partition" + partition;
83                 thingLabel = "Partition " + partition;
84                 thingUID = new ThingUID(CaddxBindingConstants.PARTITION_THING_TYPE, bridge.getUID(), thingID);
85
86                 if (partition != null) {
87                     properties = Map.of(CaddxPartitionConfiguration.PARTITION_NUMBER, partition);
88                     representationProperty = CaddxPartitionConfiguration.PARTITION_NUMBER;
89                 }
90
91                 break;
92             case ZONE:
93                 thingID = "zone" + zone;
94                 thingLabel = "Zone " + zone;
95                 thingUID = new ThingUID(CaddxBindingConstants.ZONE_THING_TYPE, bridge.getUID(), thingID);
96
97                 if (zone != null) {
98                     properties = Map.of(CaddxZoneConfiguration.ZONE_NUMBER, zone);
99                     representationProperty = CaddxZoneConfiguration.ZONE_NUMBER;
100                 }
101                 break;
102             case KEYPAD:
103                 thingID = "keypad";
104                 thingLabel = "Keypad";
105                 thingUID = new ThingUID(CaddxBindingConstants.KEYPAD_THING_TYPE, bridge.getUID(), thingID);
106
107                 if (keypad != null) {
108                     properties = Map.of(CaddxKeypadConfiguration.KEYPAD_ADDRESS, keypad);
109                     representationProperty = CaddxKeypadConfiguration.KEYPAD_ADDRESS;
110                 }
111                 break;
112         }
113
114         if (thingUID != null) {
115             DiscoveryResult discoveryResult;
116
117             if (properties != null && representationProperty != null) {
118                 discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
119                         .withRepresentationProperty(representationProperty).withBridge(bridge.getUID())
120                         .withLabel(thingLabel).build();
121             } else {
122                 discoveryResult = DiscoveryResultBuilder.create(thingUID).withBridge(bridge.getUID())
123                         .withLabel(thingLabel).build();
124             }
125
126             thingDiscovered(discoveryResult);
127         } else {
128             logger.warn("addThing(): Unable to Add Caddx Alarm Thing to Inbox!");
129         }
130     }
131
132     /**
133      * Activates the Discovery Service.
134      */
135     @Override
136     public void activate() {
137         CaddxBridgeHandler handler = caddxBridgeHandler;
138         if (handler != null) {
139             handler.registerDiscoveryService(this);
140         }
141     }
142
143     /**
144      * Deactivates the Discovery Service.
145      */
146     @Override
147     public void deactivate() {
148         CaddxBridgeHandler handler = caddxBridgeHandler;
149         if (handler != null) {
150             handler.unregisterDiscoveryService();
151         }
152     }
153
154     @Override
155     public void setThingHandler(@Nullable ThingHandler handler) {
156         if (handler instanceof CaddxBridgeHandler bridgeHandler) {
157             caddxBridgeHandler = bridgeHandler;
158         }
159     }
160
161     @Override
162     public @Nullable ThingHandler getThingHandler() {
163         return caddxBridgeHandler;
164     }
165 }