2 * Copyright (c) 2010-2022 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.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.sncf.internal.SncfException;
24 import org.openhab.binding.sncf.internal.dto.PlaceNearby;
25 import org.openhab.binding.sncf.internal.handler.SncfBridgeHandler;
26 import org.openhab.core.config.discovery.AbstractDiscoveryService;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.i18n.LocationProvider;
29 import org.openhab.core.library.types.PointType;
30 import org.openhab.core.thing.ThingUID;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.openhab.core.thing.binding.ThingHandlerService;
33 import org.osgi.service.component.annotations.Activate;
34 import org.osgi.service.component.annotations.Component;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * The {@link SncfDiscoveryService} searches for available
40 * station discoverable through API
42 * @author Gaƫl L'hopital - Initial contribution
44 @Component(service = ThingHandlerService.class)
46 public class SncfDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
47 private static final int SEARCH_TIME = 7;
49 private final Logger logger = LoggerFactory.getLogger(SncfDiscoveryService.class);
51 private @Nullable LocationProvider locationProvider;
52 private @Nullable SncfBridgeHandler bridgeHandler;
54 private int searchRange = 1500;
57 public SncfDiscoveryService() {
58 super(SUPPORTED_THING_TYPES_UIDS, SEARCH_TIME, false);
62 public void deactivate() {
67 public void startScan() {
68 SncfBridgeHandler handler = bridgeHandler;
69 LocationProvider provider = locationProvider;
70 if (provider != null && handler != null) {
71 PointType location = provider.getLocation();
72 if (location != null) {
73 ThingUID bridgeUID = handler.getThing().getUID();
76 List<PlaceNearby> places = handler.discoverNearby(location, searchRange);
77 if (places != null && !places.isEmpty()) {
78 places.forEach(place -> {
79 // stop_point:SNCF:87386573:Bus
80 List<String> idElts = new LinkedList<String>(Arrays.asList(place.id.split(":")));
83 thingDiscovered(DiscoveryResultBuilder
84 .create(new ThingUID(STATION_THING_TYPE, bridgeUID, String.join("_", idElts)))
85 .withLabel(String.format("%s (%s)", place.stopPoint.name, idElts.get(1))
87 .withBridge(bridgeUID).withRepresentationProperty(STOP_POINT_ID)
88 .withProperty(STOP_POINT_ID, place.id).build());
91 logger.info("No station found in a perimeter of {} m, extending search", searchRange);
94 } catch (SncfException e) {
95 logger.warn("Error calling SNCF Api : {}", e.getMessage());
98 logger.info("Please set a system location to enable station discovery");
104 public void setThingHandler(ThingHandler handler) {
105 if (handler instanceof SncfBridgeHandler) {
106 this.bridgeHandler = (SncfBridgeHandler) handler;
107 this.locationProvider = ((SncfBridgeHandler) handler).getLocationProvider();
112 public @Nullable ThingHandler getThingHandler() {
113 return bridgeHandler;