Simplify DateTimeType handling for Insteon

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2024-11-16 00:08:26 +01:00 committed by lsiepel
parent e898312e9e
commit 6e2eb70950

View File

@ -18,8 +18,6 @@ import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode; import java.math.RoundingMode;
import java.time.Instant; import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -601,19 +599,13 @@ public abstract class MessageHandler extends BaseFeatureHandler {
@Override @Override
public void handleMessage(byte cmd1, Msg msg) { public void handleMessage(byte cmd1, Msg msg) {
Instant instant = Instant.ofEpochMilli(msg.getTimestamp()); Instant timestamp = Instant.ofEpochMilli(msg.getTimestamp());
ZonedDateTime timestamp = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()); Instant lastTimestamp = feature.getState() instanceof DateTimeType datetime ? datetime.getInstant() : null;
ZonedDateTime lastTimestamp = getLastTimestamp(); // update state if not defined or is older than message timestamp
// set last time if not defined yet or message timestamp is greater than last value if (lastTimestamp == null || lastTimestamp.isBefore(timestamp)) {
if (lastTimestamp == null || timestamp.compareTo(lastTimestamp) > 0) {
feature.updateState(new DateTimeType(timestamp)); feature.updateState(new DateTimeType(timestamp));
} }
} }
private @Nullable ZonedDateTime getLastTimestamp() {
State state = feature.getState();
return state instanceof DateTimeType datetime ? datetime.getZonedDateTime() : null;
}
} }
/** /**