Skip to content

Commit

Permalink
[astro] Add null annotations on main classes (openhab#7852)
Browse files Browse the repository at this point in the history
Config classes simplified

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
Signed-off-by: Daan Meijer <daan@studioseptember.nl>
  • Loading branch information
lolodomo authored and DaanMeijer committed Sep 1, 2020
1 parent c3c3593 commit 29039a7
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,56 +12,17 @@
*/
package org.openhab.binding.astro.internal.config;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

/**
* Channel configuration from Eclipse SmartHome.
*
* @author Gerhard Riegler - Initial contribution
*/

@NonNullByDefault
public class AstroChannelConfig {
private Integer offset;
private String earliest;
private String latest;

/**
* Returns the offset.
*/
public Integer getOffset() {
return offset;
}

/**
* Sets the offset.
*/
public void setOffset(Integer offset) {
this.offset = offset;
}

/**
* Returns the earliest time.
*/
public String getEarliest() {
return earliest;
}

/**
* Sets the earliest time.
*/
public void setEarliest(String earliest) {
this.earliest = earliest;
}

/**
* Returns the latest time.
*/
public String getLatest() {
return latest;
}

/**
* Sets the latest time.
*/
public void setLatest(String latest) {
this.latest = latest;
}
public int offset = 0;
public @Nullable String earliest;
public @Nullable String latest;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,22 @@
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

/**
* Thing configuration from Eclipse SmartHome.
*
* @author Gerhard Riegler - Initial contribution
*/
@NonNullByDefault
public class AstroThingConfig {
private String geolocation;
private Double altitude;
private Double latitude;
private Double longitude;
private Integer interval;
private String thingUid;
public @Nullable String geolocation;
public @Nullable Double altitude;
public @Nullable Double latitude;
public @Nullable Double longitude;
public int interval = 300;
private @Nullable String thingUid;

/**
* Splits the geolocation into latitude and longitude.
Expand All @@ -48,56 +51,14 @@ public void parseGeoLocation() {
}
}

private Double toDouble(String value) {
private @Nullable Double toDouble(String value) {
try {
return Double.parseDouble(StringUtils.trimToNull(value));
} catch (NumberFormatException ex) {
return null;
}
}

/**
* Returns the geolocation.
*/
public String getGeolocation() {
return geolocation;
}

/**
* Returns the latitude.
*/
public Double getLatitude() {
return latitude;
}

/**
* Returns the longitude.
*/
public Double getLongitude() {
return longitude;
}

/**
* Returns the longitude.
*/
public Double getAltitude() {
return altitude;
}

/**
* Returns the interval.
*/
public Integer getInterval() {
return interval;
}

/**
* Returns the thing uid as string.
*/
public String getThingUid() {
return thingUid;
}

/**
* Sets the thing uid as string.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.scheduler.CronScheduler;
import org.eclipse.smarthome.core.scheduler.ScheduledCompletableFuture;
import org.eclipse.smarthome.core.thing.Channel;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingStatusDetail;
import org.eclipse.smarthome.core.thing.binding.BaseThingHandler;
import org.eclipse.smarthome.core.types.Command;
import org.openhab.binding.astro.internal.config.AstroChannelConfig;
Expand All @@ -52,6 +55,7 @@
* @author Gerhard Riegler - Initial contribution
* @author Amit Kumar Mondal - Implementation to be compliant with ESH Scheduler
*/
@NonNullByDefault
public abstract class AstroThingHandler extends BaseThingHandler {

private static final String DAILY_MIDNIGHT = "30 0 0 * * ? *";
Expand All @@ -62,13 +66,16 @@ public abstract class AstroThingHandler extends BaseThingHandler {
/** Scheduler to schedule jobs */
private final CronScheduler cronScheduler;

private int linkedPositionalChannels = 0;
protected AstroThingConfig thingConfig;
private final Lock monitor = new ReentrantLock();

private ScheduledCompletableFuture<?> dailyJob;
private final Set<ScheduledFuture<?>> scheduledFutures = new HashSet<>();

private int linkedPositionalChannels = 0;

protected AstroThingConfig thingConfig = new AstroThingConfig();

private @Nullable ScheduledCompletableFuture<?> dailyJob;

public AstroThingHandler(Thing thing, CronScheduler scheduler) {
super(thing);
this.cronScheduler = scheduler;
Expand All @@ -82,21 +89,21 @@ public void initialize() {
thingConfig.setThingUid(thingUid);
boolean validConfig = true;

if (StringUtils.trimToNull(thingConfig.getGeolocation()) == null) {
if (StringUtils.trimToNull(thingConfig.geolocation) == null) {
logger.error("Astro parameter geolocation is mandatory and must be configured, disabling thing '{}'",
thingUid);
validConfig = false;
} else {
thingConfig.parseGeoLocation();
}

if (thingConfig.getLatitude() == null || thingConfig.getLongitude() == null) {
if (thingConfig.latitude == null || thingConfig.longitude == null) {
logger.error(
"Astro parameters geolocation could not be split into latitude and longitude, disabling thing '{}'",
thingUid);
validConfig = false;
}
if (thingConfig.getInterval() == null || thingConfig.getInterval() < 1 || thingConfig.getInterval() > 86400) {
if (thingConfig.interval < 1 || thingConfig.interval > 86400) {
logger.error("Astro parameter interval must be in the range of 1-86400, disabling thing '{}'", thingUid);
validConfig = false;
}
Expand All @@ -106,7 +113,7 @@ public void initialize() {
updateStatus(ONLINE);
restartJobs();
} else {
updateStatus(OFFLINE);
updateStatus(OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
}
logger.debug("Thing {} initialized {}", getThing().getUID(), getThing().getStatus());
}
Expand All @@ -132,7 +139,11 @@ public void handleCommand(ChannelUID channelUID, Command command) {
* Iterates all channels of the thing and updates their states.
*/
public void publishPlanet() {
logger.debug("Publishing planet {} for thing {}", getPlanet().getClass().getSimpleName(), getThing().getUID());
Planet planet = getPlanet();
if (planet == null) {
return;
}
logger.debug("Publishing planet {} for thing {}", planet.getClass().getSimpleName(), getThing().getUID());
for (Channel channel : getThing().getChannels()) {
if (channel.getKind() != TRIGGER) {
publishChannelIfLinked(channel.getUID());
Expand Down Expand Up @@ -170,10 +181,6 @@ private void restartJobs() {
stopJobs();
if (getThing().getStatus() == ONLINE) {
String thingUID = getThing().getUID().toString();
if (cronScheduler == null) {
logger.warn("Thread Pool Executor is not available");
return;
}
// Daily Job
Job runnable = getDailyJob();
dailyJob = cronScheduler.schedule(runnable, DAILY_MIDNIGHT);
Expand All @@ -185,10 +192,10 @@ private void restartJobs() {
// Use scheduleAtFixedRate to avoid time drift associated with scheduleWithFixedDelay
if (isPositionalChannelLinked()) {
Job positionalJob = new PositionalJob(thingUID);
ScheduledFuture<?> future = scheduler.scheduleAtFixedRate(positionalJob, 0,
thingConfig.getInterval(), TimeUnit.SECONDS);
ScheduledFuture<?> future = scheduler.scheduleAtFixedRate(positionalJob, 0, thingConfig.interval,
TimeUnit.SECONDS);
scheduledFutures.add(future);
logger.info("Scheduled {} every {} seconds", positionalJob, thingConfig.getInterval());
logger.info("Scheduled {} every {} seconds", positionalJob, thingConfig.interval);
}
}
} finally {
Expand All @@ -203,12 +210,11 @@ private void stopJobs() {
logger.debug("Stopping scheduled jobs for thing {}", getThing().getUID());
monitor.lock();
try {
if (cronScheduler != null) {
if (dailyJob != null) {
dailyJob.cancel(true);
}
dailyJob = null;
ScheduledCompletableFuture<?> job = dailyJob;
if (job != null) {
job.cancel(true);
}
dailyJob = null;
for (ScheduledFuture<?> future : scheduledFutures) {
if (!future.isDone()) {
future.cancel(true);
Expand Down Expand Up @@ -316,7 +322,7 @@ private void tidyScheduledFutures() {
/**
* Returns the {@link Planet} instance (cannot be {@code null})
*/
public abstract Planet getPlanet();
public abstract @Nullable Planet getPlanet();

/**
* Returns the channelIds for positional calculation (cannot be {@code null})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.HashSet;
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.scheduler.CronScheduler;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
Expand All @@ -34,14 +36,15 @@
* @author Gerhard Riegler - Initial contribution
* @author Amit Kumar Mondal - Implementation to be compliant with ESH Scheduler
*/
@NonNullByDefault
public class MoonHandler extends AstroThingHandler {

public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = new HashSet<>(Arrays.asList(THING_TYPE_MOON));

private final String[] positionalChannelIds = new String[] { "phase#name", "phase#age", "phase#agePercent",
"phase#ageDegree", "phase#illumination", "position#azimuth", "position#elevation", "zodiac#sign" };
private final MoonCalc moonCalc = new MoonCalc();
private Moon moon;
private @Nullable Moon moon;

/**
* Constructor
Expand All @@ -59,12 +62,15 @@ public void publishDailyInfo() {
@Override
public void publishPositionalInfo() {
initializeMoon();
moonCalc.setPositionalInfo(Calendar.getInstance(), thingConfig.getLatitude(), thingConfig.getLongitude(), moon);
Double latitude = thingConfig.latitude;
Double longitude = thingConfig.longitude;
moonCalc.setPositionalInfo(Calendar.getInstance(), latitude != null ? latitude : 0,
longitude != null ? longitude : 0, moon);
publishPlanet();
}

@Override
public Planet getPlanet() {
public @Nullable Planet getPlanet() {
return moon;
}

Expand All @@ -85,6 +91,9 @@ protected Job getDailyJob() {
}

private void initializeMoon() {
moon = moonCalc.getMoonInfo(Calendar.getInstance(), thingConfig.getLatitude(), thingConfig.getLongitude());
Double latitude = thingConfig.latitude;
Double longitude = thingConfig.longitude;
moon = moonCalc.getMoonInfo(Calendar.getInstance(), latitude != null ? latitude : 0,
longitude != null ? longitude : 0);
}
}
Loading

0 comments on commit 29039a7

Please sign in to comment.