Skip to content

Commit

Permalink
Added null check for metric timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
djnewbould committed Jun 28, 2024
1 parent a7f645d commit 2e17e6c
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion influxdb-sparkplug-ingester/lib/mqttclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,17 @@ export default class MQTTClient {
if (!birth) {
logger.error(`Metric ${metric.alias} is unknown for ${topic.address.group}/${topic.address.node}/${topic.address.device}`);
}
const metricTimestamp = new Date(metric.timestamp);

let metricTimestamp: Date
if (metric.timestamp) {
metricTimestamp = new Date(metric.timestamp);
} else if (payload.timestamp) {
// Metrics might not have a timestamp so use the packet timestamp if we have it.
metricTimestamp = new Date(payload.timestamp);
} else {
// No timestamp can be found on the metric or the payload, just use the current time instead.
metricTimestamp = new Date();
}
// Send each metric to InfluxDB
this.writeToInfluxDB(birth, topic, metric.value, metricTimestamp)
});
Expand Down

0 comments on commit 2e17e6c

Please sign in to comment.