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.action;
15 import java.time.ZonedDateTime;
17 import javax.measure.quantity.Angle;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.astro.internal.AstroBindingConstants;
22 import org.openhab.binding.astro.internal.handler.AstroThingHandler;
23 import org.openhab.binding.astro.internal.handler.SunHandler;
24 import org.openhab.binding.astro.internal.model.Radiation;
25 import org.openhab.binding.astro.internal.model.SunPhaseName;
26 import org.openhab.core.automation.annotation.ActionInput;
27 import org.openhab.core.automation.annotation.ActionOutput;
28 import org.openhab.core.automation.annotation.RuleAction;
29 import org.openhab.core.library.dimension.Intensity;
30 import org.openhab.core.library.types.QuantityType;
31 import org.openhab.core.thing.binding.ThingActions;
32 import org.openhab.core.thing.binding.ThingActionsScope;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Defines the automation thing actions for the Astro binding.
40 * @author Gaƫl L'hopital - Initial contribution
42 @ThingActionsScope(name = "astro")
44 public class AstroActions implements ThingActions {
46 private final Logger logger = LoggerFactory.getLogger(AstroActions.class);
47 private @Nullable AstroThingHandler handler;
49 public AstroActions() {
50 logger.debug("Astro actions service instanciated");
54 public void setThingHandler(@Nullable ThingHandler handler) {
55 if (handler instanceof AstroThingHandler) {
56 this.handler = (AstroThingHandler) handler;
61 public @Nullable ThingHandler getThingHandler() {
65 @RuleAction(label = "get the azimuth", description = "Get the azimuth for a given time.")
66 public @Nullable @ActionOutput(name = "getAzimuth", label = "Azimuth", type = "org.openhab.core.library.types.QuantityType<javax.measure.quantity.Angle>") QuantityType<Angle> getAzimuth(
67 @ActionInput(name = "date", label = "Date", required = false, description = "Considered date") @Nullable ZonedDateTime date) {
68 logger.debug("Astro action 'getAzimuth' called");
69 AstroThingHandler theHandler = this.handler;
70 if (theHandler != null) {
71 return theHandler.getAzimuth(date != null ? date : ZonedDateTime.now());
73 logger.info("Astro Action service ThingHandler is null!");
78 @RuleAction(label = "get the elevation", description = "Get the elevation for a given time.")
79 public @Nullable @ActionOutput(name = "getElevation", label = "Elevation", type = "org.openhab.core.library.types.QuantityType<javax.measure.quantity.Angle>") QuantityType<Angle> getElevation(
80 @ActionInput(name = "date", label = "Date", required = false, description = "Considered date") @Nullable ZonedDateTime date) {
81 logger.debug("Astro action 'getElevation' called");
82 AstroThingHandler theHandler = this.handler;
83 if (theHandler != null) {
84 return theHandler.getElevation(date != null ? date : ZonedDateTime.now());
86 logger.info("Astro Action service ThingHandler is null!");
91 @RuleAction(label = "get the total sun radiation", description = "Get the total sun radiation for a given time.")
92 public @Nullable @ActionOutput(name = "getTotalRadiation", label = "Total Radiation", type = "org.openhab.core.library.types.QuantityType<org.openhab.core.library.dimension.Intensity>") QuantityType<Intensity> getTotalRadiation(
93 @ActionInput(name = "date", label = "Date", required = false, description = "Considered date") @Nullable ZonedDateTime date) {
94 logger.debug("Astro action 'getTotalRadiation' called");
95 AstroThingHandler theHandler = this.handler;
96 if (theHandler != null) {
97 if (theHandler instanceof SunHandler sunHandler) {
98 Radiation radiation = sunHandler.getRadiationAt(date != null ? date : ZonedDateTime.now());
99 return radiation.getTotal();
101 logger.info("Astro Action service ThingHandler is not a SunHandler!");
104 logger.info("Astro Action service ThingHandler is null!");
109 @RuleAction(label = "get the date time of a sun event", description = "Get the date time of a sun event.")
110 public @Nullable @ActionOutput(name = "getEventTime", type = "java.time.ZonedDateTime") ZonedDateTime getEventTime(
111 @ActionInput(name = "phaseName", label = "Phase", required = true, description = "Requested phase") String phaseName,
112 @ActionInput(name = "date", label = "Date", required = false, description = "Considered date") @Nullable ZonedDateTime date,
113 @ActionInput(name = "moment", label = "Moment", required = false, defaultValue = "START", description = "Either START or END") @Nullable String moment) {
114 logger.debug("Sun action 'getEventTime' called");
116 AstroThingHandler theHandler = this.handler;
117 if (theHandler != null) {
118 if (theHandler instanceof SunHandler) {
119 SunHandler handler = (SunHandler) theHandler;
120 SunPhaseName phase = SunPhaseName.valueOf(phaseName.toUpperCase());
121 return handler.getEventTime(phase, date != null ? date : ZonedDateTime.now(),
122 moment == null || AstroBindingConstants.EVENT_START.equalsIgnoreCase(moment));
124 logger.info("Astro Action service ThingHandler is not a SunHandler!");
127 logger.info("Astro Action service ThingHandler is null!");
129 } catch (IllegalArgumentException e) {
130 logger.info("Parameter {} is not a valid phase name", phaseName);
135 public static @Nullable QuantityType<Angle> getElevation(ThingActions actions, @Nullable ZonedDateTime date) {
136 return ((AstroActions) actions).getElevation(date);
139 public static @Nullable QuantityType<Angle> getAzimuth(ThingActions actions, @Nullable ZonedDateTime date) {
140 return ((AstroActions) actions).getAzimuth(date);
143 public static @Nullable QuantityType<Intensity> getTotalRadiation(ThingActions actions,
144 @Nullable ZonedDateTime date) {
145 return ((AstroActions) actions).getTotalRadiation(date);
148 public static @Nullable ZonedDateTime getEventTime(ThingActions actions, @Nullable String phaseName,
149 @Nullable ZonedDateTime date, @Nullable String moment) {
150 if (phaseName != null) {
151 return ((AstroActions) actions).getEventTime(phaseName, date, moment);
153 throw new IllegalArgumentException("phaseName can not be null");