]> git.basschouten.com Git - openhab-addons.git/blob
5c9857aabce7979cb8132ef21a6666403be95cf7
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.action;
14
15 import java.time.ZonedDateTime;
16
17 import javax.measure.quantity.Angle;
18
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;
34
35 /**
36  * Defines the automation thing actions for the Astro binding.
37  *
38  * @author GaĆ«l L'hopital - Initial contribution
39  */
40 @ThingActionsScope(name = "astro")
41 @NonNullByDefault
42 public class AstroActions implements ThingActions {
43
44     private final Logger logger = LoggerFactory.getLogger(AstroActions.class);
45     private @Nullable AstroThingHandler handler;
46
47     public AstroActions() {
48         logger.debug("Astro actions service instanciated");
49     }
50
51     @Override
52     public void setThingHandler(@Nullable ThingHandler handler) {
53         if (handler instanceof AstroThingHandler) {
54             this.handler = (AstroThingHandler) handler;
55         }
56     }
57
58     @Override
59     public @Nullable ThingHandler getThingHandler() {
60         return handler;
61     }
62
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());
70         } else {
71             logger.info("Astro Action service ThingHandler is null!");
72         }
73         return null;
74     }
75
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());
83         } else {
84             logger.info("Astro Action service ThingHandler is null!");
85         }
86         return null;
87     }
88
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");
95         try {
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));
103                 } else {
104                     logger.info("Astro Action service ThingHandler is not a SunHandler!");
105                 }
106             } else {
107                 logger.info("Astro Action service ThingHandler is null!");
108             }
109         } catch (IllegalArgumentException e) {
110             logger.info("Parameter {} is not a valid phase name", phaseName);
111         }
112         return null;
113     }
114
115     public static @Nullable QuantityType<Angle> getElevation(ThingActions actions, @Nullable ZonedDateTime date) {
116         return ((AstroActions) actions).getElevation(date);
117     }
118
119     public static @Nullable QuantityType<Angle> getAzimuth(ThingActions actions, @Nullable ZonedDateTime date) {
120         return ((AstroActions) actions).getAzimuth(date);
121     }
122
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);
127         } else {
128             throw new IllegalArgumentException("phaseName can not be null");
129         }
130     }
131 }