2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.astro.internal.handler;
15 import java.time.ZonedDateTime;
16 import java.util.Calendar;
17 import java.util.GregorianCalendar;
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;
30 * The SunHandler is responsible for updating calculated sun data.
32 * @author Gerhard Riegler - Initial contribution
33 * @author Amit Kumar Mondal - Implementation to be compliant with ESH Scheduler
36 public class SunHandler extends AstroThingHandler {
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;
46 public SunHandler(Thing thing, final CronScheduler scheduler, final TimeZoneProvider timeZoneProvider) {
47 super(thing, scheduler, timeZoneProvider);
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);
59 sun.getEclipse().setElevations(this, timeZoneProvider);
65 public @Nullable Planet getPlanet() {
70 public void dispose() {
76 protected String[] getPositionalChannelIds() {
77 return positionalChannelIds;
81 protected Job getDailyJob() {
82 return new DailyJobSun(thing.getUID().getAsString(), this);
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);
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);
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());
115 public @Nullable Position getPositionAt(ZonedDateTime date) {
116 Sun localSun = getPositionedSunAt(date);
117 return localSun.getPosition();
120 public @Nullable Radiation getRadiationAt(ZonedDateTime date) {
121 Sun localSun = getPositionedSunAt(date);
122 return localSun.getRadiation();