]> git.basschouten.com Git - openhab-addons.git/blob
de77d49d9fa875ed4ac69ae237f322d0d728ba20
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.pilight.internal.discovery;
14
15 import static org.openhab.binding.pilight.internal.PilightBindingConstants.*;
16
17 import java.util.Date;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Optional;
22 import java.util.Set;
23 import java.util.concurrent.CompletableFuture;
24 import java.util.concurrent.ScheduledFuture;
25 import java.util.concurrent.TimeUnit;
26
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;
44
45 /**
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.
48  *
49  * @author Niklas Dörfler - Initial contribution
50  */
51 @Component(scope = ServiceScope.PROTOTYPE, service = PilightDeviceDiscoveryService.class)
52 @NonNullByDefault
53 public class PilightDeviceDiscoveryService extends AbstractThingHandlerDiscoveryService<PilightBridgeHandler> {
54     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = PilightHandlerFactory.SUPPORTED_THING_TYPES_UIDS;
55
56     private static final int AUTODISCOVERY_SEARCH_TIME_SEC = 10;
57     private static final int AUTODISCOVERY_BACKGROUND_SEARCH_INTERVAL_SEC = 60 * 10;
58
59     private final Logger logger = LoggerFactory.getLogger(PilightDeviceDiscoveryService.class);
60
61     private @NonNullByDefault({}) ThingUID bridgeUID;
62
63     private @Nullable ScheduledFuture<?> backgroundDiscoveryJob;
64     private CompletableFuture<Config> configFuture;
65     private CompletableFuture<List<Status>> statusFuture;
66
67     public PilightDeviceDiscoveryService() {
68         super(PilightBridgeHandler.class, SUPPORTED_THING_TYPES_UIDS, AUTODISCOVERY_SEARCH_TIME_SEC);
69         configFuture = new CompletableFuture<>();
70         statusFuture = new CompletableFuture<>();
71     }
72
73     @Override
74     protected void startScan() {
75         configFuture = new CompletableFuture<>();
76         statusFuture = new CompletableFuture<>();
77
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))
82                         .findFirst();
83
84                 final ThingTypeUID thingTypeUID;
85                 final String typeString;
86
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";
100                     } else {
101                         thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_GENERIC.getId());
102                         typeString = "Generic";
103                     }
104                 } else {
105                     thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_GENERIC.getId());
106                     typeString = "Generic";
107                 }
108
109                 final ThingUID thingUID = new ThingUID(thingTypeUID, thingHandler.getThing().getUID(), deviceId);
110
111                 final Map<String, Object> properties = new HashMap<>();
112                 properties.put(PROPERTY_NAME, deviceId);
113
114                 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withThingType(thingTypeUID)
115                         .withProperties(properties).withBridge(bridgeUID).withRepresentationProperty(PROPERTY_NAME)
116                         .withLabel("Pilight " + typeString + " Device '" + deviceId + "'").build();
117
118                 thingDiscovered(discoveryResult);
119             });
120         });
121
122         thingHandler.refreshConfigAndStatus();
123     }
124
125     @Override
126     protected synchronized void stopScan() {
127         super.stopScan();
128         configFuture.cancel(true);
129         statusFuture.cancel(true);
130         removeOlderResults(getTimestampOfLastScan(), bridgeUID);
131     }
132
133     @Override
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);
140         }
141     }
142
143     @Override
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;
150         }
151     }
152
153     @Override
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);
160
161         super.initialize();
162         super.modified(Map.of(DiscoveryService.CONFIG_PROPERTY_BACKGROUND_DISCOVERY, discoveryEnabled));
163     }
164
165     @Override
166     public void dispose() {
167         super.dispose();
168         removeOlderResults(getTimestampOfLastScan(), bridgeUID);
169         thingHandler.unregisterDiscoveryListener();
170     }
171
172     /**
173      * Method used to get pilight device config into the discovery class.
174      *
175      * @param config config to get
176      */
177     public void setConfig(Config config) {
178         configFuture.complete(config);
179     }
180
181     /**
182      * Method used to get pilight device status list into the discovery class.
183      *
184      * @param status list of status objects
185      */
186     public void setStatus(List<Status> status) {
187         statusFuture.complete(status);
188     }
189
190     @Override
191     public Set<ThingTypeUID> getSupportedThingTypes() {
192         return SUPPORTED_THING_TYPES_UIDS;
193     }
194 }