Skip to content

Commit

Permalink
apply JSON.parse to all measure values
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvaroVega committed Oct 11, 2023
1 parent e02d10d commit d9c57fc
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 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 @@ -277,17 +282,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 +308,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 +326,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

0 comments on commit d9c57fc

Please sign in to comment.