2 * Copyright (c) 2010-2024 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.sncf.internal.discovery;
15 import static org.openhab.binding.sncf.internal.SncfBindingConstants.*;
17 import java.util.Arrays;
18 import java.util.LinkedList;
19 import java.util.List;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.sncf.internal.SncfException;
23 import org.openhab.binding.sncf.internal.dto.PlaceNearby;
24 import org.openhab.binding.sncf.internal.handler.SncfBridgeHandler;
25 import org.openhab.core.config.discovery.AbstractThingHandlerDiscoveryService;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.i18n.LocationProvider;
28 import org.openhab.core.library.types.PointType;
29 import org.openhab.core.thing.ThingUID;
30 import org.osgi.service.component.annotations.Activate;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Reference;
33 import org.osgi.service.component.annotations.ServiceScope;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * The {@link SncfDiscoveryService} searches for available
39 * station discoverable through API
41 * @author Gaƫl L'hopital - Initial contribution
43 @Component(scope = ServiceScope.PROTOTYPE, service = SncfDiscoveryService.class)
45 public class SncfDiscoveryService extends AbstractThingHandlerDiscoveryService<SncfBridgeHandler> {
46 private static final int SEARCH_TIME = 7;
48 private final Logger logger = LoggerFactory.getLogger(SncfDiscoveryService.class);
50 private @NonNullByDefault({}) LocationProvider locationProvider;
52 private int searchRange = 1500;
55 public SncfDiscoveryService() {
56 super(SncfBridgeHandler.class, SUPPORTED_THING_TYPES_UIDS, SEARCH_TIME, false);
59 @Reference(unbind = "-")
60 public void setLocationProvider(LocationProvider locationProvider) {
61 this.locationProvider = locationProvider;
65 public void startScan() {
66 PointType location = locationProvider.getLocation();
67 if (location != null) {
68 ThingUID bridgeUID = thingHandler.getThing().getUID();
71 List<PlaceNearby> places = thingHandler.discoverNearby(location, searchRange);
72 if (places != null && !places.isEmpty()) {
73 places.forEach(place -> {
74 // stop_point:SNCF:87386573:Bus
75 List<String> idElts = new LinkedList<String>(Arrays.asList(place.id.split(":")));
78 thingDiscovered(DiscoveryResultBuilder
79 .create(new ThingUID(STATION_THING_TYPE, bridgeUID, String.join("_", idElts)))
81 String.format("%s (%s)", place.stopPoint.name, idElts.get(1)).replace("-", "_"))
82 .withBridge(bridgeUID).withRepresentationProperty(STOP_POINT_ID)
83 .withProperty(STOP_POINT_ID, place.id).build());
86 logger.info("No station found in a perimeter of {} m, extending search", searchRange);
89 } catch (SncfException e) {
90 logger.warn("Error calling SNCF Api : {}", e.getMessage());
93 logger.info("Please set a system location to enable station discovery");