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.paradoxalarm.internal.discovery;
15 import static org.openhab.binding.paradoxalarm.internal.handlers.ParadoxAlarmBindingConstants.*;
17 import java.util.HashMap;
18 import java.util.List;
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;
36 * The {@link ParadoxDiscoveryService} is responsible for discovery of partitions, zones and the panel once bridge is
39 * @author Konstnatin Polihronov - Initial Contribution
41 public class ParadoxDiscoveryService extends AbstractDiscoveryService {
43 private final Logger logger = LoggerFactory.getLogger(ParadoxDiscoveryService.class);
45 private ParadoxIP150BridgeHandler ip150BridgeHandler;
47 public ParadoxDiscoveryService(ParadoxIP150BridgeHandler ip150BridgeHandler) {
48 super(SUPPORTED_THING_TYPES_UIDS, 15, false);
49 this.ip150BridgeHandler = ip150BridgeHandler;
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());
61 logger.debug("Communicator null or not online. Trace:", new ParadoxRuntimeException());
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());
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);
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();
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("id", partition.getId()).build();
93 logger.debug("Partition DiscoveryResult={}", result);
95 thingDiscovered(result);
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();
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()).build();
109 logger.debug("Zone DiscoveryResult={}", result);
111 thingDiscovered(result);