2 * Copyright (c) 2010-2020 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.openweathermap.internal.discovery;
15 import static org.openhab.binding.openweathermap.internal.OpenWeatherMapBindingConstants.*;
17 import java.util.Date;
19 import java.util.Objects;
20 import java.util.concurrent.ScheduledFuture;
21 import java.util.concurrent.TimeUnit;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.openweathermap.internal.handler.AbstractOpenWeatherMapHandler;
26 import org.openhab.binding.openweathermap.internal.handler.OpenWeatherMapAPIHandler;
27 import org.openhab.core.config.discovery.AbstractDiscoveryService;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.i18n.LocaleProvider;
30 import org.openhab.core.i18n.LocationProvider;
31 import org.openhab.core.i18n.TranslationProvider;
32 import org.openhab.core.library.types.PointType;
33 import org.openhab.core.thing.ThingUID;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * The {@link OpenWeatherMapDiscoveryService} creates things based on the configured location.
40 * @author Christoph Weitkamp - Initial Contribution
43 public class OpenWeatherMapDiscoveryService extends AbstractDiscoveryService {
45 private final Logger logger = LoggerFactory.getLogger(OpenWeatherMapDiscoveryService.class);
47 private static final int DISCOVERY_TIMEOUT_SECONDS = 2;
48 private static final int DISCOVERY_INTERVAL_SECONDS = 60;
49 private @Nullable ScheduledFuture<?> discoveryJob;
50 private final LocationProvider locationProvider;
51 private @Nullable PointType previousLocation;
53 private final OpenWeatherMapAPIHandler bridgeHandler;
56 * Creates an OpenWeatherMapLocationDiscoveryService.
58 public OpenWeatherMapDiscoveryService(OpenWeatherMapAPIHandler bridgeHandler, LocationProvider locationProvider,
59 LocaleProvider localeProvider, TranslationProvider i18nProvider) {
60 super(AbstractOpenWeatherMapHandler.SUPPORTED_THING_TYPES, DISCOVERY_TIMEOUT_SECONDS);
61 this.bridgeHandler = bridgeHandler;
62 this.locationProvider = locationProvider;
63 this.localeProvider = localeProvider;
64 this.i18nProvider = i18nProvider;
69 protected void activate(@Nullable Map<String, Object> configProperties) {
70 super.activate(configProperties);
74 public void deactivate() {
75 removeOlderResults(new Date().getTime(), bridgeHandler.getThing().getUID());
80 protected void startScan() {
81 logger.debug("Start manual OpenWeatherMap Location discovery scan.");
82 scanForNewLocation(false);
86 protected synchronized void stopScan() {
87 logger.debug("Stop manual OpenWeatherMap Location discovery scan.");
92 protected void startBackgroundDiscovery() {
93 if (discoveryJob == null || discoveryJob.isCancelled()) {
94 logger.debug("Start OpenWeatherMap Location background discovery job at interval {} s.",
95 DISCOVERY_INTERVAL_SECONDS);
96 discoveryJob = scheduler.scheduleWithFixedDelay(() -> {
97 scanForNewLocation(true);
98 }, 0, DISCOVERY_INTERVAL_SECONDS, TimeUnit.SECONDS);
103 protected void stopBackgroundDiscovery() {
104 if (discoveryJob != null && !discoveryJob.isCancelled()) {
105 logger.debug("Stop OpenWeatherMap Location background discovery job.");
106 if (discoveryJob.cancel(true)) {
112 private void scanForNewLocation(boolean updateOnlyIfNewLocation) {
113 PointType currentLocation = locationProvider.getLocation();
114 if (currentLocation == null) {
115 logger.debug("Location is not set -> Will not provide any discovery results.");
116 } else if (!Objects.equals(currentLocation, previousLocation)) {
117 logger.debug("Location has been changed from {} to {} -> Creating new discovery results.", previousLocation,
119 createResults(currentLocation);
120 previousLocation = currentLocation;
121 } else if (!updateOnlyIfNewLocation) {
122 createResults(currentLocation);
126 private void createResults(PointType location) {
127 String locationString = location.toFullString();
128 ThingUID bridgeUID = bridgeHandler.getThing().getUID();
129 createWeatherAndForecastResult(locationString, bridgeUID);
130 createUVIndexResult(locationString, bridgeUID);
133 private void createWeatherAndForecastResult(String location, ThingUID bridgeUID) {
134 thingDiscovered(DiscoveryResultBuilder.create(new ThingUID(THING_TYPE_WEATHER_AND_FORECAST, bridgeUID, LOCAL))
135 .withLabel("Local weather and forecast").withProperty(CONFIG_LOCATION, location)
136 .withRepresentationProperty(CONFIG_LOCATION).withBridge(bridgeUID).build());
139 private void createUVIndexResult(String location, ThingUID bridgeUID) {
140 thingDiscovered(DiscoveryResultBuilder.create(new ThingUID(THING_TYPE_UVINDEX, bridgeUID, LOCAL))
141 .withLabel("Local UV Index").withProperty(CONFIG_LOCATION, location)
142 .withRepresentationProperty(CONFIG_LOCATION).withBridge(bridgeUID).build());