From 0dc0b0d2564193794738204cbbad5ced28fb2504 Mon Sep 17 00:00:00 2001 From: Sascha Doemer Date: Fri, 1 Mar 2024 21:48:08 +0100 Subject: [PATCH] Bugfix/fix invalid json (#39) * Introduce DateTimeFormatter in DeviceMeasurementBuilder This commit introduces a DateTimeFormatter within the DeviceMeasurementBuilder for accurate date-time formatting. Changes also include updated attribute naming for 'location' and 'dateObserved', adhering to the FIWARE Context Broker's supported format. * Modify JSON formatting in Metadata class This commit changes the JSON string formatting method in the Metadata class. The value variable is now enclosed within quotation marks, ensuring proper JSON output irrespective of the value's data type. * Update version in pom.xml file The version of fiware-integration-layer in the pom.xml file has been updated from 10.1.0 to 10.2.0. This update signifies a minor release with bug fixes and feature improvements. --- pom.xml | 2 +- .../model/builder/DeviceMeasurementBuilder.java | 14 +++++++++++--- .../app/fivegla/fiware/model/generic/Metadata.java | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 926be72..bd14b95 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ de.app.5gla fiware-integration-layer - 10.1.0 + 10.2.0 5gLa FIWARE integration layer https://github.com/vitrum-connect/5gla-fiware-integration-layer diff --git a/src/main/java/de/app/fivegla/fiware/model/builder/DeviceMeasurementBuilder.java b/src/main/java/de/app/fivegla/fiware/model/builder/DeviceMeasurementBuilder.java index 8f2ecbf..f8670e2 100644 --- a/src/main/java/de/app/fivegla/fiware/model/builder/DeviceMeasurementBuilder.java +++ b/src/main/java/de/app/fivegla/fiware/model/builder/DeviceMeasurementBuilder.java @@ -7,6 +7,8 @@ import de.app.fivegla.fiware.model.generic.Metadata; import java.time.Instant; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; /** @@ -16,6 +18,11 @@ public final class DeviceMeasurementBuilder { private final DeviceMeasurement deviceMeasurement; + private final DateTimeFormatter formatter = DateTimeFormatter + // The supported format for the FIWARE Context Broker is "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'". + // 2017-06-17T07:21:24.238Z + .ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") + .withZone(ZoneId.systemDefault()); /** * The DeviceMeasurementBuilder class is responsible for building instances of the DeviceMeasurement class. @@ -46,8 +53,9 @@ public DeviceMeasurementBuilder withId(String id) { */ public DeviceMeasurementBuilder withLocation(double latitude, double longitude) { var attribute = new Attribute(); - attribute.setValue("{\"type\":\"Point\",\"coordinates\":[" + longitude + "," + latitude + "]}"); + attribute.setName("location"); attribute.setType(FiwareTypes.GEO_POINT.getKey()); + attribute.setValue("{\"type\":\"Point\",\"coordinates\":[" + longitude + "," + latitude + "]}"); deviceMeasurement.setLocation(attribute); return this; } @@ -68,9 +76,9 @@ public DeviceMeasurementBuilder withMeasurement(String name, String type, String measurement.setValue(value); var metadata = new ArrayList(); var dateObservedMetadata = new Metadata(); - dateObservedMetadata.setName("dateObservedMetadata"); + dateObservedMetadata.setName("dateObserved"); dateObservedMetadata.setType(FiwareTypes.DATE_TIME.getKey()); - dateObservedMetadata.setValue(dateObservedMetadata.toString()); + dateObservedMetadata.setValue(formatter.format(dateObserved)); metadata.add(dateObservedMetadata); if (null != metadataEntries && metadataEntries.length > 0) { for (var metadataEntry : metadataEntries) { diff --git a/src/main/java/de/app/fivegla/fiware/model/generic/Metadata.java b/src/main/java/de/app/fivegla/fiware/model/generic/Metadata.java index e9add85..7edbe14 100644 --- a/src/main/java/de/app/fivegla/fiware/model/generic/Metadata.java +++ b/src/main/java/de/app/fivegla/fiware/model/generic/Metadata.java @@ -35,6 +35,6 @@ public class Metadata { private String value; public String asJson() { - return "{\"name\":\"" + name + "\",\"type\":\"" + type + "\",\"value\":" + value + "}"; + return "{\"name\":\"" + name + "\",\"type\":\"" + type + "\",\"value\":\"" + value + "\"}"; } }