2 * Copyright (c) 2010-2022 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.Collections;
18 import java.util.Comparator;
19 import java.util.HashMap;
20 import java.util.LinkedHashMap;
22 import java.util.Optional;
23 import java.util.stream.Collectors;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.openhab.binding.synopanalyzer.internal.config.SynopAnalyzerConfiguration;
27 import org.openhab.binding.synopanalyzer.internal.synop.StationDB;
28 import org.openhab.binding.synopanalyzer.internal.synop.StationDB.Station;
29 import org.openhab.core.config.discovery.AbstractDiscoveryService;
30 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
31 import org.openhab.core.i18n.LocationProvider;
32 import org.openhab.core.library.types.DecimalType;
33 import org.openhab.core.library.types.PointType;
34 import org.openhab.core.thing.ThingUID;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * The {@link SynopAnalyzerDiscoveryService} creates things based on the configured location.
41 * @author Gaƫl L'hopital - Initial Contribution
44 public class SynopAnalyzerDiscoveryService extends AbstractDiscoveryService {
45 private static final int DISCOVER_TIMEOUT_SECONDS = 5;
47 private final Logger logger = LoggerFactory.getLogger(SynopAnalyzerDiscoveryService.class);
48 private final Map<Integer, Double> distances = new HashMap<>();
49 private final LocationProvider locationProvider;
50 private final StationDB stationDB;
53 * Creates a SynopAnalyzerDiscoveryService with enabled autostart.
56 public SynopAnalyzerDiscoveryService(StationDB stationDB, LocationProvider locationProvider) {
57 super(Collections.singleton(THING_SYNOP), DISCOVER_TIMEOUT_SECONDS);
58 this.locationProvider = locationProvider;
59 this.stationDB = stationDB;
63 public void startScan() {
64 logger.debug("Starting Synop Analyzer discovery scan");
65 PointType location = locationProvider.getLocation();
66 if (location == null) {
67 logger.debug("LocationProvider.getLocation() is not set -> Will not provide any discovery results");
70 createResults(location);
73 public void createResults(PointType serverLocation) {
76 stationDB.stations.forEach(s -> {
77 PointType stationLocation = new PointType(s.getLocation());
78 DecimalType distance = serverLocation.distanceFrom(stationLocation);
79 distances.put(s.idOmm, distance.doubleValue());
82 Map<Integer, Double> result = distances.entrySet().stream()
83 .sorted(Map.Entry.comparingByValue(Comparator.naturalOrder())).collect(Collectors.toMap(
84 Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new));
86 Integer nearestId = result.entrySet().iterator().next().getKey();
87 Optional<Station> station = stationDB.stations.stream().filter(s -> s.idOmm == nearestId).findFirst();
88 thingDiscovered(DiscoveryResultBuilder.create(new ThingUID(THING_SYNOP, nearestId.toString()))
89 .withLabel(String.format("Synop : %s", station.get().usualName))
90 .withProperty(SynopAnalyzerConfiguration.STATION_ID, nearestId)
91 .withRepresentationProperty(SynopAnalyzerConfiguration.STATION_ID).build());