]> git.basschouten.com Git - openhab-addons.git/blob
a38ec0924ccf385080006e71a69d5458eea4f123
[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.gpstracker.internal.discovery;
14
15 import java.util.Date;
16 import java.util.HashSet;
17 import java.util.Map;
18 import java.util.Set;
19
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;
33
34 /**
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.
38  *
39  * @author Gabor Bicskei - Initial contribution
40  */
41 @NonNullByDefault
42 @Component(service = { DiscoveryService.class,
43         TrackerDiscoveryService.class }, configurationPid = "discovery.gpstracker")
44 public class TrackerDiscoveryService extends AbstractDiscoveryService {
45     /**
46      * Discovery timeout
47      */
48     private static final int TIMEOUT = 1;
49
50     /**
51      * Registry of tracker to discover next time
52      */
53     private Set<String> trackersToDiscover = new HashSet<>();
54
55     /**
56      * Constructor.
57      *
58      * @throws IllegalArgumentException thrown by the super constructor
59      */
60     public TrackerDiscoveryService() throws IllegalArgumentException {
61         super(GPSTrackerBindingConstants.SUPPORTED_THING_TYPES_UIDS, TIMEOUT, true);
62     }
63
64     /**
65      * Called when the source tracker is not registered as a thing. These undiscovered trackers will be registered by
66      * the discovery service.
67      *
68      * @param trackerId Tracker id.
69      */
70     public void addTracker(String trackerId) {
71         trackersToDiscover.add(trackerId);
72         if (isBackgroundDiscoveryEnabled()) {
73             createDiscoveryResult(trackerId);
74         }
75     }
76
77     /**
78      * Unregister the tracker after the thing handles is created.
79      *
80      * @param trackerId Tracker id to unregister
81      */
82     public void removeTracker(String trackerId) {
83         trackersToDiscover.remove(trackerId);
84     }
85
86     @Override
87     protected void startScan() {
88         trackersToDiscover.forEach(this::createDiscoveryResult);
89     }
90
91     /**
92      * Create discovery result form the tracker id.
93      *
94      * @param trackerId Tracker id.
95      */
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)
101                 .build();
102         this.thingDiscovered(discoveryResult);
103     }
104
105     @Override
106     @Activate
107     protected void activate(@Nullable Map<String, Object> configProperties) {
108         super.activate(configProperties);
109     }
110
111     @Override
112     @Modified
113     protected void modified(@Nullable Map<String, Object> configProperties) {
114         super.modified(configProperties);
115     }
116
117     @Override
118     @Deactivate
119     protected void deactivate() {
120         removeOlderResults(new Date().getTime());
121         super.deactivate();
122     }
123
124     @Override
125     protected void stopScan() {
126         super.stopScan();
127         removeOlderResults(getTimestampOfLastScan());
128     }
129 }