]> git.basschouten.com Git - openhab-addons.git/blob
4b4a3e8d38aed81fdd08b50b2a0d77a6f860241f
[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.dscalarm.internal.discovery;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import org.openhab.binding.dscalarm.internal.DSCAlarmBindingConstants;
19 import org.openhab.binding.dscalarm.internal.DSCAlarmEvent;
20 import org.openhab.binding.dscalarm.internal.DSCAlarmMessage.DSCAlarmMessageInfoType;
21 import org.openhab.binding.dscalarm.internal.config.DSCAlarmPartitionConfiguration;
22 import org.openhab.binding.dscalarm.internal.config.DSCAlarmZoneConfiguration;
23 import org.openhab.binding.dscalarm.internal.handler.DSCAlarmBaseBridgeHandler;
24 import org.openhab.binding.dscalarm.internal.handler.DSCAlarmThingType;
25 import org.openhab.core.config.discovery.AbstractDiscoveryService;
26 import org.openhab.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.ThingUID;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * This class is responsible for discovering DSC Alarm Things via the bridge.
35  *
36  * @author Russell Stephens - Initial Contribution
37  *
38  */
39 public class DSCAlarmDiscoveryService extends AbstractDiscoveryService {
40
41     private final Logger logger = LoggerFactory.getLogger(DSCAlarmDiscoveryService.class);
42
43     /**
44      * DSC Alarm Bridge handler.
45      */
46     DSCAlarmBaseBridgeHandler dscAlarmBridgeHandler;
47
48     /**
49      * Constructor.
50      *
51      * @param dscAlarmBridgeHandler
52      */
53     public DSCAlarmDiscoveryService(DSCAlarmBaseBridgeHandler dscAlarmBridgeHandler) {
54         super(DSCAlarmBindingConstants.SUPPORTED_THING_TYPES_UIDS, 15, true);
55         this.dscAlarmBridgeHandler = dscAlarmBridgeHandler;
56     }
57
58     /**
59      * Activates the Discovery Service.
60      */
61     public void activate() {
62         dscAlarmBridgeHandler.registerDiscoveryService(this);
63     }
64
65     /**
66      * Deactivates the Discovery Service.
67      */
68     @Override
69     public void deactivate() {
70         dscAlarmBridgeHandler.unregisterDiscoveryService();
71     }
72
73     /**
74      * Method to add a Thing to the Inbox.
75      *
76      * @param bridge
77      * @param dscAlarmThingType
78      * @param event
79      */
80     public void addThing(Bridge bridge, DSCAlarmThingType dscAlarmThingType, DSCAlarmEvent event) {
81         logger.trace("addThing(): Adding new DSC Alarm {} to the inbox", dscAlarmThingType.getLabel());
82
83         ThingUID thingUID = null;
84         String thingID = "";
85         String thingLabel = "";
86         Map<String, Object> properties = null;
87
88         int partitionNumber = Integer
89                 .parseInt(event.getDSCAlarmMessage().getMessageInfo(DSCAlarmMessageInfoType.PARTITION));
90         int zoneNumber = Integer.parseInt(event.getDSCAlarmMessage().getMessageInfo(DSCAlarmMessageInfoType.ZONE));
91
92         switch (dscAlarmThingType) {
93             case PANEL:
94                 thingID = "panel";
95                 thingLabel = "Panel";
96                 thingUID = new ThingUID(DSCAlarmBindingConstants.PANEL_THING_TYPE, bridge.getUID(), thingID);
97                 break;
98             case PARTITION:
99                 if (partitionNumber >= 1 && partitionNumber <= 8) {
100                     thingID = "partition" + String.valueOf(partitionNumber);
101                     thingLabel = "Partition " + String.valueOf(partitionNumber);
102                     properties = new HashMap<>(0);
103                     thingUID = new ThingUID(DSCAlarmBindingConstants.PARTITION_THING_TYPE, bridge.getUID(), thingID);
104                     properties.put(DSCAlarmPartitionConfiguration.PARTITION_NUMBER, partitionNumber);
105                 }
106
107                 break;
108             case ZONE:
109                 if (zoneNumber >= 1 && zoneNumber <= 64) {
110                     thingID = "zone" + String.valueOf(zoneNumber);
111                     thingLabel = "Zone " + String.valueOf(zoneNumber);
112                     properties = new HashMap<>(0);
113                     thingUID = new ThingUID(DSCAlarmBindingConstants.ZONE_THING_TYPE, bridge.getUID(), thingID);
114                     properties.put(DSCAlarmZoneConfiguration.ZONE_NUMBER, zoneNumber);
115                 }
116                 break;
117             case KEYPAD:
118                 thingID = "keypad";
119                 thingLabel = "Keypad";
120                 thingUID = new ThingUID(DSCAlarmBindingConstants.KEYPAD_THING_TYPE, bridge.getUID(), thingID);
121                 break;
122         }
123
124         if (thingUID != null) {
125             DiscoveryResult discoveryResult;
126
127             if (properties != null) {
128                 discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
129                         .withBridge(bridge.getUID()).withLabel(thingLabel).build();
130             } else {
131                 discoveryResult = DiscoveryResultBuilder.create(thingUID).withBridge(bridge.getUID())
132                         .withLabel(thingLabel).build();
133             }
134
135             thingDiscovered(discoveryResult);
136         } else {
137             logger.debug("addThing(): Unable to Add DSC Alarm Thing to Inbox!");
138         }
139     }
140
141     @Override
142     protected void startScan() {
143         // Can be ignored here as discovery is via the bridge
144     }
145 }