Skip to content

Commit 18862d3

Browse files
authored
[shelly] Fix timeDuration handling in DTOs (openhab#17689)
Signed-off-by: Jan N. Klug <[email protected]>
1 parent 631199a commit 18862d3

File tree

4 files changed

+14
-13
lines changed

4 files changed

+14
-13
lines changed

bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiClient.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -605,12 +605,12 @@ private boolean updateDimmerStatus(ShellySettingsStatus status, @Nullable Shelly
605605
return channelUpdate ? ShellyComponents.updateDimmers(getThing(), status) : false;
606606
}
607607

608-
protected @Nullable Integer getDuration(@Nullable Double timerStartedAt, @Nullable Integer timerDuration) {
608+
protected @Nullable Integer getDuration(@Nullable Double timerStartedAt, @Nullable Double timerDuration) {
609609
if (timerStartedAt == null || timerDuration == null) {
610610
return null;
611611
}
612-
int duration = (int) (now() - timerStartedAt.longValue());
613-
return duration <= timerDuration ? timerDuration - duration : 0;
612+
double duration = now() - timerStartedAt;
613+
return duration <= timerDuration ? (int) (timerDuration - duration) : 0;
614614
}
615615

616616
// Addon

bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiJsonDTO.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ public static class Shelly2DeviceStatusLight {
580580
@SerializedName("timer_started_at")
581581
public Double timerStartedAt;
582582
@SerializedName("timer_duration")
583-
public Integer timerDuration;
583+
public Double timerDuration;
584584
}
585585

586586
public static class Shelly2DeviceStatusResult {
@@ -882,7 +882,7 @@ public static class Shelly2Pm1Status {
882882
@SerializedName("timer_started_at")
883883
public Double timerStartetAt;
884884
@SerializedName("timer_duration")
885-
public Integer timerDuration;
885+
public Double timerDuration;
886886
public Double apower;
887887
public Double voltage;
888888
public Double current;

bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/handler/ShellyBaseHandler.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public abstract class ShellyBaseHandler extends BaseThingHandler
120120
private String lastWakeupReason = "";
121121

122122
// Scheduler
123-
private long watchdog = now();
123+
private double watchdog = now();
124124
protected int scheduledUpdates = 0;
125125
private int skipCount = UPDATE_SKIP_COUNT;
126126
private int skipUpdate = 0;
@@ -724,9 +724,9 @@ public void restartWatchdog() {
724724
}
725725

726726
private boolean isWatchdogExpired() {
727-
long delta = now() - watchdog;
727+
double delta = now() - watchdog;
728728
if ((watchdog > 0) && (delta > profile.updatePeriod)) {
729-
stats.remainingWatchdog = delta;
729+
stats.remainingWatchdog = (long) delta;
730730
return true;
731731
}
732732
return false;
@@ -761,7 +761,7 @@ public void fillDeviceStatus(ShellySettingsStatus status, boolean updated) {
761761
stats.timeoutErrors = api.getTimeoutErrors();
762762
stats.timeoutsRecorvered = api.getTimeoutsRecovered();
763763
}
764-
stats.remainingWatchdog = watchdog > 0 ? now() - watchdog : 0;
764+
stats.remainingWatchdog = watchdog > 0 ? (long) (now() - watchdog) : 0;
765765

766766
// Check various device indicators like overheating
767767
if (checkRestarted(status)) {
@@ -855,7 +855,7 @@ public void postEvent(String event, boolean force) {
855855
triggerChannel(channelId, event);
856856
cache.updateChannel(channelId, getStringType(event.toUpperCase()));
857857
stats.lastAlarm = event;
858-
stats.lastAlarmTs = now();
858+
stats.lastAlarmTs = (long) now();
859859
stats.alarms++;
860860
}
861861
}

bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/util/ShellyUtils.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.time.ZoneId;
2828
import java.time.ZonedDateTime;
2929
import java.time.format.DateTimeFormatter;
30+
import java.time.temporal.ChronoUnit;
3031

3132
import javax.measure.Unit;
3233

@@ -290,12 +291,12 @@ public static String urlEncode(String input) {
290291
}
291292
}
292293

293-
public static Long now() {
294-
return System.currentTimeMillis() / 1000L;
294+
public static double now() {
295+
return System.currentTimeMillis() / 1000.0;
295296
}
296297

297298
public static DateTimeType getTimestamp() {
298-
return new DateTimeType(ZonedDateTime.ofInstant(Instant.ofEpochSecond(now()), ZoneId.systemDefault()));
299+
return new DateTimeType(ZonedDateTime.now().truncatedTo(ChronoUnit.SECONDS));
299300
}
300301

301302
public static DateTimeType getTimestamp(String zone, long timestamp) {

0 commit comments

Comments
 (0)