This repository has been archived by the owner on May 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 787
/
AstroDiscoveryService.java
121 lines (107 loc) · 4.83 KB
/
AstroDiscoveryService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* Copyright (c) 2014-2017 by the respective copyright holders.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.smarthome.binding.astro.discovery;
import static org.eclipse.smarthome.binding.astro.AstroBindingConstants.*;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import org.eclipse.smarthome.config.discovery.AbstractDiscoveryService;
import org.eclipse.smarthome.config.discovery.DiscoveryResultBuilder;
import org.eclipse.smarthome.core.i18n.LocationProvider;
import org.eclipse.smarthome.core.library.types.PointType;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The {@link AstroDiscoveryService} creates things based on the configured location.
*
* @author Gerhard Riegler - Initial Contribution
* @author Stefan Triller - Use configured location
*/
public class AstroDiscoveryService extends AbstractDiscoveryService {
private final Logger logger = LoggerFactory.getLogger(AstroDiscoveryService.class);
private static final int DISCOVER_TIMEOUT_SECONDS = 30;
private static final int LOCATION_CHANGED_CHECK_INTERVAL = 60;
private LocationProvider locationProvider;
private ScheduledFuture<?> astroDiscoveryJob;
private PointType previousLocation;
private static final ThingUID sunThing = new ThingUID(THING_TYPE_SUN, LOCAL);
private static final ThingUID moonThing = new ThingUID(THING_TYPE_MOON, LOCAL);
/**
* Creates a AstroDiscoveryService with enabled autostart.
*/
public AstroDiscoveryService() {
super(new HashSet<>(Arrays.asList(new ThingTypeUID(BINDING_ID, "-"))), DISCOVER_TIMEOUT_SECONDS, true);
}
@Override
protected void activate(Map<String, Object> configProperties) {
super.activate(configProperties);
}
@Override
protected void modified(Map<String, Object> configProperties) {
super.modified(configProperties);
}
@Override
protected void startScan() {
logger.debug("Starting Astro discovery scan");
PointType location = locationProvider.getLocation();
if (location == null) {
logger.debug("LocationProvider.getLocation() is not set -> Will not provide any discovery results");
return;
}
createResults(location);
}
@Override
protected void startBackgroundDiscovery() {
if (astroDiscoveryJob == null) {
astroDiscoveryJob = scheduler.scheduleWithFixedDelay(() -> {
PointType currentLocation = locationProvider.getLocation();
if (!Objects.equals(currentLocation, previousLocation)) {
logger.debug("Location has been changed from {} to {}: Creating new discovery results",
previousLocation, currentLocation);
createResults(currentLocation);
previousLocation = currentLocation;
}
}, 0, LOCATION_CHANGED_CHECK_INTERVAL, TimeUnit.SECONDS);
logger.debug("Scheduled astro location-changed job every {} seconds", LOCATION_CHANGED_CHECK_INTERVAL);
}
}
@Override
protected void stopBackgroundDiscovery() {
logger.debug("Stopping Astro device background discovery");
if (astroDiscoveryJob != null && !astroDiscoveryJob.isCancelled()) {
if (astroDiscoveryJob.cancel(true)) {
astroDiscoveryJob = null;
logger.debug("Stopped Astro device background discovery");
}
}
}
public void createResults(PointType location) {
String propGeolocation;
if (location.getAltitude() != null) {
propGeolocation = String.format("%s,%s,%s", location.getLatitude(), location.getLongitude(),
location.getAltitude());
} else {
propGeolocation = String.format("%s,%s", location.getLatitude(), location.getLongitude());
}
thingDiscovered(DiscoveryResultBuilder.create(sunThing).withLabel("Local Sun")
.withProperty("geolocation", propGeolocation).build());
thingDiscovered(DiscoveryResultBuilder.create(moonThing).withLabel("Local Moon")
.withProperty("geolocation", propGeolocation).build());
}
protected void setLocationProvider(LocationProvider locationProvider) {
this.locationProvider = locationProvider;
}
protected void unsetLocationProvider(LocationProvider locationProvider) {
this.locationProvider = null;
}
}