]> git.basschouten.com Git - openhab-addons.git/blob
35eb914ee76e263415c66b9fba82afdc241c1a06
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.Iterator;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Map.Entry;
21 import java.util.Set;
22 import java.util.TreeMap;
23
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;
39
40 /**
41  * The {@link SynopAnalyzerDiscoveryService} discovers synop stations based on the configured location.
42  *
43  * @author GaĆ«l L'hopital - Initial Contribution
44  */
45 @Component(service = DiscoveryService.class)
46 @NonNullByDefault
47 public class SynopAnalyzerDiscoveryService extends AbstractDiscoveryService {
48     private static final int DISCOVER_TIMEOUT_SECONDS = 2;
49
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;
54
55     @Activate
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();
61     }
62
63     @Override
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");
69             return;
70         }
71         createResults(location);
72     }
73
74     public void createResults(PointType serverLocation) {
75         Map<Double, Station> distances = new TreeMap<>();
76
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);
82             }
83         });
84
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();
90
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());
95         } else {
96             logger.info("No Synop station available at a radius higher than {} m - resetting to 0 m", radius);
97             radius = 0;
98         }
99     }
100 }