]> git.basschouten.com Git - openhab-addons.git/blob
f1036c83ce2446e68b5699fd3c3e19eb8a47254f
[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.vigicrues.internal.discovery;
14
15 import static org.openhab.binding.vigicrues.internal.VigiCruesBindingConstants.*;
16
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;
33
34 /**
35  * The {@link VigiCruesDiscoveryService} searches for available
36  * hydro stations discoverable through API
37  *
38  * @author GaĆ«l L'hopital - Initial contribution
39  */
40 @Component(service = DiscoveryService.class, configurationPid = "discovery.vigicrues")
41 @NonNullByDefault
42 public class VigiCruesDiscoveryService extends AbstractDiscoveryService {
43     private static final int SEARCH_TIME = 5;
44
45     private final Logger logger = LoggerFactory.getLogger(VigiCruesDiscoveryService.class);
46     private final LocationProvider locationProvider;
47     private final ApiHandler apiHandler;
48
49     private int searchRange = 10;
50
51     @Activate
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;
56     }
57
58     @Override
59     public void startScan() {
60         PointType location = locationProvider.getLocation();
61         if (location != null) {
62             try {
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());
70                     });
71                 } else {
72                     logger.info("No station exists in a neighbourhood of {} km", searchRange);
73                 }
74             } catch (VigiCruesException e) {
75                 logger.warn("Error discovering nearby hydro stations : {}", e.getMessage());
76             }
77             searchRange += 10;
78         }
79         stopScan();
80     }
81 }