]> git.basschouten.com Git - openhab-addons.git/blob
ea61e4d45226e0bdd226331c6a419582a32a00e4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.astro.internal.job;
14
15 import static org.openhab.binding.astro.internal.AstroBindingConstants.*;
16 import static org.openhab.binding.astro.internal.job.Job.*;
17 import static org.openhab.binding.astro.internal.model.SunPhaseName.*;
18
19 import java.util.Calendar;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.astro.internal.handler.AstroThingHandler;
23 import org.openhab.binding.astro.internal.model.Eclipse;
24 import org.openhab.binding.astro.internal.model.Planet;
25 import org.openhab.binding.astro.internal.model.Sun;
26
27 /**
28  * Daily scheduled jobs For Sun planet
29  *
30  * @author Gerhard Riegler - Initial contribution
31  * @author Amit Kumar Mondal - Implementation to be compliant with ESH Scheduler
32  */
33 @NonNullByDefault
34 public final class DailyJobSun extends AbstractJob {
35
36     private final AstroThingHandler handler;
37
38     /**
39      * Constructor
40      *
41      * @param thingUID the Thing UID
42      * @param handler the {@link AstroThingHandler} instance
43      * @throws IllegalArgumentException
44      *             if {@code thingUID} or {@code handler} is {@code null}
45      */
46     public DailyJobSun(String thingUID, AstroThingHandler handler) {
47         super(thingUID);
48         this.handler = handler;
49     }
50
51     @Override
52     public void run() {
53         handler.publishDailyInfo();
54         String thingUID = getThingUID();
55         LOGGER.debug("Scheduled Astro event-jobs for thing {}", thingUID);
56
57         Planet planet = handler.getPlanet();
58         if (planet == null) {
59             LOGGER.error("Planet not instantiated");
60             return;
61         }
62         Sun sun = (Sun) planet;
63         scheduleRange(thingUID, handler, sun.getRise(), EVENT_CHANNEL_ID_RISE);
64         scheduleRange(thingUID, handler, sun.getSet(), EVENT_CHANNEL_ID_SET);
65         scheduleRange(thingUID, handler, sun.getNoon(), EVENT_CHANNEL_ID_NOON);
66         scheduleRange(thingUID, handler, sun.getNight(), EVENT_CHANNEL_ID_NIGHT);
67         scheduleRange(thingUID, handler, sun.getMorningNight(), EVENT_CHANNEL_ID_MORNING_NIGHT);
68         scheduleRange(thingUID, handler, sun.getAstroDawn(), EVENT_CHANNEL_ID_ASTRO_DAWN);
69         scheduleRange(thingUID, handler, sun.getNauticDawn(), EVENT_CHANNEL_ID_NAUTIC_DAWN);
70         scheduleRange(thingUID, handler, sun.getCivilDawn(), EVENT_CHANNEL_ID_CIVIL_DAWN);
71         scheduleRange(thingUID, handler, sun.getAstroDusk(), EVENT_CHANNEL_ID_ASTRO_DUSK);
72         scheduleRange(thingUID, handler, sun.getNauticDusk(), EVENT_CHANNEL_ID_NAUTIC_DUSK);
73         scheduleRange(thingUID, handler, sun.getCivilDusk(), EVENT_CHANNEL_ID_CIVIL_DUSK);
74         scheduleRange(thingUID, handler, sun.getEveningNight(), EVENT_CHANNEL_ID_EVENING_NIGHT);
75         scheduleRange(thingUID, handler, sun.getDaylight(), EVENT_CHANNEL_ID_DAYLIGHT);
76
77         Eclipse eclipse = sun.getEclipse();
78         eclipse.getKinds().forEach(eclipseKind -> {
79             Calendar eclipseDate = eclipse.getDate(eclipseKind);
80             if (eclipseDate != null) {
81                 scheduleEvent(thingUID, handler, eclipseDate, eclipseKind.toString(), EVENT_CHANNEL_ID_ECLIPSE, false);
82             }
83         });
84
85         // schedule republish jobs
86         schedulePublishPlanet(thingUID, handler, sun.getZodiac().getEnd());
87         schedulePublishPlanet(thingUID, handler, sun.getSeason().getNextSeason());
88
89         // schedule phase jobs
90         scheduleSunPhase(thingUID, handler, SUN_RISE, sun.getRise().getStart());
91         scheduleSunPhase(thingUID, handler, SUN_SET, sun.getSet().getStart());
92         scheduleSunPhase(thingUID, handler, NIGHT, sun.getNight().getStart());
93         scheduleSunPhase(thingUID, handler, DAYLIGHT, sun.getDaylight().getStart());
94         scheduleSunPhase(thingUID, handler, ASTRO_DAWN, sun.getAstroDawn().getStart());
95         scheduleSunPhase(thingUID, handler, NAUTIC_DAWN, sun.getNauticDawn().getStart());
96         scheduleSunPhase(thingUID, handler, CIVIL_DAWN, sun.getCivilDawn().getStart());
97         scheduleSunPhase(thingUID, handler, ASTRO_DUSK, sun.getAstroDusk().getStart());
98         scheduleSunPhase(thingUID, handler, NAUTIC_DUSK, sun.getNauticDusk().getStart());
99         scheduleSunPhase(thingUID, handler, CIVIL_DUSK, sun.getCivilDusk().getStart());
100     }
101
102     @Override
103     public String toString() {
104         return "Daily job sun " + getThingUID();
105     }
106 }