From d79cb49eb3a0f91e3e4a55046d5b96da8d7f1e1e Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Thu, 25 Jan 2024 11:13:36 +0100 Subject: [PATCH 01/11] use targetEntityId and targetEntityType from subscription --- .../northBound/contextServer-NGSI-v2.js | 41 +++++++++++++------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/lib/services/northBound/contextServer-NGSI-v2.js b/lib/services/northBound/contextServer-NGSI-v2.js index 360def2ea..694b6410c 100644 --- a/lib/services/northBound/contextServer-NGSI-v2.js +++ b/lib/services/northBound/contextServer-NGSI-v2.js @@ -292,19 +292,36 @@ function handleNotificationNgsi2(req, res, next) { } } } - deviceService.getDeviceByNameAndType( - dataElement.id, - dataElement.type, - req.headers['fiware-service'], - req.headers['fiware-servicepath'], - function (error, device) { - if (error) { - callback(error); - } else { - callback(null, device, atts); + + if (dataElement.targetEntityId && dataElement.targetEntityType) { + deviceService.getDeviceByNameAndType( + dataElement.targetEntityId, + dataElement.targetEntityType, + req.headers['fiware-service'], + req.headers['fiware-servicepath'], + function (error, device) { + if (error) { + callback(error); + } else { + callback(null, device, atts); + } } - } - ); + ); + } else { + deviceService.getDeviceByNameAndType( + dataElement.id, + dataElement.type, + req.headers['fiware-service'], + req.headers['fiware-servicepath'], + function (error, device) { + if (error) { + callback(error); + } else { + callback(null, device, atts); + } + } + ); + } } function applyNotificationMiddlewares(device, values, callback) { From b29e4c6c06691acc1d345b9484c6c0cabf3c0392 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Thu, 25 Jan 2024 11:27:37 +0100 Subject: [PATCH 02/11] fix access to targetEntityId and targetEntityType --- lib/services/northBound/contextServer-NGSI-v2.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/services/northBound/contextServer-NGSI-v2.js b/lib/services/northBound/contextServer-NGSI-v2.js index 694b6410c..2585b998f 100644 --- a/lib/services/northBound/contextServer-NGSI-v2.js +++ b/lib/services/northBound/contextServer-NGSI-v2.js @@ -292,11 +292,11 @@ function handleNotificationNgsi2(req, res, next) { } } } - + logger.debug(context, 'extracted atts %j from dataElement %j', atts, dataElement); if (dataElement.targetEntityId && dataElement.targetEntityType) { deviceService.getDeviceByNameAndType( - dataElement.targetEntityId, - dataElement.targetEntityType, + dataElement.targetEntityId.value, + dataElement.targetEntityType.value, req.headers['fiware-service'], req.headers['fiware-servicepath'], function (error, device) { From 28bde7c1457c6d1a1cf4d2745c287f7d8037ebb2 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Mon, 29 Jan 2024 17:02:02 +0100 Subject: [PATCH 03/11] export executeUpdateSideEffects --- lib/fiware-iotagent-lib.js | 1 + lib/services/northBound/contextServer.js | 1 + 2 files changed, 2 insertions(+) diff --git a/lib/fiware-iotagent-lib.js b/lib/fiware-iotagent-lib.js index 90a847313..973db5c7d 100644 --- a/lib/fiware-iotagent-lib.js +++ b/lib/fiware-iotagent-lib.js @@ -317,6 +317,7 @@ exports.setDataUpdateHandler = contextServer.setUpdateHandler; exports.setCommandHandler = contextServer.setCommandHandler; exports.setMergePatchHandler = contextServer.setMergePatchHandler; exports.setDataQueryHandler = contextServer.setQueryHandler; +exports.executeUpdateSideEffects = contextServer.executeUpdateSideEffects; exports.setConfigurationHandler = contextServer.setConfigurationHandler; exports.setRemoveConfigurationHandler = contextServer.setRemoveConfigurationHandler; exports.setProvisioningHandler = contextServer.setProvisioningHandler; diff --git a/lib/services/northBound/contextServer.js b/lib/services/northBound/contextServer.js index f328e6730..e707fbc61 100644 --- a/lib/services/northBound/contextServer.js +++ b/lib/services/northBound/contextServer.js @@ -157,4 +157,5 @@ exports.setCommandHandler = intoTrans(context, setCommandHandler); exports.setNotificationHandler = intoTrans(context, setNotificationHandler); exports.addNotificationMiddleware = intoTrans(context, addNotificationMiddleware); exports.setQueryHandler = intoTrans(context, setQueryHandler); +exports.executeUpdateSideEffects = intoTrans(context, contextServerUtils.executeUpdateSideEffects); exports.init = init; From ce01e879e081806f19814ae6f452ae705443a954 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Tue, 30 Jan 2024 10:34:54 +0100 Subject: [PATCH 04/11] fix export executeUpdateSideEffects --- lib/services/northBound/contextServer.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/services/northBound/contextServer.js b/lib/services/northBound/contextServer.js index e707fbc61..f901de3df 100644 --- a/lib/services/northBound/contextServer.js +++ b/lib/services/northBound/contextServer.js @@ -30,7 +30,7 @@ const context = { op: 'IoTAgentNGSI.ContextServer' }; const contextServerUtils = require('./contextServerUtils'); - +const executeUpdateSideEffects = contextServerUtils.executeUpdateSideEffects; let contextServerHandler; /** @@ -157,5 +157,5 @@ exports.setCommandHandler = intoTrans(context, setCommandHandler); exports.setNotificationHandler = intoTrans(context, setNotificationHandler); exports.addNotificationMiddleware = intoTrans(context, addNotificationMiddleware); exports.setQueryHandler = intoTrans(context, setQueryHandler); -exports.executeUpdateSideEffects = intoTrans(context, contextServerUtils.executeUpdateSideEffects); +exports.executeUpdateSideEffects = intoTrans(context, executeUpdateSideEffects); exports.init = init; From 50dcd61325fb0f5ecdeded7d86777c76120b9bff Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Tue, 30 Jan 2024 12:44:14 +0100 Subject: [PATCH 05/11] add missed exports.executeUpdateSideEffects --- lib/services/northBound/northboundServer.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/services/northBound/northboundServer.js b/lib/services/northBound/northboundServer.js index 5ed44f8c4..2f331b359 100644 --- a/lib/services/northBound/northboundServer.js +++ b/lib/services/northBound/northboundServer.js @@ -123,6 +123,7 @@ exports.setRemoveDeviceHandler = intoTrans(context, deviceProvisioning.setRemove exports.addDeviceProvisionMiddleware = deviceProvisioning.addDeviceProvisionMiddleware; exports.addConfigurationProvisionMiddleware = groupProvisioning.addConfigurationProvisionMiddleware; exports.addNotificationMiddleware = contextServer.addNotificationMiddleware; +exports.executeUpdateSideEffects = contextServer.executeUpdateSideEffects; exports.clear = clear; exports.start = intoTrans(context, start); exports.stop = intoTrans(context, stop); From bbbe3343766e7c4c94cd1d0b9be67bd93c4f4ecf Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Tue, 30 Jan 2024 15:08:22 +0100 Subject: [PATCH 06/11] update CNR --- CHANGES_NEXT_RELEASE | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index debfe9a5a..49c0da1f5 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1 +1,2 @@ +- ADD: extend handleNotificationNgsi2 to allow new commands from CB notifications (#1455) - ADD: progress non expected id and type in measures under `measure_` suffix From 010931f10b19f11cbd82fe79c81d527cde0c1a38 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Wed, 31 Jan 2024 09:38:41 +0100 Subject: [PATCH 07/11] explain how CB notifies a command to iotagent --- doc/devel/northboundinteractions.md | 91 ++++++++++++++++++++++++++++- 1 file changed, 89 insertions(+), 2 deletions(-) diff --git a/doc/devel/northboundinteractions.md b/doc/devel/northboundinteractions.md index 0554dbae4..9e1f0806b 100644 --- a/doc/devel/northboundinteractions.md +++ b/doc/devel/northboundinteractions.md @@ -825,8 +825,95 @@ Fiware-Correlator: 9cae9496-8ec7-11e6-80fc-fa163e734aab } ``` -The IoT Agent detects the selected attribute is a command, and replies to the Context Broker with the following payload -(200 OK): +A new way to ContextBroker provides a command to a IoTAgent is through notifications. In this case CB notify the command +to the IotAgent with a request like the following: + +```bash +POST /notify HTTP/1.1 +Host: : +fiware-service: workshop +Fiware-ServicePath: /iota2ngsi +Accept: application/json +Content-length: 290 +Content-type: application/json; charset=utf-8 +Fiware-Correlator: 9cae9496-8ec7-11e6-80fc-fa163e734aab + +{ + "subscriptionId": "60b0cedd497e8b681d40b58e", + "data": [{ + "id": "123456abcdefg", + "type": "switchOnOffExecution", + "targetEntityId": { + "type": "Text", + "value": "Dev0001", + "metadata": {} + }, + "targetEntityType": { + "type": "Text", + "value": "device", + "metadata": {} + }, + "execTs": { + "type": "DateTime", + "value": "2020-05-27T00:00:00.000Z", + "metadata": {} + }, + "cmd": { + "type": "Text", + "value": "switch", + "metadata": {} + }, + "params": { + "type": "Text", + "value": 54, 12", + "metadata": {} + }, + "status": { + "type": "Text", + "value": "FORWARDED", + "metadata": {} + }, + "info": { + "type": "Text", + "value": null, + "metadata": {} + }, + "onDelivered": { + "type": "Request", + "value": { + } + }, + "onOk": { + "type": "Request", + "value": { + } + }, + "onError": { + "type": "Request", + "value": { + } + }, + "onInfo": { + "type": "Request", + "value": { + } + }, + "cmdExecution": { + "type": "value", + "value": true, + "metadata": {} + }, + "dateExpiration": { + "type": "DateTime", + "value": "2020-05-27T20:00:00.000Z", + "metadata": {} + } + }] +} +``` + +In both cases (update or command) the IoT Agent detects the selected attribute is a command, and replies to the Context +Broker with the following payload (200 OK): ```json [ From 1058a05d4d13314c319665a05fab1b84056e361f Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Wed, 31 Jan 2024 10:04:34 +0100 Subject: [PATCH 08/11] add new fields to command --- lib/model/Command.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/model/Command.js b/lib/model/Command.js index 5205ea0f0..6331c8649 100644 --- a/lib/model/Command.js +++ b/lib/model/Command.js @@ -31,6 +31,15 @@ const Command = new Schema({ value: Object, service: { type: String, lowercase: true }, subservice: String, + execTs: { type: Date }, + status: Object, + info: Object, + onDelivered: Object, + onOk: Object, + onError: Object, + onInfo: Object, + cmdExecution: Object, + dateExpiration: { type: Date }, creationDate: { type: Date, default: Date.now } }); From b92f5b64eee5cba285a9057a4d2d3d937b0fc49d Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Wed, 31 Jan 2024 10:09:50 +0100 Subject: [PATCH 09/11] add new command fields --- .../commands/commandRegistryMongoDB.js | 34 +++++++++++++------ lib/services/commands/commandService.js | 6 ++-- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/lib/services/commands/commandRegistryMongoDB.js b/lib/services/commands/commandRegistryMongoDB.js index 31efedc3a..c94d93efc 100644 --- a/lib/services/commands/commandRegistryMongoDB.js +++ b/lib/services/commands/commandRegistryMongoDB.js @@ -76,7 +76,21 @@ function updateCommand(service, subservice, deviceId, command, callback) { function createCommand(service, subservice, deviceId, command, callback) { /* eslint-disable-next-line new-cap */ const commandObj = new Command.model(); - const attributeList = ['name', 'type', 'value']; + const attributeList = [ + 'name', + 'type', + 'value', + // new Command fields + 'execTs', + 'status', + 'info', + 'onDelivered', + 'onOk', + 'onError', + 'onInfo', + 'cmdExecution', + 'dateExpiration' + ]; for (let i = 0; i < attributeList.length; i++) { commandObj[attributeList[i]] = command[attributeList[i]]; @@ -124,15 +138,15 @@ function listCommands(service, subservice, deviceId, callback) { const query = Command.model.find(condition).sort(); - async.series([query.exec.bind(query), Command.model.countDocuments.bind(Command.model, condition)], function ( - error, - results - ) { - callback(error, { - count: results[1], - commands: results[0].map(toObjectFn) - }); - }); + async.series( + [query.exec.bind(query), Command.model.countDocuments.bind(Command.model, condition)], + function (error, results) { + callback(error, { + count: results[1], + commands: results[0].map(toObjectFn) + }); + } + ); } function remove(service, subservice, deviceId, name, callback) { diff --git a/lib/services/commands/commandService.js b/lib/services/commands/commandService.js index 757e4d2ba..af766a6a0 100644 --- a/lib/services/commands/commandService.js +++ b/lib/services/commands/commandService.js @@ -43,7 +43,7 @@ function listCommands(service, subservice, deviceId, callback) { } function addCommand(service, subservice, deviceId, command, callback) { - logger.debug(context, 'Adding command [%j] to the queue for device [%s]', command, deviceId); + logger.debug(context, 'Adding command [%j] to the queue for deviceId [%s]', command, deviceId); config.getCommandRegistry().add(service, subservice, deviceId, command, callback); } @@ -73,13 +73,13 @@ function addCommandDevice(service, subservice, device, command, callback) { } function updateCommand(service, subservice, deviceId, name, value, callback) { - logger.debug(context, 'Updating command [%s] for device [%s] with value [%s]', name, deviceId, value); + logger.debug(context, 'Updating command [%s] for deviceId [%s] with value [%s]', name, deviceId, value); config.getCommandRegistry().update(service, subservice, deviceId, value, callback); } function removeCommand(service, subservice, deviceId, name, callback) { - logger.debug(context, 'Removing command [%s] from device [%s]', name, deviceId); + logger.debug(context, 'Removing command [%s] from deviceId [%s]', name, deviceId); config.getCommandRegistry().remove(service, subservice, deviceId, name, callback); } From ad89896c8b61016da42dbf4b3f9aab27c280ce39 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Mon, 5 Feb 2024 12:11:51 +0100 Subject: [PATCH 10/11] fix json --- doc/devel/northboundinteractions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/devel/northboundinteractions.md b/doc/devel/northboundinteractions.md index 9e1f0806b..783b3ab50 100644 --- a/doc/devel/northboundinteractions.md +++ b/doc/devel/northboundinteractions.md @@ -865,7 +865,7 @@ Fiware-Correlator: 9cae9496-8ec7-11e6-80fc-fa163e734aab }, "params": { "type": "Text", - "value": 54, 12", + "value": "54, 12", "metadata": {} }, "status": { From b07c2efc6c02c015fa4a7db7d8f7549fc34c4eed Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Mon, 5 Feb 2024 13:09:05 +0100 Subject: [PATCH 11/11] update CNR --- CHANGES_NEXT_RELEASE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index bad315481..5252a0fbb 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1 +1 @@ -- ADD: extend handleNotificationNgsi2 to allow new commands from CB notifications (#1455) +- ADD: extend handleNotificationNgsi2 to allow receive commands from CB notifications (#1455)