Search in sources :

Example 1 with SunEclipse

use of org.eclipse.smarthome.binding.astro.internal.model.SunEclipse in project smarthome by eclipse.

the class SunCalc method getSunInfo.

private Sun getSunInfo(Calendar calendar, double latitude, double longitude, Double altitude, boolean onlyAstro) {
    double lw = -longitude * DEG2RAD;
    double phi = latitude * DEG2RAD;
    double j = DateTimeUtils.midnightDateToJulianDate(calendar) + 0.5;
    double n = getJulianCycle(j, lw);
    double js = getApproxSolarTransit(0, lw, n);
    double m = getSolarMeanAnomaly(js);
    double c = getEquationOfCenter(m);
    double lsun = getEclipticLongitude(m, c);
    double d = getSunDeclination(lsun);
    double jtransit = getSolarTransit(js, m, lsun);
    double w0 = getHourAngle(H0, phi, d);
    double w1 = getHourAngle(H0 + SUN_DIAMETER, phi, d);
    double jset = getSunsetJulianDate(w0, m, lsun, lw, n);
    double jsetstart = getSunsetJulianDate(w1, m, lsun, lw, n);
    double jrise = getSunriseJulianDate(jtransit, jset);
    double jriseend = getSunriseJulianDate(jtransit, jsetstart);
    double w2 = getHourAngle(H1, phi, d);
    double jnau = getSunsetJulianDate(w2, m, lsun, lw, n);
    double jciv2 = getSunriseJulianDate(jtransit, jnau);
    double w3 = getHourAngle(H2, phi, d);
    double w4 = getHourAngle(H3, phi, d);
    double jastro = getSunsetJulianDate(w3, m, lsun, lw, n);
    double jdark = getSunsetJulianDate(w4, m, lsun, lw, n);
    double jnau2 = getSunriseJulianDate(jtransit, jastro);
    double jastro2 = getSunriseJulianDate(jtransit, jdark);
    Sun sun = new Sun();
    sun.setAstroDawn(new Range(DateTimeUtils.toCalendar(jastro2), DateTimeUtils.toCalendar(jnau2)));
    sun.setAstroDusk(new Range(DateTimeUtils.toCalendar(jastro), DateTimeUtils.toCalendar(jdark)));
    if (onlyAstro) {
        return sun;
    }
    sun.setNoon(new Range(DateTimeUtils.toCalendar(jtransit), DateTimeUtils.toCalendar(jtransit + JD_ONE_MINUTE_FRACTION)));
    sun.setRise(new Range(DateTimeUtils.toCalendar(jrise), DateTimeUtils.toCalendar(jriseend)));
    sun.setSet(new Range(DateTimeUtils.toCalendar(jsetstart), DateTimeUtils.toCalendar(jset)));
    sun.setCivilDawn(new Range(DateTimeUtils.toCalendar(jciv2), DateTimeUtils.toCalendar(jrise)));
    sun.setCivilDusk(new Range(DateTimeUtils.toCalendar(jset), DateTimeUtils.toCalendar(jnau)));
    sun.setNauticDawn(new Range(DateTimeUtils.toCalendar(jnau2), DateTimeUtils.toCalendar(jciv2)));
    sun.setNauticDusk(new Range(DateTimeUtils.toCalendar(jnau), DateTimeUtils.toCalendar(jastro)));
    boolean isSunUpAllDay = isSunUpAllDay(calendar, latitude, longitude, altitude);
    // daylight
    Range daylightRange = new Range();
    if (sun.getRise().getStart() == null && sun.getRise().getEnd() == null) {
        if (isSunUpAllDay) {
            daylightRange = new Range(DateTimeUtils.truncateToMidnight(calendar), DateTimeUtils.truncateToMidnight(addDays(calendar, 1)));
        }
    } else {
        daylightRange = new Range(sun.getRise().getEnd(), sun.getSet().getStart());
    }
    sun.setDaylight(daylightRange);
    // morning night
    Sun sunYesterday = getSunInfo(addDays(calendar, -1), latitude, longitude, altitude, true);
    Range morningNightRange = null;
    if (sunYesterday.getAstroDusk().getEnd() != null && DateUtils.isSameDay(sunYesterday.getAstroDusk().getEnd(), calendar)) {
        morningNightRange = new Range(sunYesterday.getAstroDusk().getEnd(), sun.getAstroDawn().getStart());
    } else if (isSunUpAllDay || sun.getAstroDawn().getStart() == null) {
        morningNightRange = new Range();
    } else {
        morningNightRange = new Range(DateTimeUtils.truncateToMidnight(calendar), sun.getAstroDawn().getStart());
    }
    sun.setMorningNight(morningNightRange);
    // evening night
    Range eveningNightRange = null;
    if (sun.getAstroDusk().getEnd() != null && DateUtils.isSameDay(sun.getAstroDusk().getEnd(), calendar)) {
        eveningNightRange = new Range(sun.getAstroDusk().getEnd(), DateTimeUtils.truncateToMidnight(addDays(calendar, 1)));
    } else {
        eveningNightRange = new Range();
    }
    sun.setEveningNight(eveningNightRange);
    // night
    if (isSunUpAllDay) {
        sun.setNight(new Range());
    } else {
        Sun sunTomorrow = getSunInfo(addDays(calendar, 1), latitude, longitude, altitude, true);
        sun.setNight(new Range(sun.getAstroDusk().getEnd(), sunTomorrow.getAstroDawn().getStart()));
    }
    // eclipse
    SunEclipse eclipse = sun.getEclipse();
    MoonCalc mc = new MoonCalc();
    double partial = mc.getEclipse(calendar, MoonCalc.ECLIPSE_TYPE_SUN, j, MoonCalc.ECLIPSE_MODE_PARTIAL);
    eclipse.setPartial(DateTimeUtils.toCalendar(partial));
    double ring = mc.getEclipse(calendar, MoonCalc.ECLIPSE_TYPE_SUN, j, MoonCalc.ECLIPSE_MODE_RING);
    eclipse.setRing(DateTimeUtils.toCalendar(ring));
    double total = mc.getEclipse(calendar, MoonCalc.ECLIPSE_TYPE_SUN, j, MoonCalc.ECLIPSE_MODE_TOTAL);
    eclipse.setTotal(DateTimeUtils.toCalendar(total));
    SunZodiacCalc zodiacCalc = new SunZodiacCalc();
    sun.setZodiac(zodiacCalc.getZodiac(calendar));
    SeasonCalc seasonCalc = new SeasonCalc();
    sun.setSeason(seasonCalc.getSeason(calendar, latitude));
    // phase
    for (Entry<SunPhaseName, Range> rangeEntry : sun.getAllRanges().entrySet()) {
        SunPhaseName entryPhase = rangeEntry.getKey();
        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);
            }
        }
    }
    return sun;
}
Also used : SunEclipse(org.eclipse.smarthome.binding.astro.internal.model.SunEclipse) Range(org.eclipse.smarthome.binding.astro.internal.model.Range) Sun(org.eclipse.smarthome.binding.astro.internal.model.Sun) SunPhaseName(org.eclipse.smarthome.binding.astro.internal.model.SunPhaseName)

