diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 9b68faf5..e91c5ecb 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -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) + 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/lib/ulParser.js b/lib/ulParser.js index e3c62453..17cc73dd 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; } } @@ -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 }; } @@ -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); @@ -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); @@ -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); } diff --git a/test/unit/ngsiv2/config-test.js b/test/unit/ngsiv2/config-test.js index ceb6e58c..76ecd9fa 100644 --- a/test/unit/ngsiv2/config-test.js +++ b/test/unit/ngsiv2/config-test.js @@ -45,7 +45,6 @@ config.amqp = { config.iota = { logLevel: 'FATAL', - autocast: true, contextBroker: { host: '192.168.1.1', port: '1026', diff --git a/test/unit/ngsiv2/ultralightMeasures-test.js b/test/unit/ngsiv2/ultralightMeasures-test.js index aaa4299e..4f4f7ff4 100644 --- a/test/unit/ngsiv2/ultralightMeasures-test.js +++ b/test/unit/ngsiv2/ultralightMeasures-test.js @@ -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 () { @@ -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 () { @@ -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 () { @@ -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); }); }); @@ -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 () {