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