]> git.basschouten.com Git - openhab-addons.git/blob
8091432463d540b6a827d88967535af6bf8d0cdf
[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.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.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;
37
38 /**
39  * The {@link SncfDiscoveryService} searches for available
40  * station discoverable through API
41  *
42  * @author GaĆ«l L'hopital - Initial contribution
43  */
44 @Component(service = ThingHandlerService.class)
45 @NonNullByDefault
46 public class SncfDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
47     private static final int SEARCH_TIME = 7;
48
49     private final Logger logger = LoggerFactory.getLogger(SncfDiscoveryService.class);
50
51     private @Nullable LocationProvider locationProvider;
52     private @Nullable SncfBridgeHandler bridgeHandler;
53
54     private int searchRange = 1500;
55
56     @Activate
57     public SncfDiscoveryService() {
58         super(SUPPORTED_THING_TYPES_UIDS, SEARCH_TIME, false);
59     }
60
61     @Override
62     public void deactivate() {
63         super.deactivate();
64     }
65
66     @Override
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();
74                 searchRange += 500;
75                 try {
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(":")));
81                             idElts.remove(0);
82                             idElts.remove(0);
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))
86                                             .replace("-", "_"))
87                                     .withBridge(bridgeUID).withRepresentationProperty(STOP_POINT_ID)
88                                     .withProperty(STOP_POINT_ID, place.id).build());
89                         });
90                     } else {
91                         logger.info("No station found in a perimeter of {} m, extending search", searchRange);
92                         startScan();
93                     }
94                 } catch (SncfException e) {
95                     logger.warn("Error calling SNCF Api : {}", e.getMessage());
96                 }
97             } else {
98                 logger.info("Please set a system location to enable station discovery");
99             }
100         }
101     }
102
103     @Override
104     public void setThingHandler(ThingHandler handler) {
105         if (handler instanceof SncfBridgeHandler) {
106             this.bridgeHandler = (SncfBridgeHandler) handler;
107             this.locationProvider = ((SncfBridgeHandler) handler).getLocationProvider();
108         }
109     }
110
111     @Override
112     public @Nullable ThingHandler getThingHandler() {
113         return bridgeHandler;
114     }
115 }