Example 2 with SunEclipse

use of org.eclipse.smarthome.binding.astro.internal.model.SunEclipse in project smarthome by eclipse.

the class DailyJobSun method run.

@Override
public void run() {
    handler.publishDailyInfo();
    String thingUID = getThingUID();
    LOGGER.info("Scheduled Astro event-jobs for thing {}", thingUID);
    Planet planet = handler.getPlanet();
    if (planet == null) {
        LOGGER.error("Planet not instantiated");
        return;
    }
    Sun sun = (Sun) planet;
    scheduleRange(thingUID, handler, sun.getRise(), EVENT_CHANNEL_ID_RISE);
    scheduleRange(thingUID, handler, sun.getSet(), EVENT_CHANNEL_ID_SET);
    scheduleRange(thingUID, handler, sun.getNoon(), EVENT_CHANNEL_ID_NOON);
    scheduleRange(thingUID, handler, sun.getNight(), EVENT_CHANNEL_ID_NIGHT);
    scheduleRange(thingUID, handler, sun.getMorningNight(), EVENT_CHANNEL_ID_MORNING_NIGHT);
    scheduleRange(thingUID, handler, sun.getAstroDawn(), EVENT_CHANNEL_ID_ASTRO_DAWN);
    scheduleRange(thingUID, handler, sun.getNauticDawn(), EVENT_CHANNEL_ID_NAUTIC_DAWN);
    scheduleRange(thingUID, handler, sun.getCivilDawn(), EVENT_CHANNEL_ID_CIVIL_DAWN);
    scheduleRange(thingUID, handler, sun.getAstroDusk(), EVENT_CHANNEL_ID_ASTRO_DUSK);
    scheduleRange(thingUID, handler, sun.getNauticDusk(), EVENT_CHANNEL_ID_NAUTIC_DUSK);
    scheduleRange(thingUID, handler, sun.getCivilDusk(), EVENT_CHANNEL_ID_CIVIL_DUSK);
    scheduleRange(thingUID, handler, sun.getEveningNight(), EVENT_CHANNEL_ID_EVENING_NIGHT);
    scheduleRange(thingUID, handler, sun.getDaylight(), EVENT_CHANNEL_ID_DAYLIGHT);
    SunEclipse eclipse = sun.getEclipse();
    scheduleEvent(thingUID, handler, eclipse.getPartial(), EVENT_ECLIPSE_PARTIAL, EVENT_CHANNEL_ID_ECLIPSE, false);
    scheduleEvent(thingUID, handler, eclipse.getTotal(), EVENT_ECLIPSE_TOTAL, EVENT_CHANNEL_ID_ECLIPSE, false);
    scheduleEvent(thingUID, handler, eclipse.getRing(), EVENT_ECLIPSE_RING, EVENT_CHANNEL_ID_ECLIPSE, false);
    // schedule republish jobs
    schedulePublishPlanet(thingUID, handler, sun.getZodiac().getEnd());
    schedulePublishPlanet(thingUID, handler, sun.getSeason().getNextSeason());
    // schedule phase jobs
    scheduleSunPhase(thingUID, handler, SUN_RISE, sun.getRise().getStart());
    scheduleSunPhase(thingUID, handler, SUN_SET, sun.getSet().getStart());
    scheduleSunPhase(thingUID, handler, NIGHT, sun.getNight().getStart());
    scheduleSunPhase(thingUID, handler, DAYLIGHT, sun.getDaylight().getStart());
    scheduleSunPhase(thingUID, handler, ASTRO_DAWN, sun.getAstroDawn().getStart());
    scheduleSunPhase(thingUID, handler, NAUTIC_DAWN, sun.getNauticDawn().getStart());
    scheduleSunPhase(thingUID, handler, CIVIL_DAWN, sun.getCivilDawn().getStart());
    scheduleSunPhase(thingUID, handler, ASTRO_DUSK, sun.getAstroDusk().getStart());
    scheduleSunPhase(thingUID, handler, NAUTIC_DUSK, sun.getNauticDusk().getStart());
    scheduleSunPhase(thingUID, handler, CIVIL_DUSK, sun.getCivilDusk().getStart());
}
Also used : SunEclipse(org.eclipse.smarthome.binding.astro.internal.model.SunEclipse) Planet(org.eclipse.smarthome.binding.astro.internal.model.Planet) Sun(org.eclipse.smarthome.binding.astro.internal.model.Sun)

Aggregations

Sun (org.eclipse.smarthome.binding.astro.internal.model.Sun)2 SunEclipse (org.eclipse.smarthome.binding.astro.internal.model.SunEclipse)2 Planet (org.eclipse.smarthome.binding.astro.internal.model.Planet)1 Range (org.eclipse.smarthome.binding.astro.internal.model.Range)1 SunPhaseName (org.eclipse.smarthome.binding.astro.internal.model.SunPhaseName)1