2 * Copyright (c) 2010-2020 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.synopanalyser.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.synopanalyser.internal.synop.StationDB;
27 import org.openhab.binding.synopanalyser.internal.synop.StationDB.Station;
28 import org.openhab.binding.synopanalyzer.internal.config.SynopAnalyzerConfiguration;
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 final Logger logger = LoggerFactory.getLogger(SynopAnalyzerDiscoveryService.class);
46 private static final int DISCOVER_TIMEOUT_SECONDS = 5;
47 private LocationProvider locationProvider;
48 private final StationDB stationDB;
49 private final Map<Integer, Double> distances = new HashMap<>();
52 * Creates a SynopAnalyzerDiscoveryService with enabled autostart.
55 public SynopAnalyzerDiscoveryService(StationDB stationDB, LocationProvider locationProvider) {
56 super(Collections.singleton(THING_SYNOP), DISCOVER_TIMEOUT_SECONDS);
57 this.locationProvider = locationProvider;
58 this.stationDB = stationDB;
62 public void startScan() {
63 logger.debug("Starting Synop Analyzer discovery scan");
64 PointType location = locationProvider.getLocation();
65 if (location == null) {
66 logger.debug("LocationProvider.getLocation() is not set -> Will not provide any discovery results");
69 createResults(location);
72 public void createResults(PointType serverLocation) {
75 stationDB.stations.forEach(s -> {
76 PointType stationLocation = new PointType(s.getLocation());
77 DecimalType distance = serverLocation.distanceFrom(stationLocation);
78 distances.put(s.idOmm, distance.doubleValue());
81 Map<Integer, Double> result = distances.entrySet().stream()
82 .sorted(Map.Entry.comparingByValue(Comparator.naturalOrder())).collect(Collectors.toMap(
83 Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new));
85 Integer nearestId = result.entrySet().iterator().next().getKey();
86 Optional<Station> station = stationDB.stations.stream().filter(s -> s.idOmm == nearestId).findFirst();
87 thingDiscovered(DiscoveryResultBuilder.create(new ThingUID(THING_SYNOP, Integer.toString(nearestId)))
88 .withLabel("Synop : " + station.get().usualName)
89 .withProperty(SynopAnalyzerConfiguration.STATION_ID, nearestId)
90 .withRepresentationProperty(SynopAnalyzerConfiguration.STATION_ID).build());