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

Commit

Permalink
Astro: Sun phase calculation fix (#4158)
Browse files Browse the repository at this point in the history
* Fixed sun phase calculation
* Removed noon from sun phase, because it is not a real phase
* Fixed range matching

Signed-off-by: Gerhard Riegler <gerhard.riegler@gmail.com>
  • Loading branch information
gerrieg authored and sjsf committed Aug 30, 2017
1 parent abb299d commit 0d932b7
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
Expand Up @@ -51,7 +51,7 @@ Optionally, a refresh `interval` (in seconds) can be defined to also calculate p
* `total, partial, ring` (DateTime)
* **group** `phase`
* **channel**
* `name` (String), values: `SUN_RISE, ASTRO_DAWN, NAUTIC_DAWN, CIVIL_DAWN, CIVIL_DUSK, NAUTIC_DUSK, ASTRO_DUSK, SUN_SET, DAYLIGHT, NOON, NIGHT`
* `name` (String), values: `SUN_RISE, ASTRO_DAWN, NAUTIC_DAWN, CIVIL_DAWN, CIVIL_DUSK, NAUTIC_DUSK, ASTRO_DUSK, SUN_SET, DAYLIGHT, NIGHT`
* **thing** `moon`
* **group** `rise, set`
* **channel**
Expand Down
Expand Up @@ -198,7 +198,7 @@ private Sun getSunInfo(Calendar calendar, double latitude, double longitude, Dou
if (sunYesterday.getAstroDusk().getEnd() != null
&& DateUtils.isSameDay(sunYesterday.getAstroDusk().getEnd(), calendar)) {
morningNightRange = new Range(sunYesterday.getAstroDusk().getEnd(), sun.getAstroDawn().getStart());
} else if (isSunUpAllDay) {
} else if (isSunUpAllDay || sun.getAstroDawn().getStart() == null) {
morningNightRange = new Range();
} else {
morningNightRange = new Range(DateTimeUtils.truncateToMidnight(calendar), sun.getAstroDawn().getStart());
Expand Down Expand Up @@ -243,8 +243,10 @@ private Sun getSunInfo(Calendar calendar, double latitude, double longitude, Dou
// phase
for (Entry<SunPhaseName, Range> rangeEntry : sun.getAllRanges().entrySet()) {
SunPhaseName entryPhase = rangeEntry.getKey();
if (entryPhase != SunPhaseName.MORNING_NIGHT && entryPhase != SunPhaseName.EVENING_NIGHT) {
if (rangeEntry.getValue().matches(Calendar.getInstance())) {
if (rangeEntry.getValue().matches(Calendar.getInstance())) {
if (entryPhase == SunPhaseName.MORNING_NIGHT || entryPhase == SunPhaseName.EVENING_NIGHT) {
sun.getPhase().setName(SunPhaseName.NIGHT);
} else {
sun.getPhase().setName(entryPhase);
}
}
Expand Down
Expand Up @@ -78,7 +78,6 @@ public void run() {
// schedule phase jobs
scheduleSunPhase(thingUID, handler, SUN_RISE, sun.getRise().getStart());
scheduleSunPhase(thingUID, handler, SUN_SET, sun.getSet().getStart());
scheduleSunPhase(thingUID, handler, NOON, sun.getNoon().getStart());
scheduleSunPhase(thingUID, handler, NIGHT, sun.getNight().getStart());
scheduleSunPhase(thingUID, handler, DAYLIGHT, sun.getDaylight().getStart());
scheduleSunPhase(thingUID, handler, ASTRO_DAWN, sun.getAstroDawn().getStart());
Expand Down
Expand Up @@ -62,10 +62,13 @@ public long getDuration() {
* Returns true, if the given calendar matches into the range.
*/
public boolean matches(Calendar cal) {
if (start != null && end != null) {
return cal.getTimeInMillis() >= start.getTimeInMillis() && cal.getTimeInMillis() < end.getTimeInMillis();
if (start == null && end == null) {
return false;
}
return false;
long matchStart = start != null ? start.getTimeInMillis()
: DateTimeUtils.truncateToMidnight(cal).getTimeInMillis();
long matchEnd = end != null ? end.getTimeInMillis() : DateTimeUtils.endOfDayDate(cal).getTimeInMillis();
return cal.getTimeInMillis() >= matchStart && cal.getTimeInMillis() < matchEnd;
}

@Override
Expand Down
Expand Up @@ -29,7 +29,7 @@ public class DateTimeUtils {

public static final double J1970 = 2440588.0;
public static final double MILLISECONDS_PER_DAY = 1000 * 60 * 60 * 24;

/** Constructor */
private DateTimeUtils() {
throw new IllegalAccessError("Non-instantiable");
Expand Down Expand Up @@ -93,13 +93,20 @@ public static double midnightDateToJulianDate(Calendar calendar) {
}

/**
* Returns the end of day julian date from the calendar object.
* Returns the end of day from the calendar object.
*/
public static double endOfDayDateToJulianDate(Calendar calendar) {
public static Calendar endOfDayDate(Calendar calendar) {
Calendar cal = (Calendar) calendar.clone();
cal = DateUtils.ceiling(cal, Calendar.DATE);
cal.add(Calendar.MILLISECOND, -1);
return dateToJulianDate(cal);
return cal;
}

/**
* Returns the end of day julian date from the calendar object.
*/
public static double endOfDayDateToJulianDate(Calendar calendar) {
return dateToJulianDate(endOfDayDate(calendar));
}

/**
Expand Down

0 comments on commit 0d932b7

Please sign in to comment.