From 18862d3e28d1061b1daca5a54f547236bbf7df10 Mon Sep 17 00:00:00 2001 From: J-N-K Date: Sun, 3 Nov 2024 20:55:25 +0100 Subject: [PATCH] [shelly] Fix timeDuration handling in DTOs (#17689) Signed-off-by: Jan N. Klug --- .../binding/shelly/internal/api2/Shelly2ApiClient.java | 6 +++--- .../shelly/internal/api2/Shelly2ApiJsonDTO.java | 4 ++-- .../shelly/internal/handler/ShellyBaseHandler.java | 10 +++++----- .../binding/shelly/internal/util/ShellyUtils.java | 7 ++++--- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiClient.java b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiClient.java index b8cdcee251ef6..138d7fb4f6470 100644 --- a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiClient.java +++ b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiClient.java @@ -605,12 +605,12 @@ private boolean updateDimmerStatus(ShellySettingsStatus status, @Nullable Shelly return channelUpdate ? ShellyComponents.updateDimmers(getThing(), status) : false; } - protected @Nullable Integer getDuration(@Nullable Double timerStartedAt, @Nullable Integer timerDuration) { + protected @Nullable Integer getDuration(@Nullable Double timerStartedAt, @Nullable Double timerDuration) { if (timerStartedAt == null || timerDuration == null) { return null; } - int duration = (int) (now() - timerStartedAt.longValue()); - return duration <= timerDuration ? timerDuration - duration : 0; + double duration = now() - timerStartedAt; + return duration <= timerDuration ? (int) (timerDuration - duration) : 0; } // Addon diff --git a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiJsonDTO.java b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiJsonDTO.java index e85ad3e57be3d..55cadfd360e90 100644 --- a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiJsonDTO.java +++ b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiJsonDTO.java @@ -580,7 +580,7 @@ public static class Shelly2DeviceStatusLight { @SerializedName("timer_started_at") public Double timerStartedAt; @SerializedName("timer_duration") - public Integer timerDuration; + public Double timerDuration; } public static class Shelly2DeviceStatusResult { @@ -882,7 +882,7 @@ public static class Shelly2Pm1Status { @SerializedName("timer_started_at") public Double timerStartetAt; @SerializedName("timer_duration") - public Integer timerDuration; + public Double timerDuration; public Double apower; public Double voltage; public Double current; diff --git a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/handler/ShellyBaseHandler.java b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/handler/ShellyBaseHandler.java index 36ff91f8682d7..ab3e20dc498a0 100644 --- a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/handler/ShellyBaseHandler.java +++ b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/handler/ShellyBaseHandler.java @@ -120,7 +120,7 @@ public abstract class ShellyBaseHandler extends BaseThingHandler private String lastWakeupReason = ""; // Scheduler - private long watchdog = now(); + private double watchdog = now(); protected int scheduledUpdates = 0; private int skipCount = UPDATE_SKIP_COUNT; private int skipUpdate = 0; @@ -724,9 +724,9 @@ public void restartWatchdog() { } private boolean isWatchdogExpired() { - long delta = now() - watchdog; + double delta = now() - watchdog; if ((watchdog > 0) && (delta > profile.updatePeriod)) { - stats.remainingWatchdog = delta; + stats.remainingWatchdog = (long) delta; return true; } return false; @@ -761,7 +761,7 @@ public void fillDeviceStatus(ShellySettingsStatus status, boolean updated) { stats.timeoutErrors = api.getTimeoutErrors(); stats.timeoutsRecorvered = api.getTimeoutsRecovered(); } - stats.remainingWatchdog = watchdog > 0 ? now() - watchdog : 0; + stats.remainingWatchdog = watchdog > 0 ? (long) (now() - watchdog) : 0; // Check various device indicators like overheating if (checkRestarted(status)) { @@ -855,7 +855,7 @@ public void postEvent(String event, boolean force) { triggerChannel(channelId, event); cache.updateChannel(channelId, getStringType(event.toUpperCase())); stats.lastAlarm = event; - stats.lastAlarmTs = now(); + stats.lastAlarmTs = (long) now(); stats.alarms++; } } diff --git a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/util/ShellyUtils.java b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/util/ShellyUtils.java index db494f2c177ec..735921565b438 100644 --- a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/util/ShellyUtils.java +++ b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/util/ShellyUtils.java @@ -27,6 +27,7 @@ import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; import javax.measure.Unit; @@ -290,12 +291,12 @@ public static String urlEncode(String input) { } } - public static Long now() { - return System.currentTimeMillis() / 1000L; + public static double now() { + return System.currentTimeMillis() / 1000.0; } public static DateTimeType getTimestamp() { - return new DateTimeType(ZonedDateTime.ofInstant(Instant.ofEpochSecond(now()), ZoneId.systemDefault())); + return new DateTimeType(ZonedDateTime.now().truncatedTo(ChronoUnit.SECONDS)); } public static DateTimeType getTimestamp(String zone, long timestamp) {