From f62a42ada4b564c0a94d36b9d907208f582cb32a Mon Sep 17 00:00:00 2001 From: Zaid Abdulla Date: Mon, 1 Feb 2021 13:05:55 -0800 Subject: [PATCH] Fix edge case bug preventing GPN on root param --- lib/db.ts | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/db.ts b/lib/db.ts index c87b9cf..bac2af7 100644 --- a/lib/db.ts +++ b/lib/db.ts @@ -131,7 +131,12 @@ export async function fetchDevice( const device = await devicesCollection.findOne({ _id: id }); if (!device) return null; - function storeParams(obj, path: string, pathLength: number, ts): void { + function storeParams( + obj, + path: string, + pathLength: number, + ts: number + ): void { if (obj["_timestamp"]) obj["_timestamp"] = +obj["_timestamp"]; if (obj["_attributesTimestamp"]) obj["_attributesTimestamp"] = +obj["_attributesTimestamp"]; @@ -162,19 +167,22 @@ export async function fetchDevice( if (obj["_accessList"] != null) attrs.accessList = [obj["_attributesTimestamp"] || 1, obj["_accessList"]]; - res.push([Path.parse(path.slice(0, -1)), t, attrs]); + res.push([Path.parse(path), t, attrs]); for (const [k, v] of Object.entries(obj)) { if (!k.startsWith("_")) { obj["_object"] = true; - storeParams(v, path + k + ".", pathLength + 1, obj["_timestamp"]); + storeParams(v, `${path}.${k}`, pathLength + 1, obj["_timestamp"]); } } - if ((obj["_object"] || !pathLength) && obj["_timestamp"]) - res.push([Path.parse(path + "*"), obj["_timestamp"]]); + if (obj["_object"] && obj["_timestamp"]) + res.push([Path.parse(path + ".*"), obj["_timestamp"]]); } + const ts: number = device["_timestamp"] || 0; + if (ts) res.push([Path.parse("*"), ts]); + for (const [k, v] of Object.entries(device)) { switch (k) { case "_lastInform": @@ -302,10 +310,11 @@ export async function fetchDevice( }, ]); } + break; + default: + if (!k.startsWith("_")) storeParams(v, k, 1, ts); } } - - storeParams(device, "", 0, 0); return res; }