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.SunPhaseName;
25 import org.openhab.core.automation.annotation.ActionInput;
26 import org.openhab.core.automation.annotation.ActionOutput;
27 import org.openhab.core.automation.annotation.RuleAction;
28 import org.openhab.core.library.types.QuantityType;
29 import org.openhab.core.thing.binding.ThingActions;
30 import org.openhab.core.thing.binding.ThingActionsScope;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Defines the automation thing actions for the Astro binding.
38 * @author Gaƫl L'hopital - Initial contribution
40 @ThingActionsScope(name = "astro")
42 public class AstroActions implements ThingActions {
44 private final Logger logger = LoggerFactory.getLogger(AstroActions.class);
45 private @Nullable AstroThingHandler handler;
47 public AstroActions() {
48 logger.debug("Astro actions service instanciated");
52 public void setThingHandler(@Nullable ThingHandler handler) {
53 if (handler instanceof AstroThingHandler) {
54 this.handler = (AstroThingHandler) handler;
59 public @Nullable ThingHandler getThingHandler() {
63 @RuleAction(label = "get the azimuth", description = "Get the azimuth for a given time.")
64 public @Nullable @ActionOutput(name = "getAzimuth", label = "Azimuth", type = "org.openhab.core.library.types.QuantityType<javax.measure.quantity.Angle>") QuantityType<Angle> getAzimuth(
65 @ActionInput(name = "date", label = "Date", required = false, description = "Considered date") @Nullable ZonedDateTime date) {
66 logger.debug("Astro action 'getAzimuth' called");
67 AstroThingHandler theHandler = this.handler;
68 if (theHandler != null) {
69 return theHandler.getAzimuth(date != null ? date : ZonedDateTime.now());
71 logger.info("Astro Action service ThingHandler is null!");
76 @RuleAction(label = "get the elevation", description = "Get the elevation for a given time.")
77 public @Nullable @ActionOutput(name = "getElevation", label = "Elevation", type = "org.openhab.core.library.types.QuantityType<javax.measure.quantity.Angle>") QuantityType<Angle> getElevation(
78 @ActionInput(name = "date", label = "Date", required = false, description = "Considered date") @Nullable ZonedDateTime date) {
79 logger.debug("Astro action 'getElevation' called");
80 AstroThingHandler theHandler = this.handler;
81 if (theHandler != null) {
82 return theHandler.getElevation(date != null ? date : ZonedDateTime.now());
84 logger.info("Astro Action service ThingHandler is null!");
89 @RuleAction(label = "get the date time of a sun event", description = "Get the date time of a sun event.")
90 public @Nullable @ActionOutput(name = "getEventTime", type = "java.time.ZonedDateTime") ZonedDateTime getEventTime(
91 @ActionInput(name = "phaseName", label = "Phase", required = true, description = "Requested phase") String phaseName,
92 @ActionInput(name = "date", label = "Date", required = false, description = "Considered date") @Nullable ZonedDateTime date,
93 @ActionInput(name = "moment", label = "Moment", required = false, defaultValue = "START", description = "Either START or END") @Nullable String moment) {
94 logger.debug("Sun action 'getEventTime' called");
96 AstroThingHandler theHandler = this.handler;
97 if (theHandler != null) {
98 if (theHandler instanceof SunHandler) {
99 SunHandler handler = (SunHandler) theHandler;
100 SunPhaseName phase = SunPhaseName.valueOf(phaseName.toUpperCase());
101 return handler.getEventTime(phase, date != null ? date : ZonedDateTime.now(),
102 moment == null || AstroBindingConstants.EVENT_START.equalsIgnoreCase(moment));
104 logger.info("Astro Action service ThingHandler is not a SunHandler!");
107 logger.info("Astro Action service ThingHandler is null!");
109 } catch (IllegalArgumentException e) {
110 logger.info("Parameter {} is not a valid phase name", phaseName);
115 public static @Nullable QuantityType<Angle> getElevation(ThingActions actions, @Nullable ZonedDateTime date) {
116 return ((AstroActions) actions).getElevation(date);
119 public static @Nullable QuantityType<Angle> getAzimuth(ThingActions actions, @Nullable ZonedDateTime date) {
120 return ((AstroActions) actions).getAzimuth(date);
123 public static @Nullable ZonedDateTime getEventTime(ThingActions actions, @Nullable String phaseName,
124 @Nullable ZonedDateTime date, @Nullable String moment) {
125 if (phaseName != null) {
126 return ((AstroActions) actions).getEventTime(phaseName, date, moment);
128 throw new IllegalArgumentException("phaseName can not be null");