2 * Copyright (c) 2010-2021 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.pilight.internal.discovery;
15 import static org.openhab.binding.pilight.internal.PilightBindingConstants.*;
18 import java.util.concurrent.CompletableFuture;
19 import java.util.concurrent.ScheduledFuture;
20 import java.util.concurrent.TimeUnit;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.pilight.internal.PilightHandlerFactory;
25 import org.openhab.binding.pilight.internal.dto.Config;
26 import org.openhab.binding.pilight.internal.dto.DeviceType;
27 import org.openhab.binding.pilight.internal.dto.Status;
28 import org.openhab.binding.pilight.internal.handler.PilightBridgeHandler;
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.ThingTypeUID;
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 PilightDeviceDiscoveryService} discovers pilight devices after a bridge thing has been created and
41 * connected to the pilight daemon. Things are discovered periodically in the background or after a manual trigger.
43 * @author Niklas Dörfler - Initial contribution
46 public class PilightDeviceDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
48 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = PilightHandlerFactory.SUPPORTED_THING_TYPES_UIDS;
50 private static final int AUTODISCOVERY_SEARCH_TIME_SEC = 10;
51 private static final int AUTODISCOVERY_BACKGROUND_SEARCH_INTERVAL_SEC = 60 * 10;
53 private final Logger logger = LoggerFactory.getLogger(PilightDeviceDiscoveryService.class);
55 private @Nullable PilightBridgeHandler pilightBridgeHandler;
56 private @Nullable ThingUID bridgeUID;
58 private @Nullable ScheduledFuture<?> backgroundDiscoveryJob;
59 private CompletableFuture<Config> configFuture;
60 private CompletableFuture<List<Status>> statusFuture;
62 public PilightDeviceDiscoveryService() {
63 super(SUPPORTED_THING_TYPES_UIDS, AUTODISCOVERY_SEARCH_TIME_SEC);
64 configFuture = new CompletableFuture<>();
65 statusFuture = new CompletableFuture<>();
69 protected void startScan() {
70 if (pilightBridgeHandler != null) {
71 configFuture = new CompletableFuture<>();
72 statusFuture = new CompletableFuture<>();
74 configFuture.thenAcceptBoth(statusFuture, (config, allStatus) -> {
75 removeOlderResults(getTimestampOfLastScan(), bridgeUID);
76 config.getDevices().forEach((deviceId, device) -> {
77 if (this.pilightBridgeHandler != null) {
78 final Optional<Status> status = allStatus.stream()
79 .filter(s -> s.getDevices().contains(deviceId)).findFirst();
81 final ThingTypeUID thingTypeUID;
82 final String typeString;
84 if (status.isPresent()) {
85 if (status.get().getType().equals(DeviceType.SWITCH)) {
86 thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_SWITCH.getId());
87 typeString = "Switch";
88 } else if (status.get().getType().equals(DeviceType.DIMMER)) {
89 thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_DIMMER.getId());
90 typeString = "Dimmer";
91 } else if (status.get().getType().equals(DeviceType.VALUE)) {
92 thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_GENERIC.getId());
93 typeString = "Generic";
94 } else if (status.get().getType().equals(DeviceType.CONTACT)) {
95 thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_CONTACT.getId());
96 typeString = "Contact";
98 thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_GENERIC.getId());
99 typeString = "Generic";
102 thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_GENERIC.getId());
103 typeString = "Generic";
106 final @Nullable PilightBridgeHandler pilightBridgeHandler = this.pilightBridgeHandler;
107 if (pilightBridgeHandler != null) {
108 final ThingUID thingUID = new ThingUID(thingTypeUID,
109 pilightBridgeHandler.getThing().getUID(), deviceId);
111 final Map<String, Object> properties = new HashMap<>();
112 properties.put(PROPERTY_NAME, deviceId);
114 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID)
115 .withThingType(thingTypeUID).withProperties(properties).withBridge(bridgeUID)
116 .withRepresentationProperty(PROPERTY_NAME)
117 .withLabel("Pilight " + typeString + " Device '" + deviceId + "'").build();
119 thingDiscovered(discoveryResult);
125 final @Nullable PilightBridgeHandler pilightBridgeHandler = this.pilightBridgeHandler;
126 if (pilightBridgeHandler != null) {
127 pilightBridgeHandler.refreshConfigAndStatus();
133 protected synchronized void stopScan() {
135 configFuture.cancel(true);
136 statusFuture.cancel(true);
137 if (bridgeUID != null) {
138 removeOlderResults(getTimestampOfLastScan(), bridgeUID);
143 protected void startBackgroundDiscovery() {
144 logger.debug("Start Pilight device background discovery");
145 final @Nullable ScheduledFuture<?> backgroundDiscoveryJob = this.backgroundDiscoveryJob;
146 if (backgroundDiscoveryJob == null || backgroundDiscoveryJob.isCancelled()) {
147 this.backgroundDiscoveryJob = scheduler.scheduleWithFixedDelay(this::startScan, 20,
148 AUTODISCOVERY_BACKGROUND_SEARCH_INTERVAL_SEC, TimeUnit.SECONDS);
153 protected void stopBackgroundDiscovery() {
154 logger.debug("Stop Pilight device background discovery");
155 final @Nullable ScheduledFuture<?> backgroundDiscoveryJob = this.backgroundDiscoveryJob;
156 if (backgroundDiscoveryJob != null) {
157 backgroundDiscoveryJob.cancel(true);
158 this.backgroundDiscoveryJob = null;
163 public void setThingHandler(final ThingHandler handler) {
164 if (handler instanceof PilightBridgeHandler) {
165 this.pilightBridgeHandler = (PilightBridgeHandler) handler;
166 final @Nullable PilightBridgeHandler pilightBridgeHandler = this.pilightBridgeHandler;
167 if (pilightBridgeHandler != null) {
168 bridgeUID = pilightBridgeHandler.getThing().getUID();
174 public @Nullable ThingHandler getThingHandler() {
175 return pilightBridgeHandler;
179 public void activate() {
180 super.activate(null);
181 final @Nullable PilightBridgeHandler pilightBridgeHandler = this.pilightBridgeHandler;
182 if (pilightBridgeHandler != null) {
183 pilightBridgeHandler.registerDiscoveryListener(this);
188 public void deactivate() {
189 if (bridgeUID != null) {
190 removeOlderResults(getTimestampOfLastScan(), bridgeUID);
193 final @Nullable PilightBridgeHandler pilightBridgeHandler = this.pilightBridgeHandler;
194 if (pilightBridgeHandler != null) {
195 pilightBridgeHandler.unregisterDiscoveryListener();
202 * Method used to get pilight device config into the discovery class.
204 * @param config config to get
206 public void setConfig(Config config) {
207 configFuture.complete(config);
211 * Method used to get pilight device status list into the discovery class.
213 * @param status list of status objects
215 public void setStatus(List<Status> status) {
216 statusFuture.complete(status);
220 public Set<ThingTypeUID> getSupportedThingTypes() {
221 return SUPPORTED_THING_TYPES_UIDS;