]> git.basschouten.com Git - openhab-addons.git/blob
f09e4b6e2219702f38cb890be5cdb8717deb7231
[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.alarmdecoder.internal;
14
15 import static org.openhab.binding.alarmdecoder.internal.AlarmDecoderBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.Map;
20 import java.util.Set;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.alarmdecoder.internal.handler.ADBridgeHandler;
24 import org.openhab.binding.alarmdecoder.internal.handler.ZoneHandler;
25 import org.openhab.core.config.discovery.AbstractDiscoveryService;
26 import org.openhab.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.thing.ThingUID;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * The {@link AlarmDecoderDiscoveryService} handles discovery of devices as they are identified by the bridge handler.
34  * Requests from the framework to startScan() are ignored, since no active scanning is possible.
35  *
36  * @author Bob Adair - Initial contribution
37  */
38 @NonNullByDefault
39 public class AlarmDecoderDiscoveryService extends AbstractDiscoveryService {
40
41     private final Logger logger = LoggerFactory.getLogger(AlarmDecoderDiscoveryService.class);
42
43     private ADBridgeHandler bridgeHandler;
44     private final Set<String> discoveredZoneSet = new HashSet<>();
45     private final Set<Integer> discoveredRFZoneSet = new HashSet<>();
46
47     public AlarmDecoderDiscoveryService(ADBridgeHandler bridgeHandler) throws IllegalArgumentException {
48         super(DISCOVERABLE_DEVICE_TYPE_UIDS, 0, false);
49         this.bridgeHandler = bridgeHandler;
50     }
51
52     @Override
53     protected void startScan() {
54         // Ignore start scan requests
55     }
56
57     public void processZone(int address, int channel) {
58         String token = ZoneHandler.zoneID(address, channel);
59         if (!discoveredZoneSet.contains(token)) {
60             notifyDiscoveryOfZone(address, channel, token);
61             discoveredZoneSet.add(token);
62         }
63     }
64
65     public void processRFZone(int serial) {
66         if (!discoveredRFZoneSet.contains(serial)) {
67             notifyDiscoveryOfRFZone(serial);
68             discoveredRFZoneSet.add(serial);
69         }
70     }
71
72     private void notifyDiscoveryOfZone(int address, int channel, String idString) {
73         ThingUID bridgeUID = bridgeHandler.getThing().getUID();
74         ThingUID uid = new ThingUID(THING_TYPE_ZONE, bridgeUID, idString);
75
76         Map<String, Object> properties = new HashMap<>();
77         properties.put(PROPERTY_ADDRESS, address);
78         properties.put(PROPERTY_CHANNEL, channel);
79         properties.put(PROPERTY_ID, idString);
80
81         DiscoveryResult result = DiscoveryResultBuilder.create(uid).withBridge(bridgeUID).withProperties(properties)
82                 .withRepresentationProperty(PROPERTY_ID).build();
83         thingDiscovered(result);
84         logger.debug("Discovered Zone {}", uid);
85     }
86
87     private void notifyDiscoveryOfRFZone(Integer serial) {
88         ThingUID bridgeUID = bridgeHandler.getThing().getUID();
89         ThingUID uid = new ThingUID(THING_TYPE_RFZONE, bridgeUID, serial.toString());
90
91         Map<String, Object> properties = new HashMap<>();
92         properties.put(PROPERTY_SERIAL, serial);
93
94         DiscoveryResult result = DiscoveryResultBuilder.create(uid).withBridge(bridgeUID).withProperties(properties)
95                 .withRepresentationProperty(PROPERTY_SERIAL).build();
96         thingDiscovered(result);
97         logger.debug("Discovered RF Zone{}", uid);
98     }
99 }