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

Commit

Permalink
Use Lamda expression instead of additional public class
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Triller <stefan.triller@telekom.de>
  • Loading branch information
triller-telekom committed Jun 23, 2017
1 parent 38cea63 commit 6d0005c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 52 deletions.

This file was deleted.

Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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.

Copy link
@amitjoy

amitjoy Jun 23, 2017

Contributor

Just to be concise, you can also use - if (!previousLocation.equals(currentLocation)) but this will work if you are certain that previousLocation is not null.

This comment has been minimized.

Copy link
@maggu2810

maggu2810 Jun 23, 2017

Contributor

What about

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() {
if (backgroundJob != null) {
scheduledExecutor.remove(backgroundJob);
backgroundJob = null;
logger.debug("Stop Astro device background discovery");

This comment has been minimized.

Copy link
@amitjoy

amitjoy Jun 23, 2017

Contributor

For better logging, you can add - logger.debug("Stopping Astro device background discovery..."); and add logger.debug("Stopped Astro device background discovery"); after you cancel it.

if (astroDiscoveryJob != null && !astroDiscoveryJob.isCancelled()) {
astroDiscoveryJob.cancel(true);
astroDiscoveryJob = null;
}
}

Expand Down

0 comments on commit 6d0005c

Please sign in to comment.