Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[shelly] Fix timeDuration handling in DTOs #17689

Merged
merged 1 commit into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -591,12 +591,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 @@ -872,7 +872,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