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.alarmdecoder.internal;
15 import static org.openhab.binding.alarmdecoder.internal.AlarmDecoderBindingConstants.*;
17 import java.util.HashMap;
18 import java.util.HashSet;
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;
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.
36 * @author Bob Adair - Initial contribution
39 public class AlarmDecoderDiscoveryService extends AbstractDiscoveryService {
41 private final Logger logger = LoggerFactory.getLogger(AlarmDecoderDiscoveryService.class);
43 private ADBridgeHandler bridgeHandler;
44 private final Set<String> discoveredZoneSet = new HashSet<>();
45 private final Set<Integer> discoveredRFZoneSet = new HashSet<>();
47 public AlarmDecoderDiscoveryService(ADBridgeHandler bridgeHandler) throws IllegalArgumentException {
48 super(DISCOVERABLE_DEVICE_TYPE_UIDS, 0, false);
49 this.bridgeHandler = bridgeHandler;
53 protected void startScan() {
54 // Ignore start scan requests
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);
65 public void processRFZone(int serial) {
66 if (!discoveredRFZoneSet.contains(serial)) {
67 notifyDiscoveryOfRFZone(serial);
68 discoveredRFZoneSet.add(serial);
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);
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);
81 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withBridge(bridgeUID).withProperties(properties)
82 .withRepresentationProperty(PROPERTY_ID).build();
83 thingDiscovered(result);
84 logger.debug("Discovered Zone {}", uid);
87 private void notifyDiscoveryOfRFZone(Integer serial) {
88 ThingUID bridgeUID = bridgeHandler.getThing().getUID();
89 ThingUID uid = new ThingUID(THING_TYPE_RFZONE, bridgeUID, serial.toString());
91 Map<String, Object> properties = new HashMap<>();
92 properties.put(PROPERTY_SERIAL, serial);
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);