]> git.basschouten.com Git - openhab-addons.git/blob
4663bedb96b4cda9ffbe44fc0cd1d30933331517
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.sncf.internal.discovery;
14
15 import static org.openhab.binding.sncf.internal.SncfBindingConstants.*;
16
17 import java.util.Arrays;
18 import java.util.LinkedList;
19 import java.util.List;
20
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;
36
37 /**
38  * The {@link SncfDiscoveryService} searches for available
39  * station discoverable through API
40  *
41  * @author GaĆ«l L'hopital - Initial contribution
42  */
43 @Component(scope = ServiceScope.PROTOTYPE, service = SncfDiscoveryService.class)
44 @NonNullByDefault
45 public class SncfDiscoveryService extends AbstractThingHandlerDiscoveryService<SncfBridgeHandler> {
46     private static final int SEARCH_TIME = 7;
47
48     private final Logger logger = LoggerFactory.getLogger(SncfDiscoveryService.class);
49
50     private @NonNullByDefault({}) LocationProvider locationProvider;
51
52     private int searchRange = 1500;
53
54     @Activate
55     public SncfDiscoveryService() {
56         super(SncfBridgeHandler.class, SUPPORTED_THING_TYPES_UIDS, SEARCH_TIME, false);
57     }
58
59     @Reference(unbind = "-")
60     public void setLocationProvider(LocationProvider locationProvider) {
61         this.locationProvider = locationProvider;
62     }
63
64     @Override
65     public void startScan() {
66         PointType location = locationProvider.getLocation();
67         if (location != null) {
68             ThingUID bridgeUID = thingHandler.getThing().getUID();
69             searchRange += 500;
70             try {
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(":")));
76                         idElts.remove(0);
77                         idElts.remove(0);
78                         thingDiscovered(DiscoveryResultBuilder
79                                 .create(new ThingUID(STATION_THING_TYPE, bridgeUID, String.join("_", idElts)))
80                                 .withLabel(
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());
84                     });
85                 } else {
86                     logger.info("No station found in a perimeter of {} m, extending search", searchRange);
87                     startScan();
88                 }
89             } catch (SncfException e) {
90                 logger.warn("Error calling SNCF Api : {}", e.getMessage());
91             }
92         } else {
93             logger.info("Please set a system location to enable station discovery");
94         }
95     }
96 }