]> git.basschouten.com Git - openhab-addons.git/blob
72e6dfb118077d1c22c467508c0772c7c4b8a0ba
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.synopanalyzer.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.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;
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 static final int DISCOVER_TIMEOUT_SECONDS = 5;
46
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;
51
52     /**
53      * Creates a SynopAnalyzerDiscoveryService with enabled autostart.
54      *
55      */
56     public SynopAnalyzerDiscoveryService(StationDB stationDB, LocationProvider locationProvider) {
57         super(Collections.singleton(THING_SYNOP), DISCOVER_TIMEOUT_SECONDS);
58         this.locationProvider = locationProvider;
59         this.stationDB = stationDB;
60     }
61
62     @Override
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");
68             return;
69         }
70         createResults(location);
71     }
72
73     public void createResults(PointType serverLocation) {
74         distances.clear();
75
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());
80         });
81
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));
85
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());
92     }
93 }