2 * Copyright (c) 2010-2023 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.Planet;
25 import org.openhab.binding.astro.internal.model.Position;
26 import org.openhab.binding.astro.internal.model.Range;
27 import org.openhab.binding.astro.internal.model.Sun;
28 import org.openhab.binding.astro.internal.model.SunPhaseName;
29 import org.openhab.core.i18n.TimeZoneProvider;
30 import org.openhab.core.scheduler.CronScheduler;
31 import org.openhab.core.thing.Thing;
34 * The SunHandler is responsible for updating calculated sun data.
36 * @author Gerhard Riegler - Initial contribution
37 * @author Amit Kumar Mondal - Implementation to be compliant with ESH Scheduler
40 public class SunHandler extends AstroThingHandler {
42 private final String[] positionalChannelIds = new String[] { "position#azimuth", "position#elevation",
43 "radiation#direct", "radiation#diffuse", "radiation#total" };
44 private final SunCalc sunCalc = new SunCalc();
45 private @NonNullByDefault({}) Sun sun;
50 public SunHandler(Thing thing, final CronScheduler scheduler, final TimeZoneProvider timeZoneProvider) {
51 super(thing, scheduler, timeZoneProvider);
55 public void publishPositionalInfo() {
56 sun = getSunAt(ZonedDateTime.now());
57 Double latitude = thingConfig.latitude;
58 Double longitude = thingConfig.longitude;
59 Double altitude = thingConfig.altitude;
60 sunCalc.setPositionalInfo(Calendar.getInstance(), latitude != null ? latitude : 0,
61 longitude != null ? longitude : 0, altitude != null ? altitude : 0, sun);
63 sun.getEclipse().setElevations(this, timeZoneProvider);
69 public @Nullable Planet getPlanet() {
74 public void dispose() {
80 protected String[] getPositionalChannelIds() {
81 return positionalChannelIds;
85 protected Job getDailyJob() {
86 return new DailyJobSun(thing.getUID().getAsString(), this);
89 private Sun getSunAt(ZonedDateTime date) {
90 Double latitude = thingConfig.latitude;
91 Double longitude = thingConfig.longitude;
92 Double altitude = thingConfig.altitude;
93 return sunCalc.getSunInfo(GregorianCalendar.from(date), latitude != null ? latitude : 0,
94 longitude != null ? longitude : 0, altitude != null ? altitude : 0,
95 thingConfig.useMeteorologicalSeason);
98 public @Nullable ZonedDateTime getEventTime(SunPhaseName sunPhase, ZonedDateTime date, boolean begin) {
99 Range eventRange = getSunAt(date).getAllRanges().get(sunPhase);
100 if (eventRange != null) {
101 Calendar cal = begin ? eventRange.getStart() : eventRange.getEnd();
102 return ZonedDateTime.ofInstant(cal.toInstant(), date.getZone());
109 public @Nullable Position getPositionAt(ZonedDateTime date) {
110 Sun localSun = getSunAt(date);
111 Double latitude = thingConfig.latitude;
112 Double longitude = thingConfig.longitude;
113 Double altitude = thingConfig.altitude;
114 sunCalc.setPositionalInfo(GregorianCalendar.from(date), latitude != null ? latitude : 0,
115 longitude != null ? longitude : 0, altitude != null ? altitude : 0, localSun);
116 return localSun.getPosition();