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

Astro: Sun phase calculation fix #4158

Merged
merged 3 commits into from Aug 30, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,10 @@ 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();
}
return false;
long matchStart = start != null ? start.getTimeInMillis()
: DateTimeUtils.truncateToMidnight(cal).getTimeInMillis();
long matchEnd = end != null ? end.getTimeInMillis() : DateTimeUtils.truncateToMidnight(cal).getTimeInMillis();
return cal.getTimeInMillis() >= matchStart && cal.getTimeInMillis() < matchEnd;
Copy link
Contributor

@sjsf sjsf Aug 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If no start/end are given, then both conditions are comparing against DateTimeUtils.truncateToMidnight(cal). How can this ever be true? Doesn't matchEnd needs 24h added in this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Range start/end are always for the current day. If no start/end are given (e.g night in northern hemisphere), there is nothing to match and should return false. So it's OK.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I see. Thanks for the clarification!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, wait, I still don't fully get it - can it happen that only one is given? I.e. if there is only a start, then it would also return false (because the second part is using the previous midnight), even if cal is after matchStart.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right! end must be truncated to endOfDay and not to midnight. Thanks!!!

}

@Override
Expand Down