]> git.basschouten.com Git - openhab-addons.git/commitdiff
[astro] Add getTotalRadiation to AstroActions (#14756)
authorDave <47106837+sislakd@users.noreply.github.com>
Fri, 7 Apr 2023 20:17:32 +0000 (22:17 +0200)
committerGitHub <noreply@github.com>
Fri, 7 Apr 2023 20:17:32 +0000 (22:17 +0200)
* Add getTotalRadiation action

Signed-off-by: David Sislak <sisdale@seznam.cz>
bundles/org.openhab.binding.astro/README.md
bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/action/AstroActions.java
bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/handler/SunHandler.java

index 3f9de1e514dcda43e1f0b2a13bda958c7e5bfff6..e906f05c35164b70bd68be31bfbdd665203b0f71 100644 (file)
@@ -265,6 +265,16 @@ Example :
  logInfo("AstroActions", "{} will be positioned at elevation {} - azimuth {}",sunEvent, elevation.toString,azimuth.toString)
 ```
 
+### getTotalRadiation(timeStamp)
+
+Retrieves the total radiation (QuantityType<Intensity>) of the sun at the requested instant.
+Thing method only applies to Sun thing type.
+
+```java
+ val totalRadiation = sunActions.getTotalRadiation(ZonedDateTime.now)
+ logInfo("AstroActions", "Currently, the total sun radiation is {}", totalRadiation.toString)
+```
+
 ## Tips
 
 Do not worry if for example the "astro dawn" is undefined at your location.
index 5c9857aabce7979cb8132ef21a6666403be95cf7..ed6b007ab132ab16f9035989ea850021120f3d60 100644 (file)
@@ -21,10 +21,12 @@ import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.binding.astro.internal.AstroBindingConstants;
 import org.openhab.binding.astro.internal.handler.AstroThingHandler;
 import org.openhab.binding.astro.internal.handler.SunHandler;
+import org.openhab.binding.astro.internal.model.Radiation;
 import org.openhab.binding.astro.internal.model.SunPhaseName;
 import org.openhab.core.automation.annotation.ActionInput;
 import org.openhab.core.automation.annotation.ActionOutput;
 import org.openhab.core.automation.annotation.RuleAction;
+import org.openhab.core.library.dimension.Intensity;
 import org.openhab.core.library.types.QuantityType;
 import org.openhab.core.thing.binding.ThingActions;
 import org.openhab.core.thing.binding.ThingActionsScope;
@@ -86,6 +88,24 @@ public class AstroActions implements ThingActions {
         return null;
     }
 
+    @RuleAction(label = "get the total sun radiation", description = "Get the total sun radiation for a given time.")
+    public @Nullable @ActionOutput(name = "getTotalRadiation", label = "Total Radiation", type = "org.openhab.core.library.types.QuantityType<org.openhab.core.library.dimension.Intensity>") QuantityType<Intensity> getTotalRadiation(
+            @ActionInput(name = "date", label = "Date", required = false, description = "Considered date") @Nullable ZonedDateTime date) {
+        logger.debug("Astro action 'getTotalRadiation' called");
+        AstroThingHandler theHandler = this.handler;
+        if (theHandler != null) {
+            if (theHandler instanceof SunHandler sunHandler) {
+                Radiation radiation = sunHandler.getRadiationAt(date != null ? date : ZonedDateTime.now());
+                return radiation.getTotal();
+            } else {
+                logger.info("Astro Action service ThingHandler is not a SunHandler!");
+            }
+        } else {
+            logger.info("Astro Action service ThingHandler is null!");
+        }
+        return null;
+    }
+
     @RuleAction(label = "get the date time of a sun event", description = "Get the date time of a sun event.")
     public @Nullable @ActionOutput(name = "getEventTime", type = "java.time.ZonedDateTime") ZonedDateTime getEventTime(
             @ActionInput(name = "phaseName", label = "Phase", required = true, description = "Requested phase") String phaseName,
@@ -120,6 +140,11 @@ public class AstroActions implements ThingActions {
         return ((AstroActions) actions).getAzimuth(date);
     }
 
+    public static @Nullable QuantityType<Intensity> getTotalRadiation(ThingActions actions,
+            @Nullable ZonedDateTime date) {
+        return ((AstroActions) actions).getTotalRadiation(date);
+    }
+
     public static @Nullable ZonedDateTime getEventTime(ThingActions actions, @Nullable String phaseName,
             @Nullable ZonedDateTime date, @Nullable String moment) {
         if (phaseName != null) {
index 6c1c2b7741a95ac26f21e33d5d84eca7fc0ce5f8..da394b681287a882b9877699f29d20d5ed88bf0b 100644 (file)
@@ -21,11 +21,7 @@ import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.binding.astro.internal.calc.SunCalc;
 import org.openhab.binding.astro.internal.job.DailyJobSun;
 import org.openhab.binding.astro.internal.job.Job;
-import org.openhab.binding.astro.internal.model.Planet;
-import org.openhab.binding.astro.internal.model.Position;
-import org.openhab.binding.astro.internal.model.Range;
-import org.openhab.binding.astro.internal.model.Sun;
-import org.openhab.binding.astro.internal.model.SunPhaseName;
+import org.openhab.binding.astro.internal.model.*;
 import org.openhab.core.i18n.TimeZoneProvider;
 import org.openhab.core.scheduler.CronScheduler;
 import org.openhab.core.thing.Thing;
@@ -95,6 +91,16 @@ public class SunHandler extends AstroThingHandler {
                 thingConfig.useMeteorologicalSeason);
     }
 
+    private Sun getPositionedSunAt(ZonedDateTime date) {
+        Sun localSun = getSunAt(date);
+        Double latitude = thingConfig.latitude;
+        Double longitude = thingConfig.longitude;
+        Double altitude = thingConfig.altitude;
+        sunCalc.setPositionalInfo(GregorianCalendar.from(date), latitude != null ? latitude : 0,
+                longitude != null ? longitude : 0, altitude != null ? altitude : 0, localSun);
+        return localSun;
+    }
+
     public @Nullable ZonedDateTime getEventTime(SunPhaseName sunPhase, ZonedDateTime date, boolean begin) {
         Range eventRange = getSunAt(date).getAllRanges().get(sunPhase);
         if (eventRange != null) {
@@ -107,12 +113,12 @@ public class SunHandler extends AstroThingHandler {
 
     @Override
     public @Nullable Position getPositionAt(ZonedDateTime date) {
-        Sun localSun = getSunAt(date);
-        Double latitude = thingConfig.latitude;
-        Double longitude = thingConfig.longitude;
-        Double altitude = thingConfig.altitude;
-        sunCalc.setPositionalInfo(GregorianCalendar.from(date), latitude != null ? latitude : 0,
-                longitude != null ? longitude : 0, altitude != null ? altitude : 0, localSun);
+        Sun localSun = getPositionedSunAt(date);
         return localSun.getPosition();
     }
+
+    public @Nullable Radiation getRadiationAt(ZonedDateTime date) {
+        Sun localSun = getPositionedSunAt(date);
+        return localSun.getRadiation();
+    }
 }