2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.airquality.internal.discovery;
15 import static org.openhab.binding.airquality.internal.AirQualityBindingConstants.*;
16 import static org.openhab.binding.airquality.internal.AirQualityConfiguration.LOCATION;
18 import java.util.HashMap;
20 import java.util.Objects;
21 import java.util.concurrent.ScheduledFuture;
22 import java.util.concurrent.TimeUnit;
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;
40 * The {@link AirQualityDiscoveryService} creates things based on the configured location.
42 * @author Gaƫl L'hopital - Initial Contribution
44 @Component(service = DiscoveryService.class, configurationPid = "discovery.airquality")
46 public class AirQualityDiscoveryService extends AbstractDiscoveryService {
47 private final Logger logger = LoggerFactory.getLogger(AirQualityDiscoveryService.class);
49 private static final int DISCOVER_TIMEOUT_SECONDS = 10;
50 private static final int LOCATION_CHANGED_CHECK_INTERVAL = 60;
52 private final LocationProvider locationProvider;
53 private @Nullable ScheduledFuture<?> discoveryJob;
54 private @Nullable PointType previousLocation;
57 * Creates a AirQualityDiscoveryService with enabled autostart.
60 public AirQualityDiscoveryService(@Reference LocationProvider locationProvider) {
61 super(SUPPORTED_THING_TYPES, DISCOVER_TIMEOUT_SECONDS, true);
62 this.locationProvider = locationProvider;
66 protected void activate(@Nullable Map<String, Object> configProperties) {
67 super.activate(configProperties);
72 protected void modified(@Nullable Map<String, Object> configProperties) {
73 super.modified(configProperties);
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");
84 createResults(location);
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;
98 }, 0, LOCATION_CHANGED_CHECK_INTERVAL, TimeUnit.SECONDS);
99 logger.debug("Scheduled Air Qualitylocation-changed job every {} seconds", LOCATION_CHANGED_CHECK_INTERVAL);
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());
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)) {
118 logger.debug("Stopped Air Quality background discovery");