diff --git a/lib/ulParser.js b/lib/ulParser.js index e3c62453..74aa817f 100644 --- a/lib/ulParser.js +++ b/lib/ulParser.js @@ -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; } } @@ -107,7 +112,7 @@ function parseMeasure(group, numberOfBars) { if (timestamp) { returnValue[constants.TIMESTAMP_ATTRIBUTE] = timestamp; } - + config.getLogger().debug(context, 'parseMeasure %j', returnValue); return returnValue; } } @@ -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); @@ -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); @@ -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); }