Skip to content

Commit

Permalink
[astro] Fix returning wrong sun phase name (openhab#14078)
Browse files Browse the repository at this point in the history
* Add tests and fix very minor bug
* Correct wrong test
* Update tests and fix sorting
* Some checkstyle improvements

Signed-off-by: lsiepel <leosiepel@gmail.com>
  • Loading branch information
lsiepel authored and borazslo committed Jan 8, 2023
1 parent e7f2625 commit 4ca7cad
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
*/
package org.openhab.binding.astro.internal.calc;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.openhab.binding.astro.internal.model.Eclipse;
Expand Down Expand Up @@ -251,9 +257,9 @@ private Sun getSunInfo(Calendar calendar, double latitude, double longitude, Dou
sun.setSeason(seasonCalc.getSeason(calendar, latitude, useMeteorologicalSeason));

// phase
for (Entry<SunPhaseName, Range> rangeEntry : sun.getAllRanges().entrySet()) {
for (Entry<SunPhaseName, Range> rangeEntry : sortByValue(sun.getAllRanges()).entrySet()) {
SunPhaseName entryPhase = rangeEntry.getKey();
if (rangeEntry.getValue().matches(Calendar.getInstance())) {
if (rangeEntry.getValue().matches(calendar)) {
if (entryPhase == SunPhaseName.MORNING_NIGHT || entryPhase == SunPhaseName.EVENING_NIGHT) {
sun.getPhase().setName(SunPhaseName.NIGHT);
} else {
Expand Down Expand Up @@ -336,4 +342,24 @@ private double getSunsetJulianDate(double w0, double m, double Lsun, double lw,
private double getSunriseJulianDate(double jtransit, double jset) {
return jtransit - (jset - jtransit);
}

public static Map<SunPhaseName, Range> sortByValue(Map<SunPhaseName, Range> map) {
List<Entry<SunPhaseName, Range>> list = new ArrayList<>(map.entrySet());

Collections.sort(list, new Comparator<Entry<SunPhaseName, Range>>() {
@Override
public int compare(Entry<SunPhaseName, Range> p1, Entry<SunPhaseName, Range> p2) {
Range p1Range = p1.getValue();
Range p2Range = p2.getValue();
return p1Range.compareTo(p2Range);
}
});

Map<SunPhaseName, Range> result = new LinkedHashMap<>();
for (Entry<SunPhaseName, Range> entry : list) {
result.put(entry.getKey(), entry.getValue());
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public class SunZodiacCalc {
* Returns the zodiac for the specified calendar.
*/
public Optional<SunZodiac> getZodiac(Calendar calendar) {

int year = calendar.get(Calendar.YEAR);
List<SunZodiac> zodiacs;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ public static void scheduleEvent(String thingUID, AstroThingHandler astroHandler
*/
public static void scheduleEvent(String thingUID, AstroThingHandler astroHandler, Calendar eventAt,
List<String> events, String channelId, boolean configAlreadyApplied) {

if (events.isEmpty()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class MoonPhase {
private Calendar firstQuarter;
private Calendar full;
private Calendar thirdQuarter;
private Calendar _new;
private Calendar newCalendar;
private double age;
private double illumination;
private double agePercent;
Expand Down Expand Up @@ -85,14 +85,14 @@ public void setThirdQuarter(Calendar thirdQuarter) {
* Returns the date of the new moon.
*/
public Calendar getNew() {
return _new;
return newCalendar;
}

/**
* Sets the date of the new moon.
*/
public void setNew(Calendar _new) {
this._new = _new;
public void setNew(Calendar newCalendar) {
this.newCalendar = newCalendar;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static org.openhab.core.library.unit.MetricPrefix.MILLI;

import java.util.Calendar;
import java.util.Comparator;

import javax.measure.quantity.Time;

Expand Down Expand Up @@ -81,4 +82,13 @@ public boolean matches(Calendar cal) {
long matchEnd = end != null ? end.getTimeInMillis() : DateTimeUtils.endOfDayDate(cal).getTimeInMillis();
return cal.getTimeInMillis() >= matchStart && cal.getTimeInMillis() < matchEnd;
}

private static Comparator<Calendar> nullSafeCalendarComparator = Comparator.nullsFirst(Calendar::compareTo);

private static Comparator<Range> rangeComparator = Comparator.comparing(Range::getStart, nullSafeCalendarComparator)
.thenComparing(Range::getEnd, nullSafeCalendarComparator);

public int compareTo(Range that) {
return rangeComparator.compare(this, that);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public void testGetSunInfoForOldDate() {
assertNotNull(sun.getMorningNight());
assertNotNull(sun.getEveningNight());

// for an old date the phase is always null
assertNull(sun.getPhase().getName());
// for an old date the phase should also be calculated
assertNotNull(sun.getPhase().getName());
}

@Test
Expand Down Expand Up @@ -274,6 +274,34 @@ public void testRangesForCoherenceBetweenAstroDuskEndAndEveningNightStart() {
sun.getAllRanges().get(SunPhaseName.EVENING_NIGHT).getStart());
}

@Test
public void testIssue7642CivilDawnEnd() {
TimeZone tZone = TimeZone.getTimeZone("Europe/London");
Calendar tDate = SunCalcTest.newCalendar(2020, Calendar.MAY, 13, 5, 12, tZone);

Sun sun = sunCalc.getSunInfo(tDate, 53.524695, -2.4, 0.0, true);
assertEquals(SunPhaseName.CIVIL_DAWN, sun.getPhase().getName());
}

@Test
public void testIssue7642SunRiseStart() {
// SunCalc.ranges was not sorted, causing unexpected output in corner cases.
TimeZone tZone = TimeZone.getTimeZone("Europe/London");
Calendar tDate = SunCalcTest.newCalendar(2020, Calendar.MAY, 13, 5, 13, tZone);

Sun sun = sunCalc.getSunInfo(tDate, 53.524695, -2.4, 0.0, true);
assertEquals(SunPhaseName.SUN_RISE, sun.getPhase().getName());
}

@Test
public void testIssue7642DaylightStart() {
TimeZone tZone = TimeZone.getTimeZone("Europe/London");
Calendar tDate = SunCalcTest.newCalendar(2020, Calendar.MAY, 13, 5, 18, tZone);

Sun sun = sunCalc.getSunInfo(tDate, 53.524695, -2.4, 0.0, true);
assertEquals(SunPhaseName.DAYLIGHT, sun.getPhase().getName());
}

/***
* Constructs a <code>GregorianCalendar</code> with the given date and time set
* for the provided time zone.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class SunTest {
private Sun sun;
private AstroChannelConfig config;

private static ZoneId ZONE = ZoneId.systemDefault();
private static final ZoneId ZONE = ZoneId.systemDefault();

@BeforeEach
public void init() {
Expand Down

0 comments on commit 4ca7cad

Please sign in to comment.