2 * Copyright (c) 2010-2024 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.*;
17 import java.util.Date;
18 import java.util.HashMap;
19 import java.util.List;
21 import java.util.Optional;
23 import java.util.concurrent.CompletableFuture;
24 import java.util.concurrent.ScheduledFuture;
25 import java.util.concurrent.TimeUnit;
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.openhab.binding.pilight.internal.PilightHandlerFactory;
30 import org.openhab.binding.pilight.internal.dto.Config;
31 import org.openhab.binding.pilight.internal.dto.DeviceType;
32 import org.openhab.binding.pilight.internal.dto.Status;
33 import org.openhab.binding.pilight.internal.handler.PilightBridgeHandler;
34 import org.openhab.core.config.discovery.AbstractThingHandlerDiscoveryService;
35 import org.openhab.core.config.discovery.DiscoveryResult;
36 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
37 import org.openhab.core.config.discovery.DiscoveryService;
38 import org.openhab.core.thing.ThingTypeUID;
39 import org.openhab.core.thing.ThingUID;
40 import org.osgi.service.component.annotations.Component;
41 import org.osgi.service.component.annotations.ServiceScope;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * The {@link PilightDeviceDiscoveryService} discovers pilight devices after a bridge thing has been created and
47 * connected to the pilight daemon. Things are discovered periodically in the background or after a manual trigger.
49 * @author Niklas Dörfler - Initial contribution
51 @Component(scope = ServiceScope.PROTOTYPE, service = PilightDeviceDiscoveryService.class)
53 public class PilightDeviceDiscoveryService extends AbstractThingHandlerDiscoveryService<PilightBridgeHandler> {
54 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = PilightHandlerFactory.SUPPORTED_THING_TYPES_UIDS;
56 private static final int AUTODISCOVERY_SEARCH_TIME_SEC = 10;
57 private static final int AUTODISCOVERY_BACKGROUND_SEARCH_INTERVAL_SEC = 60 * 10;
59 private final Logger logger = LoggerFactory.getLogger(PilightDeviceDiscoveryService.class);
61 private @NonNullByDefault({}) ThingUID bridgeUID;
63 private @Nullable ScheduledFuture<?> backgroundDiscoveryJob;
64 private CompletableFuture<Config> configFuture;
65 private CompletableFuture<List<Status>> statusFuture;
67 public PilightDeviceDiscoveryService() {
68 super(PilightBridgeHandler.class, SUPPORTED_THING_TYPES_UIDS, AUTODISCOVERY_SEARCH_TIME_SEC);
69 configFuture = new CompletableFuture<>();
70 statusFuture = new CompletableFuture<>();
74 protected void startScan() {
75 configFuture = new CompletableFuture<>();
76 statusFuture = new CompletableFuture<>();
78 configFuture.thenAcceptBoth(statusFuture, (config, allStatus) -> {
79 removeOlderResults(getTimestampOfLastScan(), bridgeUID);
80 config.getDevices().forEach((deviceId, device) -> {
81 final Optional<Status> status = allStatus.stream().filter(s -> s.getDevices().contains(deviceId))
84 final ThingTypeUID thingTypeUID;
85 final String typeString;
87 if (status.isPresent()) {
88 if (status.get().getType().equals(DeviceType.SWITCH)) {
89 thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_SWITCH.getId());
90 typeString = "Switch";
91 } else if (status.get().getType().equals(DeviceType.DIMMER)) {
92 thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_DIMMER.getId());
93 typeString = "Dimmer";
94 } else if (status.get().getType().equals(DeviceType.VALUE)) {
95 thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_GENERIC.getId());
96 typeString = "Generic";
97 } else if (status.get().getType().equals(DeviceType.CONTACT)) {
98 thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_CONTACT.getId());
99 typeString = "Contact";
101 thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_GENERIC.getId());
102 typeString = "Generic";
105 thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_GENERIC.getId());
106 typeString = "Generic";
109 final ThingUID thingUID = new ThingUID(thingTypeUID, thingHandler.getThing().getUID(), deviceId);
111 final Map<String, Object> properties = new HashMap<>();
112 properties.put(PROPERTY_NAME, deviceId);
114 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withThingType(thingTypeUID)
115 .withProperties(properties).withBridge(bridgeUID).withRepresentationProperty(PROPERTY_NAME)
116 .withLabel("Pilight " + typeString + " Device '" + deviceId + "'").build();
118 thingDiscovered(discoveryResult);
122 thingHandler.refreshConfigAndStatus();
126 protected synchronized void stopScan() {
128 configFuture.cancel(true);
129 statusFuture.cancel(true);
130 removeOlderResults(getTimestampOfLastScan(), bridgeUID);
134 protected void startBackgroundDiscovery() {
135 logger.debug("Start Pilight device background discovery");
136 final @Nullable ScheduledFuture<?> backgroundDiscoveryJob = this.backgroundDiscoveryJob;
137 if (backgroundDiscoveryJob == null || backgroundDiscoveryJob.isCancelled()) {
138 this.backgroundDiscoveryJob = scheduler.scheduleWithFixedDelay(this::startScan, 20,
139 AUTODISCOVERY_BACKGROUND_SEARCH_INTERVAL_SEC, TimeUnit.SECONDS);
144 protected void stopBackgroundDiscovery() {
145 logger.debug("Stop Pilight device background discovery");
146 final @Nullable ScheduledFuture<?> backgroundDiscoveryJob = this.backgroundDiscoveryJob;
147 if (backgroundDiscoveryJob != null) {
148 backgroundDiscoveryJob.cancel(true);
149 this.backgroundDiscoveryJob = null;
154 public void initialize() {
155 bridgeUID = thingHandler.getThing().getUID();
156 boolean discoveryEnabled = false;
157 removeOlderResults(new Date().getTime(), thingHandler.getThing().getUID());
158 discoveryEnabled = thingHandler.isBackgroundDiscoveryEnabled();
159 thingHandler.registerDiscoveryListener(this);
162 super.modified(Map.of(DiscoveryService.CONFIG_PROPERTY_BACKGROUND_DISCOVERY, discoveryEnabled));
166 public void dispose() {
168 removeOlderResults(getTimestampOfLastScan(), bridgeUID);
169 thingHandler.unregisterDiscoveryListener();
173 * Method used to get pilight device config into the discovery class.
175 * @param config config to get
177 public void setConfig(Config config) {
178 configFuture.complete(config);
182 * Method used to get pilight device status list into the discovery class.
184 * @param status list of status objects
186 public void setStatus(List<Status> status) {
187 statusFuture.complete(status);
191 public Set<ThingTypeUID> getSupportedThingTypes() {
192 return SUPPORTED_THING_TYPES_UIDS;