]> git.basschouten.com Git - openhab-addons.git/blob
f28e6d3e51b61f654740fc9a78c5d74d87e7f2ff
[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.digiplex.internal.discovery;
14
15 import static org.openhab.binding.digiplex.internal.DigiplexBindingConstants.*;
16
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.Map;
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(Collections.singleton(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().anyMatch(format -> {
104             return String.format(format, response.zoneNo).equals(response.zoneName);
105         });
106     }
107
108     @Override
109     @SuppressWarnings("null")
110     public void handleAreaLabelResponse(AreaLabelResponse response) {
111         // we have no other option to check whether area is actually enabled than to compare its name with the default
112         if (response.success && response.areaName.equals(String.format(AREA_DEFAULT_NAME, response.areaNo))) {
113             return;
114         }
115
116         ThingUID bridgeUID = bridgeHandler.getThing().getUID();
117         ThingUID thingUID = new ThingUID(THING_TYPE_AREA, bridgeUID, String.format("area%d", response.areaNo));
118
119         Map<String, Object> properties = new HashMap<>(1);
120         properties.put(PROPERTY_AREA_NO, Integer.toString(response.areaNo));
121
122         DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUID)
123                 .withProperties(properties).withLabel(response.areaName).build();
124
125         thingDiscovered(discoveryResult);
126     }
127
128     @Override
129     public void setThingHandler(@Nullable ThingHandler handler) {
130         if (handler instanceof DigiplexBridgeHandler) {
131             bridgeHandler = (DigiplexBridgeHandler) handler;
132             bridgeHandler.registerMessageHandler(this);
133         }
134     }
135
136     @Override
137     public @Nullable ThingHandler getThingHandler() {
138         return bridgeHandler;
139     }
140
141     @Override
142     public void activate() {
143         super.activate(null);
144     }
145
146     @Override
147     public void deactivate() {
148         super.deactivate();
149     }
150 }