]> git.basschouten.com Git - openhab-addons.git/blob
cd4f123872c6601435c7747018109f384601bd77
[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.scheduleEvent;
17
18 import java.util.Calendar;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.astro.internal.handler.AstroThingHandler;
22 import org.openhab.binding.astro.internal.model.Eclipse;
23 import org.openhab.binding.astro.internal.model.Moon;
24 import org.openhab.binding.astro.internal.model.MoonPhase;
25 import org.openhab.binding.astro.internal.model.Planet;
26
27 /**
28  * Daily scheduled jobs for Moon 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 DailyJobMoon 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 if {@code thingUID} or {@code handler} is {@code null}
44      */
45     public DailyJobMoon(String thingUID, AstroThingHandler handler) {
46         super(thingUID);
47         this.handler = handler;
48     }
49
50     @Override
51     public void run() {
52         handler.publishDailyInfo();
53         String thingUID = getThingUID();
54         LOGGER.debug("Scheduled Astro event-jobs for thing {}", thingUID);
55
56         Planet planet = handler.getPlanet();
57         if (planet == null) {
58             LOGGER.error("Planet not instantiated");
59             return;
60         }
61         Moon moon = (Moon) planet;
62         scheduleEvent(thingUID, handler, moon.getRise().getStart(), EVENT_START, EVENT_CHANNEL_ID_RISE, false);
63         scheduleEvent(thingUID, handler, moon.getSet().getEnd(), EVENT_END, EVENT_CHANNEL_ID_SET, false);
64
65         MoonPhase moonPhase = moon.getPhase();
66         scheduleEvent(thingUID, handler, moonPhase.getFirstQuarter(), EVENT_PHASE_FIRST_QUARTER,
67                 EVENT_CHANNEL_ID_MOON_PHASE, false);
68         scheduleEvent(thingUID, handler, moonPhase.getThirdQuarter(), EVENT_PHASE_THIRD_QUARTER,
69                 EVENT_CHANNEL_ID_MOON_PHASE, false);
70         scheduleEvent(thingUID, handler, moonPhase.getFull(), EVENT_PHASE_FULL, EVENT_CHANNEL_ID_MOON_PHASE, false);
71         scheduleEvent(thingUID, handler, moonPhase.getNew(), EVENT_PHASE_NEW, EVENT_CHANNEL_ID_MOON_PHASE, false);
72
73         Eclipse eclipse = moon.getEclipse();
74         eclipse.getKinds().forEach(eclipseKind -> {
75             Calendar eclipseDate = eclipse.getDate(eclipseKind);
76             if (eclipseDate != null) {
77                 scheduleEvent(thingUID, handler, eclipseDate, eclipseKind.toString(), EVENT_CHANNEL_ID_ECLIPSE, false);
78             }
79         });
80
81         scheduleEvent(thingUID, handler, moon.getPerigee().getDate(), EVENT_PERIGEE, EVENT_CHANNEL_ID_PERIGEE, false);
82         scheduleEvent(thingUID, handler, moon.getApogee().getDate(), EVENT_APOGEE, EVENT_CHANNEL_ID_APOGEE, false);
83     }
84
85     @Override
86     public String toString() {
87         return "Daily job moon " + getThingUID();
88     }
89 }