From 4ad5eba7e071b9c60928adaf4494f35958265840 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Fri, 26 Nov 2021 21:03:11 +0100 Subject: [PATCH] Change tag-srtting to recursive by field This will properly set e.g. arrays --- src/streamHandler/PacketInfluxPointFactory.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/streamHandler/PacketInfluxPointFactory.js b/src/streamHandler/PacketInfluxPointFactory.js index a89415e..147658e 100644 --- a/src/streamHandler/PacketInfluxPointFactory.js +++ b/src/streamHandler/PacketInfluxPointFactory.js @@ -42,8 +42,11 @@ class PacketInfluxPointFactory extends Transform{ let point = new Point(measurement); // Create point // Set tags - TAG_LIST.filter(tag => Object.keys(packet).includes(tag)) - .forEach(tag => point.tag(tag, packet[tag])); + TAG_LIST.filter(tag => Object.keys(packet).includes(tag)) // Filter tags available on object + .filter(tag => packet[tag] != null) // Filter tags not falsy on object + .forEach(tag => { + tagObjectRecursively(point, tag, packet[tag]); + }); point.setField('value', packet[objKey]); // Set field @@ -54,6 +57,15 @@ class PacketInfluxPointFactory extends Transform{ } } +function tagObjectRecursively(point, tag, field, suffix = ""){ + if(typeof(field) == "object"){ + // TODO: Convert boolean-arrays like "packet.flags" to key: value + Object.entries(field).map(([key, value]) => { + tagObjectRecursively(point, tag, value, `_${key}${suffix}`); + }); + } + else point.tag(tag+suffix, field); +} /** Mapping for type -> field-method */ const POINT_FIELD_TYPE = new Map([