2 * Copyright (c) 2010-2023 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.weatherunderground.internal.discovery;
15 import static org.openhab.binding.weatherunderground.internal.WeatherUndergroundBindingConstants.*;
16 import static org.openhab.binding.weatherunderground.internal.config.WeatherUndergroundConfiguration.*;
18 import java.util.HashMap;
19 import java.util.Locale;
21 import java.util.Objects;
23 import java.util.concurrent.ScheduledFuture;
24 import java.util.concurrent.TimeUnit;
26 import org.openhab.binding.weatherunderground.internal.handler.WeatherUndergroundHandler;
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.library.types.PointType;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.ThingUID;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * The {@link WeatherUndergroundDiscoveryService} creates things based on the configured location.
40 * @author Laurent Garnier - Initial Contribution
41 * @author Laurent Garnier - Consider locale (language) when discovering a new thing
43 public class WeatherUndergroundDiscoveryService extends AbstractDiscoveryService {
45 private final Logger logger = LoggerFactory.getLogger(WeatherUndergroundDiscoveryService.class);
46 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_WEATHER);
47 private static final int DISCOVER_TIMEOUT_SECONDS = 2;
48 private static final int LOCATION_CHANGED_CHECK_INTERVAL = 60;
50 private final LocaleProvider localeProvider;
51 private final LocationProvider locationProvider;
52 private ScheduledFuture<?> discoveryJob;
53 private PointType previousLocation;
54 private String previousLanguage;
55 private String previousCountry;
57 private final ThingUID bridgeUID;
60 * Creates a WeatherUndergroundDiscoveryService with enabled autostart.
63 public WeatherUndergroundDiscoveryService(ThingUID bridgeUID, LocaleProvider localeProvider,
64 LocationProvider locationProvider) {
65 super(SUPPORTED_THING_TYPES, DISCOVER_TIMEOUT_SECONDS, true);
66 this.bridgeUID = bridgeUID;
67 this.localeProvider = localeProvider;
68 this.locationProvider = locationProvider;
71 /* We override this method to allow a call from the thing handler factory */
73 public void activate(Map<String, Object> configProperties) {
74 super.activate(configProperties);
77 /* We override this method to allow a call from the thing handler factory */
79 public void deactivate() {
84 protected void startScan() {
85 logger.debug("Starting Weather Underground discovery scan");
86 PointType location = locationProvider.getLocation();
87 if (location == null) {
88 logger.debug("LocationProvider.getLocation() is not set -> Will not provide any discovery results");
91 createResults(location, localeProvider.getLocale());
95 protected void startBackgroundDiscovery() {
96 logger.debug("Starting Weather Underground device background discovery");
97 if (discoveryJob == null || discoveryJob.isCancelled()) {
98 discoveryJob = scheduler.scheduleWithFixedDelay(() -> {
99 PointType currentLocation = locationProvider.getLocation();
100 String currentLanguage = localeProvider.getLocale().getLanguage();
101 String currentCountry = localeProvider.getLocale().getCountry();
102 if (currentLocation != null) {
103 boolean update = false;
104 if (!Objects.equals(currentLocation, previousLocation)) {
105 logger.debug("Location has been changed from {} to {}: Creating new discovery result",
106 previousLocation, currentLocation);
108 } else if (!Objects.equals(currentLanguage, previousLanguage)) {
109 logger.debug("Language has been changed from {} to {}: Creating new discovery result",
110 previousLanguage, currentLanguage);
112 } else if (!Objects.equals(currentCountry, previousCountry)) {
113 logger.debug("Country has been changed from {} to {}: Creating new discovery result",
114 previousCountry, currentCountry);
118 createResults(currentLocation, localeProvider.getLocale());
119 previousLocation = currentLocation;
120 previousLanguage = currentLanguage;
121 previousCountry = currentCountry;
124 }, 0, LOCATION_CHANGED_CHECK_INTERVAL, TimeUnit.SECONDS);
125 logger.debug("Scheduled Weather Underground location-changed job every {} seconds",
126 LOCATION_CHANGED_CHECK_INTERVAL);
131 protected void stopBackgroundDiscovery() {
132 logger.debug("Stopping Weather Underground device background discovery");
133 if (discoveryJob != null && !discoveryJob.isCancelled()) {
134 discoveryJob.cancel(true);
136 logger.debug("Stopped Weather Underground device background discovery");
140 private void createResults(PointType location, Locale locale) {
141 ThingUID localWeatherThing = new ThingUID(THING_TYPE_WEATHER, bridgeUID, LOCAL);
142 Map<String, Object> properties = new HashMap<>(3);
143 properties.put(LOCATION, String.format("%s,%s", location.getLatitude(), location.getLongitude()));
144 String lang = WeatherUndergroundHandler.getCodeFromLanguage(locale);
145 if (!lang.isEmpty()) {
146 properties.put(LANGUAGE, lang);
148 thingDiscovered(DiscoveryResultBuilder.create(localWeatherThing).withLabel("Local Weather")
149 .withProperties(properties).withBridge(bridgeUID).build());