]> git.basschouten.com Git - openhab-addons.git/blob
c735a976e36a51d93718feb731e9899db09ece46
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.*;
18 import java.util.concurrent.CompletableFuture;
19 import java.util.concurrent.ScheduledFuture;
20 import java.util.concurrent.TimeUnit;
21
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;
38
39 /**
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.
42  *
43  * @author Niklas Dörfler - Initial contribution
44  */
45 @NonNullByDefault
46 public class PilightDeviceDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
47
48     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = PilightHandlerFactory.SUPPORTED_THING_TYPES_UIDS;
49
50     private static final int AUTODISCOVERY_SEARCH_TIME_SEC = 10;
51     private static final int AUTODISCOVERY_BACKGROUND_SEARCH_INTERVAL_SEC = 60 * 10;
52
53     private final Logger logger = LoggerFactory.getLogger(PilightDeviceDiscoveryService.class);
54
55     private @Nullable PilightBridgeHandler pilightBridgeHandler;
56     private @Nullable ThingUID bridgeUID;
57
58     private @Nullable ScheduledFuture<?> backgroundDiscoveryJob;
59     private CompletableFuture<Config> configFuture;
60     private CompletableFuture<List<Status>> statusFuture;
61
62     public PilightDeviceDiscoveryService() {
63         super(SUPPORTED_THING_TYPES_UIDS, AUTODISCOVERY_SEARCH_TIME_SEC);
64         configFuture = new CompletableFuture<>();
65         statusFuture = new CompletableFuture<>();
66     }
67
68     @Override
69     protected void startScan() {
70         if (pilightBridgeHandler != null) {
71             configFuture = new CompletableFuture<>();
72             statusFuture = new CompletableFuture<>();
73
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();
80
81                         final ThingTypeUID thingTypeUID;
82                         final String typeString;
83
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";
97                             } else {
98                                 thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_GENERIC.getId());
99                                 typeString = "Generic";
100                             }
101                         } else {
102                             thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_GENERIC.getId());
103                             typeString = "Generic";
104                         }
105
106                         final @Nullable PilightBridgeHandler pilightBridgeHandler = this.pilightBridgeHandler;
107                         if (pilightBridgeHandler != null) {
108                             final ThingUID thingUID = new ThingUID(thingTypeUID,
109                                     pilightBridgeHandler.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)
115                                     .withThingType(thingTypeUID).withProperties(properties).withBridge(bridgeUID)
116                                     .withRepresentationProperty(PROPERTY_NAME)
117                                     .withLabel("Pilight " + typeString + " Device '" + deviceId + "'").build();
118
119                             thingDiscovered(discoveryResult);
120                         }
121                     }
122                 });
123             });
124
125             final @Nullable PilightBridgeHandler pilightBridgeHandler = this.pilightBridgeHandler;
126             if (pilightBridgeHandler != null) {
127                 pilightBridgeHandler.refreshConfigAndStatus();
128             }
129         }
130     }
131
132     @Override
133     protected synchronized void stopScan() {
134         super.stopScan();
135         configFuture.cancel(true);
136         statusFuture.cancel(true);
137         if (bridgeUID != null) {
138             removeOlderResults(getTimestampOfLastScan(), bridgeUID);
139         }
140     }
141
142     @Override
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);
149         }
150     }
151
152     @Override
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;
159         }
160     }
161
162     @Override
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();
169             }
170         }
171     }
172
173     @Override
174     public @Nullable ThingHandler getThingHandler() {
175         return pilightBridgeHandler;
176     }
177
178     @Override
179     public void activate() {
180         super.activate(null);
181         final @Nullable PilightBridgeHandler pilightBridgeHandler = this.pilightBridgeHandler;
182         if (pilightBridgeHandler != null) {
183             pilightBridgeHandler.registerDiscoveryListener(this);
184         }
185     }
186
187     @Override
188     public void deactivate() {
189         if (bridgeUID != null) {
190             removeOlderResults(getTimestampOfLastScan(), bridgeUID);
191         }
192
193         final @Nullable PilightBridgeHandler pilightBridgeHandler = this.pilightBridgeHandler;
194         if (pilightBridgeHandler != null) {
195             pilightBridgeHandler.unregisterDiscoveryListener();
196         }
197
198         super.deactivate();
199     }
200
201     /**
202      * Method used to get pilight device config into the discovery class.
203      *
204      * @param config config to get
205      */
206     public void setConfig(Config config) {
207         configFuture.complete(config);
208     }
209
210     /**
211      * Method used to get pilight device status list into the discovery class.
212      *
213      * @param status list of status objects
214      */
215     public void setStatus(List<Status> status) {
216         statusFuture.complete(status);
217     }
218
219     @Override
220     public Set<ThingTypeUID> getSupportedThingTypes() {
221         return SUPPORTED_THING_TYPES_UIDS;
222     }
223 }