Skip to content

Commit

Permalink
Merge branch 'master' into task/propage_entity_name_exp_type_information
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvaroVega authored Oct 11, 2023
2 parents cd1d4ae + 1d1c088 commit 74b742b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 23 deletions.
3 changes: 2 additions & 1 deletion CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- Fix: ensure service and subservice from device in logs about error proccesing message
- Fix: use entityNameExp with autoprovisioned devices (iota-node-lib#1145)
- Fix: use but not store timestamp and entityNameExp from group with autoprovisioned devices (iotagent-node-lib#1504, partially)
- Fix: remove autocast (iota-node-lib#1501)

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
32 changes: 21 additions & 11 deletions lib/ulParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ function addAttributePair(collection, pair) {
if (!pair || pair.length !== 2) {
throw new errors.ParseError('Extracting attribute:' + JSON.stringify(pair));
} else {
collection[pair[0]] = pair[1];
try {
collection[pair[0]] = JSON.parse(pair[1]);
} catch (e) {
collection[pair[0]] = pair[1];
}

return collection;
}
}
Expand Down Expand Up @@ -107,7 +112,7 @@ function parseMeasure(group, numberOfBars) {
if (timestamp) {
returnValue[constants.TIMESTAMP_ATTRIBUTE] = timestamp;
}

config.getLogger().debug(context, 'parseMeasure %j', returnValue);
return returnValue;
}
}
Expand Down Expand Up @@ -226,11 +231,16 @@ function result(payload) {
}

const deviceData = fields[0].split('@');

let value;
try {
value = JSON.parse(fields[1]);
} catch (e) {
value = fields[1];
}
return {
deviceId: deviceData[0],
command: deviceData[1],
result: fields[1]
result: value
};
}

Expand Down Expand Up @@ -277,17 +287,17 @@ function serializedPayloadCommand(payload, command) {
*/
function createCommandPayload(device, command, attributes) {
function addAttributes(current, key) {
let cmd = device && device.commands.find((att) => att.name === current.split('@')[1]);
const cmd = device && device.commands.find((att) => att.name === current.split('@')[1]);
let value = attributes[key];
if (cmd && cmd.expression) {
let parser = iotAgentLib.dataPlugins.expressionTransformation;
const parser = iotAgentLib.dataPlugins.expressionTransformation;
// The context for the JEXL expression should be the ID, TYPE, S, SS
let attrList = iotAgentLib.dataPlugins.utils.getIdTypeServSubServiceFromDevice(device);
attrList = device.staticAttributes
? attrList.concat(device.staticAttributes).concat({ key: value })
: attrList.concat({ key: value });
config.getLogger().debug(context, 'attrList [%j] for device %j', attrList, device);
let ctxt = parser.extractContext(attrList, device);
const ctxt = parser.extractContext(attrList, device);
let valueRes = null;
try {
valueRes = parser.applyExpression(cmd.expression, ctxt, device);
Expand All @@ -303,16 +313,16 @@ function createCommandPayload(device, command, attributes) {
if (typeof attributes === 'object') {
return Object.keys(attributes).reduce(addAttributes, device.id + '@' + command, command);
}
let cmd = device && device.commands.find((att) => att.name === command);
const cmd = device && device.commands.find((att) => att.name === command);
if (cmd && cmd.expression) {
let parser = iotAgentLib.dataPlugins.expressionTransformation;
const parser = iotAgentLib.dataPlugins.expressionTransformation;
// The context for the JEXL expression should be the ID, TYPE, S, SS
let attrList = iotAgentLib.dataPlugins.utils.getIdTypeServSubServiceFromDevice(device);
attrList = device.staticAttributes
? attrList.concat(device.staticAttributes).concat({ command: attributes })
: attrList.concat({ command: attributes });
config.getLogger().debug(context, 'attrList [%j] for device %j', attrList, device);
let ctxt = parser.extractContext(attrList, device);
const ctxt = parser.extractContext(attrList, device);
let valueRes = null;
try {
valueRes = parser.applyExpression(cmd.expression, ctxt, device);
Expand All @@ -321,7 +331,7 @@ function createCommandPayload(device, command, attributes) {
}
attributes = valueRes ? valueRes : cmd.expression;
}
let payload = device.id + '@' + command + '|' + attributes;
const payload = device.id + '@' + command + '|' + attributes;
return serializedPayloadCommand(payload, cmd);
}

Expand Down
1 change: 0 additions & 1 deletion test/unit/ngsiv2/config-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ config.amqp = {

config.iota = {
logLevel: 'FATAL',
autocast: true,
contextBroker: {
host: '192.168.1.1',
port: '1026',
Expand Down
16 changes: 8 additions & 8 deletions test/unit/ngsiv2/ultralightMeasures-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('Ultralight 2.0 Parser: measures', function () {
result.length.should.equal(1);
should.exist(result[0]);
should.exist(result[0].a);
result[0].a.should.equal('1');
result[0].a.should.equal(1);
});
});
describe('When a payload with a multiple measures is parsed', function () {
Expand All @@ -46,9 +46,9 @@ describe('Ultralight 2.0 Parser: measures', function () {
result.length.should.equal(1);
should.exist(result[0]);
should.exist(result[0].c);
result[0].c.should.equal('7');
result[0].c.should.equal(7);
should.exist(result[0].b);
result[0].b.should.equal('18');
result[0].b.should.equal(18);
});
});
describe('When a payload with timestamp information is parsed', function () {
Expand All @@ -64,7 +64,7 @@ describe('Ultralight 2.0 Parser: measures', function () {
should.exist(result[0].TimeInstant);
result[0].TimeInstant.should.equal('2016-06-13T00:35:30Z');
should.exist(result[0].lle);
result[0].lle.should.equal('100');
result[0].lle.should.equal(100);
});
});
describe('When a payload with an initial bar and multiple measures is parsed', function () {
Expand All @@ -76,9 +76,9 @@ describe('Ultralight 2.0 Parser: measures', function () {
result.length.should.equal(1);
should.exist(result[0]);
should.exist(result[0].c);
result[0].c.should.equal('7');
result[0].c.should.equal(7);
should.exist(result[0].b);
result[0].b.should.equal('18');
result[0].b.should.equal(18);
});
});

Expand All @@ -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 74b742b

Please sign in to comment.