]> git.basschouten.com Git - openhab-addons.git/blob
4494c4e52125ef7ba284dfe9999db06494ec04db
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.openuv.internal.discovery;
14
15 import static org.openhab.binding.openuv.internal.OpenUVBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Objects;
20 import java.util.concurrent.ScheduledFuture;
21 import java.util.concurrent.TimeUnit;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.openuv.internal.handler.OpenUVBridgeHandler;
26 import org.openhab.core.config.discovery.AbstractDiscoveryService;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.i18n.LocationProvider;
29 import org.openhab.core.library.types.PointType;
30 import org.openhab.core.thing.ThingUID;
31 import org.osgi.service.component.annotations.Modified;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * The {@link OpenUVDiscoveryService} creates things based on the configured location.
37  *
38  * @author GaĆ«l L'hopital - Initial Contribution
39  */
40 @NonNullByDefault
41 public class OpenUVDiscoveryService extends AbstractDiscoveryService {
42     private final Logger logger = LoggerFactory.getLogger(OpenUVDiscoveryService.class);
43
44     private static final int DISCOVER_TIMEOUT_SECONDS = 10;
45     private static final int LOCATION_CHANGED_CHECK_INTERVAL = 60;
46
47     private final LocationProvider locationProvider;
48     private final OpenUVBridgeHandler bridgeHandler;
49     private @Nullable ScheduledFuture<?> discoveryJob;
50     private @Nullable PointType previousLocation;
51
52     /**
53      * Creates a OpenUVDiscoveryService with enabled autostart.
54      */
55     public OpenUVDiscoveryService(OpenUVBridgeHandler bridgeHandler, LocationProvider locationProvider) {
56         super(SUPPORTED_THING_TYPES_UIDS, DISCOVER_TIMEOUT_SECONDS, true);
57         this.locationProvider = locationProvider;
58         this.bridgeHandler = bridgeHandler;
59     }
60
61     @Override
62     protected void activate(@Nullable Map<String, @Nullable Object> configProperties) {
63         super.activate(configProperties);
64     }
65
66     @Override
67     @Modified
68     protected void modified(@Nullable Map<String, @Nullable Object> configProperties) {
69         super.modified(configProperties);
70     }
71
72     @Override
73     protected void startScan() {
74         logger.debug("Starting OpenUV discovery scan");
75         PointType location = locationProvider.getLocation();
76         if (location == null) {
77             logger.debug("LocationProvider.getLocation() is not set -> Will not provide any discovery results");
78             return;
79         }
80         createResults(location);
81     }
82
83     @Override
84     protected void startBackgroundDiscovery() {
85         if (discoveryJob == null) {
86             discoveryJob = scheduler.scheduleWithFixedDelay(() -> {
87                 PointType currentLocation = locationProvider.getLocation();
88                 if (currentLocation != null && !Objects.equals(currentLocation, previousLocation)) {
89                     logger.debug("Location has been changed from {} to {}: Creating new discovery results",
90                             previousLocation, currentLocation);
91                     createResults(currentLocation);
92                     previousLocation = currentLocation;
93                 }
94             }, 0, LOCATION_CHANGED_CHECK_INTERVAL, TimeUnit.SECONDS);
95             logger.debug("Scheduled OpenUV-changed job every {} seconds", LOCATION_CHANGED_CHECK_INTERVAL);
96         }
97     }
98
99     public void createResults(PointType location) {
100         ThingUID bridgeUID = bridgeHandler.getThing().getUID();
101         ThingUID localOpenUVThing = new ThingUID(LOCATION_REPORT_THING_TYPE, bridgeUID, LOCAL);
102         Map<String, Object> properties = new HashMap<>();
103         properties.put(LOCATION, location.toString());
104         thingDiscovered(DiscoveryResultBuilder.create(localOpenUVThing).withLabel("Local UV Information")
105                 .withProperties(properties).withRepresentationProperty(location.toString()).withBridge(bridgeUID)
106                 .build());
107     }
108
109     @SuppressWarnings("null")
110     @Override
111     protected void stopBackgroundDiscovery() {
112         logger.debug("Stopping OpenUV background discovery");
113         if (discoveryJob != null && !discoveryJob.isCancelled()) {
114             if (discoveryJob.cancel(true)) {
115                 discoveryJob = null;
116                 logger.debug("Stopped OpenUV background discovery");
117             }
118         }
119     }
120 }