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.fmiweather.internal.discovery;
15 import static org.openhab.binding.fmiweather.internal.BindingConstants.*;
16 import static org.openhab.binding.fmiweather.internal.discovery.CitiesOfFinland.CITIES_OF_FINLAND;
18 import java.util.Collections;
19 import java.util.LinkedList;
20 import java.util.List;
21 import java.util.Objects;
23 import java.util.TreeSet;
24 import java.util.concurrent.ScheduledFuture;
25 import java.util.concurrent.TimeUnit;
26 import java.util.stream.Collectors;
27 import java.util.stream.Stream;
29 import org.eclipse.jdt.annotation.NonNullByDefault;
30 import org.eclipse.jdt.annotation.Nullable;
31 import org.openhab.binding.fmiweather.internal.BindingConstants;
32 import org.openhab.binding.fmiweather.internal.client.Client;
33 import org.openhab.binding.fmiweather.internal.client.Location;
34 import org.openhab.binding.fmiweather.internal.client.exception.FMIResponseException;
35 import org.openhab.binding.fmiweather.internal.client.exception.FMIUnexpectedResponseException;
36 import org.openhab.core.cache.ExpiringCache;
37 import org.openhab.core.config.discovery.AbstractDiscoveryService;
38 import org.openhab.core.config.discovery.DiscoveryResult;
39 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
40 import org.openhab.core.config.discovery.DiscoveryService;
41 import org.openhab.core.i18n.LocationProvider;
42 import org.openhab.core.library.types.DecimalType;
43 import org.openhab.core.library.types.PointType;
44 import org.openhab.core.thing.ThingTypeUID;
45 import org.openhab.core.thing.ThingUID;
46 import org.osgi.service.component.annotations.Component;
47 import org.osgi.service.component.annotations.Reference;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * The {@link FMIWeatherDiscoveryService} creates things based on the configured location.
54 * @author Sami Salonen - Initial contribution
57 @Component(service = DiscoveryService.class, configurationPid = "discovery.fmiweather")
58 public class FMIWeatherDiscoveryService extends AbstractDiscoveryService {
59 private final Logger logger = LoggerFactory.getLogger(FMIWeatherDiscoveryService.class);
61 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_OBSERVATION, THING_TYPE_FORECAST);
62 private static final long STATIONS_CACHE_MILLIS = TimeUnit.HOURS.toMillis(12);
63 private static final int STATIONS_TIMEOUT_MILLIS = (int) TimeUnit.SECONDS.toMillis(10);
64 private static final int DISCOVER_TIMEOUT_SECONDS = 5;
65 private static final int LOCATION_CHANGED_CHECK_INTERVAL_SECONDS = 60;
66 private static final int FIND_STATION_METERS = 80_000;
68 private @Nullable ScheduledFuture<?> discoveryJob;
69 private @Nullable PointType previousLocation;
70 private @NonNullByDefault({}) LocationProvider locationProvider;
72 private ExpiringCache<Set<Location>> stationsCache = new ExpiringCache<>(STATIONS_CACHE_MILLIS, () -> {
74 return new Client().queryWeatherStations(STATIONS_TIMEOUT_MILLIS);
75 } catch (FMIUnexpectedResponseException e) {
76 logger.warn("Unexpected error with the response, potentially API format has changed. Printing out details",
78 } catch (FMIResponseException e) {
79 logger.warn("Error when querying stations, {}: {}", e.getClass().getSimpleName(), e.getMessage());
81 // Return empty set on errors
82 return Collections.emptySet();
86 * Creates a {@link FMIWeatherDiscoveryService} with immediately enabled background discovery.
88 public FMIWeatherDiscoveryService() {
89 super(SUPPORTED_THING_TYPES, DISCOVER_TIMEOUT_SECONDS, true);
93 protected void startScan() {
94 PointType location = null;
95 logger.debug("Starting FMI Weather discovery scan");
96 LocationProvider locationProvider = getLocationProvider();
97 location = locationProvider.getLocation();
98 if (location == null) {
99 logger.debug("LocationProvider.getLocation() is not set -> Will discover all stations");
101 createResults(location);
105 protected void startBackgroundDiscovery() {
106 if (discoveryJob == null) {
107 discoveryJob = scheduler.scheduleWithFixedDelay(() -> {
108 PointType currentLocation = locationProvider.getLocation();
109 if (!Objects.equals(currentLocation, previousLocation)) {
110 logger.debug("Location has been changed from {} to {}: Creating new discovery results",
111 previousLocation, currentLocation);
112 createResults(currentLocation);
113 previousLocation = currentLocation;
115 }, 0, LOCATION_CHANGED_CHECK_INTERVAL_SECONDS, TimeUnit.SECONDS);
116 logger.debug("Scheduled FMI Weather location-changed discovery job every {} seconds",
117 LOCATION_CHANGED_CHECK_INTERVAL_SECONDS);
121 public void createResults(@Nullable PointType location) {
122 createForecastForCurrentLocation(location);
123 createForecastsForCities(location);
124 createObservationsForStations(location);
127 private void createForecastForCurrentLocation(@Nullable PointType currentLocation) {
128 if (currentLocation != null) {
129 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(UID_LOCAL_FORECAST)
130 .withLabel(String.format("FMI local weather forecast"))
131 .withProperty(LOCATION,
132 String.format("%s,%s", currentLocation.getLatitude(), currentLocation.getLongitude()))
133 .withRepresentationProperty(LOCATION).build();
134 thingDiscovered(discoveryResult);
138 private void createForecastsForCities(@Nullable PointType currentLocation) {
139 CITIES_OF_FINLAND.stream().filter(location2 -> isClose(currentLocation, location2)).forEach(city -> {
140 DiscoveryResult discoveryResult = DiscoveryResultBuilder
141 .create(new ThingUID(THING_TYPE_FORECAST, cleanId(String.format("city_%s", city.name))))
142 .withProperty(LOCATION,
143 String.format("%s,%s", city.latitude.toPlainString(), city.longitude.toPlainString()))
144 .withLabel(String.format("FMI weather forecast for %s", city.name))
145 .withRepresentationProperty(LOCATION).build();
146 thingDiscovered(discoveryResult);
150 private void createObservationsForStations(@Nullable PointType location) {
151 List<Location> candidateStations = new LinkedList<>();
152 List<Location> filteredStations = new LinkedList<>();
153 cachedStations().peek(station -> {
154 if (logger.isDebugEnabled()) {
155 candidateStations.add(station);
157 }).filter(location2 -> isClose(location, location2)).peek(station -> {
158 if (logger.isDebugEnabled()) {
159 filteredStations.add(station);
161 }).forEach(station -> {
162 DiscoveryResult discoveryResult = DiscoveryResultBuilder
163 .create(new ThingUID(THING_TYPE_OBSERVATION,
164 cleanId(String.format("station_%s_%s", station.id, station.name))))
165 .withLabel(String.format("FMI weather observation for %s", station.name))
166 .withProperty(BindingConstants.FMISID, station.id)
167 .withRepresentationProperty(BindingConstants.FMISID).build();
168 thingDiscovered(discoveryResult);
170 if (logger.isDebugEnabled()) {
171 logger.debug("Candidate stations: {}",
172 candidateStations.stream().map(station -> String.format("%s (%s)", station.name, station.id))
173 .collect(Collectors.toCollection(TreeSet<String>::new)));
174 logger.debug("Filtered stations: {}",
175 filteredStations.stream().map(station -> String.format("%s (%s)", station.name, station.id))
176 .collect(Collectors.toCollection(TreeSet<String>::new)));
180 private static String cleanId(String id) {
181 return id.replace("ä", "a").replace("ö", "o").replace("å", "a").replace("Ä", "A").replace("Ö", "O")
182 .replace("Å", "a").replaceAll("[^a-zA-Z0-9_]", "_");
185 private static boolean isClose(@Nullable PointType location, Location location2) {
186 return location == null ? true
187 : new PointType(new DecimalType(location2.latitude), new DecimalType(location2.longitude))
188 .distanceFrom(location).doubleValue() < FIND_STATION_METERS;
191 @SuppressWarnings("null")
192 private Stream<Location> cachedStations() {
193 Set<Location> stations = stationsCache.getValue();
194 if (stations.isEmpty()) {
195 stationsCache.invalidateValue();
197 return stationsCache.getValue().stream();
201 protected void stopBackgroundDiscovery() {
202 logger.debug("Stopping FMI Weather background discovery");
203 ScheduledFuture<?> discoveryJob = this.discoveryJob;
204 if (discoveryJob != null) {
205 if (discoveryJob.cancel(true)) {
206 this.discoveryJob = null;
207 logger.debug("Stopped FMI Weather background discovery");
213 protected void setLocationProvider(LocationProvider locationProvider) {
214 this.locationProvider = locationProvider;
217 protected void unsetLocationProvider(LocationProvider provider) {
218 this.locationProvider = null;
221 protected LocationProvider getLocationProvider() {
222 return locationProvider;