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.digiplex.internal.discovery;
15 import static org.openhab.binding.digiplex.internal.DigiplexBindingConstants.*;
17 import java.util.HashMap;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.digiplex.internal.communication.AreaLabelRequest;
24 import org.openhab.binding.digiplex.internal.communication.AreaLabelResponse;
25 import org.openhab.binding.digiplex.internal.communication.DigiplexMessageHandler;
26 import org.openhab.binding.digiplex.internal.communication.DigiplexRequest;
27 import org.openhab.binding.digiplex.internal.communication.ZoneLabelRequest;
28 import org.openhab.binding.digiplex.internal.communication.ZoneLabelResponse;
29 import org.openhab.binding.digiplex.internal.handler.DigiplexBridgeHandler;
30 import org.openhab.core.config.discovery.AbstractDiscoveryService;
31 import org.openhab.core.config.discovery.DiscoveryResult;
32 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
33 import org.openhab.core.config.discovery.DiscoveryService;
34 import org.openhab.core.thing.ThingUID;
35 import org.openhab.core.thing.binding.ThingHandler;
36 import org.openhab.core.thing.binding.ThingHandlerService;
39 * Service for discovering things on Digiplex alarm systems
41 * @author Robert Michalak - Initial contribution
45 public class DigiplexDiscoveryService extends AbstractDiscoveryService
46 implements DiscoveryService, ThingHandlerService, DigiplexMessageHandler {
48 private static final int MAX_ZONE = 96;
49 private static final int MAX_AREA = 8;
51 private static final int DISCOVERY_TIMEOUT = 30;
53 private @Nullable DigiplexBridgeHandler bridgeHandler;
55 public DigiplexDiscoveryService() {
56 super(Set.of(THING_TYPE_ZONE), DISCOVERY_TIMEOUT, false);
60 @SuppressWarnings("null")
61 protected void startScan() {
62 bridgeHandler.registerMessageHandler(this);
64 for (int i = 1; i <= MAX_ZONE; i++) {
65 DigiplexRequest command = new ZoneLabelRequest(i);
66 bridgeHandler.sendRequest(command);
69 for (int i = 1; i <= MAX_AREA; i++) {
70 DigiplexRequest command = new AreaLabelRequest(i);
71 bridgeHandler.sendRequest(command);
76 @SuppressWarnings("null")
77 protected synchronized void stopScan() {
78 bridgeHandler.unregisterMessageHandler(this);
83 @SuppressWarnings("null")
84 public void handleZoneLabelResponse(ZoneLabelResponse response) {
85 // we have no other option to check whether zone is actually enabled than to compare its name with the default
86 if (isDefaultName(response)) {
90 ThingUID bridgeUID = bridgeHandler.getThing().getUID();
91 ThingUID thingUID = new ThingUID(THING_TYPE_ZONE, bridgeUID, String.format("zone%d", response.zoneNo));
93 Map<String, Object> properties = new HashMap<>(1);
94 properties.put(PROPERTY_ZONE_NO, Integer.toString(response.zoneNo));
96 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUID)
97 .withProperties(properties).withLabel(response.zoneName).build();
99 thingDiscovered(discoveryResult);
102 private boolean isDefaultName(ZoneLabelResponse response) {
103 return ZONE_DEFAULT_NAMES.stream()
104 .anyMatch(format -> String.format(format, response.zoneNo).equals(response.zoneName));
108 @SuppressWarnings("null")
109 public void handleAreaLabelResponse(AreaLabelResponse response) {
110 // we have no other option to check whether area is actually enabled than to compare its name with the default
111 if (response.success && response.areaName.equals(String.format(AREA_DEFAULT_NAME, response.areaNo))) {
115 ThingUID bridgeUID = bridgeHandler.getThing().getUID();
116 ThingUID thingUID = new ThingUID(THING_TYPE_AREA, bridgeUID, String.format("area%d", response.areaNo));
118 Map<String, Object> properties = new HashMap<>(1);
119 properties.put(PROPERTY_AREA_NO, Integer.toString(response.areaNo));
121 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUID)
122 .withProperties(properties).withLabel(response.areaName).build();
124 thingDiscovered(discoveryResult);
128 public void setThingHandler(@Nullable ThingHandler handler) {
129 if (handler instanceof DigiplexBridgeHandler digiplexBridgeHandler) {
130 bridgeHandler = digiplexBridgeHandler;
131 bridgeHandler.registerMessageHandler(this);
136 public @Nullable ThingHandler getThingHandler() {
137 return bridgeHandler;
141 public void activate() {
142 super.activate(null);
146 public void deactivate() {