From 31ac8d3c01a422fbc1fef559fbde1a979edc27f6 Mon Sep 17 00:00:00 2001 From: "D. Berge" Date: Mon, 28 Jul 2025 10:07:49 +0200 Subject: [PATCH] Add toJSON() function to binary decoder --- lib/www/server/lib/binary/decode.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/www/server/lib/binary/decode.js b/lib/www/server/lib/binary/decode.js index ce30b04..0fe0152 100644 --- a/lib/www/server/lib/binary/decode.js +++ b/lib/www/server/lib/binary/decode.js @@ -103,7 +103,30 @@ function unpack (data, index=0, endianness = platformEndianness) { return bundles; } +function toJSON (data, index=0, endianness = platformEndianness) { + const items = []; + const chunks = unpack(data, index, endianness); + for (const chunk of chunks) { + const type = chunk.type; + const sequence = chunk.seq; + let point = chunk.sp0; + let timestamp = Number(chunk.ts0); + for (let i=0; i < chunk.count; i++) { + const longitude = chunk.positions[i*2 + 0]; + const latitude = chunk.positions[i*2 + 1]; + timestamp += chunk.Δts[i]; + const Δi = chunk.Δij[i*2 + 0]; + const Δj = chunk.Δij[i*2 + 1]; + + items.push({ type, sequence, point, timestamp, longitude, latitude, Δi, Δj }); + point += chunk.Δsp; + } + } + return items; +} + module.exports = { unbundle, - unpack + unpack, + toJSON };