]> git.basschouten.com Git - openhab-addons.git/blob
d3163f16c2def372dd6e01618f39323601e91bf5
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.synopanalyser.internal.discovery;
14
15 import static org.openhab.binding.synopanalyzer.internal.SynopAnalyzerBindingConstants.THING_SYNOP;
16
17 import java.util.Collections;
18 import java.util.Comparator;
19 import java.util.HashMap;
20 import java.util.LinkedHashMap;
21 import java.util.Map;
22 import java.util.Optional;
23 import java.util.stream.Collectors;
24
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;
37
38 /**
39  * The {@link SynopAnalyzerDiscoveryService} creates things based on the configured location.
40  *
41  * @author GaĆ«l L'hopital - Initial Contribution
42  */
43 @NonNullByDefault
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<>();
50
51     /**
52      * Creates a SynopAnalyzerDiscoveryService with enabled autostart.
53      *
54      */
55     public SynopAnalyzerDiscoveryService(StationDB stationDB, LocationProvider locationProvider) {
56         super(Collections.singleton(THING_SYNOP), DISCOVER_TIMEOUT_SECONDS);
57         this.locationProvider = locationProvider;
58         this.stationDB = stationDB;
59     }
60
61     @Override
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");
67             return;
68         }
69         createResults(location);
70     }
71
72     public void createResults(PointType serverLocation) {
73         distances.clear();
74
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());
79         });
80
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));
84
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());
91     }
92 }