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