diff --git a/modules/hivemq-edge-module-file/src/main/java/com/hivemq/edge/adapters/file/payload/FileJsonPayloadCreator.java b/modules/hivemq-edge-module-file/src/main/java/com/hivemq/edge/adapters/file/payload/FileJsonPayloadCreator.java index c5d18533e..5e4e35796 100644 --- a/modules/hivemq-edge-module-file/src/main/java/com/hivemq/edge/adapters/file/payload/FileJsonPayloadCreator.java +++ b/modules/hivemq-edge-module-file/src/main/java/com/hivemq/edge/adapters/file/payload/FileJsonPayloadCreator.java @@ -17,11 +17,11 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.hivemq.adapter.sdk.api.config.PollingContext; import com.hivemq.adapter.sdk.api.data.DataPoint; import com.hivemq.adapter.sdk.api.data.JsonPayloadCreator; import com.hivemq.adapter.sdk.api.data.ProtocolAdapterDataSample; import com.hivemq.edge.adapters.file.FilePollingProtocolAdapter; -import com.hivemq.edge.adapters.file.config.FileToMqttMapping; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,18 +37,22 @@ public class FileJsonPayloadCreator implements JsonPayloadCreator { @Override public @NotNull List convertToJson( final @NotNull ProtocolAdapterDataSample sample, final @NotNull ObjectMapper objectMapper) { - List payloads = new ArrayList<>(); - for (DataPoint dataPoint : sample.getDataPoints()) { + final List payloads = new ArrayList<>(); + for (final DataPoint dataPoint : sample.getDataPoints()) { try { - FileDataPoint fileDataPoint = (FileDataPoint) dataPoint; - final FilePayload value = new FilePayload(System.currentTimeMillis(), - sample.getPollingContext().getUserProperties(), + + final PollingContext pollingContext = sample.getPollingContext(); + + final FileDataPoint fileDataPoint = (FileDataPoint) dataPoint; + final FilePayload value = new FilePayload(pollingContext.getUserProperties(), dataPoint.getTagValue(), - dataPoint.getTagName(), - fileDataPoint.getTag().getDefinition().getContentType()); + fileDataPoint.getTag().getDefinition().getContentType(), + pollingContext.getIncludeTagNames() ? dataPoint.getTagName() : null, + pollingContext.getIncludeTimestamp() ? System.currentTimeMillis() : null); + payloads.add(objectMapper.writeValueAsBytes(value)); - } catch (JsonProcessingException e) { + } catch (final JsonProcessingException e) { LOG.warn("Unable to create payload for data data point '{}'. Skipping this data point.", dataPoint); } } diff --git a/modules/hivemq-edge-module-file/src/main/java/com/hivemq/edge/adapters/file/payload/FilePayload.java b/modules/hivemq-edge-module-file/src/main/java/com/hivemq/edge/adapters/file/payload/FilePayload.java index b1450becb..91226a03e 100644 --- a/modules/hivemq-edge-module-file/src/main/java/com/hivemq/edge/adapters/file/payload/FilePayload.java +++ b/modules/hivemq-edge-module-file/src/main/java/com/hivemq/edge/adapters/file/payload/FilePayload.java @@ -27,8 +27,9 @@ @SuppressWarnings({"FieldCanBeLocal", "unused"}) public class FilePayload { + @JsonInclude(JsonInclude.Include.NON_NULL) @JsonProperty("timestamp") - private final @NotNull Long timestamp; + private final @Nullable Long timestamp; @JsonInclude(JsonInclude.Include.NON_EMPTY) private final @Nullable List userProperties; @@ -36,15 +37,19 @@ public class FilePayload { @JsonProperty("value") private final @NotNull Object value; - @JsonProperty("tagName") - private final @NotNull String tagName; - @JsonProperty("contentType") private final @NotNull String contentType; + @JsonInclude(JsonInclude.Include.NON_NULL) + @JsonProperty("tagName") + private final @Nullable String tagName; + public FilePayload( - final @NotNull Long timestamp, final @NotNull List userProperties, - final @NotNull Object value, final @NotNull String tagName, final @NotNull ContentType contentType) { + final @NotNull List userProperties, + final @NotNull Object value, + final @NotNull ContentType contentType, + final @Nullable String tagName, + final @Nullable Long timestamp) { this.timestamp = timestamp; this.userProperties = userProperties; this.value = value;