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 send ngsiv2 measure as a request to CB #1520

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- Allow send ngsiv2 measure as a request to ContextBroker
Copy link
Member

Choose a reason for hiding this comment

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

No associated issue number?

Copy link
Member Author

Choose a reason for hiding this comment

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

No issue number

Copy link
Member

Choose a reason for hiding this comment

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

NTC

- Large refactor of IOTA Lib code to make it simpler
- Remove: time compression support
- Remove: autocast (including env var IOTA_AUTOCAST) (#1498)
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 @@ -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;
Expand Down
49 changes: 32 additions & 17 deletions lib/services/ngsi/entities-NGSI-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,11 @@ function sendUpdateValueNgsi2(entityName, measures, typeInformation, token, call
typeof currentAttr.entity_name == 'string'
) {
try {
logger.debug(context,
logger.debug(
context,
'Evaluating attribute: %j, for entity_name(exp):%j, with ctxt: %j',
currentAttr.name,
currentAttr.entity_name,
currentAttr.name,
currentAttr.entity_name,
jexlctxt
);
attrEntityName = jexlParser.applyExpression(currentAttr.entity_name, jexlctxt, typeInformation);
Expand Down Expand Up @@ -451,10 +452,11 @@ function sendUpdateValueNgsi2(entityName, measures, typeInformation, token, call
} catch (e) {
valueExpression = null;
}
logger.debug(context,
'Evaluated attr: %j, with expression: %j, and ctxt: %j resulting: %j',
logger.debug(
context,
'Evaluated attr: %j, with expression: %j, and ctxt: %j resulting: %j',
currentAttr.name,
currentAttr.expression,
currentAttr.expression,
jexlctxt,
valueExpression
);
Expand Down Expand Up @@ -564,7 +566,7 @@ function sendUpdateValueNgsi2(entityName, measures, typeInformation, token, call
}
}
}

let url = '/v2/op/update';
let options = NGSIUtils.createRequestObject(url, typeInformation, token);
options.json = payload;
Expand Down Expand Up @@ -607,15 +609,9 @@ function sendUpdateValueNgsi2(entityName, measures, typeInformation, token, call
} // else: keep current options object created for a batch update

//Send the NGSI request
logger.debug(context,
'Updating device value in the Context Broker at: %j',
options.url
);
logger.debug(context,
'Using the following NGSI v2 request: %j',
options
);

logger.debug(context, 'Updating device value in the Context Broker at: %j', options.url);
logger.debug(context, 'Using the following NGSI v2 request: %j', options);

request(
options,
generateNGSI2OperationHandler('update', entityName, typeInformation, token, options, callback)
Expand All @@ -631,11 +627,30 @@ 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);
let payload = {
actionType: 'append',
entities: measure.length ? measure : [measure]
};
options.json = payload;
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.formatGeoAttrs = formatGeoAttrs;
13 changes: 13 additions & 0 deletions lib/services/ngsi/ngsiService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(entityName, 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
Expand Down Expand Up @@ -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);
Expand Down
Loading