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.synopanalyzer.internal.discovery;
15 import static org.openhab.binding.synopanalyzer.internal.SynopAnalyzerBindingConstants.THING_SYNOP;
17 import java.util.Iterator;
18 import java.util.List;
20 import java.util.Map.Entry;
22 import java.util.TreeMap;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.openhab.binding.synopanalyzer.internal.config.SynopAnalyzerConfiguration;
26 import org.openhab.binding.synopanalyzer.internal.stationdb.Station;
27 import org.openhab.binding.synopanalyzer.internal.stationdb.StationDbService;
28 import org.openhab.core.config.discovery.AbstractDiscoveryService;
29 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
30 import org.openhab.core.config.discovery.DiscoveryService;
31 import org.openhab.core.i18n.LocationProvider;
32 import org.openhab.core.library.types.PointType;
33 import org.openhab.core.thing.ThingUID;
34 import org.osgi.service.component.annotations.Activate;
35 import org.osgi.service.component.annotations.Component;
36 import org.osgi.service.component.annotations.Reference;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * The {@link SynopAnalyzerDiscoveryService} discovers synop stations based on the configured location.
43 * @author Gaƫl L'hopital - Initial Contribution
45 @Component(service = DiscoveryService.class)
47 public class SynopAnalyzerDiscoveryService extends AbstractDiscoveryService {
48 private static final int DISCOVER_TIMEOUT_SECONDS = 2;
50 private final Logger logger = LoggerFactory.getLogger(SynopAnalyzerDiscoveryService.class);
51 private final LocationProvider locationProvider;
52 private final List<Station> stations;
53 private double radius = 0;
56 public SynopAnalyzerDiscoveryService(@Reference StationDbService dBService,
57 @Reference LocationProvider locationProvider) {
58 super(Set.of(THING_SYNOP), DISCOVER_TIMEOUT_SECONDS);
59 this.locationProvider = locationProvider;
60 this.stations = dBService.getStations();
64 public void startScan() {
65 logger.debug("Starting Synop Analyzer discovery scan");
66 PointType location = locationProvider.getLocation();
67 if (location == null) {
68 logger.debug("LocationProvider.getLocation() is not set -> Will not provide any discovery results");
71 createResults(location);
74 public void createResults(PointType serverLocation) {
75 Map<Double, Station> distances = new TreeMap<>();
77 stations.forEach(station -> {
78 PointType stationLocation = new PointType(station.getLocation());
79 double distance = serverLocation.distanceFrom(stationLocation).doubleValue();
80 if (distance > radius) {
81 distances.put(distance, station);
85 Iterator<Entry<Double, Station>> stationIterator = distances.entrySet().iterator();
86 if (stationIterator.hasNext()) {
87 Entry<Double, Station> nearest = stationIterator.next();
88 Station station = nearest.getValue();
89 radius = nearest.getKey();
91 thingDiscovered(DiscoveryResultBuilder.create(new ThingUID(THING_SYNOP, Integer.toString(station.idOmm)))
92 .withLabel(String.format("Synop : %s", station.usualName))
93 .withProperty(SynopAnalyzerConfiguration.STATION_ID, station.idOmm)
94 .withRepresentationProperty(SynopAnalyzerConfiguration.STATION_ID).build());
96 logger.info("No Synop station available at a radius higher than {} m - resetting to 0 m", radius);