Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
Astro Background discovery for location changes (#3725)
Browse files Browse the repository at this point in the history
* Astro Background discovery for location changes

Signed-off-by: Stefan Triller <stefan.triller@telekom.de>
  • Loading branch information
triller-telekom authored and kaikreuzer committed Jun 24, 2017
1 parent b802ef8 commit 76f70a1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 16 deletions.
24 changes: 15 additions & 9 deletions extensions/binding/org.eclipse.smarthome.binding.astro/README.md
Expand Up @@ -10,17 +10,19 @@ This binding supports two Things: Sun and Moon

## Discovery

Discovery is not necessary, because all calculations are done within the binding.
If you have a location set (see for ex. PaperUI: "Configuration - System - Regional Settings - Location"), this will automatically create a "Local Sun" and a "Local Moon" item for this location.

If you change your system location the background discovery detects this change and applies it to the "Local Sun" and a "Local Moon" items automatically.

## Binding Configuration

No binding configuration required.

## Thing Configuration

A thing requires the geolocation (latitude, longitude) for which the calculation is done.
A thing requires the geolocation (latitude,longitude[,altitude]) for which the calculation is done.
Optionally, a refresh interval (in seconds) can be defined to also calculate positional data like azimuth and elevation.
An complementary altitude (optional) configuration item can also be specified to sharpen results provided by Radiation group.
The complementary altitude part of the geolocation parameter is optional and sharpens results provided by the Radiation group.

## Channels

Expand Down Expand Up @@ -123,26 +125,26 @@ sunrise is 22:10 but `latest` is set to 20:00 so the event/datetime value is mov
Things:

```
astro:sun:home [ geolocation="xx.xxxxxx,xx.xxxxxx", altitude=100, interval=60 ]
astro:moon:home [ geolocation="xx.xxxxxx,xx.xxxxxx", interval=60 ]
astro:sun:home [ geolocation="xx.xxxxxx,xx.xxxxxx,xx.xxx", interval=60 ]
astro:moon:home [ geolocation="xx.xxxxxx,xx.xxxxxx,xx.xxx", interval=60 ]
```

or optionally with an event offset

```
astro:sun:home [ geolocation="xx.xxxxxx,xx.xxxxxx", altitude=100, interval=60 ] {
astro:sun:home [ geolocation="xx.xxxxxx,xx.xxxxxx,xx.xxx", interval=60 ] {
Channels:
Type rangeEvent : rise#event [
offset=-30
]
}
astro:moon:home [ geolocation="xx.xxxxxx,xx.xxxxxx", interval=60 ]
astro:moon:home [ geolocation="xx.xxxxxx,xx.xxxxxx,xx.xxx", interval=60 ]
```

or a datetime offset

```
astro:sun:home [ geolocation="xx.xxxxxx,xx.xxxxxx", altitude=100, interval=60 ] {
astro:sun:home [ geolocation="xx.xxxxxx,xx.xxxxxx,xx.xxx", interval=60 ] {
Channels:
Type start : rise#start [
offset=5
Expand All @@ -156,7 +158,7 @@ astro:sun:home [ geolocation="xx.xxxxxx,xx.xxxxxx", altitude=100, interval=60 ]
or a offset and latest

```
astro:sun:home [ geolocation="xx.xxxxxx,xx.xxxxxx", altitude=100, interval=60 ] {
astro:sun:home [ geolocation="xx.xxxxxx,xx.xxxxxx,xx.xxx", interval=60 ] {
Channels:
Type rangeEvent : rise#event [
offset=-10,
Expand Down Expand Up @@ -187,3 +189,7 @@ then
...
end
```

## Tips

Do not worry if for example the "astro dawn" is "-" in the place that you set for your item. The reason might be that you live in a northern country and it is summer, because then by definition the sun is not 18 degrees below the horizon in the morning. For details see: https://en.wikipedia.org/wiki/Dawn Also while reading this wikipedia article you might come to the conclusion that you want to use "civil dawn" anyway.
Expand Up @@ -11,8 +11,10 @@

import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.eclipse.smarthome.binding.astro.AstroBindingConstants;
import org.eclipse.smarthome.config.discovery.AbstractDiscoveryService;
import org.eclipse.smarthome.config.discovery.DiscoveryResultBuilder;
import org.eclipse.smarthome.core.i18n.LocationProvider;
Expand All @@ -31,29 +33,60 @@
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 disabled autostart.
* Creates a AstroDiscoveryService with enabled autostart.
*/
public AstroDiscoveryService() {
super(new HashSet<>(Arrays.asList(new ThingTypeUID(BINDING_ID, "-"))), DISCOVER_TIMEOUT_SECONDS, false);
super(new HashSet<>(Arrays.asList(new ThingTypeUID(BINDING_ID, "-"))), DISCOVER_TIMEOUT_SECONDS, true);
}

@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);
}

ThingUID sunThing = new ThingUID(AstroBindingConstants.THING_TYPE_SUN, LOCAL);
ThingUID moonThing = new ThingUID(AstroBindingConstants.THING_TYPE_MOON, LOCAL);
@Override
protected void startBackgroundDiscovery() {
if (astroDiscoveryJob == null) {
astroDiscoveryJob = scheduler.scheduleAtFixedRate(() -> {
PointType currentLocation = locationProvider.getLocation();
if (!Objects.equals(currentLocation, previousLocation)) {
logger.info("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(),
Expand Down

0 comments on commit 76f70a1

Please sign in to comment.