]> git.basschouten.com Git - openhab-addons.git/blob
c4af735d7bce59bd8db421a15f9f4830998fe73a
[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.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;
36
37 /**
38  * Defines the automation thing actions for the Astro binding.
39  *
40  * @author GaĆ«l L'hopital - Initial contribution
41  */
42 @ThingActionsScope(name = "astro")
43 @NonNullByDefault
44 public class AstroActions implements ThingActions {
45
46     private final Logger logger = LoggerFactory.getLogger(AstroActions.class);
47     private @Nullable AstroThingHandler handler;
48
49     public AstroActions() {
50         logger.debug("Astro actions service instanciated");
51     }
52
53     @Override
54     public void setThingHandler(@Nullable ThingHandler handler) {
55         if (handler instanceof AstroThingHandler thingHandler) {
56             this.handler = thingHandler;
57         }
58     }
59
60     @Override
61     public @Nullable ThingHandler getThingHandler() {
62         return handler;
63     }
64
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());
72         } else {
73             logger.info("Astro Action service ThingHandler is null!");
74         }
75         return null;
76     }
77
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());
85         } else {
86             logger.info("Astro Action service ThingHandler is null!");
87         }
88         return null;
89     }
90
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();
100             } else {
101                 logger.info("Astro Action service ThingHandler is not a SunHandler!");
102             }
103         } else {
104             logger.info("Astro Action service ThingHandler is null!");
105         }
106         return null;
107     }
108
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");
115         try {
116             AstroThingHandler theHandler = this.handler;
117             if (theHandler != null) {
118                 if (theHandler instanceof SunHandler sunHandler) {
119                     SunPhaseName phase = SunPhaseName.valueOf(phaseName.toUpperCase());
120                     return sunHandler.getEventTime(phase, date != null ? date : ZonedDateTime.now(),
121                             moment == null || AstroBindingConstants.EVENT_START.equalsIgnoreCase(moment));
122                 } else {
123                     logger.info("Astro Action service ThingHandler is not a SunHandler!");
124                 }
125             } else {
126                 logger.info("Astro Action service ThingHandler is null!");
127             }
128         } catch (IllegalArgumentException e) {
129             logger.info("Parameter {} is not a valid phase name", phaseName);
130         }
131         return null;
132     }
133
134     public static @Nullable QuantityType<Angle> getElevation(ThingActions actions, @Nullable ZonedDateTime date) {
135         return ((AstroActions) actions).getElevation(date);
136     }
137
138     public static @Nullable QuantityType<Angle> getAzimuth(ThingActions actions, @Nullable ZonedDateTime date) {
139         return ((AstroActions) actions).getAzimuth(date);
140     }
141
142     public static @Nullable QuantityType<Intensity> getTotalRadiation(ThingActions actions,
143             @Nullable ZonedDateTime date) {
144         return ((AstroActions) actions).getTotalRadiation(date);
145     }
146
147     public static @Nullable ZonedDateTime getEventTime(ThingActions actions, @Nullable String phaseName,
148             @Nullable ZonedDateTime date, @Nullable String moment) {
149         if (phaseName != null) {
150             return ((AstroActions) actions).getEventTime(phaseName, date, moment);
151         } else {
152             throw new IllegalArgumentException("phaseName can not be null");
153         }
154     }
155 }