]> git.basschouten.com Git - openhab-addons.git/blob
e4f60da7f23221c35c7ad0e78c32ab88e7087cae
[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.pilight.internal.discovery;
14
15 import static org.openhab.binding.pilight.internal.PilightBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Optional;
21 import java.util.Set;
22 import java.util.concurrent.CompletableFuture;
23 import java.util.concurrent.ScheduledFuture;
24 import java.util.concurrent.TimeUnit;
25
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.openhab.binding.pilight.internal.PilightHandlerFactory;
29 import org.openhab.binding.pilight.internal.dto.Config;
30 import org.openhab.binding.pilight.internal.dto.DeviceType;
31 import org.openhab.binding.pilight.internal.dto.Status;
32 import org.openhab.binding.pilight.internal.handler.PilightBridgeHandler;
33 import org.openhab.core.config.discovery.AbstractDiscoveryService;
34 import org.openhab.core.config.discovery.DiscoveryResult;
35 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
36 import org.openhab.core.thing.ThingTypeUID;
37 import org.openhab.core.thing.ThingUID;
38 import org.openhab.core.thing.binding.ThingHandler;
39 import org.openhab.core.thing.binding.ThingHandlerService;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * The {@link PilightDeviceDiscoveryService} discovers pilight devices after a bridge thing has been created and
45  * connected to the pilight daemon. Things are discovered periodically in the background or after a manual trigger.
46  *
47  * @author Niklas Dörfler - Initial contribution
48  */
49 @NonNullByDefault
50 public class PilightDeviceDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
51
52     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = PilightHandlerFactory.SUPPORTED_THING_TYPES_UIDS;
53
54     private static final int AUTODISCOVERY_SEARCH_TIME_SEC = 10;
55     private static final int AUTODISCOVERY_BACKGROUND_SEARCH_INTERVAL_SEC = 60 * 10;
56
57     private final Logger logger = LoggerFactory.getLogger(PilightDeviceDiscoveryService.class);
58
59     private @Nullable PilightBridgeHandler pilightBridgeHandler;
60     private @Nullable ThingUID bridgeUID;
61
62     private @Nullable ScheduledFuture<?> backgroundDiscoveryJob;
63     private CompletableFuture<Config> configFuture;
64     private CompletableFuture<List<Status>> statusFuture;
65
66     public PilightDeviceDiscoveryService() {
67         super(SUPPORTED_THING_TYPES_UIDS, AUTODISCOVERY_SEARCH_TIME_SEC);
68         configFuture = new CompletableFuture<>();
69         statusFuture = new CompletableFuture<>();
70     }
71
72     @Override
73     protected void startScan() {
74         if (pilightBridgeHandler != null) {
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                     if (this.pilightBridgeHandler != null) {
82                         final Optional<Status> status = allStatus.stream()
83                                 .filter(s -> s.getDevices().contains(deviceId)).findFirst();
84
85                         final ThingTypeUID thingTypeUID;
86                         final String typeString;
87
88                         if (status.isPresent()) {
89                             if (status.get().getType().equals(DeviceType.SWITCH)) {
90                                 thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_SWITCH.getId());
91                                 typeString = "Switch";
92                             } else if (status.get().getType().equals(DeviceType.DIMMER)) {
93                                 thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_DIMMER.getId());
94                                 typeString = "Dimmer";
95                             } else if (status.get().getType().equals(DeviceType.VALUE)) {
96                                 thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_GENERIC.getId());
97                                 typeString = "Generic";
98                             } else if (status.get().getType().equals(DeviceType.CONTACT)) {
99                                 thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_CONTACT.getId());
100                                 typeString = "Contact";
101                             } else {
102                                 thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_GENERIC.getId());
103                                 typeString = "Generic";
104                             }
105                         } else {
106                             thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_GENERIC.getId());
107                             typeString = "Generic";
108                         }
109
110                         final @Nullable PilightBridgeHandler pilightBridgeHandler = this.pilightBridgeHandler;
111                         if (pilightBridgeHandler != null) {
112                             final ThingUID thingUID = new ThingUID(thingTypeUID,
113                                     pilightBridgeHandler.getThing().getUID(), deviceId);
114
115                             final Map<String, Object> properties = new HashMap<>();
116                             properties.put(PROPERTY_NAME, deviceId);
117
118                             DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID)
119                                     .withThingType(thingTypeUID).withProperties(properties).withBridge(bridgeUID)
120                                     .withRepresentationProperty(PROPERTY_NAME)
121                                     .withLabel("Pilight " + typeString + " Device '" + deviceId + "'").build();
122
123                             thingDiscovered(discoveryResult);
124                         }
125                     }
126                 });
127             });
128
129             final @Nullable PilightBridgeHandler pilightBridgeHandler = this.pilightBridgeHandler;
130             if (pilightBridgeHandler != null) {
131                 pilightBridgeHandler.refreshConfigAndStatus();
132             }
133         }
134     }
135
136     @Override
137     protected synchronized void stopScan() {
138         super.stopScan();
139         configFuture.cancel(true);
140         statusFuture.cancel(true);
141         if (bridgeUID != null) {
142             removeOlderResults(getTimestampOfLastScan(), bridgeUID);
143         }
144     }
145
146     @Override
147     protected void startBackgroundDiscovery() {
148         logger.debug("Start Pilight device background discovery");
149         final @Nullable ScheduledFuture<?> backgroundDiscoveryJob = this.backgroundDiscoveryJob;
150         if (backgroundDiscoveryJob == null || backgroundDiscoveryJob.isCancelled()) {
151             this.backgroundDiscoveryJob = scheduler.scheduleWithFixedDelay(this::startScan, 20,
152                     AUTODISCOVERY_BACKGROUND_SEARCH_INTERVAL_SEC, TimeUnit.SECONDS);
153         }
154     }
155
156     @Override
157     protected void stopBackgroundDiscovery() {
158         logger.debug("Stop Pilight device background discovery");
159         final @Nullable ScheduledFuture<?> backgroundDiscoveryJob = this.backgroundDiscoveryJob;
160         if (backgroundDiscoveryJob != null) {
161             backgroundDiscoveryJob.cancel(true);
162             this.backgroundDiscoveryJob = null;
163         }
164     }
165
166     @Override
167     public void setThingHandler(final ThingHandler handler) {
168         if (handler instanceof PilightBridgeHandler) {
169             this.pilightBridgeHandler = (PilightBridgeHandler) handler;
170             final @Nullable PilightBridgeHandler pilightBridgeHandler = this.pilightBridgeHandler;
171             if (pilightBridgeHandler != null) {
172                 bridgeUID = pilightBridgeHandler.getThing().getUID();
173             }
174         }
175     }
176
177     @Override
178     public @Nullable ThingHandler getThingHandler() {
179         return pilightBridgeHandler;
180     }
181
182     @Override
183     public void activate() {
184         super.activate(null);
185         final @Nullable PilightBridgeHandler pilightBridgeHandler = this.pilightBridgeHandler;
186         if (pilightBridgeHandler != null) {
187             pilightBridgeHandler.registerDiscoveryListener(this);
188         }
189     }
190
191     @Override
192     public void deactivate() {
193         if (bridgeUID != null) {
194             removeOlderResults(getTimestampOfLastScan(), bridgeUID);
195         }
196
197         final @Nullable PilightBridgeHandler pilightBridgeHandler = this.pilightBridgeHandler;
198         if (pilightBridgeHandler != null) {
199             pilightBridgeHandler.unregisterDiscoveryListener();
200         }
201
202         super.deactivate();
203     }
204
205     /**
206      * Method used to get pilight device config into the discovery class.
207      *
208      * @param config config to get
209      */
210     public void setConfig(Config config) {
211         configFuture.complete(config);
212     }
213
214     /**
215      * Method used to get pilight device status list into the discovery class.
216      *
217      * @param status list of status objects
218      */
219     public void setStatus(List<Status> status) {
220         statusFuture.complete(status);
221     }
222
223     @Override
224     public Set<ThingTypeUID> getSupportedThingTypes() {
225         return SUPPORTED_THING_TYPES_UIDS;
226     }
227 }