2 * Copyright (c) 2010-2023 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.vigicrues.internal.discovery;
15 import static org.openhab.binding.vigicrues.internal.VigiCruesBindingConstants.*;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.vigicrues.internal.StationConfiguration;
19 import org.openhab.binding.vigicrues.internal.api.ApiHandler;
20 import org.openhab.binding.vigicrues.internal.api.VigiCruesException;
21 import org.openhab.binding.vigicrues.internal.dto.hubeau.HubEauResponse;
22 import org.openhab.core.config.discovery.AbstractDiscoveryService;
23 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
24 import org.openhab.core.config.discovery.DiscoveryService;
25 import org.openhab.core.i18n.LocationProvider;
26 import org.openhab.core.library.types.PointType;
27 import org.openhab.core.thing.ThingUID;
28 import org.osgi.service.component.annotations.Activate;
29 import org.osgi.service.component.annotations.Component;
30 import org.osgi.service.component.annotations.Reference;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * The {@link VigiCruesDiscoveryService} searches for available
36 * hydro stations discoverable through API
38 * @author Gaƫl L'hopital - Initial contribution
40 @Component(service = DiscoveryService.class, configurationPid = "discovery.vigicrues")
42 public class VigiCruesDiscoveryService extends AbstractDiscoveryService {
43 private static final int SEARCH_TIME = 5;
45 private final Logger logger = LoggerFactory.getLogger(VigiCruesDiscoveryService.class);
46 private final LocationProvider locationProvider;
47 private final ApiHandler apiHandler;
49 private int searchRange = 10;
52 public VigiCruesDiscoveryService(@Reference ApiHandler apiHandler, @Reference LocationProvider locationProvider) {
53 super(SUPPORTED_THING_TYPES_UIDS, SEARCH_TIME, false);
54 this.apiHandler = apiHandler;
55 this.locationProvider = locationProvider;
59 public void startScan() {
60 PointType location = locationProvider.getLocation();
61 if (location != null) {
63 HubEauResponse response = apiHandler.discoverStations(location, searchRange);
64 if (response.count > 0) {
65 response.stations.stream().filter(station -> station.enService).forEach(station -> {
66 thingDiscovered(DiscoveryResultBuilder
67 .create(new ThingUID(THING_TYPE_STATION, station.codeStation))
68 .withLabel(station.libelleStation).withRepresentationProperty(StationConfiguration.ID)
69 .withProperty(StationConfiguration.ID, station.codeStation).build());
72 logger.info("No station exists in a neighbourhood of {} km", searchRange);
74 } catch (VigiCruesException e) {
75 logger.warn("Error discovering nearby hydro stations : {}", e.getMessage());