From cc4b5902c7e26905a2a4d4722b98d52b76bf480c Mon Sep 17 00:00:00 2001 From: Luca Arato Date: Mon, 19 Aug 2024 18:02:51 +0200 Subject: [PATCH] Fix expiry timestamp Co-authored-by: Antonio Gisondi Signed-off-by: Luca Arato --- .../devicesdk/android/AstarteAndroidFailedMessage.java | 9 +++++++-- .../devicesdk/generic/AstarteGenericFailedMessage.java | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/DeviceSDKAndroid/src/main/java/org/astarteplatform/devicesdk/android/AstarteAndroidFailedMessage.java b/DeviceSDKAndroid/src/main/java/org/astarteplatform/devicesdk/android/AstarteAndroidFailedMessage.java index 4429dbe..1019c4f 100644 --- a/DeviceSDKAndroid/src/main/java/org/astarteplatform/devicesdk/android/AstarteAndroidFailedMessage.java +++ b/DeviceSDKAndroid/src/main/java/org/astarteplatform/devicesdk/android/AstarteAndroidFailedMessage.java @@ -4,6 +4,7 @@ import androidx.room.ColumnInfo; import androidx.room.Entity; import androidx.room.PrimaryKey; +import java.util.concurrent.TimeUnit; import org.astarteplatform.devicesdk.transport.AstarteFailedMessage; @Entity(tableName = "failed_messages") @@ -30,7 +31,7 @@ public AstarteAndroidFailedMessage(String topic, byte[] payload, int qos) { } public AstarteAndroidFailedMessage(String topic, byte[] payload, int qos, int relativeExpiry) { - this.absoluteExpiry = System.currentTimeMillis() + relativeExpiry; + this.absoluteExpiry = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(relativeExpiry); this.topic = topic; this.payload = payload; this.qos = qos; @@ -61,7 +62,11 @@ public void setAbsoluteExpiry(long absoluteExpiry) { @Override public boolean isExpired() { - return absoluteExpiry > System.currentTimeMillis(); + if (absoluteExpiry <= 0) { + return false; + } + + return absoluteExpiry < System.currentTimeMillis(); } public long getStorageId() { diff --git a/DeviceSDKGeneric/src/main/java/org/astarteplatform/devicesdk/generic/AstarteGenericFailedMessage.java b/DeviceSDKGeneric/src/main/java/org/astarteplatform/devicesdk/generic/AstarteGenericFailedMessage.java index cbf78a2..e14643a 100644 --- a/DeviceSDKGeneric/src/main/java/org/astarteplatform/devicesdk/generic/AstarteGenericFailedMessage.java +++ b/DeviceSDKGeneric/src/main/java/org/astarteplatform/devicesdk/generic/AstarteGenericFailedMessage.java @@ -3,6 +3,7 @@ import com.j256.ormlite.field.DataType; import com.j256.ormlite.field.DatabaseField; import com.j256.ormlite.table.DatabaseTable; +import java.util.concurrent.TimeUnit; import org.astarteplatform.devicesdk.transport.AstarteFailedMessage; @DatabaseTable(tableName = "failed_messages") @@ -34,7 +35,7 @@ public AstarteGenericFailedMessage(String topic, byte[] payload, int qos) { } public AstarteGenericFailedMessage(String topic, byte[] payload, int qos, int relativeExpiry) { - this.absoluteExpiry = System.currentTimeMillis() + relativeExpiry; + this.absoluteExpiry = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(relativeExpiry); this.topic = topic; this.payload = payload; this.qos = qos; @@ -57,7 +58,11 @@ public int getQos() { @Override public boolean isExpired() { - return absoluteExpiry > System.currentTimeMillis(); + if (absoluteExpiry <= 0) { + return false; + } + + return absoluteExpiry < System.currentTimeMillis(); } public long getStorageId() {