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 new /iot/groups API route #1648

Merged
merged 7 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,8 @@ module.exports = {
ATTRIBUTE_DEFAULT,
DATETIME_DEFAULT,

CONFIGGROUP_TERM: 'groups',
CONFIGGROUP_API_PATH: '/iot/groups',

getInitialValueForType
};
58 changes: 52 additions & 6 deletions lib/services/northBound/deviceGroupAdministrationServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@ const restUtils = require('./restUtils');
const groupService = require('./../groups/groupService');
const async = require('async');
const apply = async.apply;
const templateGroup = require('../../templates/deviceGroup.json');
const templateGroupService = require('../../templates/deviceGroup.json');
let configurationHandler;
let removeConfigurationHandler;
let configurationMiddleware = [];
const _ = require('underscore');
const mandatoryHeaders = ['fiware-service', 'fiware-servicepath'];
const mandatoryParameters = ['resource', 'apikey'];
const constants = require('../../constants');

// Create new templeate for configuration groups replacing services by CONFIGGROUP_TERM
mapedraza marked this conversation as resolved.
Show resolved Hide resolved
const templateGroup = JSON.parse(JSON.stringify(templateGroupService));
templateGroup.properties[constants.CONFIGGROUP_TERM] = templateGroup.properties.services;
delete templateGroup.properties.services;

const apiToInternal = {
entity_type: 'type',
Expand Down Expand Up @@ -118,6 +124,8 @@ function applyConfigurationMiddlewares(newConfiguration, callback) {
* @param {Function} next Invokes the next middleware in the chain.
*/
function handleCreateDeviceGroup(req, res, next) {
req.body = checkAndModifyGroupRecv(req._parsedUrl.pathname, req.body);

for (let i = 0; i < req.body.services.length; i++) {
req.body.services[i] = applyMap(apiToInternal, req.body.services[i]);
req.body.services[i].service = req.headers['fiware-service'];
Expand Down Expand Up @@ -161,8 +169,7 @@ function handleListDeviceGroups(req, res, next) {
} else {
translatedGroup = applyMap(internalToApi, group);
}

res.status(200).send(translatedGroup);
res.status(200).send(checkAndModifyGroupResp(req._parsedUrl.pathname, translatedGroup));
}
};

Expand Down Expand Up @@ -267,21 +274,32 @@ function loadContextRoutes(router) {
router.post(
'/iot/services',
restUtils.checkRequestAttributes('headers', mandatoryHeaders),
restUtils.checkBody(templateGroupService),
handleCreateDeviceGroup
);

router.post(
'/iot/groups',
restUtils.checkRequestAttributes('headers', mandatoryHeaders),
restUtils.checkBody(templateGroup),
handleCreateDeviceGroup
);
Comment on lines 276 to 287
Copy link
Collaborator Author

@mapedraza mapedraza Aug 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Needed 2 router.post() entries in order to allow to use a different template for each query
NTD.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got that. Maybe CheckBody should take care of it. If it can do comlex checkings.

Copy link
Member

@mrutid mrutid Aug 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"revalidator" dependency doesn't allow conditional checking. The point here is that limits on auxiliar libs shouldn't drive to special "code structures" at the top level code. Top Level code is more important than a limited validation library. I think uniformity at this level is more important than check two options of JSON (probably this can be achieved by using async.some module already required)


router.get('/iot/services', restUtils.checkRequestAttributes('headers', mandatoryHeaders), handleListDeviceGroups);
router.get(
['/iot/services', '/iot/groups'],
restUtils.checkRequestAttributes('headers', mandatoryHeaders),
handleListDeviceGroups
);

router.put(
'/iot/services',
['/iot/services', '/iot/groups'],
restUtils.checkRequestAttributes('headers', mandatoryHeaders),
restUtils.checkRequestAttributes('query', mandatoryParameters),
handleModifyDeviceGroups
);

router.delete(
'/iot/services',
['/iot/services', '/iot/groups'],
restUtils.checkRequestAttributes('headers', mandatoryHeaders),
restUtils.checkRequestAttributes('query', mandatoryParameters),
handleDeleteDeviceGroups
Expand All @@ -306,6 +324,34 @@ function clear(callback) {
callback();
}

function renameObjetKeys(keysMap, obj) {
return Object.keys(obj).reduce((acc, key) => {
// Si la clave existe en el mapa, usar la clave renombrada
mapedraza marked this conversation as resolved.
Show resolved Hide resolved
const renamedKey = keysMap[key] || key;

// Agregar la clave renombrada al acumulador
acc[renamedKey] = obj[key];

return acc;
mapedraza marked this conversation as resolved.
Show resolved Hide resolved
}, {});
}

function checkAndModifyGroupResp(route, payload) {
if (route === '/iot/services') {
return payload;
} else {
mapedraza marked this conversation as resolved.
Show resolved Hide resolved
return renameObjetKeys({ services: 'groups' }, payload);
mapedraza marked this conversation as resolved.
Show resolved Hide resolved
}
}

function checkAndModifyGroupRecv(route, payload) {
if (route === '/iot/services') {
return payload;
} else {
mapedraza marked this conversation as resolved.
Show resolved Hide resolved
return renameObjetKeys({ groups: 'services' }, payload);
mapedraza marked this conversation as resolved.
Show resolved Hide resolved
}
}

exports.loadContextRoutes = loadContextRoutes;
exports.setConfigurationHandler = setConfigurationHandler;
exports.setRemoveConfigurationHandler = setRemoveConfigurationHandler;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"groups": [
{
"resource": "/deviceTest",
"apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732",
"entity_type": "Light",
"trust": "8970A9078A803H3BL98PINEQRW8342HBAMS",
"cbHost": "http://unexistentHost:1026",
"commands": [
{
"name": "wheel1",
"type": "Wheel"
}
],
"lazy": [
{
"name": "luminescence",
"type": "Lumens"
}
],
"attributes": [
{
"name": "status",
"type": "Boolean"
}
]
},
{
"resource": "/deviceTest",
"apikey": "23HJK3Y9090DSFL173209HV8801232",
"entity_type": "Termometer",
"trust": "BL9803H3QRW8342HBAMS8A8",
"cbHost": "http://unexistentHost:1026",
"commands": [
{
"name": "temperature",
"type": "degrees"
}
],
"lazy": [],
"attributes": []
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"groups": [
{
"resource": "/deviceDuplicateGroup",
"apikey": "JK09H3L12K09H3L123HJK0732L123HJK38K09H3",
"entity_type": "CommandMachinery",
"trust": "8970A9078A803H3BL98PINEQRW8342HBAMS",
"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"
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"groups": [
{
"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"
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"groups": [
{
"resource": "/deviceTest",
"apikey": "754KL23Y9090DSFL123HSFL12380KL23Y2",
"entity_type": "AnotherMachine",
"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