Skip to content

Commit

Permalink
[shelly] Fix timeDuration handling in DTOs (#17689)
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <[email protected]>
  • Loading branch information
J-N-K authored Nov 3, 2024
1 parent 631199a commit 18862d3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 18862d3

Please sign in to comment.