Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow receive commands by CB notifications #1560

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d79cb49
use targetEntityId and targetEntityType from subscription
AlvaroVega Jan 25, 2024
b29e4c6
fix access to targetEntityId and targetEntityType
AlvaroVega Jan 25, 2024
28bde7c
export executeUpdateSideEffects
AlvaroVega Jan 29, 2024
9ec6e26
Merge branch 'master' into task/notify_cmd
AlvaroVega Jan 30, 2024
ce01e87
fix export executeUpdateSideEffects
AlvaroVega Jan 30, 2024
5014749
Merge branch 'master' into task/notify_cmd
AlvaroVega Jan 30, 2024
50dcd61
add missed exports.executeUpdateSideEffects
AlvaroVega Jan 30, 2024
bbbe334
update CNR
AlvaroVega Jan 30, 2024
48cf466
Merge branch 'master' into task/notify_cmd
AlvaroVega Jan 30, 2024
010931f
explain how CB notifies a command to iotagent
AlvaroVega Jan 31, 2024
1058a05
add new fields to command
AlvaroVega Jan 31, 2024
b92f5b6
add new command fields
AlvaroVega Jan 31, 2024
ad89896
fix json
AlvaroVega Feb 5, 2024
b07c2ef
update CNR
AlvaroVega Feb 5, 2024
0ecb5aa
Merge branch 'master' into task/notify_cmd
fgalan Feb 5, 2024
d6bb406
Merge branch 'master' into task/notify_cmd
AlvaroVega Feb 21, 2024
cc3d345
Merge branch 'master' into task/notify_cmd
AlvaroVega Feb 28, 2024
ae08bd6
Merge branch 'master' into task/notify_cmd
AlvaroVega Apr 17, 2024
5b11591
Merge branch 'master' into task/notify_cmd
AlvaroVega Jun 20, 2024
186ef0a
Merge branch 'master' into task/notify_cmd
AlvaroVega Aug 19, 2024
a690249
Merge branch 'master' into task/notify_cmd
AlvaroVega Aug 30, 2024
902402d
Merge branch 'master' into task/notify_cmd
AlvaroVega Sep 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
- Add: attribute metadata and static attributes metadata added to jexl context (#1630)
- Add /iot/groups API endpoints (as equivalent to /iot/services) (#752)
- Deprecated: /iot/services API routes
- Add: extend handleNotificationNgsi2 to allow receive commands from CB notifications (#1455)
91 changes: 89 additions & 2 deletions doc/devel/northboundinteractions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: <target-host>:<northbound_port>
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
[
Expand Down
1 change: 1 addition & 0 deletions lib/fiware-iotagent-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,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;
Expand Down
9 changes: 9 additions & 0 deletions lib/model/Command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
});

Expand Down
16 changes: 15 additions & 1 deletion lib/services/commands/commandRegistryMongoDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,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]];
Expand Down
6 changes: 3 additions & 3 deletions lib/services/commands/commandService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
Expand Down
41 changes: 29 additions & 12 deletions lib/services/northBound/contextServer-NGSI-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
logger.debug(context, 'extracted atts %j from dataElement %j', atts, dataElement);
if (dataElement.targetEntityId && dataElement.targetEntityType) {
deviceService.getDeviceByNameAndType(
dataElement.targetEntityId.value,
dataElement.targetEntityType.value,
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) {
Expand Down
3 changes: 2 additions & 1 deletion lib/services/northBound/contextServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const context = {
op: 'IoTAgentNGSI.ContextServer'
};
const contextServerUtils = require('./contextServerUtils');

const executeUpdateSideEffects = contextServerUtils.executeUpdateSideEffects;
let contextServerHandler;

/**
Expand Down Expand Up @@ -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, executeUpdateSideEffects);
exports.init = init;
1 change: 1 addition & 0 deletions lib/services/northBound/northboundServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,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);
Expand Down
Loading