Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Support timezone in the get datapoints query payloads (#2595)

Eric Bariauxf93404f1ca08
diff --git a/model/src/main/java/org/openremote/model/datapoint/query/AssetDatapointQuery.java b/model/src/main/java/org/openremote/model/datapoint/query/AssetDatapointQuery.java
index e12690d63..6ad9745ca 100644
--- a/model/src/main/java/org/openremote/model/datapoint/query/AssetDatapointQuery.java
+++ b/model/src/main/java/org/openremote/model/datapoint/query/AssetDatapointQuery.java
@@ -21,6 +21,7 @@ package org.openremote.model.datapoint.query;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.openremote.model.attribute.AttributeRef;
import java.io.Serializable;
@@ -43,7 +44,9 @@ public abstract class AssetDatapointQuery implements Serializable {
public long fromTimestamp;
public long toTimestamp;
+ @JsonDeserialize(using = AssetDatapointQueryLocalDateTimeDeserializer.class)
public LocalDateTime fromTime;
+ @JsonDeserialize(using = AssetDatapointQueryLocalDateTimeDeserializer.class)
public LocalDateTime toTime;
public String getSQLQuery(String tableName, Class<?> attributeType) throws IllegalStateException {
diff --git a/model/src/main/java/org/openremote/model/datapoint/query/AssetDatapointQueryLocalDateTimeDeserializer.java b/model/src/main/java/org/openremote/model/datapoint/query/AssetDatapointQueryLocalDateTimeDeserializer.java
new file mode 100644
index 000000000..14ecaaa8c
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/datapoint/query/AssetDatapointQueryLocalDateTimeDeserializer.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2026, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.model.datapoint.query;
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonToken;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonDeserializer;
+import com.fasterxml.jackson.databind.exc.InvalidFormatException;
+
+import java.io.IOException;
+import java.time.LocalDateTime;
+import java.time.OffsetDateTime;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeParseException;
+
+/**
+ * Deserializes datapoint query times from either local date-time strings or
+ * offset date-time strings and normalizes offset date-times to server local time.
+ */
+public class AssetDatapointQueryLocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
+
+ @Override
+ public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
+ if (p.currentToken() == JsonToken.VALUE_NULL) {
+ return null;
+ }
+ if (p.currentToken() != JsonToken.VALUE_STRING) {
+ return (LocalDateTime) ctxt.handleUnexpectedToken(LocalDateTime.class, p);
+ }
+
+ String raw = p.getText();
+ String value = raw != null ? raw.trim() : "";
+ if (value.isEmpty()) {
+ return null;
+ }
+
+ try {
+ return LocalDateTime.parse(value, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
+ } catch (DateTimeParseException ignored) {
+ // Keep trying offset-aware parsing below.
+ }
+
+ try {
+ return OffsetDateTime.parse(value, DateTimeFormatter.ISO_OFFSET_DATE_TIME)
+ .atZoneSameInstant(ZoneId.systemDefault())
+ .toLocalDateTime();
+ } catch (DateTimeParseException ex) {
+ throw InvalidFormatException.from(
+ p,
+ "Expected ISO_LOCAL_DATE_TIME or ISO_OFFSET_DATE_TIME for AssetDatapointQuery time field",
+ value,
+ LocalDateTime.class
+ );
+ }
+ }
+}
diff --git a/test/src/test/groovy/org/openremote/manager/datapoint/AssetDatapointQueryDeserializationTest.groovy b/test/src/test/groovy/org/openremote/manager/datapoint/AssetDatapointQueryDeserializationTest.groovy
new file mode 100644
index 000000000..f4a13c521
--- /dev/null
+++ b/test/src/test/groovy/org/openremote/manager/datapoint/AssetDatapointQueryDeserializationTest.groovy
@@ -0,0 +1,78 @@
+package org.openremote.manager.datapoint
+
+import org.openremote.model.datapoint.query.AssetDatapointAllQuery
+import org.openremote.model.datapoint.query.AssetDatapointQuery
+import org.openremote.model.util.ValueUtil
+import spock.lang.Specification
+
+import java.time.LocalDateTime
+import java.time.OffsetDateTime
+import java.time.ZoneId
+
+class AssetDatapointQueryDeserializationTest extends Specification {
+
+ def "AssetDatapointQuery deserializes fromTime without timezone as LocalDateTime"() {
+ given:
+ def json = '''
+ {
+ "type": "all",
+ "fromTime": "2026-02-12T00:00:00.000",
+ "toTime": "2026-02-12T01:00:00.000"
+ }
+ '''
+
+ when:
+ AssetDatapointQuery query = ValueUtil.JSON.readValue(json, AssetDatapointQuery.class)
+
+ then:
+ query instanceof AssetDatapointAllQuery
+ query.fromTime == LocalDateTime.of(2026, 2, 12, 0, 0, 0, 0)
+ query.toTime == LocalDateTime.of(2026, 2, 12, 1, 0, 0, 0)
+ }
+
+ def "AssetDatapointQuery deserializes fromTime with Z and converts to server timezone"() {
+ given:
+ def json = '''
+ {
+ "type": "all",
+ "fromTime": "2026-02-12T00:00:00.000Z",
+ "toTime": "2026-02-12T01:00:00.000Z"
+ }
+ '''
+
+ when:
+ AssetDatapointQuery query = ValueUtil.JSON.readValue(json, AssetDatapointQuery.class)
+
+ then:
+ query instanceof AssetDatapointAllQuery
+ query.fromTime == OffsetDateTime.parse("2026-02-12T00:00:00.000Z")
+ .atZoneSameInstant(ZoneId.systemDefault())
+ .toLocalDateTime()
+ query.toTime == OffsetDateTime.parse("2026-02-12T01:00:00.000Z")
+ .atZoneSameInstant(ZoneId.systemDefault())
+ .toLocalDateTime()
+ }
+
+ def "AssetDatapointQuery deserializes fromTime with explicit numeric offset and converts to server timezone"() {
+ given:
+ def json = '''
+ {
+ "type": "all",
+ "fromTime": "2026-02-12T00:00:00.000+02:00",
+ "toTime": "2026-02-12T01:00:00.000+02:00"
+ }
+ '''
+
+ when:
+ AssetDatapointQuery query = ValueUtil.JSON.readValue(json, AssetDatapointQuery.class)
+
+ then:
+ query instanceof AssetDatapointAllQuery
+ query.fromTime == OffsetDateTime.parse("2026-02-12T00:00:00.000+02:00")
+ .atZoneSameInstant(ZoneId.systemDefault())
+ .toLocalDateTime()
+ query.toTime == OffsetDateTime.parse("2026-02-12T01:00:00.000+02:00")
+ .atZoneSameInstant(ZoneId.systemDefault())
+ .toLocalDateTime()
+ }
+}