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.remoteopenhab.internal.discovery;
15 import static org.openhab.binding.remoteopenhab.internal.config.RemoteopenhabThingConfiguration.THING_UID;
17 import java.util.List;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.remoteopenhab.internal.RemoteopenhabBindingConstants;
23 import org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabStatusInfo;
24 import org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabThing;
25 import org.openhab.binding.remoteopenhab.internal.exceptions.RemoteopenhabException;
26 import org.openhab.binding.remoteopenhab.internal.handler.RemoteopenhabBridgeHandler;
27 import org.openhab.binding.remoteopenhab.internal.listener.RemoteopenhabThingsDataListener;
28 import org.openhab.binding.remoteopenhab.internal.rest.RemoteopenhabRestClient;
29 import org.openhab.core.config.discovery.AbstractDiscoveryService;
30 import org.openhab.core.config.discovery.DiscoveryResult;
31 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
32 import org.openhab.core.thing.ThingStatus;
33 import org.openhab.core.thing.ThingUID;
34 import org.openhab.core.thing.binding.ThingHandler;
35 import org.openhab.core.thing.binding.ThingHandlerService;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * The {@link RemoteopenhabDiscoveryService} is responsible for discovering all the remote things
41 * available in the remote openHAB server.
43 * @author Laurent Garnier - Initial contribution
46 public class RemoteopenhabDiscoveryService extends AbstractDiscoveryService
47 implements ThingHandlerService, RemoteopenhabThingsDataListener {
49 private final Logger logger = LoggerFactory.getLogger(RemoteopenhabDiscoveryService.class);
51 private static final int SEARCH_TIME = 10;
53 private @NonNullByDefault({}) RemoteopenhabBridgeHandler bridgeHandler;
54 private @NonNullByDefault({}) RemoteopenhabRestClient restClient;
56 public RemoteopenhabDiscoveryService() {
57 super(RemoteopenhabBindingConstants.SUPPORTED_THING_TYPES_UIDS, SEARCH_TIME, false);
61 public void setThingHandler(ThingHandler handler) {
62 if (handler instanceof RemoteopenhabBridgeHandler remoteopenhabBridgeHandler) {
63 this.bridgeHandler = remoteopenhabBridgeHandler;
64 this.restClient = bridgeHandler.gestRestClient();
69 public @Nullable ThingHandler getThingHandler() {
74 public void activate() {
75 ThingHandlerService.super.activate();
76 restClient.addThingsDataListener(this);
80 public void deactivate() {
81 restClient.removeThingsDataListener(this);
86 protected void startScan() {
87 logger.debug("Starting discovery scan for remote things");
88 if (bridgeHandler.getThing().getStatus() == ThingStatus.ONLINE) {
90 List<RemoteopenhabThing> things = restClient.getRemoteThings();
91 ThingUID bridgeUID = bridgeHandler.getThing().getUID();
92 for (RemoteopenhabThing thing : things) {
93 createDiscoveryResult(thing, bridgeUID);
95 } catch (RemoteopenhabException e) {
96 logger.debug("Scan for remote things failed", e);
102 protected synchronized void stopScan() {
104 removeOlderResults(getTimestampOfLastScan(), bridgeHandler.getThing().getUID());
108 public void onThingStatusUpdated(String thingUID, RemoteopenhabStatusInfo statusInfo) {
113 public void onThingAdded(RemoteopenhabThing thing) {
114 createDiscoveryResult(thing, bridgeHandler.getThing().getUID());
118 public void onThingRemoved(RemoteopenhabThing thing) {
119 removeDiscoveryResult(thing, bridgeHandler.getThing().getUID());
123 public void onChannelTriggered(String channelUID, @Nullable String event) {
127 private void createDiscoveryResult(RemoteopenhabThing thing, ThingUID bridgeUID) {
128 ThingUID thingUID = buildThingUID(thing, bridgeUID);
129 logger.debug("Create a DiscoveryResult for remote openHAB thing {} with thingUID setting {}", thingUID,
131 Map<String, Object> properties = Map.of(THING_UID, thing.uid);
132 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
133 .withRepresentationProperty(THING_UID).withBridge(bridgeUID).withLabel(thing.label).build();
134 thingDiscovered(discoveryResult);
137 private void removeDiscoveryResult(RemoteopenhabThing thing, ThingUID bridgeUID) {
138 ThingUID thingUID = buildThingUID(thing, bridgeUID);
139 logger.debug("Remove a DiscoveryResult for remote openHAB thing {} with thingUID setting {}", thingUID,
141 thingRemoved(thingUID);
144 private ThingUID buildThingUID(RemoteopenhabThing thing, ThingUID bridgeUID) {
145 return new ThingUID(RemoteopenhabBindingConstants.THING_TYPE_THING, bridgeUID,
146 thing.uid.replaceAll("[^A-Za-z0-9_]", "_"));