]> git.basschouten.com Git - openhab-addons.git/blob
2f54a3fdad4f7214a81b3fa58ca671ff4aee1fab
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.openweathermap.internal.discovery;
14
15 import static org.openhab.binding.openweathermap.internal.OpenWeatherMapBindingConstants.*;
16
17 import java.util.Date;
18 import java.util.Map;
19 import java.util.Objects;
20 import java.util.concurrent.ScheduledFuture;
21 import java.util.concurrent.TimeUnit;
22
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;
36
37 /**
38  * The {@link OpenWeatherMapDiscoveryService} creates things based on the configured location.
39  *
40  * @author Christoph Weitkamp - Initial Contribution
41  */
42 @NonNullByDefault
43 public class OpenWeatherMapDiscoveryService extends AbstractDiscoveryService {
44
45     private final Logger logger = LoggerFactory.getLogger(OpenWeatherMapDiscoveryService.class);
46
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;
52
53     private final OpenWeatherMapAPIHandler bridgeHandler;
54
55     /**
56      * Creates an OpenWeatherMapLocationDiscoveryService.
57      */
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;
65         activate(null);
66     }
67
68     @Override
69     protected void activate(@Nullable Map<String, Object> configProperties) {
70         super.activate(configProperties);
71     }
72
73     @Override
74     public void deactivate() {
75         removeOlderResults(new Date().getTime(), bridgeHandler.getThing().getUID());
76         super.deactivate();
77     }
78
79     @Override
80     protected void startScan() {
81         logger.debug("Start manual OpenWeatherMap Location discovery scan.");
82         scanForNewLocation(false);
83     }
84
85     @Override
86     protected synchronized void stopScan() {
87         logger.debug("Stop manual OpenWeatherMap Location discovery scan.");
88         super.stopScan();
89     }
90
91     @Override
92     protected void startBackgroundDiscovery() {
93         ScheduledFuture<?> localDiscoveryJob = discoveryJob;
94         if (localDiscoveryJob == null || localDiscoveryJob.isCancelled()) {
95             logger.debug("Start OpenWeatherMap Location background discovery job at interval {} s.",
96                     DISCOVERY_INTERVAL_SECONDS);
97             localDiscoveryJob = scheduler.scheduleWithFixedDelay(() -> {
98                 scanForNewLocation(true);
99             }, 0, DISCOVERY_INTERVAL_SECONDS, TimeUnit.SECONDS);
100         }
101     }
102
103     @Override
104     protected void stopBackgroundDiscovery() {
105         ScheduledFuture<?> localDiscoveryJob = discoveryJob;
106         if (localDiscoveryJob != null && !localDiscoveryJob.isCancelled()) {
107             logger.debug("Stop OpenWeatherMap Location background discovery job.");
108             if (localDiscoveryJob.cancel(true)) {
109                 discoveryJob = null;
110             }
111         }
112     }
113
114     private void scanForNewLocation(boolean updateOnlyIfNewLocation) {
115         PointType currentLocation = locationProvider.getLocation();
116         if (currentLocation == null) {
117             logger.debug("Location is not set -> Will not provide any discovery results.");
118         } else if (!Objects.equals(currentLocation, previousLocation)) {
119             logger.debug("Location has been changed from {} to {} -> Creating new discovery results.", previousLocation,
120                     currentLocation);
121             createResults(currentLocation);
122             previousLocation = currentLocation;
123         } else if (!updateOnlyIfNewLocation) {
124             createResults(currentLocation);
125         }
126     }
127
128     private void createResults(PointType location) {
129         String locationString = location.toFullString();
130         ThingUID bridgeUID = bridgeHandler.getThing().getUID();
131         createWeatherAndForecastResult(locationString, bridgeUID);
132         createAirPollutionResult(locationString, bridgeUID);
133         createOneCallResult(locationString, bridgeUID);
134         createOneCallHistoryResult(locationString, bridgeUID);
135     }
136
137     private void createWeatherAndForecastResult(String location, ThingUID bridgeUID) {
138         thingDiscovered(DiscoveryResultBuilder.create(new ThingUID(THING_TYPE_WEATHER_AND_FORECAST, bridgeUID, LOCAL))
139                 .withLabel("@text/discovery.weather-and-forecast.local.label").withProperty(CONFIG_LOCATION, location)
140                 .withRepresentationProperty(CONFIG_LOCATION).withBridge(bridgeUID).build());
141     }
142
143     private void createAirPollutionResult(String location, ThingUID bridgeUID) {
144         thingDiscovered(DiscoveryResultBuilder.create(new ThingUID(THING_TYPE_AIR_POLLUTION, bridgeUID, LOCAL))
145                 .withLabel("@text/discovery.air-pollution.local.label").withProperty(CONFIG_LOCATION, location)
146                 .withRepresentationProperty(CONFIG_LOCATION).withBridge(bridgeUID).build());
147     }
148
149     private void createOneCallResult(String location, ThingUID bridgeUID) {
150         thingDiscovered(
151                 DiscoveryResultBuilder.create(new ThingUID(THING_TYPE_ONECALL_WEATHER_AND_FORECAST, bridgeUID, LOCAL))
152                         .withLabel("@text/discovery.onecall.local.label").withProperty(CONFIG_LOCATION, location)
153                         .withRepresentationProperty(CONFIG_LOCATION).withBridge(bridgeUID).build());
154     }
155
156     private void createOneCallHistoryResult(String location, ThingUID bridgeUID) {
157         thingDiscovered(DiscoveryResultBuilder.create(new ThingUID(THING_TYPE_ONECALL_HISTORY, bridgeUID, LOCAL))
158                 .withLabel("@text/discovery.onecall-history.local.label").withProperty(CONFIG_LOCATION, location)
159                 .withRepresentationProperty(CONFIG_LOCATION).withBridge(bridgeUID).build());
160     }
161 }