From dbc34eb0cdb5c394e11a808104b70105bbeb8bcc Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Fri, 20 Oct 2023 11:03:56 +0200 Subject: [PATCH 1/6] allow send a request payload as full ngsiv2 --- lib/fiware-iotagent-lib.js | 1 + lib/services/devices/deviceService.js | 11 +++++++++-- lib/services/ngsi/entities-NGSI-v2.js | 16 ++++++++++++++++ lib/services/ngsi/ngsiService.js | 13 +++++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/lib/fiware-iotagent-lib.js b/lib/fiware-iotagent-lib.js index bdfbdeeb1..cafdad80a 100644 --- a/lib/fiware-iotagent-lib.js +++ b/lib/fiware-iotagent-lib.js @@ -299,6 +299,7 @@ exports.updateRegister = deviceService.updateRegister; exports.unregister = deviceService.unregister; exports.query = ngsi.query; exports.update = ngsi.update; +exports.sendNgsiv2Measure = ngsi.sendNgsiv2Measure; exports.setCommandResult = ngsi.setCommandResult; exports.listDevices = deviceService.listDevices; exports.getDevice = deviceService.getDevice; diff --git a/lib/services/devices/deviceService.js b/lib/services/devices/deviceService.js index 071796654..462634f8f 100644 --- a/lib/services/devices/deviceService.js +++ b/lib/services/devices/deviceService.js @@ -173,8 +173,15 @@ function mergeDeviceWithConfiguration(fields, defaults, deviceData, configuratio if (configuration && configuration.entityNameExp !== undefined) { deviceData.entityNameExp = configuration.entityNameExp; } - if (configuration && configuration.timestamp !== undefined && deviceData.timestamp === undefined) { - deviceData.timestamp = configuration.timestamp; + // if (configuration && configuration.timestamp !== undefined && deviceData.timestamp === undefined) { + // deviceData.timestamp = configuration.timestamp; + // } + if (deviceData.timestamp === undefined) { + if (configuration && configuration.timestamp !== undefined) { + deviceData.timestamp = configuration.timestamp; + } else if (config.getConfig().timestamp !== undefined) { + deviceData.timestamp = config.getConfig().timestamp; + } } logger.debug(context, 'deviceData after merge with conf: %j', deviceData); callback(null, deviceData); diff --git a/lib/services/ngsi/entities-NGSI-v2.js b/lib/services/ngsi/entities-NGSI-v2.js index 56993c3bf..212b2dcf3 100644 --- a/lib/services/ngsi/entities-NGSI-v2.js +++ b/lib/services/ngsi/entities-NGSI-v2.js @@ -1025,11 +1025,27 @@ function sendUpdateValueNgsi2(entityName, measures, typeInformation, token, call } } +/** + * Makes an update in the context broker, with the NGSIv2 entities given in the 'measures' object. + * + * @param {Object} measure NGSIv2 measure to send. + * @param {Object} typeInformation Configuration information for the device. + * @param {String} token User token to identify against the PEP Proxies (optional). + */ +function sendNgsiv2Measure(measure, typeInformation, token, callback) { + logger.debug(context, 'sendNgsiv2Measure called with: measure=%j typeInformation=%j', measure, typeInformation); + let url = '/v2/op/update'; + let options = NGSIUtils.createRequestObject(url, typeInformation, token); + ptions.json = measure; + request(options, generateNGSI2OperationHandler('update', null, typeInformation, token, options, callback)); +} + exports.sendQueryValue = sendQueryValueNgsi2; exports.sendUpdateValue = function (entityName, measures, typeInformation, token, callback) { NGSIUtils.applyMiddlewares(NGSIUtils.updateMiddleware, measures, typeInformation, () => { return sendUpdateValueNgsi2(entityName, measures, typeInformation, token, callback); }); }; +exports.sendNgsiv2Measure = sendNgsiv2Measure; exports.addTimestamp = addTimestampNgsi2; exports.formatGeoAttrs = formatGeoAttrs; diff --git a/lib/services/ngsi/ngsiService.js b/lib/services/ngsi/ngsiService.js index aea6ef802..213dc5d2b 100644 --- a/lib/services/ngsi/ngsiService.js +++ b/lib/services/ngsi/ngsiService.js @@ -83,6 +83,18 @@ function sendQueryValue(entityName, attributes, typeInformation, token, callback entityHandler.sendQueryValue(entityName, attributes, typeInformation, token, callback); } +/** + * Makes an update in the context broker, with the NGSIv2 entities given in the 'measures' object. + * + * @param {Object} measure NGSIv2 measure to send. + * @param {Object} typeInformation Configuration information for the device. + * @param {String} token User token to identify against the PEP Proxies (optional). + */ +function sendNgsiv2Measure(measure, typeInformation, token, callback) { + let localEntityHandler = require('./entities-NGSI-v2'); + localEntityHandler.sendNgsiv2Measure(measure, typeInformation, token, callback); +} + /** * Update the trust for a device or a deviceGroup depending on the source of the trust token. * Currently is a placeholder, but it is needed in case of Auth services which tokens have @@ -258,6 +270,7 @@ function resetMiddlewares(callback) { exports.update = intoTrans(context, executeWithDeviceInformation)(sendUpdateValue); exports.query = intoTrans(context, executeWithDeviceInformation)(sendQueryValue); +exports.sendNgsiv2Measure = intoTrans(context, executeWithDeviceInformation)(sendNgsiv2Measure); exports.addUpdateMiddleware = intoTrans(context, addUpdateMiddleware); exports.addQueryMiddleware = intoTrans(context, addQueryMiddleware); exports.resetMiddlewares = intoTrans(context, resetMiddlewares); From ca2e2a82c6b093f8e928afaba62c408fba118ee1 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Fri, 20 Oct 2023 11:25:01 +0200 Subject: [PATCH 2/6] fix typo --- lib/services/ngsi/entities-NGSI-v2.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/services/ngsi/entities-NGSI-v2.js b/lib/services/ngsi/entities-NGSI-v2.js index 212b2dcf3..7ade0f8b1 100644 --- a/lib/services/ngsi/entities-NGSI-v2.js +++ b/lib/services/ngsi/entities-NGSI-v2.js @@ -1036,7 +1036,7 @@ function sendNgsiv2Measure(measure, typeInformation, token, callback) { logger.debug(context, 'sendNgsiv2Measure called with: measure=%j typeInformation=%j', measure, typeInformation); let url = '/v2/op/update'; let options = NGSIUtils.createRequestObject(url, typeInformation, token); - ptions.json = measure; + options.json = measure; request(options, generateNGSI2OperationHandler('update', null, typeInformation, token, options, callback)); } From 504314f4ef05e36720ec3f8e28106fdf1fd6eb63 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Fri, 20 Oct 2023 11:58:38 +0200 Subject: [PATCH 3/6] add missed param --- lib/services/ngsi/ngsiService.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/services/ngsi/ngsiService.js b/lib/services/ngsi/ngsiService.js index 213dc5d2b..63c7ca89a 100644 --- a/lib/services/ngsi/ngsiService.js +++ b/lib/services/ngsi/ngsiService.js @@ -90,7 +90,7 @@ function sendQueryValue(entityName, attributes, typeInformation, token, callback * @param {Object} typeInformation Configuration information for the device. * @param {String} token User token to identify against the PEP Proxies (optional). */ -function sendNgsiv2Measure(measure, typeInformation, token, callback) { +function sendNgsiv2Measure(entityName, measure, typeInformation, token, callback) { let localEntityHandler = require('./entities-NGSI-v2'); localEntityHandler.sendNgsiv2Measure(measure, typeInformation, token, callback); } From cb121483c60d3105bee6cd0f4cf9a79b9b588044 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Fri, 20 Oct 2023 12:37:11 +0200 Subject: [PATCH 4/6] remove code --- lib/services/devices/deviceService.js | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/lib/services/devices/deviceService.js b/lib/services/devices/deviceService.js index 462634f8f..071796654 100644 --- a/lib/services/devices/deviceService.js +++ b/lib/services/devices/deviceService.js @@ -173,15 +173,8 @@ function mergeDeviceWithConfiguration(fields, defaults, deviceData, configuratio if (configuration && configuration.entityNameExp !== undefined) { deviceData.entityNameExp = configuration.entityNameExp; } - // if (configuration && configuration.timestamp !== undefined && deviceData.timestamp === undefined) { - // deviceData.timestamp = configuration.timestamp; - // } - if (deviceData.timestamp === undefined) { - if (configuration && configuration.timestamp !== undefined) { - deviceData.timestamp = configuration.timestamp; - } else if (config.getConfig().timestamp !== undefined) { - deviceData.timestamp = config.getConfig().timestamp; - } + if (configuration && configuration.timestamp !== undefined && deviceData.timestamp === undefined) { + deviceData.timestamp = configuration.timestamp; } logger.debug(context, 'deviceData after merge with conf: %j', deviceData); callback(null, deviceData); From fdba654f6f54f9a56a32fc28c2c547369ad2011d Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Fri, 27 Oct 2023 11:56:55 +0200 Subject: [PATCH 5/6] measure is just and object or an array of objects --- lib/services/ngsi/entities-NGSI-v2.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/services/ngsi/entities-NGSI-v2.js b/lib/services/ngsi/entities-NGSI-v2.js index 0dc036b5d..1629e4568 100644 --- a/lib/services/ngsi/entities-NGSI-v2.js +++ b/lib/services/ngsi/entities-NGSI-v2.js @@ -638,7 +638,11 @@ function sendNgsiv2Measure(measure, typeInformation, token, callback) { logger.debug(context, 'sendNgsiv2Measure called with: measure=%j typeInformation=%j', measure, typeInformation); let url = '/v2/op/update'; let options = NGSIUtils.createRequestObject(url, typeInformation, token); - options.json = measure; + let payload = { + actionType: 'append', + entities: measure.length ? measure : [measure] + }; + options.json = payload; request(options, generateNGSI2OperationHandler('update', null, typeInformation, token, options, callback)); } From f863f27116ba86e44fb16ba776754502a588dbc3 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Thu, 2 Nov 2023 10:23:04 +0100 Subject: [PATCH 6/6] update CNR --- CHANGES_NEXT_RELEASE | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index c8b33c641..e11315154 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1,3 +1,4 @@ +- Allow send ngsiv2 measure as a request to ContextBroker - Large refactor of IOTA Lib code to make it simpler - Remove: time compression support - Remove: autocast (including env var IOTA_AUTOCAST) (#1498)