]> git.basschouten.com Git - openhab-addons.git/blob
d767117285c0b8e1f2e5ff32fc227746f9364426
[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.remoteopenhab.internal.discovery;
14
15 import static org.openhab.binding.remoteopenhab.internal.config.RemoteopenhabThingConfiguration.THING_UID;
16
17 import java.util.List;
18 import java.util.Map;
19
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;
38
39 /**
40  * The {@link RemoteopenhabDiscoveryService} is responsible for discovering all the remote things
41  * available in the remote openHAB server.
42  *
43  * @author Laurent Garnier - Initial contribution
44  */
45 @NonNullByDefault
46 public class RemoteopenhabDiscoveryService extends AbstractDiscoveryService
47         implements ThingHandlerService, RemoteopenhabThingsDataListener {
48
49     private final Logger logger = LoggerFactory.getLogger(RemoteopenhabDiscoveryService.class);
50
51     private static final int SEARCH_TIME = 10;
52
53     private @NonNullByDefault({}) RemoteopenhabBridgeHandler bridgeHandler;
54     private @NonNullByDefault({}) RemoteopenhabRestClient restClient;
55
56     public RemoteopenhabDiscoveryService() {
57         super(RemoteopenhabBindingConstants.SUPPORTED_THING_TYPES_UIDS, SEARCH_TIME, false);
58     }
59
60     @Override
61     public void setThingHandler(ThingHandler handler) {
62         if (handler instanceof RemoteopenhabBridgeHandler remoteopenhabBridgeHandler) {
63             this.bridgeHandler = remoteopenhabBridgeHandler;
64             this.restClient = bridgeHandler.gestRestClient();
65         }
66     }
67
68     @Override
69     public @Nullable ThingHandler getThingHandler() {
70         return bridgeHandler;
71     }
72
73     @Override
74     public void activate() {
75         ThingHandlerService.super.activate();
76         restClient.addThingsDataListener(this);
77     }
78
79     @Override
80     public void deactivate() {
81         restClient.removeThingsDataListener(this);
82         super.deactivate();
83     }
84
85     @Override
86     protected void startScan() {
87         logger.debug("Starting discovery scan for remote things");
88         if (bridgeHandler.getThing().getStatus() == ThingStatus.ONLINE) {
89             try {
90                 List<RemoteopenhabThing> things = restClient.getRemoteThings();
91                 ThingUID bridgeUID = bridgeHandler.getThing().getUID();
92                 for (RemoteopenhabThing thing : things) {
93                     createDiscoveryResult(thing, bridgeUID);
94                 }
95             } catch (RemoteopenhabException e) {
96                 logger.debug("Scan for remote things failed", e);
97             }
98         }
99     }
100
101     @Override
102     protected synchronized void stopScan() {
103         super.stopScan();
104         removeOlderResults(getTimestampOfLastScan(), bridgeHandler.getThing().getUID());
105     }
106
107     @Override
108     public void onThingStatusUpdated(String thingUID, RemoteopenhabStatusInfo statusInfo) {
109         // Nothing to do
110     }
111
112     @Override
113     public void onThingAdded(RemoteopenhabThing thing) {
114         createDiscoveryResult(thing, bridgeHandler.getThing().getUID());
115     }
116
117     @Override
118     public void onThingRemoved(RemoteopenhabThing thing) {
119         removeDiscoveryResult(thing, bridgeHandler.getThing().getUID());
120     }
121
122     @Override
123     public void onChannelTriggered(String channelUID, @Nullable String event) {
124         // Nothing to do
125     }
126
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,
130                 thing.uid);
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);
135     }
136
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,
140                 thing.uid);
141         thingRemoved(thingUID);
142     }
143
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_]", "_"));
147     }
148 }