This repository has been archived by the owner on May 7, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Lamda expression instead of additional public class
Signed-off-by: Stefan Triller <stefan.triller@telekom.de>
- Loading branch information
1 parent
38cea63
commit 6d0005c
Showing
2 changed files
with
19 additions
and
52 deletions.
There are no files selected for viewing
27 changes: 0 additions & 27 deletions
27
...java/org/eclipse/smarthome/binding/astro/discovery/AstroDiscoveryLocationChangedTask.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
maggu2810
Contributor
|
||
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"); | ||
This comment has been minimized.
Sorry, something went wrong.
amitjoy
Contributor
|
||
if (astroDiscoveryJob != null && !astroDiscoveryJob.isCancelled()) { | ||
astroDiscoveryJob.cancel(true); | ||
astroDiscoveryJob = null; | ||
} | ||
} | ||
|
||
|
Just to be concise, you can also use -
if (!previousLocation.equals(currentLocation))
but this will work if you are certain thatpreviousLocation
is not null.