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

Add support to API route /iot/groups (/iot/services still supported) #342

Merged
merged 7 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
2 changes: 2 additions & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Add /iot/groups API endpoints support (as equivalent to /iot/services) (#270)
fgalan marked this conversation as resolved.
Show resolved Hide resolved
- Deprecated: /iot/services API routes
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ following path: ``, indicating the following information:
- _description_: Textual description for its display in portals.
- _iotagent_: URL address where requests for this IoT Agent will be redirected.
- _resource_: Unique string used to identify different IoT Agents for the same protocol.
- _services_: List of device Configurations available in the IoT Agent. The IoTA Manager saves a cache for all the
- groups: List of device Configurations available in the IoT Agent. The IoTA Manager saves a cache for all the
mapedraza marked this conversation as resolved.
Show resolved Hide resolved
configurations, aimed to be used to fasten the operations agains the IoTA databases.

The following example shows a registration of an IoT Agent that already have some configuration groups registered in the
Expand All @@ -184,7 +184,7 @@ IoT Agent:
"description": "A generic protocol",
"iotagent": "http://smartGondor.com/iot",
"resource": "/iot/d",
"services": [
"groups": [
{
"apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732",
"token": "8970A9078A803H3BL98PINEQRW8342HBAMS",
Expand Down
15 changes: 13 additions & 2 deletions lib/services/configurations.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,25 @@ function handleListRequest(req, res, next) {
if (error) {
next(error);
} else {
res.status(200).json(translateToApi(logger, configurations));
res.status(200).json(modifyPayload(req, translateToApi(logger, configurations))); // #FIXME341 - Remove modifyPayload() usage when dropping /iot/services endpoint
}
}
);
}
// #FIXME341 - Remove this function when dropping /iot/services endpoint
function modifyPayload(req, payload) {
if (req._parsedUrl.pathname === '/iot/groups') {
if (payload.services) {
payload.groups = payload.services;
delete payload.services;
}
}
return payload;
}

function loadContextRoutes(router) {
router.get('/iot/services', [validateListParameters, handleListRequest]);
router.get('/iot/services', [validateListParameters, handleListRequest]); // #FIXME341 - Remove this function when dropping /iot/services endpoint
router.get('/iot/groups', [validateListParameters, handleListRequest]);
}

exports.loadContextRoutes = loadContextRoutes;
25 changes: 21 additions & 4 deletions lib/services/iotaRedirector.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ function guessCollection(body) {
if (body.services) {
return 'services';
} else if (body.devices) {
// #FIXME341 - Remove this part of the code when dropping /iot/services endpoint
return 'devices';
} else if (body.groups) {
return 'groups';
}
return null;
}
Expand Down Expand Up @@ -214,18 +217,27 @@ function createRequest(req, protocol, body) {

options.uri = protocolAddress + req.path;

// #FIXME341 - Remove this part of the code when dropping /iot/services endpoint
if (body && body.services) {
body.services = body.services.map(function cleanProtocol(item) {
delete item.protocol;
return item;
});
// Translate body.services

if (req.method === 'PUT') {
body = body.services[0];
}
}

if (body && body.groups) {
body.groups = body.groups.map(function cleanProtocol(item) {
delete item.protocol;
return item;
});
if (req.method === 'PUT') {
body = body.groups[0];
}
}

if (req.method === 'PUT' || req.method === 'POST') {
options.body = JSON.stringify(body);
}
Expand Down Expand Up @@ -333,7 +345,10 @@ function processRequests(req, res, next) {
if (results[0][1].devices || results[0][1].device_id) {
collectionName = 'devices';
} else if (results[0][1].services) {
// #FIXME341 - Remove this part of the code when dropping /iot/services endpoint
collectionName = 'services';
} else if (results[0][1].groups) {
collectionName = 'groups';
} else {
return null;
}
Expand Down Expand Up @@ -413,8 +428,10 @@ function processRequests(req, res, next) {

function loadContextRoutes(router) {
const middlewareList = [queryParamExtractor, getProtocols, createRequests, processRequests];

router.post('/iot/services', middlewareList);
router.post('/iot/groups', middlewareList);
router.put('/iot/groups', middlewareList);
router.delete('/iot/groups', middlewareList);
router.post('/iot/services', middlewareList); // #FIXME341 - Remove this part of the code when dropping /iot/services endpoint
router.post('/iot/devices', middlewareList);
router.put('/iot/services', middlewareList);
router.delete('/iot/services', middlewareList);
Expand Down
15 changes: 14 additions & 1 deletion lib/services/protocols.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,24 @@ function deleteProtocol(req, res, next) {
});
}

// #FIXME341 - Remove this function of the code when dropping /iot/services endpoint
function multipleValidation(req, res, next) {
const logger = req.logger;
const body = req.body;

if (body.groups) {
logger.debug('Validating protocol with groups');
body.services = body.groups;
delete body.groups;
}
middleware.validateJson(protocolTemplate)(req, res, next);
}

function loadContextRoutes(router) {
router.get('/iot/protocols', [readProtocolList, handleProtocolList]);

router.post('/iot/protocols', [
middleware.validateJson(protocolTemplate),
multipleValidation, // #FIXME341 - Remove calling this function when dropping /iot/services endpoint
saveProtocol,
returnProtocolCreationResponse
]);
Expand Down
2 changes: 1 addition & 1 deletion test/examples/protocols/registrationEmpty.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"description": "A generic protocol",
"iotagent": "http://smartGondor.com/",
"resource": "/iot/d",
"services": []
"groups": []
}
2 changes: 1 addition & 1 deletion test/examples/protocols/registrationWithGroups.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "A generic protocol",
"iotagent": "http://smartGondor.com/",
"resource": "/iot/d",
"services": [
"groups": [
{
"apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732",
"token": "8970A9078A803H3BL98PINEQRW8342HBAMS",
Expand Down
2 changes: 1 addition & 1 deletion test/examples/protocols/registrationWithGroupsUpdate.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "A generic protocol updated with new information",
"iotagent": "http://smartGondor.com/New",
"resource": "/iot/d",
"services": [
"groups": [
{
"apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732",
"token": "8970A9078A803H3BL98PINEQRW8342HBAMS",
Expand Down
2 changes: 1 addition & 1 deletion test/examples/protocols/registrationWithMissingAttrs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"description": "A generic protocol",
"iotagent": "http://smartGondor.com/",
"resource": "/iot/d",
"services": [
"groups": [
{
"apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732",
"token": "8970A9078A803H3BL98PINEQRW8342HBAMS",
Expand Down
2 changes: 1 addition & 1 deletion test/examples/protocols/registrationWithNewGroups.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "A generic protocol",
"iotagent": "http://smartGondor.com/",
"resource": "/iot/d",
"services": [
"groups": [
{
"apikey": "L23123HJ01230BJ4HV87K0BMSA807898PI9H2",
"token": "90DSFLK3Y9032NEQL8970A92HBARW83403H3",
Expand Down
2 changes: 1 addition & 1 deletion test/examples/protocols/registrationWithWrongAttrs.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"iotagent": "http://smartGondor.com/",
"APIKey": "567586gyh rtygooytrdytfcy7567",
"service": "SmartGondor",
"services": [
"groups": [
{
"apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732",
"token": "8970A9078A803H3BL98PINEQRW8342HBAMS",
Expand Down
2 changes: 1 addition & 1 deletion test/examples/provisioning/getGroupList.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"services": [
"groups": [
{
"resource": "/deviceTest",
"apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732",
Expand Down
2 changes: 1 addition & 1 deletion test/examples/provisioning/postCleanGroup1.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"services": [
"groups": [
{
"resource": "/iot/d",
"apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732",
Expand Down
2 changes: 1 addition & 1 deletion test/examples/provisioning/postCleanGroup2.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"services": [
"groups": [
{
"resource": "/iot/a",
"apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732",
Expand Down
2 changes: 1 addition & 1 deletion test/examples/provisioning/postGroup.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"services": [
"groups": [
{
"resource": "/deviceTest",
"apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732",
Expand Down
2 changes: 1 addition & 1 deletion test/examples/provisioning/postGroupArray.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"services": [
"groups": [
{
"apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732",
"entity_type": "SensorMachine",
Expand Down
36 changes: 36 additions & 0 deletions test/examples/provisioning/postGroupServices.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"services": [
{
"resource": "/deviceTest",
"apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732",
"entity_type": "SensorMachine",
"trust": "8970A9078A803H3BL98PINEQRW8342HBAMS",
"cbHost": "http://unexistentHost:1026",
"commands": [
{
"name": "wheel1",
"type": "Wheel"
}
],
"lazy": [
{
"name": "luminescence",
"type": "Lumens"
}
],
"attributes": [
{
"name": "status",
"type": "Boolean"
}
],
"static_attributes": [
{
"name": "bootstrapServer",
"type": "Address",
"value": "127.0.0.1"
}
]
}
]
}
Loading
Loading