Skip to content

Commit

Permalink
parse single mqtt measure
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvaroVega committed Oct 11, 2023
1 parent 96e58e1 commit 9c67e67
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 7 additions & 2 deletions lib/commonBindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions test/unit/ngsiv2/ultralightMeasures-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 9c67e67

Please sign in to comment.