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.resol.internal.discovery;
15 import java.util.HashMap;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.resol.handler.ResolBridgeHandler;
22 import org.openhab.binding.resol.internal.ResolBindingConstants;
23 import org.openhab.core.config.discovery.AbstractDiscoveryService;
24 import org.openhab.core.config.discovery.DiscoveryResult;
25 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
26 import org.openhab.core.config.discovery.DiscoveryService;
27 import org.openhab.core.thing.ThingUID;
28 import org.openhab.core.thing.binding.ThingHandler;
29 import org.openhab.core.thing.binding.ThingHandlerService;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * The {@link ResolDeviceDiscoveryService} class handles the discovery of things.
37 * @author Raphael Mack - Initial contribution
40 public class ResolDeviceDiscoveryService extends AbstractDiscoveryService
41 implements DiscoveryService, ThingHandlerService {
43 private static final String THING_PROPERTY_TYPE = "type";
45 private final Logger logger = LoggerFactory.getLogger(ResolDeviceDiscoveryService.class);
47 private @Nullable ResolBridgeHandler resolBridgeHandler;
49 public ResolDeviceDiscoveryService() throws IllegalArgumentException {
50 super(Set.of(ResolBindingConstants.THING_TYPE_UID_DEVICE), 15, false);
53 public void addThing(ThingUID bridgeUID, String thingType, String type, String name) {
54 logger.trace("Adding new Resol thing: {}", type);
55 ThingUID thingUID = null;
57 case ResolBindingConstants.THING_ID_DEVICE:
58 thingUID = new ThingUID(ResolBindingConstants.THING_TYPE_UID_DEVICE, bridgeUID, type);
62 if (thingUID != null) {
63 logger.trace("Adding new Discovery thingType: {} bridgeType: {}", thingUID.getAsString(),
64 bridgeUID.getAsString());
66 Map<String, Object> properties = new HashMap<>(1);
67 properties.put(THING_PROPERTY_TYPE, type);
69 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUID)
70 .withRepresentationProperty(THING_PROPERTY_TYPE).withProperties(properties).withLabel(name).build();
71 logger.trace("call register: {} label: {}", discoveryResult.getBindingId(), discoveryResult.getLabel());
72 thingDiscovered(discoveryResult);
74 logger.debug("Discovered Thing is unsupported: type '{}'", type);
79 public void activate() {
80 ResolBridgeHandler resolBridgeHandler = this.resolBridgeHandler;
81 if (resolBridgeHandler != null) {
82 resolBridgeHandler.registerDiscoveryService(this);
87 public void deactivate() {
88 ResolBridgeHandler resolBridgeHandler = this.resolBridgeHandler;
89 if (resolBridgeHandler != null) {
90 resolBridgeHandler.unregisterDiscoveryService();
95 protected void startScan() {
96 ResolBridgeHandler resolBridgeHandler = this.resolBridgeHandler;
97 if (resolBridgeHandler != null) {
98 resolBridgeHandler.startScan();
103 protected void stopScan() {
104 ResolBridgeHandler resolBridgeHandler = this.resolBridgeHandler;
105 if (resolBridgeHandler != null) {
106 resolBridgeHandler.stopScan();
112 public void setThingHandler(ThingHandler handler) {
113 if (handler instanceof ResolBridgeHandler resolBridgeHandler) {
114 this.resolBridgeHandler = resolBridgeHandler;
119 public @Nullable ThingHandler getThingHandler() {
120 return resolBridgeHandler;