From 9c67e676734183f9730a504da97ecd117c01ebba Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Wed, 11 Oct 2023 16:15:46 +0200 Subject: [PATCH] parse single mqtt measure --- lib/commonBindings.js | 9 +++++++-- test/unit/ngsiv2/ultralightMeasures-test.js | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/commonBindings.js b/lib/commonBindings.js index 1e4c0676..0d0a224f 100644 --- a/lib/commonBindings.js +++ b/lib/commonBindings.js @@ -212,14 +212,19 @@ function singleMeasure(apiKey, attribute, device, messageStr, message) { config .getLogger() .debug('Processing single measure [%s] for device [%s] with apiKey [%s]', messageStr, device.id, apiKey); + let value; + try { + value = JSON.parse(messageStr); + } catch (e) { + value = messageStr || message.toString('hex'); + } const values = [ { name: attribute, type: guessType(attribute, device), - value: messageStr || message.toString('hex') + value: value } ]; - iotAgentLib.update(device.name, device.type, '', values, device, function (error) { if (error) { config.getLogger().error( diff --git a/test/unit/ngsiv2/ultralightMeasures-test.js b/test/unit/ngsiv2/ultralightMeasures-test.js index 9301ac94..4f4f7ff4 100644 --- a/test/unit/ngsiv2/ultralightMeasures-test.js +++ b/test/unit/ngsiv2/ultralightMeasures-test.js @@ -91,10 +91,10 @@ describe('Ultralight 2.0 Parser: measures', function () { result.length.should.equal(2); should.exist(result[0]); should.exist(result[0].c); - result[0].c.should.equal('7'); + result[0].c.should.equal(7); should.exist(result[1]); should.exist(result[1].b); - result[1].b.should.equal('18'); + result[1].b.should.equal(18); }); }); describe('When a payload with multiple groups and measures is parsed', function () {