[astro] Cleanse channel ids redundancies (#20105)

* Cleansing a bit channel ids
* Some more code simplification now that reflection is out of the game

Signed-off-by: Gaël L'hopital <gael@lhopital.org>
This commit is contained in:
Gaël L'hopital
2026-01-22 22:01:15 +01:00
committed by GitHub
parent 4a7cb779bd
commit 333c053769
7 changed files with 83 additions and 128 deletions
@@ -51,12 +51,23 @@ public final class AstroBindingConstants {
// regular channelIds
public static final String CHANNEL_ID_MOON_RISE_START = "rise#start";
public static final String CHANNEL_ID_MOON_RISE_END = "rise#end";
public static final String CHANNEL_ID_MOON_RISE_DURATION = "rise#duration";
public static final String CHANNEL_ID_MOON_SET_START = "set#start";
public static final String CHANNEL_ID_MOON_SET_END = "set#end";
public static final String CHANNEL_ID_MOON_SET_DURATION = "set#duration";
// Common to SUN and MOON
public static final String CHANNEL_ID_POSITION_AZIMUTH = "position#azimuth";
public static final String CHANNEL_ID_POSITION_ELEVATION = "position#elevation";
public static final String CHANNEL_ID_PHASE_NAME = "phase#name";
public static final String CHANNEL_ID_ZODIAC_SIGN = "zodiac#sign";
public static final String CHANNEL_ID_RISE_START = "rise#start";
public static final String CHANNEL_ID_RISE_END = "rise#end";
public static final String CHANNEL_ID_RISE_DURATION = "rise#duration";
public static final String CHANNEL_ID_SET_START = "set#start";
public static final String CHANNEL_ID_SET_END = "set#end";
public static final String CHANNEL_ID_SET_DURATION = "set#duration";
public static final String CHANNEL_ID_ECLIPSE_TOTAL = "eclipse#total";
public static final String CHANNEL_ID_ECLIPSE_TOTAL_ELEVATION = "eclipse#totalElevation";
public static final String CHANNEL_ID_ECLIPSE_PARTIAL = "eclipse#partial";
public static final String CHANNEL_ID_ECLIPSE_PARTIAL_ELEVATION = "eclipse#partialElevation";
// Moon Specific channels
public static final String CHANNEL_ID_MOON_PHASE_FIRST_QUARTER = "phase#firstQuarter";
public static final String CHANNEL_ID_MOON_PHASE_THIRD_QUARTER = "phase#thirdQuarter";
public static final String CHANNEL_ID_MOON_PHASE_FULL = "phase#full";
@@ -65,28 +76,15 @@ public final class AstroBindingConstants {
public static final String CHANNEL_ID_MOON_PHASE_AGE_DEGREE = "phase#ageDegree";
public static final String CHANNEL_ID_MOON_PHASE_AGE_PERCENT = "phase#agePercent";
public static final String CHANNEL_ID_MOON_PHASE_ILLUMINATION = "phase#illumination";
public static final String CHANNEL_ID_MOON_PHASE_NAME = "phase#name";
public static final String CHANNEL_ID_MOON_ECLIPSE_TOTAL = "eclipse#total";
public static final String CHANNEL_ID_MOON_ECLIPSE_TOTAL_ELEVATION = "eclipse#totalElevation";
public static final String CHANNEL_ID_MOON_ECLIPSE_PARTIAL = "eclipse#partial";
public static final String CHANNEL_ID_MOON_ECLIPSE_PARTIAL_ELEVATION = "eclipse#partialElevation";
public static final String CHANNEL_ID_MOON_DISTANCE_DATE = "distance#date";
public static final String CHANNEL_ID_MOON_DISTANCE_DISTANCE = "distance#distance";
public static final String CHANNEL_ID_MOON_PERIGEE_DATE = "perigee#date";
public static final String CHANNEL_ID_MOON_PERIGEE_DISTANCE = "perigee#distance";
public static final String CHANNEL_ID_MOON_APOGEE_DATE = "apogee#date";
public static final String CHANNEL_ID_MOON_APOGEE_DISTANCE = "apogee#distance";
public static final String CHANNEL_ID_MOON_POSITION_AZIMUTH = "position#azimuth";
public static final String CHANNEL_ID_MOON_POSITION_ELEVATION = "position#elevation";
public static final String CHANNEL_ID_MOON_POSITION_SHADE_LENGTH = "position#shadeLength";
public static final String CHANNEL_ID_MOON_ZODIAC_SIGN = "zodiac#sign";
public static final String CHANNEL_ID_SUN_RISE_START = "rise#start";
public static final String CHANNEL_ID_SUN_RISE_END = "rise#end";
public static final String CHANNEL_ID_SUN_RISE_DURATION = "rise#duration";
public static final String CHANNEL_ID_SUN_SET_START = "set#start";
public static final String CHANNEL_ID_SUN_SET_END = "set#end";
public static final String CHANNEL_ID_SUN_SET_DURATION = "set#duration";
// Sun specific channels
public static final String CHANNEL_ID_SUN_NOON_START = "noon#start";
public static final String CHANNEL_ID_SUN_NOON_END = "noon#end";
public static final String CHANNEL_ID_SUN_NOON_DURATION = "noon#duration";
@@ -123,15 +121,12 @@ public final class AstroBindingConstants {
public static final String CHANNEL_ID_SUN_DAYLIGHT_START = "daylight#start";
public static final String CHANNEL_ID_SUN_DAYLIGHT_END = "daylight#end";
public static final String CHANNEL_ID_SUN_DAYLIGHT_DURATION = "daylight#duration";
public static final String CHANNEL_ID_SUN_POSITION_AZIMUTH = "position#azimuth";
public static final String CHANNEL_ID_SUN_POSITION_ELEVATION = "position#elevation";
public static final String CHANNEL_ID_SUN_POSITION_SHADE_LENGTH = "position#shadeLength";
public static final String CHANNEL_ID_SUN_RADIATION_DIRECT = "radiation#direct";
public static final String CHANNEL_ID_SUN_RADIATION_DIFFUSE = "radiation#diffuse";
public static final String CHANNEL_ID_SUN_RADIATION_TOTAL = "radiation#total";
public static final String CHANNEL_ID_SUN_ZODIAC_START = "zodiac#start";
public static final String CHANNEL_ID_SUN_ZODIAC_END = "zodiac#end";
public static final String CHANNEL_ID_SUN_ZODIAC_SIGN = "zodiac#sign";
public static final String CHANNEL_ID_SUN_SEASON_NAME = "season#name";
public static final String CHANNEL_ID_SUN_SEASON_SPRING = "season#spring";
public static final String CHANNEL_ID_SUN_SEASON_SUMMER = "season#summer";
@@ -139,13 +134,8 @@ public final class AstroBindingConstants {
public static final String CHANNEL_ID_SUN_SEASON_WINTER = "season#winter";
public static final String CHANNEL_ID_SUN_SEASON_NEXT_NAME = "season#nextName";
public static final String CHANNEL_ID_SUN_SEASON_TIME_LEFT = "season#timeLeft";
public static final String CHANNEL_ID_SUN_ECLIPSE_TOTAL = "eclipse#total";
public static final String CHANNEL_ID_SUN_ECLIPSE_TOTAL_ELEVATION = "eclipse#totalElevation";
public static final String CHANNEL_ID_SUN_ECLIPSE_PARTIAL = "eclipse#partial";
public static final String CHANNEL_ID_SUN_ECLIPSE_PARTIAL_ELEVATION = "eclipse#partialElevation";
public static final String CHANNEL_ID_SUN_ECLIPSE_RING = "eclipse#ring";
public static final String CHANNEL_ID_SUN_ECLIPSE_RING_ELEVATION = "eclipse#ringElevation";
public static final String CHANNEL_ID_SUN_PHASE_NAME = "phase#name";
public static final String CHANNEL_ID_SUN_CIRCADIAN_BRIGHTNESS = "circadian#brightness";
public static final String CHANNEL_ID_SUN_CIRCADIAN_TEMPERATURE = "circadian#temperature";
@@ -24,7 +24,6 @@ import java.time.Instant;
import java.time.InstantSource;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.GregorianCalendar;
@@ -33,6 +32,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
@@ -162,13 +162,16 @@ public abstract class AstroThingHandler extends BaseThingHandler {
/** The source of the current time */
protected final InstantSource instantSource;
private final Set<String> positionalChannels;
public AstroThingHandler(Thing thing, final CronScheduler scheduler, final TimeZoneProvider timeZoneProvider,
LocaleProvider localeProvider, InstantSource instantSource) {
LocaleProvider localeProvider, InstantSource instantSource, Set<String> positionalChannels) {
super(thing);
this.cronScheduler = scheduler;
this.timeZoneProvider = timeZoneProvider;
this.localeProvider = localeProvider;
this.instantSource = instantSource;
this.positionalChannels = Set.copyOf(positionalChannels);
}
@Override
@@ -343,7 +346,7 @@ public abstract class AstroThingHandler extends BaseThingHandler {
* Counts positional channels and restarts Astro jobs.
*/
private void linkedChannelChange(ChannelUID channelUID) {
if (Arrays.asList(getPositionalChannelIds()).contains(channelUID.getId())) {
if (positionalChannels.contains(channelUID.getId())) {
boolean newValue = isPositionalChannelLinked();
monitor.lock();
try {
@@ -362,7 +365,6 @@ public abstract class AstroThingHandler extends BaseThingHandler {
* Returns {@code true}, if at least one positional channel is linked.
*/
private boolean isPositionalChannelLinked() {
List<String> positionalChannels = Arrays.asList(getPositionalChannelIds());
for (Channel channel : getThing().getChannels()) {
ChannelUID id = channel.getUID();
if (isLinked(id) && positionalChannels.contains(id.getId())) {
@@ -475,11 +477,6 @@ public abstract class AstroThingHandler extends BaseThingHandler {
*/
public abstract @Nullable Planet getPlanet();
/**
* Returns the channelIds for positional calculation (cannot be {@code null})
*/
protected abstract String[] getPositionalChannelIds();
/**
* Returns the daily calculation {@link Job} (cannot be {@code null})
*/
@@ -19,6 +19,7 @@ import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Set;
import java.util.TimeZone;
import org.eclipse.jdt.annotation.NonNullByDefault;
@@ -50,9 +51,10 @@ import org.slf4j.LoggerFactory;
*/
@NonNullByDefault
public class MoonHandler extends AstroThingHandler {
private static final Set<String> POSITIONAL_CHANNELS = Set.of(CHANNEL_ID_PHASE_NAME, CHANNEL_ID_MOON_PHASE_AGE,
CHANNEL_ID_MOON_PHASE_AGE_PERCENT, CHANNEL_ID_MOON_PHASE_AGE_DEGREE, CHANNEL_ID_MOON_PHASE_ILLUMINATION,
CHANNEL_ID_POSITION_AZIMUTH, CHANNEL_ID_POSITION_ELEVATION, CHANNEL_ID_ZODIAC_SIGN);
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;
private final Logger logger = LoggerFactory.getLogger(MoonHandler.class);
private volatile @Nullable Moon moon;
@@ -62,7 +64,7 @@ public class MoonHandler extends AstroThingHandler {
*/
public MoonHandler(Thing thing, final CronScheduler scheduler, final TimeZoneProvider timeZoneProvider,
LocaleProvider localeProvider, InstantSource instantSource) {
super(thing, scheduler, timeZoneProvider, localeProvider, instantSource);
super(thing, scheduler, timeZoneProvider, localeProvider, instantSource, POSITIONAL_CHANNELS);
moonCalc = new MoonCalc(instantSource);
}
@@ -100,17 +102,17 @@ public class MoonHandler extends AstroThingHandler {
return UnDefType.UNDEF;
}
switch (channel.getUID().getId()) {
case CHANNEL_ID_MOON_RISE_START:
case CHANNEL_ID_RISE_START:
return toState(moon.getRise().getStart(), channel);
case CHANNEL_ID_MOON_RISE_END:
case CHANNEL_ID_RISE_END:
return toState(moon.getRise().getEnd(), channel);
case CHANNEL_ID_MOON_RISE_DURATION:
case CHANNEL_ID_RISE_DURATION:
return toState(moon.getRise().getDuration(), channel);
case CHANNEL_ID_MOON_SET_START:
case CHANNEL_ID_SET_START:
return toState(moon.getSet().getStart(), channel);
case CHANNEL_ID_MOON_SET_END:
case CHANNEL_ID_SET_END:
return toState(moon.getSet().getEnd(), channel);
case CHANNEL_ID_MOON_SET_DURATION:
case CHANNEL_ID_SET_DURATION:
return toState(moon.getSet().getDuration(), channel);
case CHANNEL_ID_MOON_PHASE_FIRST_QUARTER:
return toState(moon.getPhase().getFirstQuarter(), channel);
@@ -128,15 +130,15 @@ public class MoonHandler extends AstroThingHandler {
return toState(moon.getPhase().getAgePercent(), channel);
case CHANNEL_ID_MOON_PHASE_ILLUMINATION:
return toState(moon.getPhase().getIllumination(), channel);
case CHANNEL_ID_MOON_PHASE_NAME:
case CHANNEL_ID_PHASE_NAME:
return toState(moon.getPhase().getName(), channel);
case CHANNEL_ID_MOON_ECLIPSE_TOTAL:
case CHANNEL_ID_ECLIPSE_TOTAL:
return toState(moon.getEclipseSet().getDate(EclipseKind.TOTAL), channel);
case CHANNEL_ID_MOON_ECLIPSE_TOTAL_ELEVATION:
case CHANNEL_ID_ECLIPSE_TOTAL_ELEVATION:
return toState(moon.getEclipseSet().getElevation(EclipseKind.TOTAL), channel);
case CHANNEL_ID_MOON_ECLIPSE_PARTIAL:
case CHANNEL_ID_ECLIPSE_PARTIAL:
return toState(moon.getEclipseSet().getDate(EclipseKind.PARTIAL), channel);
case CHANNEL_ID_MOON_ECLIPSE_PARTIAL_ELEVATION:
case CHANNEL_ID_ECLIPSE_PARTIAL_ELEVATION:
return toState(moon.getEclipseSet().getElevation(EclipseKind.PARTIAL), channel);
case CHANNEL_ID_MOON_DISTANCE_DATE:
return toState(moon.getDistanceType(DistanceType.CURRENT).getDate(), channel);
@@ -150,13 +152,13 @@ public class MoonHandler extends AstroThingHandler {
return toState(moon.getDistanceType(DistanceType.APOGEE).getDate(), channel);
case CHANNEL_ID_MOON_APOGEE_DISTANCE:
return toState(moon.getDistanceType(DistanceType.APOGEE).getDistance(), channel);
case CHANNEL_ID_MOON_POSITION_AZIMUTH:
case CHANNEL_ID_POSITION_AZIMUTH:
return toState(moon.getPosition().getAzimuth(), channel);
case CHANNEL_ID_MOON_POSITION_ELEVATION:
case CHANNEL_ID_POSITION_ELEVATION:
return toState(moon.getPosition().getElevation(), channel);
case CHANNEL_ID_MOON_POSITION_SHADE_LENGTH:
return toState(moon.getPosition().getShadeLength(), channel);
case CHANNEL_ID_MOON_ZODIAC_SIGN:
case CHANNEL_ID_ZODIAC_SIGN:
return toState(moon.getZodiac().getSign(), channel);
default:
logger.warn("Unsupported channel: {}", channel.getUID());
@@ -165,11 +167,6 @@ public class MoonHandler extends AstroThingHandler {
return UnDefType.UNDEF;
}
@Override
protected String[] getPositionalChannelIds() {
return positionalChannelIds;
}
@Override
protected Job getDailyJob(TimeZone zone, Locale locale) {
return new DailyJobMoon(this, zone, locale);
@@ -20,6 +20,7 @@ import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Set;
import java.util.TimeZone;
import org.eclipse.jdt.annotation.NonNullByDefault;
@@ -35,6 +36,7 @@ import org.openhab.binding.astro.internal.model.Position;
import org.openhab.binding.astro.internal.model.Radiation;
import org.openhab.binding.astro.internal.model.Range;
import org.openhab.binding.astro.internal.model.Season;
import org.openhab.binding.astro.internal.model.SeasonName;
import org.openhab.binding.astro.internal.model.Sun;
import org.openhab.binding.astro.internal.model.SunPhase;
import org.openhab.binding.astro.internal.util.DateTimeUtils;
@@ -56,9 +58,10 @@ import org.slf4j.LoggerFactory;
*/
@NonNullByDefault
public class SunHandler extends AstroThingHandler {
private static final Set<String> POSITIONAL_CHANNELS = Set.of(CHANNEL_ID_POSITION_AZIMUTH,
CHANNEL_ID_POSITION_ELEVATION, CHANNEL_ID_SUN_RADIATION_DIRECT, CHANNEL_ID_SUN_RADIATION_DIFFUSE,
CHANNEL_ID_SUN_RADIATION_TOTAL);
private final String[] positionalChannelIds = new String[] { "position#azimuth", "position#elevation",
"radiation#direct", "radiation#diffuse", "radiation#total" };
private final SunCalc sunCalc;
private final Logger logger = LoggerFactory.getLogger(SunHandler.class);
volatile @Nullable Sun sun;
@@ -68,7 +71,7 @@ public class SunHandler extends AstroThingHandler {
*/
public SunHandler(Thing thing, final CronScheduler scheduler, final TimeZoneProvider timeZoneProvider,
LocaleProvider localeProvider, InstantSource instantSource) {
super(thing, scheduler, timeZoneProvider, localeProvider, instantSource);
super(thing, scheduler, timeZoneProvider, localeProvider, instantSource, POSITIONAL_CHANNELS);
sunCalc = new SunCalc(instantSource);
}
@@ -114,17 +117,17 @@ public class SunHandler extends AstroThingHandler {
Range r;
Season s;
switch (channel.getUID().getId()) {
case CHANNEL_ID_SUN_RISE_START:
case CHANNEL_ID_RISE_START:
return toState(sun.getRise().getStart(), channel);
case CHANNEL_ID_SUN_RISE_END:
case CHANNEL_ID_RISE_END:
return toState(sun.getRise().getEnd(), channel);
case CHANNEL_ID_SUN_RISE_DURATION:
case CHANNEL_ID_RISE_DURATION:
return toState(sun.getRise().getDuration(), channel);
case CHANNEL_ID_SUN_SET_START:
case CHANNEL_ID_SET_START:
return toState(sun.getSet().getStart(), channel);
case CHANNEL_ID_SUN_SET_END:
case CHANNEL_ID_SET_END:
return toState(sun.getSet().getEnd(), channel);
case CHANNEL_ID_SUN_SET_DURATION:
case CHANNEL_ID_SET_DURATION:
return toState(sun.getSet().getDuration(), channel);
case CHANNEL_ID_SUN_NOON_START:
r = sun.getRange(SunPhase.NOON);
@@ -234,9 +237,9 @@ public class SunHandler extends AstroThingHandler {
case CHANNEL_ID_SUN_DAYLIGHT_DURATION:
r = sun.getRange(SunPhase.DAYLIGHT);
return r == null ? UnDefType.UNDEF : toState(r.getDuration(), channel);
case CHANNEL_ID_SUN_POSITION_AZIMUTH:
case CHANNEL_ID_POSITION_AZIMUTH:
return toState(sun.getPosition().getAzimuth(), channel);
case CHANNEL_ID_SUN_POSITION_ELEVATION:
case CHANNEL_ID_POSITION_ELEVATION:
return toState(sun.getPosition().getElevation(), channel);
case CHANNEL_ID_SUN_POSITION_SHADE_LENGTH:
return toState(sun.getPosition().getShadeLength(), channel);
@@ -250,42 +253,42 @@ public class SunHandler extends AstroThingHandler {
return toState(sun.getZodiac().getStart(), channel);
case CHANNEL_ID_SUN_ZODIAC_END:
return toState(sun.getZodiac().getEnd(), channel);
case CHANNEL_ID_SUN_ZODIAC_SIGN:
case CHANNEL_ID_ZODIAC_SIGN:
return toState(sun.getZodiac().getSign(), channel);
case CHANNEL_ID_SUN_SEASON_NAME:
s = sun.getSeason();
return s == null ? UnDefType.UNDEF : toState(s.getName(), channel);
case CHANNEL_ID_SUN_SEASON_SPRING:
s = sun.getSeason();
return s == null ? UnDefType.UNDEF : toState(s.getSpring(), channel);
return s == null ? UnDefType.UNDEF : toState(s.getSeasonStart(SeasonName.SPRING), channel);
case CHANNEL_ID_SUN_SEASON_SUMMER:
s = sun.getSeason();
return s == null ? UnDefType.UNDEF : toState(s.getSummer(), channel);
return s == null ? UnDefType.UNDEF : toState(s.getSeasonStart(SeasonName.SUMMER), channel);
case CHANNEL_ID_SUN_SEASON_AUTUMN:
s = sun.getSeason();
return s == null ? UnDefType.UNDEF : toState(s.getAutumn(), channel);
return s == null ? UnDefType.UNDEF : toState(s.getSeasonStart(SeasonName.AUTUMN), channel);
case CHANNEL_ID_SUN_SEASON_WINTER:
s = sun.getSeason();
return s == null ? UnDefType.UNDEF : toState(s.getWinter(), channel);
return s == null ? UnDefType.UNDEF : toState(s.getSeasonStart(SeasonName.WINTER), channel);
case CHANNEL_ID_SUN_SEASON_NEXT_NAME:
s = sun.getSeason();
return s == null ? UnDefType.UNDEF : toState(s.getNextName(), channel);
case CHANNEL_ID_SUN_SEASON_TIME_LEFT:
s = sun.getSeason();
return s == null ? UnDefType.UNDEF : toState(s.getTimeLeft(), channel);
case CHANNEL_ID_SUN_ECLIPSE_TOTAL:
case CHANNEL_ID_ECLIPSE_TOTAL:
return toState(sun.getEclipseSet().getDate(EclipseKind.TOTAL), channel);
case CHANNEL_ID_SUN_ECLIPSE_TOTAL_ELEVATION:
case CHANNEL_ID_ECLIPSE_TOTAL_ELEVATION:
return toState(sun.getEclipseSet().getElevation(EclipseKind.TOTAL), channel);
case CHANNEL_ID_SUN_ECLIPSE_PARTIAL:
case CHANNEL_ID_ECLIPSE_PARTIAL:
return toState(sun.getEclipseSet().getDate(EclipseKind.PARTIAL), channel);
case CHANNEL_ID_SUN_ECLIPSE_PARTIAL_ELEVATION:
case CHANNEL_ID_ECLIPSE_PARTIAL_ELEVATION:
return toState(sun.getEclipseSet().getElevation(EclipseKind.PARTIAL), channel);
case CHANNEL_ID_SUN_ECLIPSE_RING:
return toState(sun.getEclipseSet().getDate(EclipseKind.RING), channel);
case CHANNEL_ID_SUN_ECLIPSE_RING_ELEVATION:
return toState(sun.getEclipseSet().getElevation(EclipseKind.RING), channel);
case CHANNEL_ID_SUN_PHASE_NAME:
case CHANNEL_ID_PHASE_NAME:
return toState(sun.getSunPhase(), channel);
case CHANNEL_ID_SUN_CIRCADIAN_BRIGHTNESS:
return toState(sun.getCircadian().getBrightness(), channel);
@@ -298,11 +301,6 @@ public class SunHandler extends AstroThingHandler {
return UnDefType.UNDEF;
}
@Override
protected String[] getPositionalChannelIds() {
return positionalChannelIds;
}
@Override
protected Job getDailyJob(TimeZone zone, Locale locale) {
return new DailyJobSun(this, zone, locale, instantSource);
@@ -12,7 +12,7 @@
*/
package org.openhab.binding.astro.internal.job;
import static org.openhab.binding.astro.internal.AstroBindingConstants.CHANNEL_ID_SUN_PHASE_NAME;
import static org.openhab.binding.astro.internal.AstroBindingConstants.CHANNEL_ID_PHASE_NAME;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.astro.internal.handler.AstroThingHandler;
@@ -47,7 +47,7 @@ public final class SunPhaseJob extends AbstractJob {
@Override
public void run() {
try {
Channel phaseNameChannel = handler.getThing().getChannel(CHANNEL_ID_SUN_PHASE_NAME);
Channel phaseNameChannel = handler.getThing().getChannel(CHANNEL_ID_PHASE_NAME);
if (phaseNameChannel != null) {
if (handler.getPlanet() instanceof Sun theSun) {
theSun.setSunPhase(sunPhase);
@@ -78,39 +78,11 @@ public class Season {
return year;
}
private Instant getSeasonStart(SeasonName season) {
public Instant getSeasonStart(SeasonName season) {
return seasons.stream().filter(s -> s.name.equals(season) && s.year == year).map(s -> s.startsOn).findFirst()
.orElseThrow(NoSuchElementException::new);
}
/**
* Returns the date of the beginning of spring.
*/
public Instant getSpring() {
return getSeasonStart(SeasonName.SPRING);
}
/**
* Returns the date of the beginning of summer.
*/
public Instant getSummer() {
return getSeasonStart(SeasonName.SUMMER);
}
/**
* Returns the date of the beginning of autumn.
*/
public Instant getAutumn() {
return getSeasonStart(SeasonName.AUTUMN);
}
/**
* Returns the date of the beginning of winter.
*/
public Instant getWinter() {
return getSeasonStart(SeasonName.WINTER);
}
private LocalSeason getSeason(Instant when) {
return seasons.stream().filter(s -> s.contains(when)).findFirst().orElseThrow(NoSuchElementException::new);
}
@@ -27,6 +27,7 @@ import org.eclipse.jdt.annotation.Nullable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.binding.astro.internal.model.Season;
import org.openhab.binding.astro.internal.model.SeasonName;
import org.openhab.binding.astro.internal.model.Sun;
import org.openhab.binding.astro.internal.util.DateTimeUtils;
@@ -116,11 +117,11 @@ public class SeasonCalcTest {
var season = meteoSun.getSeason();
assertNotNull(season);
ZonedDateTime cal = season.getSpring().atZone(AMSTERDAM_TZ.toZoneId());
ZonedDateTime cal = season.getSeasonStart(SeasonName.SPRING).atZone(AMSTERDAM_TZ.toZoneId());
assertNotNull(cal);
var season2 = equiSun.getSeason();
assertNotNull(season2);
ZonedDateTime cal2 = season2.getSpring().atZone(AMSTERDAM_TZ.toZoneId());
ZonedDateTime cal2 = season2.getSeasonStart(SeasonName.SPRING).atZone(AMSTERDAM_TZ.toZoneId());
assertNotNull(cal2);
assertEquals(cal.getMonth(), cal2.getMonth());
assertEquals(cal.getYear(), cal2.getYear());
@@ -132,21 +133,21 @@ public class SeasonCalcTest {
public void testGetSeasonAmsterdam() {
final Season season = SeasonCalc.calculate(2020, AMSTERDAM_LATITUDE, true, AMSTERDAM_TZ, INSTANT_SOURCE);
assertNextSeason(season.getSpring(), 2020, JAN_20_2020, season, AMSTERDAM_TZ);
assertNextSeason(season.getSummer(), 2020, MAY_20_2020, season, AMSTERDAM_TZ);
assertNextSeason(season.getWinter(), 2020, SEPT_20_2020, season, AMSTERDAM_TZ);
assertNextSeason(SeasonCalc.calculate(2021, AMSTERDAM_LATITUDE, true, AMSTERDAM_TZ, INSTANT_SOURCE).getSpring(),
2021, DEC_10_2020, season, AMSTERDAM_TZ);
assertNextSeason(season.getSeasonStart(SeasonName.SPRING), 2020, JAN_20_2020, season, AMSTERDAM_TZ);
assertNextSeason(season.getSeasonStart(SeasonName.SUMMER), 2020, MAY_20_2020, season, AMSTERDAM_TZ);
assertNextSeason(season.getSeasonStart(SeasonName.WINTER), 2020, SEPT_20_2020, season, AMSTERDAM_TZ);
assertNextSeason(SeasonCalc.calculate(2021, AMSTERDAM_LATITUDE, true, AMSTERDAM_TZ, INSTANT_SOURCE)
.getSeasonStart(SeasonName.SPRING), 2021, DEC_10_2020, season, AMSTERDAM_TZ);
}
@Test
public void testGetSeasonSydney() {
final Season season = SeasonCalc.calculate(2020, SYDNEY_LATITUDE, true, SYDNEY_TZ, INSTANT_SOURCE);
assertNextSeason(season.getAutumn(), 2020, JAN_20_2020, season, SYDNEY_TZ);
assertNextSeason(season.getWinter(), 2020, MAY_20_2020, season, SYDNEY_TZ);
assertNextSeason(season.getSummer(), 2020, SEPT_20_2020, season, SYDNEY_TZ);
assertNextSeason(SeasonCalc.calculate(2021, SYDNEY_LATITUDE, true, SYDNEY_TZ, INSTANT_SOURCE).getAutumn(), 2021,
DEC_10_2020, season, SYDNEY_TZ);
assertNextSeason(season.getSeasonStart(SeasonName.AUTUMN), 2020, JAN_20_2020, season, SYDNEY_TZ);
assertNextSeason(season.getSeasonStart(SeasonName.WINTER), 2020, MAY_20_2020, season, SYDNEY_TZ);
assertNextSeason(season.getSeasonStart(SeasonName.SUMMER), 2020, SEPT_20_2020, season, SYDNEY_TZ);
assertNextSeason(SeasonCalc.calculate(2021, SYDNEY_LATITUDE, true, SYDNEY_TZ, INSTANT_SOURCE)
.getSeasonStart(SeasonName.AUTUMN), 2021, DEC_10_2020, season, SYDNEY_TZ);
}
}