]> git.basschouten.com Git - openhab-addons.git/blob
84b7a37d8bdbd709886f98dbd3f2bc23b62cc0c7
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.handler;
14
15 import java.time.ZonedDateTime;
16 import java.util.Calendar;
17 import java.util.GregorianCalendar;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.astro.internal.calc.SunCalc;
22 import org.openhab.binding.astro.internal.job.DailyJobSun;
23 import org.openhab.binding.astro.internal.job.Job;
24 import org.openhab.binding.astro.internal.model.*;
25 import org.openhab.core.i18n.TimeZoneProvider;
26 import org.openhab.core.scheduler.CronScheduler;
27 import org.openhab.core.thing.Thing;
28
29 /**
30  * The SunHandler is responsible for updating calculated sun data.
31  *
32  * @author Gerhard Riegler - Initial contribution
33  * @author Amit Kumar Mondal - Implementation to be compliant with ESH Scheduler
34  */
35 @NonNullByDefault
36 public class SunHandler extends AstroThingHandler {
37
38     private final String[] positionalChannelIds = new String[] { "position#azimuth", "position#elevation",
39             "radiation#direct", "radiation#diffuse", "radiation#total" };
40     private final SunCalc sunCalc = new SunCalc();
41     private @NonNullByDefault({}) Sun sun;
42
43     /**
44      * Constructor
45      */
46     public SunHandler(Thing thing, final CronScheduler scheduler, final TimeZoneProvider timeZoneProvider) {
47         super(thing, scheduler, timeZoneProvider);
48     }
49
50     @Override
51     public void publishPositionalInfo() {
52         sun = getSunAt(ZonedDateTime.now());
53         Double latitude = thingConfig.latitude;
54         Double longitude = thingConfig.longitude;
55         Double altitude = thingConfig.altitude;
56         sunCalc.setPositionalInfo(Calendar.getInstance(), latitude != null ? latitude : 0,
57                 longitude != null ? longitude : 0, altitude != null ? altitude : 0, sun);
58
59         sun.getEclipse().setElevations(this, timeZoneProvider);
60
61         publishPlanet();
62     }
63
64     @Override
65     public @Nullable Planet getPlanet() {
66         return sun;
67     }
68
69     @Override
70     public void dispose() {
71         super.dispose();
72         sun = null;
73     }
74
75     @Override
76     protected String[] getPositionalChannelIds() {
77         return positionalChannelIds;
78     }
79
80     @Override
81     protected Job getDailyJob() {
82         return new DailyJobSun(thing.getUID().getAsString(), this);
83     }
84
85     private Sun getSunAt(ZonedDateTime date) {
86         Double latitude = thingConfig.latitude;
87         Double longitude = thingConfig.longitude;
88         Double altitude = thingConfig.altitude;
89         return sunCalc.getSunInfo(GregorianCalendar.from(date), latitude != null ? latitude : 0,
90                 longitude != null ? longitude : 0, altitude != null ? altitude : 0,
91                 thingConfig.useMeteorologicalSeason);
92     }
93
94     private Sun getPositionedSunAt(ZonedDateTime date) {
95         Sun localSun = getSunAt(date);
96         Double latitude = thingConfig.latitude;
97         Double longitude = thingConfig.longitude;
98         Double altitude = thingConfig.altitude;
99         sunCalc.setPositionalInfo(GregorianCalendar.from(date), latitude != null ? latitude : 0,
100                 longitude != null ? longitude : 0, altitude != null ? altitude : 0, localSun);
101         return localSun;
102     }
103
104     public @Nullable ZonedDateTime getEventTime(SunPhaseName sunPhase, ZonedDateTime date, boolean begin) {
105         Range eventRange = getSunAt(date).getAllRanges().get(sunPhase);
106         if (eventRange != null) {
107             Calendar cal = begin ? eventRange.getStart() : eventRange.getEnd();
108             return ZonedDateTime.ofInstant(cal.toInstant(), date.getZone());
109         } else {
110             return null;
111         }
112     }
113
114     @Override
115     public @Nullable Position getPositionAt(ZonedDateTime date) {
116         Sun localSun = getPositionedSunAt(date);
117         return localSun.getPosition();
118     }
119
120     public @Nullable Radiation getRadiationAt(ZonedDateTime date) {
121         Sun localSun = getPositionedSunAt(date);
122         return localSun.getRadiation();
123     }
124 }