public static final String BINDING_ID = "astro";
- public static final String SUN = "sun";
- public static final String MOON = "moon";
+ private static final String SUN = "sun";
+ private static final String MOON = "moon";
public static final String LOCAL = "local";
// things
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
@Component(configurationPid = "binding.astro", service = ThingHandlerFactory.class)
public class AstroHandlerFactory extends BaseThingHandlerFactory {
- private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Stream
- .concat(SunHandler.SUPPORTED_THING_TYPES.stream(), MoonHandler.SUPPORTED_THING_TYPES.stream())
- .collect(Collectors.toSet());
+ private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_SUN, THING_TYPE_MOON);
private static final Map<String, AstroThingHandler> ASTRO_THING_HANDLERS = new HashMap<>();
private final CronScheduler scheduler;
private final TimeZoneProvider timeZoneProvider;
public class AstroActions implements ThingActions {
private final Logger logger = LoggerFactory.getLogger(AstroActions.class);
- protected @Nullable AstroThingHandler handler;
+ private @Nullable AstroThingHandler handler;
public AstroActions() {
logger.debug("Astro actions service instanciated");
/**
* Calculates the previous moon phase.
*/
- public double getPreviousPhase(Calendar cal, double jd, double mode) {
+ private double getPreviousPhase(Calendar cal, double jd, double mode) {
double tz = 0;
double phaseJd = 0;
do {
return sr;
}
- public double[] calcMoon(double t) {
+ private double[] calcMoon(double t) {
double p2 = 6.283185307;
double arc = 206264.8062;
double coseps = .91748;
/**
* Calculates sun radiation data.
*/
- public void setRadiationInfo(Calendar calendar, double elevation, Double altitude, Sun sun) {
+ private void setRadiationInfo(Calendar calendar, double elevation, Double altitude, Sun sun) {
double sinAlpha = Math.sin(DEG2RAD * elevation);
int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
*/
@NonNullByDefault
public class AstroThingConfig {
+ public static final String GEOLOCATION = "geolocation";
+ public static final String USE_METEOROLOGICAL_SEASON = "useMeteorologicalSeason";
public @Nullable String geolocation;
public @Nullable Double altitude;
public @Nullable Double latitude;
public void parseGeoLocation() {
if (geolocation != null) {
String[] geoParts = geolocation.split(",");
- if (geoParts.length == 2) {
- latitude = toDouble(geoParts[0]);
- longitude = toDouble(geoParts[1]);
- } else if (geoParts.length == 3) {
+ if (geoParts.length >= 2) {
latitude = toDouble(geoParts[0]);
longitude = toDouble(geoParts[1]);
+ }
+ if (geoParts.length == 3) {
altitude = toDouble(geoParts[2]);
}
}
package org.openhab.binding.astro.internal.discovery;
import static org.openhab.binding.astro.internal.AstroBindingConstants.*;
+import static org.openhab.binding.astro.internal.config.AstroThingConfig.*;
import java.util.Arrays;
import java.util.HashSet;
public class AstroDiscoveryService extends AbstractDiscoveryService {
private static final int DISCOVER_TIMEOUT_SECONDS = 2;
private static final int LOCATION_CHANGED_CHECK_INTERVAL = 60;
- private static final Set<String> METEO_BASED_COUNTRIES = new HashSet<>(Arrays.asList("NZ", "AU"));
+ private static final Set<String> METEO_BASED_COUNTRIES = Set.of("NZ", "AU");
private static final ThingUID SUN_THING = new ThingUID(THING_TYPE_SUN, LOCAL);
private static final ThingUID MOON_THING = new ThingUID(THING_TYPE_MOON, LOCAL);
}
public void createResults(PointType location) {
- String propGeolocation;
+ String propGeolocation = location.toString();
String country = localeProvider.getLocale().getCountry();
- propGeolocation = String.format("%s,%s,%s", location.getLatitude(), location.getLongitude(),
- location.getAltitude());
+
thingDiscovered(DiscoveryResultBuilder.create(SUN_THING).withLabel("Local Sun")
- .withProperty("geolocation", propGeolocation)
- .withProperty("useMeteorologicalSeason", METEO_BASED_COUNTRIES.contains(country))
- .withRepresentationProperty("geolocation").build());
+ .withProperty(GEOLOCATION, propGeolocation)
+ .withProperty(USE_METEOROLOGICAL_SEASON, METEO_BASED_COUNTRIES.contains(country))
+ .withRepresentationProperty(GEOLOCATION).build());
+
thingDiscovered(DiscoveryResultBuilder.create(MOON_THING).withLabel("Local Moon")
- .withProperty("geolocation", propGeolocation).withRepresentationProperty("geolocation").build());
+ .withProperty(GEOLOCATION, propGeolocation).withRepresentationProperty(GEOLOCATION).build());
}
}
import static org.openhab.core.types.RefreshType.REFRESH;
import java.lang.invoke.MethodHandles;
+import java.text.SimpleDateFormat;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.Calendar;
import javax.measure.quantity.Angle;
-import org.apache.commons.lang3.time.DateFormatUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.astro.internal.action.AstroActions;
*/
@NonNullByDefault
public abstract class AstroThingHandler extends BaseThingHandler {
-
private static final String DAILY_MIDNIGHT = "30 0 0 * * ? *";
/** Logger Instance */
- protected final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+ private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+ private final SimpleDateFormat isoFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
/** Scheduler to schedule jobs */
private final CronScheduler cronScheduler;
monitor.unlock();
}
if (logger.isDebugEnabled()) {
- String formattedDate = DateFormatUtils.ISO_DATETIME_FORMAT.format(eventAt);
+ String formattedDate = this.isoFormatter.format(eventAt);
logger.debug("Scheduled {} in {}ms (at {})", job, sleepTime, formattedDate);
}
}
*/
package org.openhab.binding.astro.internal.handler;
-import static org.openhab.binding.astro.internal.AstroBindingConstants.THING_TYPE_MOON;
-
import java.time.ZonedDateTime;
-import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
-import java.util.HashSet;
-import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.scheduler.CronScheduler;
import org.openhab.core.thing.Thing;
-import org.openhab.core.thing.ThingTypeUID;
/**
* The MoonHandler is responsible for updating calculated moon data.
@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();
*/
package org.openhab.binding.astro.internal.handler;
-import static org.openhab.binding.astro.internal.AstroBindingConstants.THING_TYPE_SUN;
-
import java.time.ZonedDateTime;
-import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
-import java.util.HashSet;
-import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.scheduler.CronScheduler;
import org.openhab.core.thing.Thing;
-import org.openhab.core.thing.ThingTypeUID;
/**
* The SunHandler is responsible for updating calculated sun data.
@NonNullByDefault
public class SunHandler extends AstroThingHandler {
- public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = new HashSet<>(Arrays.asList(THING_TYPE_SUN));
-
private final String[] positionalChannelIds = new String[] { "position#azimuth", "position#elevation",
"radiation#direct", "radiation#diffuse", "radiation#total" };
private final SunCalc sunCalc = new SunCalc();
*/
package org.openhab.binding.astro.internal.job;
-import static java.util.Arrays.asList;
-import static java.util.Calendar.SECOND;
import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.toList;
-import static org.apache.commons.lang3.time.DateUtils.truncatedEquals;
import static org.openhab.binding.astro.internal.AstroBindingConstants.*;
import static org.openhab.binding.astro.internal.util.DateTimeUtils.*;
return;
}
AstroChannelConfig config = channel.getConfiguration().as(AstroChannelConfig.class);
- Calendar configStart = applyConfig(start, config);
- Calendar configEnd = applyConfig(end, config);
+ Calendar configStart = truncateToSecond(applyConfig(start, config));
+ Calendar configEnd = truncateToSecond(applyConfig(end, config));
- if (truncatedEquals(configStart, configEnd, SECOND)) {
- scheduleEvent(thingUID, astroHandler, configStart, asList(EVENT_START, EVENT_END), channelId, true);
- } else {
- scheduleEvent(thingUID, astroHandler, configStart, EVENT_START, channelId, true);
- scheduleEvent(thingUID, astroHandler, configEnd, EVENT_END, channelId, true);
- }
+ scheduleEvent(thingUID, astroHandler, configStart, EVENT_START, channelId, true);
+ scheduleEvent(thingUID, astroHandler, configEnd, EVENT_END, channelId, true);
}
/**
import javax.measure.quantity.Angle;
+import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.unit.Units;
* @author Gaël L'hopital - Added shade length
* @author Christoph Weitkamp - Introduced UoM
*/
+@NonNullByDefault
public class Position {
private double azimuth;
*/
package org.openhab.binding.astro.internal.model;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+
/**
* All sun phases.
*
* @author Gerhard Riegler - Initial contribution
*/
+@NonNullByDefault
public enum SunPhaseName {
SUN_RISE,
ASTRO_DAWN,
*/
package org.openhab.binding.astro.internal.model;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+
/**
* All zodiac signs.
*
* @author Gerhard Riegler - Initial contribution
*/
+@NonNullByDefault
public enum ZodiacSign {
ARIES,
TAURUS,
package org.openhab.binding.astro.internal.util;
import java.util.Calendar;
-import java.util.Date;
import java.util.regex.Pattern;
-import org.apache.commons.lang3.time.DateUtils;
import org.openhab.binding.astro.internal.config.AstroChannelConfig;
import org.openhab.binding.astro.internal.model.Range;
import org.slf4j.Logger;
private static final Logger LOGGER = LoggerFactory.getLogger(DateTimeUtils.class);
private static final Pattern HHMM_PATTERN = Pattern.compile("^([0-1][0-9]|2[0-3])(:[0-5][0-9])$");
- public static final double J1970 = 2440588.0;
- public static final double MILLISECONDS_PER_DAY = 1000 * 60 * 60 * 24;
+ private static final double J1970 = 2440588.0;
+ private static final double MILLISECONDS_PER_DAY = 1000 * 60 * 60 * 24;
/** Constructor */
private DateTimeUtils() {
throw new IllegalAccessError("Non-instantiable");
}
+ /**
+ * Truncates the time from the calendar object.
+ */
+ public static Calendar truncateToSecond(Calendar calendar) {
+ Calendar cal = (Calendar) calendar.clone();
+ cal.set(Calendar.MILLISECOND, 0);
+ return cal;
+ }
+
+ /**
+ * Truncates the time from the calendar object.
+ */
+ private static Calendar truncateToMinute(Calendar calendar) {
+ Calendar cal = truncateToSecond(calendar);
+ cal.set(Calendar.SECOND, 0);
+ return cal;
+ }
+
/**
* Truncates the time from the calendar object.
*/
public static Calendar truncateToMidnight(Calendar calendar) {
- return DateUtils.truncate(calendar, Calendar.DAY_OF_MONTH);
+ Calendar cal = truncateToMinute(calendar);
+ cal.set(Calendar.HOUR_OF_DAY, 0);
+ cal.set(Calendar.MINUTE, 0);
+ return cal;
}
/**
long millis = (long) ((julianDate + 0.5 - J1970) * MILLISECONDS_PER_DAY);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(millis);
- return DateUtils.round(cal, Calendar.MINUTE);
+ int second = cal.get(Calendar.SECOND);
+ if (second > 30) {
+ cal.add(Calendar.MINUTE, 1);
+ }
+ return truncateToMinute(cal);
}
/**
* Returns the end of day from the calendar object.
*/
public static Calendar endOfDayDate(Calendar calendar) {
- Calendar cal = (Calendar) calendar.clone();
- cal = DateUtils.ceiling(cal, Calendar.DATE);
+ Calendar cal = truncateToMidnight(calendar);
+ cal.add(Calendar.DATE, 1);
cal.add(Calendar.MILLISECOND, -1);
return cal;
}
}
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
- return DateUtils.truncate(cal, Calendar.MINUTE);
+ return truncateToMinute(cal);
}
/**
* Returns true, if two calendar objects are on the same day ignoring time.
*/
public static boolean isSameDay(Calendar cal1, Calendar cal2) {
- return cal1 != null && cal2 != null && DateUtils.isSameDay(cal1, cal2);
- }
-
- /**
- * Returns a date object from a calendar.
- */
- public static Date getDate(Calendar calendar) {
- return calendar == null ? null : calendar.getTime();
+ return cal1 != null && cal2 != null && cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA)
+ && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
+ && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);
}
/**
* Returns true, if cal1 is greater or equal than cal2, ignoring seconds.
*/
public static boolean isTimeGreaterEquals(Calendar cal1, Calendar cal2) {
- Calendar truncCal1 = DateUtils.truncate(cal1, Calendar.MINUTE);
- Calendar truncCal2 = DateUtils.truncate(cal2, Calendar.MINUTE);
+ Calendar truncCal1 = truncateToMinute(cal1);
+ Calendar truncCal2 = truncateToMinute(cal2);
return truncCal1.getTimeInMillis() >= truncCal2.getTimeInMillis();
}
private static Calendar adjustTime(Calendar cal, int minutes) {
if (minutes > 0) {
- Calendar cTime = Calendar.getInstance();
- cTime = DateUtils.truncate(cal, Calendar.DAY_OF_MONTH);
+ Calendar cTime = truncateToMidnight(cal);
cTime.add(Calendar.MINUTE, minutes);
return cTime;
}
import java.math.RoundingMode;
import java.time.ZoneId;
import java.time.ZonedDateTime;
+import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
+import java.util.List;
import java.util.Objects;
import java.util.TimeZone;
* Returns the property value from the object instance, nested properties are possible. If the propertyName is for
* example rise.start, the methods getRise().getStart() are called.
*/
- public static @Nullable Object getPropertyValue(ChannelUID channelUID, Object instance) throws Exception {
- String[] properties = channelUID.getId().split("#");
- return getPropertyValue(instance, properties, 0);
+ private static @Nullable Object getPropertyValue(ChannelUID channelUID, Object instance) throws Exception {
+ ArrayList<String> properties = new ArrayList<>(List.of(channelUID.getId().split("#")));
+ return getPropertyValue(instance, properties);
}
/**
* Iterates through the nested properties and returns the getter value.
*/
@SuppressWarnings("all")
- private static @Nullable Object getPropertyValue(Object instance, String[] properties, int nestedIndex)
- throws Exception {
- String propertyName = properties[nestedIndex];
+ private static @Nullable Object getPropertyValue(Object instance, List<String> properties) throws Exception {
+ String propertyName = properties.remove(0);
Method m = instance.getClass().getMethod(toGetterString(propertyName), null);
Object result = m.invoke(instance, (Object[]) null);
- if (nestedIndex + 1 < properties.length) {
+ if (!properties.isEmpty()) {
Objects.requireNonNull(result);
- return getPropertyValue(result, properties, nestedIndex + 1);
+ return getPropertyValue(result, properties);
}
return result;
}
assertNextSeason(season.getAutumn(), DEC_10_2020, season);
}
+ @Test
+ void testTruncate() {
+ Calendar cal = newCalendar(2021, 9, 30, 11, 54, TIME_ZONE);
+ Calendar target = newCalendar(2021, 9, 30, 0, 0, TIME_ZONE);
+ Calendar truncated = DateTimeUtils.truncateToMidnight(cal);
+ assertEquals(truncated, target);
+ Calendar endOfDay = DateTimeUtils.endOfDayDate(cal);
+ Calendar target2 = new GregorianCalendar(2021, 9, 30, 23, 59, 59);
+ target2.setTimeZone(TIME_ZONE);
+ target2.set(Calendar.MILLISECOND, 999);
+ assertEquals(endOfDay, target2);
+ }
+
private void assertNextSeason(Calendar expectedSeason, Calendar date, Season season) {
assertEquals(expectedSeason, DateTimeUtils.getNext(date, season.getSpring(), season.getSummer(),
season.getAutumn(), season.getWinter()));