]> git.basschouten.com Git - openhab-addons.git/blob
c56887eb2f9d9bbc5bb9b67ab03f94e890eaae5d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.weatherunderground.internal.discovery;
14
15 import static org.openhab.binding.weatherunderground.internal.WeatherUndergroundBindingConstants.*;
16 import static org.openhab.binding.weatherunderground.internal.config.WeatherUndergroundConfiguration.*;
17
18 import java.util.HashMap;
19 import java.util.Locale;
20 import java.util.Map;
21 import java.util.Objects;
22 import java.util.Set;
23 import java.util.concurrent.ScheduledFuture;
24 import java.util.concurrent.TimeUnit;
25
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;
36
37 /**
38  * The {@link WeatherUndergroundDiscoveryService} creates things based on the configured location.
39  *
40  * @author Laurent Garnier - Initial Contribution
41  * @author Laurent Garnier - Consider locale (language) when discovering a new thing
42  */
43 public class WeatherUndergroundDiscoveryService extends AbstractDiscoveryService {
44
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;
49
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;
56
57     private final ThingUID bridgeUID;
58
59     /**
60      * Creates a WeatherUndergroundDiscoveryService with enabled autostart.
61      */
62
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;
69     }
70
71     /* We override this method to allow a call from the thing handler factory */
72     @Override
73     public void activate(Map<String, Object> configProperties) {
74         super.activate(configProperties);
75     }
76
77     /* We override this method to allow a call from the thing handler factory */
78     @Override
79     public void deactivate() {
80         super.deactivate();
81     }
82
83     @Override
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");
89             return;
90         }
91         createResults(location, localeProvider.getLocale());
92     }
93
94     @Override
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);
107                         update = true;
108                     } else if (!Objects.equals(currentLanguage, previousLanguage)) {
109                         logger.debug("Language has been changed from {} to {}: Creating new discovery result",
110                                 previousLanguage, currentLanguage);
111                         update = true;
112                     } else if (!Objects.equals(currentCountry, previousCountry)) {
113                         logger.debug("Country has been changed from {} to {}: Creating new discovery result",
114                                 previousCountry, currentCountry);
115                         update = true;
116                     }
117                     if (update) {
118                         createResults(currentLocation, localeProvider.getLocale());
119                         previousLocation = currentLocation;
120                         previousLanguage = currentLanguage;
121                         previousCountry = currentCountry;
122                     }
123                 }
124             }, 0, LOCATION_CHANGED_CHECK_INTERVAL, TimeUnit.SECONDS);
125             logger.debug("Scheduled Weather Underground location-changed job every {} seconds",
126                     LOCATION_CHANGED_CHECK_INTERVAL);
127         }
128     }
129
130     @Override
131     protected void stopBackgroundDiscovery() {
132         logger.debug("Stopping Weather Underground device background discovery");
133         if (discoveryJob != null && !discoveryJob.isCancelled()) {
134             discoveryJob.cancel(true);
135             discoveryJob = null;
136             logger.debug("Stopped Weather Underground device background discovery");
137         }
138     }
139
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);
147         }
148         thingDiscovered(DiscoveryResultBuilder.create(localWeatherThing).withLabel("Local Weather")
149                 .withProperties(properties).withBridge(bridgeUID).build());
150     }
151 }