]> git.basschouten.com Git - openhab-addons.git/blob
ba014c821e6ccd74e05662b7a478eadcbd73c864
[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.paradoxalarm.internal.discovery;
14
15 import static org.openhab.binding.paradoxalarm.internal.handlers.ParadoxAlarmBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20
21 import org.openhab.binding.paradoxalarm.internal.communication.IParadoxCommunicator;
22 import org.openhab.binding.paradoxalarm.internal.exceptions.ParadoxRuntimeException;
23 import org.openhab.binding.paradoxalarm.internal.handlers.ParadoxIP150BridgeHandler;
24 import org.openhab.binding.paradoxalarm.internal.model.ParadoxInformation;
25 import org.openhab.binding.paradoxalarm.internal.model.ParadoxPanel;
26 import org.openhab.binding.paradoxalarm.internal.model.Partition;
27 import org.openhab.binding.paradoxalarm.internal.model.Zone;
28 import org.openhab.core.config.discovery.AbstractDiscoveryService;
29 import org.openhab.core.config.discovery.DiscoveryResult;
30 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
31 import org.openhab.core.thing.ThingUID;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * The {@link ParadoxDiscoveryService} is responsible for discovery of partitions, zones and the panel once bridge is
37  * created.
38  *
39  * @author Konstnatin Polihronov - Initial Contribution
40  */
41 public class ParadoxDiscoveryService extends AbstractDiscoveryService {
42
43     private final Logger logger = LoggerFactory.getLogger(ParadoxDiscoveryService.class);
44
45     private ParadoxIP150BridgeHandler ip150BridgeHandler;
46
47     public ParadoxDiscoveryService(ParadoxIP150BridgeHandler ip150BridgeHandler) {
48         super(SUPPORTED_THING_TYPES_UIDS, 15, false);
49         this.ip150BridgeHandler = ip150BridgeHandler;
50     }
51
52     @Override
53     protected void startScan() {
54         IParadoxCommunicator communicator = ip150BridgeHandler.getCommunicator();
55         if (communicator != null && communicator.isOnline()) {
56             ParadoxPanel panel = ip150BridgeHandler.getPanel();
57             discoverPanel(panel.getPanelInformation());
58             discoverPartitions(panel.getPartitions());
59             discoverZones(panel.getZones());
60         } else {
61             logger.debug("Communicator null or not online. Trace:", new ParadoxRuntimeException());
62         }
63     }
64
65     private void discoverPanel(ParadoxInformation panelInformation) {
66         if (panelInformation != null) {
67             Map<String, Object> properties = new HashMap<>();
68             properties.put(PANEL_TYPE_PROPERTY_NAME, panelInformation.getPanelType().name());
69             properties.put(PANEL_SERIAL_NUMBER_PROPERTY_NAME, panelInformation.getSerialNumber());
70             properties.put(PANEL_APPLICATION_VERSION_PROPERTY_NAME, panelInformation.getApplicationVersion());
71             properties.put(PANEL_BOOTLOADER_VERSION_PROPERTY_NAME, panelInformation.getBootLoaderVersion());
72             properties.put(PANEL_HARDWARE_VERSION_PROPERTY_NAME, panelInformation.getHardwareVersion());
73
74             ThingUID bridgeUid = ip150BridgeHandler.getThing().getUID();
75             ThingUID thingUID = new ThingUID(PANEL_THING_TYPE_UID, bridgeUid, PARADOX_PANEL_THING_TYPE_ID);
76             DiscoveryResult result = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
77                     .withBridge(bridgeUid).withLabel("Paradox panel - " + panelInformation.getPanelType()).build();
78             logger.debug("Panel DiscoveryResult={}", result);
79             thingDiscovered(result);
80         }
81     }
82
83     private void discoverPartitions(List<Partition> partitions) {
84         partitions.stream().forEach(partition -> {
85             String thingId = PARTITION_THING_TYPE_ID + partition.getId();
86             String label = partition.getLabel();
87             ThingUID bridgeUid = ip150BridgeHandler.getThing().getUID();
88
89             ThingUID thingUID = new ThingUID(PARTITION_THING_TYPE_UID, bridgeUid, thingId);
90             DiscoveryResult result = DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUid)
91                     .withLabel("Partition " + label).withProperty(PARTITION_THING_TYPE_ID, thingId)
92                     .withProperty("label", label).withProperty("id", partition.getId()).build();
93             logger.debug("Partition DiscoveryResult={}", result);
94
95             thingDiscovered(result);
96         });
97     }
98
99     private void discoverZones(List<Zone> zones) {
100         zones.stream().forEach(zone -> {
101             String thingId = zone.getLabel().replaceAll(" ", "_");
102             String label = zone.getLabel();
103             ThingUID bridgeUid = ip150BridgeHandler.getThing().getUID();
104
105             ThingUID thingUID = new ThingUID(ZONE_THING_TYPE_UID, bridgeUid, thingId);
106             DiscoveryResult result = DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUid)
107                     .withLabel("Zone " + label).withProperty(ZONE_THING_TYPE_ID, thingId)
108                     .withProperty("id", zone.getId()).withProperty("label", label).build();
109             logger.debug("Zone DiscoveryResult={}", result);
110
111             thingDiscovered(result);
112         });
113     }
114 }