]> git.basschouten.com Git - openhab-addons.git/blob
d93000d0204e0fdeb6f84540d641273d710e3a5d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.digiplex.internal.discovery;
14
15 import static org.openhab.binding.digiplex.internal.DigiplexBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Set;
20
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;
37
38 /**
39  * Service for discovering things on Digiplex alarm systems
40  *
41  * @author Robert Michalak - Initial contribution
42  *
43  */
44 @NonNullByDefault
45 public class DigiplexDiscoveryService extends AbstractDiscoveryService
46         implements DiscoveryService, ThingHandlerService, DigiplexMessageHandler {
47
48     private static final int MAX_ZONE = 96;
49     private static final int MAX_AREA = 8;
50
51     private static final int DISCOVERY_TIMEOUT = 30;
52
53     private @Nullable DigiplexBridgeHandler bridgeHandler;
54
55     public DigiplexDiscoveryService() {
56         super(Set.of(THING_TYPE_ZONE), DISCOVERY_TIMEOUT, false);
57     }
58
59     @Override
60     @SuppressWarnings("null")
61     protected void startScan() {
62         bridgeHandler.registerMessageHandler(this);
63         // find zones
64         for (int i = 1; i <= MAX_ZONE; i++) {
65             DigiplexRequest command = new ZoneLabelRequest(i);
66             bridgeHandler.sendRequest(command);
67         }
68         // find areas
69         for (int i = 1; i <= MAX_AREA; i++) {
70             DigiplexRequest command = new AreaLabelRequest(i);
71             bridgeHandler.sendRequest(command);
72         }
73     }
74
75     @Override
76     @SuppressWarnings("null")
77     protected synchronized void stopScan() {
78         bridgeHandler.unregisterMessageHandler(this);
79         super.stopScan();
80     }
81
82     @Override
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)) {
87             return;
88         }
89
90         ThingUID bridgeUID = bridgeHandler.getThing().getUID();
91         ThingUID thingUID = new ThingUID(THING_TYPE_ZONE, bridgeUID, String.format("zone%d", response.zoneNo));
92
93         Map<String, Object> properties = new HashMap<>(1);
94         properties.put(PROPERTY_ZONE_NO, Integer.toString(response.zoneNo));
95
96         DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUID)
97                 .withProperties(properties).withLabel(response.zoneName).build();
98
99         thingDiscovered(discoveryResult);
100     }
101
102     private boolean isDefaultName(ZoneLabelResponse response) {
103         return ZONE_DEFAULT_NAMES.stream()
104                 .anyMatch(format -> String.format(format, response.zoneNo).equals(response.zoneName));
105     }
106
107     @Override
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))) {
112             return;
113         }
114
115         ThingUID bridgeUID = bridgeHandler.getThing().getUID();
116         ThingUID thingUID = new ThingUID(THING_TYPE_AREA, bridgeUID, String.format("area%d", response.areaNo));
117
118         Map<String, Object> properties = new HashMap<>(1);
119         properties.put(PROPERTY_AREA_NO, Integer.toString(response.areaNo));
120
121         DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUID)
122                 .withProperties(properties).withLabel(response.areaName).build();
123
124         thingDiscovered(discoveryResult);
125     }
126
127     @Override
128     public void setThingHandler(@Nullable ThingHandler handler) {
129         if (handler instanceof DigiplexBridgeHandler digiplexBridgeHandler) {
130             bridgeHandler = digiplexBridgeHandler;
131             bridgeHandler.registerMessageHandler(this);
132         }
133     }
134
135     @Override
136     public @Nullable ThingHandler getThingHandler() {
137         return bridgeHandler;
138     }
139
140     @Override
141     public void activate() {
142         super.activate(null);
143     }
144
145     @Override
146     public void deactivate() {
147         super.deactivate();
148     }
149 }