From e49ddf0b9b7428984373d65ab2c0f96e0407bc4a Mon Sep 17 00:00:00 2001 From: Stefan Triller Date: Thu, 22 Jun 2017 18:13:52 +0200 Subject: [PATCH 1/6] Astro Background discovery for location changes Signed-off-by: Stefan Triller --- .../README.md | 24 +++++--- .../AstroDiscoveryLocationChangedTask.java | 23 ++++++++ .../discovery/AstroDiscoveryService.java | 58 ++++++++++++++++--- 3 files changed, 87 insertions(+), 18 deletions(-) create mode 100644 extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryLocationChangedTask.java diff --git a/extensions/binding/org.eclipse.smarthome.binding.astro/README.md b/extensions/binding/org.eclipse.smarthome.binding.astro/README.md index fe27e97bd5d..a58ebcb2e29 100644 --- a/extensions/binding/org.eclipse.smarthome.binding.astro/README.md +++ b/extensions/binding/org.eclipse.smarthome.binding.astro/README.md @@ -10,7 +10,9 @@ This binding supports two Things: Sun and Moon ## Discovery -Discovery is not necessary, because all calculations are done within the binding. +You can use the auto discovery if you have a location set in "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 @@ -18,9 +20,9 @@ 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 @@ -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 @@ -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, @@ -187,3 +189,7 @@ then ... end ``` + +##Tipps + +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. diff --git a/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryLocationChangedTask.java b/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryLocationChangedTask.java new file mode 100644 index 00000000000..1b3789d8afe --- /dev/null +++ b/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryLocationChangedTask.java @@ -0,0 +1,23 @@ +package org.eclipse.smarthome.binding.astro.discovery; + +import org.eclipse.smarthome.core.library.types.PointType; + +public class AstroDiscoveryLocationChangedTask implements Runnable { + + private AstroDiscoveryService astroDiscoveryService; + private PointType previousLocation; + + public AstroDiscoveryLocationChangedTask(AstroDiscoveryService astroDiscoveryService) { + this.astroDiscoveryService = astroDiscoveryService; + } + + @Override + public void run() { + PointType currentLocation = astroDiscoveryService.getLocationProvider().getLocation(); + if (currentLocation != previousLocation) { + astroDiscoveryService.createResults(currentLocation); + previousLocation = currentLocation; + } + } + +} diff --git a/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryService.java b/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryService.java index 4756e4a7e39..7647bdae2df 100644 --- a/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryService.java +++ b/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryService.java @@ -9,14 +9,19 @@ import static org.eclipse.smarthome.binding.astro.AstroBindingConstants.*; +import java.text.ParseException; import java.util.Arrays; import java.util.HashSet; -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; import org.eclipse.smarthome.core.library.types.PointType; +import org.eclipse.smarthome.core.scheduler.CronExpression; +import org.eclipse.smarthome.core.scheduler.CronHelper; +import org.eclipse.smarthome.core.scheduler.Expression; +import org.eclipse.smarthome.core.scheduler.ExpressionThreadPoolManager; +import org.eclipse.smarthome.core.scheduler.ExpressionThreadPoolManager.ExpressionThreadPoolExecutor; import org.eclipse.smarthome.core.thing.ThingTypeUID; import org.eclipse.smarthome.core.thing.ThingUID; import org.slf4j.Logger; @@ -31,29 +36,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 final ExpressionThreadPoolExecutor scheduledExecutor; + private Runnable backgroundJob; + + private static ThingUID SUN_THING = new ThingUID(THING_TYPE_SUN, LOCAL); + private static ThingUID MOON_THING = 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); + + scheduledExecutor = ExpressionThreadPoolManager.getExpressionScheduledPool("astro"); } @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() { + Expression expression = null; + try { + expression = new CronExpression( + CronHelper.createCronForRepeatEverySeconds(LOCATION_CHANGED_CHECK_INTERVAL)); + } catch (ParseException e) { + return; + } + if (expression != null && backgroundJob == null) { + AstroDiscoveryLocationChangedTask job = new AstroDiscoveryLocationChangedTask(this); + scheduledExecutor.schedule(job, expression); + logger.info("Scheduled astro location-changed job every {} seconds", LOCATION_CHANGED_CHECK_INTERVAL); + backgroundJob = job; + } + } - ThingUID sunThing = new ThingUID(AstroBindingConstants.THING_TYPE_SUN, LOCAL); - ThingUID moonThing = new ThingUID(AstroBindingConstants.THING_TYPE_MOON, LOCAL); + @Override + protected void stopBackgroundDiscovery() { + if (backgroundJob != null) { + scheduledExecutor.remove(backgroundJob); + backgroundJob = null; + } + } + public void createResults(PointType location) { String propGeolocation; if (location.getAltitude() != null) { propGeolocation = String.format("%s,%s,%s", location.getLatitude(), location.getLongitude(), @@ -61,12 +97,16 @@ protected void startScan() { } else { propGeolocation = String.format("%s,%s", location.getLatitude(), location.getLongitude()); } - thingDiscovered(DiscoveryResultBuilder.create(sunThing).withLabel("Local Sun") + thingDiscovered(DiscoveryResultBuilder.create(SUN_THING).withLabel("Local Sun") .withProperty("geolocation", propGeolocation).build()); - thingDiscovered(DiscoveryResultBuilder.create(moonThing).withLabel("Local Moon") + thingDiscovered(DiscoveryResultBuilder.create(MOON_THING).withLabel("Local Moon") .withProperty("geolocation", propGeolocation).build()); } + public LocationProvider getLocationProvider() { + return locationProvider; + } + protected void setLocationProvider(LocationProvider locationProvider) { this.locationProvider = locationProvider; } From 38cea63cd1942b1e0a035903f17cf7f43ba2f316 Mon Sep 17 00:00:00 2001 From: Stefan Triller Date: Fri, 23 Jun 2017 08:56:24 +0200 Subject: [PATCH 2/6] Fix location comparison Signed-off-by: Stefan Triller --- .../org.eclipse.smarthome.binding.astro/README.md | 2 +- .../discovery/AstroDiscoveryLocationChangedTask.java | 10 +++++++--- .../binding/astro/discovery/AstroDiscoveryService.java | 6 +----- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/extensions/binding/org.eclipse.smarthome.binding.astro/README.md b/extensions/binding/org.eclipse.smarthome.binding.astro/README.md index a58ebcb2e29..ba856250aba 100644 --- a/extensions/binding/org.eclipse.smarthome.binding.astro/README.md +++ b/extensions/binding/org.eclipse.smarthome.binding.astro/README.md @@ -190,6 +190,6 @@ then end ``` -##Tipps +## Tipps 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. diff --git a/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryLocationChangedTask.java b/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryLocationChangedTask.java index 1b3789d8afe..d779349c310 100644 --- a/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryLocationChangedTask.java +++ b/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryLocationChangedTask.java @@ -1,20 +1,24 @@ package org.eclipse.smarthome.binding.astro.discovery; +import org.eclipse.smarthome.core.i18n.LocationProvider; import org.eclipse.smarthome.core.library.types.PointType; public class AstroDiscoveryLocationChangedTask implements Runnable { private AstroDiscoveryService astroDiscoveryService; private PointType previousLocation; + private LocationProvider locationProvider; - public AstroDiscoveryLocationChangedTask(AstroDiscoveryService astroDiscoveryService) { + public AstroDiscoveryLocationChangedTask(AstroDiscoveryService astroDiscoveryService, + LocationProvider locationProvider) { this.astroDiscoveryService = astroDiscoveryService; + this.locationProvider = locationProvider; } @Override public void run() { - PointType currentLocation = astroDiscoveryService.getLocationProvider().getLocation(); - if (currentLocation != previousLocation) { + PointType currentLocation = locationProvider.getLocation(); + if ((currentLocation != null) && !currentLocation.equals(previousLocation)) { astroDiscoveryService.createResults(currentLocation); previousLocation = currentLocation; } diff --git a/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryService.java b/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryService.java index 7647bdae2df..e7c06860a27 100644 --- a/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryService.java +++ b/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryService.java @@ -74,7 +74,7 @@ protected void startBackgroundDiscovery() { return; } if (expression != null && backgroundJob == null) { - AstroDiscoveryLocationChangedTask job = new AstroDiscoveryLocationChangedTask(this); + AstroDiscoveryLocationChangedTask job = new AstroDiscoveryLocationChangedTask(this, locationProvider); scheduledExecutor.schedule(job, expression); logger.info("Scheduled astro location-changed job every {} seconds", LOCATION_CHANGED_CHECK_INTERVAL); backgroundJob = job; @@ -103,10 +103,6 @@ public void createResults(PointType location) { .withProperty("geolocation", propGeolocation).build()); } - public LocationProvider getLocationProvider() { - return locationProvider; - } - protected void setLocationProvider(LocationProvider locationProvider) { this.locationProvider = locationProvider; } From 6d0005c7e057cd9e46928f7cf3af94c71017fbc5 Mon Sep 17 00:00:00 2001 From: Stefan Triller Date: Fri, 23 Jun 2017 10:11:29 +0200 Subject: [PATCH 3/6] Use Lamda expression instead of additional public class Signed-off-by: Stefan Triller --- .../AstroDiscoveryLocationChangedTask.java | 27 ------------ .../discovery/AstroDiscoveryService.java | 44 ++++++++----------- 2 files changed, 19 insertions(+), 52 deletions(-) delete mode 100644 extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryLocationChangedTask.java diff --git a/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryLocationChangedTask.java b/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryLocationChangedTask.java deleted file mode 100644 index d779349c310..00000000000 --- a/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryLocationChangedTask.java +++ /dev/null @@ -1,27 +0,0 @@ -package org.eclipse.smarthome.binding.astro.discovery; - -import org.eclipse.smarthome.core.i18n.LocationProvider; -import org.eclipse.smarthome.core.library.types.PointType; - -public class AstroDiscoveryLocationChangedTask implements Runnable { - - private AstroDiscoveryService astroDiscoveryService; - private PointType previousLocation; - private LocationProvider locationProvider; - - public AstroDiscoveryLocationChangedTask(AstroDiscoveryService astroDiscoveryService, - LocationProvider locationProvider) { - this.astroDiscoveryService = astroDiscoveryService; - this.locationProvider = locationProvider; - } - - @Override - public void run() { - PointType currentLocation = locationProvider.getLocation(); - if ((currentLocation != null) && !currentLocation.equals(previousLocation)) { - astroDiscoveryService.createResults(currentLocation); - previousLocation = currentLocation; - } - } - -} diff --git a/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryService.java b/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryService.java index e7c06860a27..325635cffca 100644 --- a/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryService.java +++ b/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryService.java @@ -9,19 +9,15 @@ import static org.eclipse.smarthome.binding.astro.AstroBindingConstants.*; -import java.text.ParseException; import java.util.Arrays; import java.util.HashSet; +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.scheduler.CronExpression; -import org.eclipse.smarthome.core.scheduler.CronHelper; -import org.eclipse.smarthome.core.scheduler.Expression; -import org.eclipse.smarthome.core.scheduler.ExpressionThreadPoolManager; -import org.eclipse.smarthome.core.scheduler.ExpressionThreadPoolManager.ExpressionThreadPoolExecutor; import org.eclipse.smarthome.core.thing.ThingTypeUID; import org.eclipse.smarthome.core.thing.ThingUID; import org.slf4j.Logger; @@ -38,8 +34,8 @@ public class AstroDiscoveryService extends AbstractDiscoveryService { private static final int DISCOVER_TIMEOUT_SECONDS = 30; private static final int LOCATION_CHANGED_CHECK_INTERVAL = 60; private LocationProvider locationProvider; - private final ExpressionThreadPoolExecutor scheduledExecutor; - private Runnable backgroundJob; + private ScheduledFuture astroDiscoveryJob; + private PointType previousLocation; private static ThingUID SUN_THING = new ThingUID(THING_TYPE_SUN, LOCAL); private static ThingUID MOON_THING = new ThingUID(THING_TYPE_MOON, LOCAL); @@ -49,8 +45,6 @@ public class AstroDiscoveryService extends AbstractDiscoveryService { */ public AstroDiscoveryService() { super(new HashSet<>(Arrays.asList(new ThingTypeUID(BINDING_ID, "-"))), DISCOVER_TIMEOUT_SECONDS, true); - - scheduledExecutor = ExpressionThreadPoolManager.getExpressionScheduledPool("astro"); } @Override @@ -66,26 +60,26 @@ protected void startScan() { @Override protected void startBackgroundDiscovery() { - Expression expression = null; - try { - expression = new CronExpression( - CronHelper.createCronForRepeatEverySeconds(LOCATION_CHANGED_CHECK_INTERVAL)); - } catch (ParseException e) { - return; - } - if (expression != null && backgroundJob == null) { - AstroDiscoveryLocationChangedTask job = new AstroDiscoveryLocationChangedTask(this, locationProvider); - scheduledExecutor.schedule(job, expression); - logger.info("Scheduled astro location-changed job every {} seconds", LOCATION_CHANGED_CHECK_INTERVAL); - backgroundJob = job; + if (astroDiscoveryJob == null) { + astroDiscoveryJob = scheduler.scheduleAtFixedRate(() -> { + PointType currentLocation = locationProvider.getLocation(); + if ((currentLocation != null) && !currentLocation.equals(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() { - if (backgroundJob != null) { - scheduledExecutor.remove(backgroundJob); - backgroundJob = null; + logger.debug("Stop Astro device background discovery"); + if (astroDiscoveryJob != null && !astroDiscoveryJob.isCancelled()) { + astroDiscoveryJob.cancel(true); + astroDiscoveryJob = null; } } From ef2e5fe0536c20204d9f04f2fd61078ad0a4f4eb Mon Sep 17 00:00:00 2001 From: Stefan Triller Date: Fri, 23 Jun 2017 10:16:23 +0200 Subject: [PATCH 4/6] Missing README changes Signed-off-by: Stefan Triller --- .../binding/org.eclipse.smarthome.binding.astro/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extensions/binding/org.eclipse.smarthome.binding.astro/README.md b/extensions/binding/org.eclipse.smarthome.binding.astro/README.md index ba856250aba..46f7ad1c1b1 100644 --- a/extensions/binding/org.eclipse.smarthome.binding.astro/README.md +++ b/extensions/binding/org.eclipse.smarthome.binding.astro/README.md @@ -10,7 +10,7 @@ This binding supports two Things: Sun and Moon ## Discovery -You can use the auto discovery if you have a location set in "Configuration - System - Regional Settings - Location". This will automatically create a "Local Sun" and a "Local Moon" item for this location. +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. @@ -20,7 +20,7 @@ No binding configuration required. ## Thing Configuration -A thing requires the geolocation (latitude, longitude, (altitude)) 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. The complementary altitude part of the geolocation parameter is optional and sharpens results provided by the Radiation group. From 84bd4c4016b831bb3caa519fb77db50f7d429df9 Mon Sep 17 00:00:00 2001 From: Stefan Triller Date: Fri, 23 Jun 2017 10:38:34 +0200 Subject: [PATCH 5/6] Improve debug logging Signed-off-by: Stefan Triller --- .../binding/astro/discovery/AstroDiscoveryService.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryService.java b/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryService.java index 325635cffca..a89dc2534e6 100644 --- a/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryService.java +++ b/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryService.java @@ -76,10 +76,11 @@ protected void startBackgroundDiscovery() { @Override protected void stopBackgroundDiscovery() { - logger.debug("Stop Astro device background discovery"); + logger.debug("Stopping Astro device background discovery"); if (astroDiscoveryJob != null && !astroDiscoveryJob.isCancelled()) { astroDiscoveryJob.cancel(true); astroDiscoveryJob = null; + logger.debug("Stopped Astro device background discovery"); } } From 36beb068daa25de3ed0d8cad3b9b986b0c5694b0 Mon Sep 17 00:00:00 2001 From: Stefan Triller Date: Fri, 23 Jun 2017 11:53:27 +0200 Subject: [PATCH 6/6] Use Objects.equals, final variables, better debug logs Signed-off-by: Stefan Triller --- .../README.md | 2 +- .../astro/discovery/AstroDiscoveryService.java | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/extensions/binding/org.eclipse.smarthome.binding.astro/README.md b/extensions/binding/org.eclipse.smarthome.binding.astro/README.md index 46f7ad1c1b1..147a1fdac30 100644 --- a/extensions/binding/org.eclipse.smarthome.binding.astro/README.md +++ b/extensions/binding/org.eclipse.smarthome.binding.astro/README.md @@ -190,6 +190,6 @@ then end ``` -## Tipps +## 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. diff --git a/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryService.java b/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryService.java index a89dc2534e6..e8d377ae1f3 100644 --- a/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryService.java +++ b/extensions/binding/org.eclipse.smarthome.binding.astro/src/main/java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryService.java @@ -11,6 +11,7 @@ import java.util.Arrays; import java.util.HashSet; +import java.util.Objects; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; @@ -37,8 +38,8 @@ public class AstroDiscoveryService extends AbstractDiscoveryService { private ScheduledFuture astroDiscoveryJob; private PointType previousLocation; - private static ThingUID SUN_THING = new ThingUID(THING_TYPE_SUN, LOCAL); - private static ThingUID MOON_THING = new ThingUID(THING_TYPE_MOON, LOCAL); + 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. @@ -63,7 +64,7 @@ protected void startBackgroundDiscovery() { if (astroDiscoveryJob == null) { astroDiscoveryJob = scheduler.scheduleAtFixedRate(() -> { PointType currentLocation = locationProvider.getLocation(); - if ((currentLocation != null) && !currentLocation.equals(previousLocation)) { + if (!Objects.equals(currentLocation, previousLocation)) { logger.info("Location has been changed from {} to {}: Creating new Discovery Results", previousLocation, currentLocation); createResults(currentLocation); @@ -78,9 +79,10 @@ protected void startBackgroundDiscovery() { protected void stopBackgroundDiscovery() { logger.debug("Stopping Astro device background discovery"); if (astroDiscoveryJob != null && !astroDiscoveryJob.isCancelled()) { - astroDiscoveryJob.cancel(true); - astroDiscoveryJob = null; - logger.debug("Stopped Astro device background discovery"); + if (astroDiscoveryJob.cancel(true)) { + astroDiscoveryJob = null; + logger.debug("Stopped Astro device background discovery"); + } } } @@ -92,9 +94,9 @@ public void createResults(PointType location) { } else { propGeolocation = String.format("%s,%s", location.getLatitude(), location.getLongitude()); } - thingDiscovered(DiscoveryResultBuilder.create(SUN_THING).withLabel("Local Sun") + thingDiscovered(DiscoveryResultBuilder.create(sunThing).withLabel("Local Sun") .withProperty("geolocation", propGeolocation).build()); - thingDiscovered(DiscoveryResultBuilder.create(MOON_THING).withLabel("Local Moon") + thingDiscovered(DiscoveryResultBuilder.create(moonThing).withLabel("Local Moon") .withProperty("geolocation", propGeolocation).build()); }