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