2 * Copyright (c) 2010-2023 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.gpstracker.internal.discovery;
15 import java.util.Date;
16 import java.util.HashSet;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.gpstracker.internal.GPSTrackerBindingConstants;
23 import org.openhab.binding.gpstracker.internal.config.ConfigHelper;
24 import org.openhab.core.config.discovery.AbstractDiscoveryService;
25 import org.openhab.core.config.discovery.DiscoveryResult;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.config.discovery.DiscoveryService;
28 import org.openhab.core.thing.ThingUID;
29 import org.osgi.service.component.annotations.Activate;
30 import org.osgi.service.component.annotations.Component;
31 import org.osgi.service.component.annotations.Deactivate;
32 import org.osgi.service.component.annotations.Modified;
35 * The {@link TrackerDiscoveryService} class provides discovery service for the binding to discover trackers. Discovery
36 * process is initiated by the tracker by sending a GPS log record. Based on the tracker id received in thin record an
37 * entry is created in the Inbox for the thing representing the tracker.
39 * @author Gabor Bicskei - Initial contribution
42 @Component(service = { DiscoveryService.class,
43 TrackerDiscoveryService.class }, configurationPid = "discovery.gpstracker")
44 public class TrackerDiscoveryService extends AbstractDiscoveryService {
48 private static final int TIMEOUT = 1;
51 * Registry of tracker to discover next time
53 private Set<String> trackersToDiscover = new HashSet<>();
58 * @throws IllegalArgumentException thrown by the super constructor
60 public TrackerDiscoveryService() throws IllegalArgumentException {
61 super(GPSTrackerBindingConstants.SUPPORTED_THING_TYPES_UIDS, TIMEOUT, true);
65 * Called when the source tracker is not registered as a thing. These undiscovered trackers will be registered by
66 * the discovery service.
68 * @param trackerId Tracker id.
70 public void addTracker(String trackerId) {
71 trackersToDiscover.add(trackerId);
72 if (isBackgroundDiscoveryEnabled()) {
73 createDiscoveryResult(trackerId);
78 * Unregister the tracker after the thing handles is created.
80 * @param trackerId Tracker id to unregister
82 public void removeTracker(String trackerId) {
83 trackersToDiscover.remove(trackerId);
87 protected void startScan() {
88 trackersToDiscover.forEach(this::createDiscoveryResult);
92 * Create discovery result form the tracker id.
94 * @param trackerId Tracker id.
96 private void createDiscoveryResult(String trackerId) {
97 ThingUID id = new ThingUID(GPSTrackerBindingConstants.THING_TYPE_TRACKER, trackerId);
98 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(id)
99 .withProperty(ConfigHelper.CONFIG_TRACKER_ID, trackerId)
100 .withThingType(GPSTrackerBindingConstants.THING_TYPE_TRACKER).withLabel("GPS Tracker " + trackerId)
102 this.thingDiscovered(discoveryResult);
107 protected void activate(@Nullable Map<String, Object> configProperties) {
108 super.activate(configProperties);
113 protected void modified(@Nullable Map<String, Object> configProperties) {
114 super.modified(configProperties);
119 protected void deactivate() {
120 removeOlderResults(new Date().getTime());
125 protected void stopScan() {
127 removeOlderResults(getTimestampOfLastScan());