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

send update for multiple measures #826

Merged
merged 11 commits into from
May 23, 2024
Merged
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
3 changes: 2 additions & 1 deletion CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- Fix: default express limit to 1Mb instead default 100Kb and allow change it throught a conf env var 'IOTA_EXPRESS_LIMIT' (#827)
- Fix: allow send multiple measures to CB in a batch (POST /v2/op/update) and sorted by TimeInstant when possible, instead of using multiples single request (#825, iotagent-node-lib#1612)
- Fix: default express limit to 1Mb instead default 100Kb and allow change it throught a conf env var 'IOTA_EXPRESS_LIMIT' (#827)
29 changes: 11 additions & 18 deletions lib/bindings/HTTPBinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const constants = require('../constants');
let context = {
op: 'IOTAJSON.HTTP.Binding'
};

const config = require('../configService');
let httpBindingServer;
const transport = 'HTTP';
Expand Down Expand Up @@ -355,19 +356,13 @@ function handleIncomingMeasure(req, res, next) {
if (attributeArr.length === 0) {
finishSouthBoundTransaction(next);
} else {
for (const j in attributeArr) {
attributeValues = attributeArr[j];
config
.getLogger()
.debug(
context,
'Processing measure device %s with attributeArr %j attributeValues %j',
device.name,
attributeArr,
attributeValues
);
if (req.isCommand) {
const executions = [];
config
.getLogger()
.debug(context, 'Processing measure device %s with attributeArr %j', device.name, attributeArr);
if (req.isCommand) {
const executions = [];
for (const j in attributeArr) {
attributeValues = attributeArr[j];
for (const k in attributeValues) {
executions.push(
iotAgentLib.setCommandResult.bind(
Expand All @@ -382,12 +377,10 @@ function handleIncomingMeasure(req, res, next) {
)
);
}
async.parallel(executions, updateCommandHandler);
} else if (attributeValues.length > 0) {
iotAgentLib.update(device.name, device.type, '', attributeValues, device, updateCommandHandler);
} else {
finishSouthBoundTransaction(next);
}
async.parallel(executions, updateCommandHandler);
} else {
iotAgentLib.update(device.name, device.type, '', attributeArr, device, updateCommandHandler);
}
}
}
Expand Down
56 changes: 27 additions & 29 deletions lib/commonBindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,38 +296,36 @@ function multipleMeasures(apiKey, deviceId, device, messageObj) {
} else {
attributesArray = [values];
}
for (const k in attributesArray) {
config
.getLogger()
.debug(
context,
'Processing multiple measures for device %s with apiKey %s values %j',
deviceId,
apiKey,
attributesArray[k]
config
.getLogger()
.debug(
context,
'Processing multiple measures for device %s with apiKey %s values %j',
deviceId,
apiKey,
attributesArray
);
iotAgentLib.update(device.name, device.type, '', attributesArray, device, function (error) {
if (error) {
config.getLogger().error(
ctxt,
/*jshint quotmark: double */
"MEASURES-002: Couldn't send the updated values to the Context Broker due to an error: %j",
/*jshint quotmark: single */
error
);
iotAgentLib.update(device.name, device.type, '', attributesArray[k], device, function (error) {
if (error) {
config.getLogger().error(
} else {
config
.getLogger()
.info(
ctxt,
/*jshint quotmark: double */
"MEASURES-002: Couldn't send the updated values to the Context Broker due to an error: %j",
/*jshint quotmark: single */
error
'Multiple measures for device %s with apiKey %s successfully updated',
deviceId,
apiKey
);
} else {
config
.getLogger()
.info(
ctxt,
'Multiple measures for device %s with apiKey %s successfully updated',
deviceId,
apiKey
);
}
finishSouthBoundTransaction(null);
});
}
}
finishSouthBoundTransaction(null);
});
}
}

Expand Down
13 changes: 2 additions & 11 deletions test/unit/ngsiv2/HTTP_receive_ngsild_measures-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ describe('HTTP: NGSILD Measure reception ', function () {
async.series([iotAgentLib.clearAll, iotaJson.stop], done);
});


describe('When a POST single NGSILD entity measure with NGSILD format arrives for the HTTP binding and NGSILD is the expected payload type', function () {
const optionsMeasure = {
url: 'http://localhost:' + config.http.port + '/iot/json/',
Expand Down Expand Up @@ -227,16 +226,8 @@ describe('HTTP: NGSILD Measure reception ', function () {
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.post(
'/v2/entities?options=upsert',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/ngsildPayloadMeasure.json')
)
.reply(204);
contextBrokerMock
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.post(
'/v2/entities?options=upsert',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/ngsildPayloadMeasure2.json')
'/v2/op/update',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/ngsildPayloadMultipleMeasure.json')
)
.reply(204);
});
Expand Down
24 changes: 4 additions & 20 deletions test/unit/ngsiv2/HTTP_receive_ngsiv2_measures-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,8 @@ describe('HTTP: NGSIv2 Measure reception ', function () {
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.post(
'/v2/entities?options=upsert',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/ngsiv2PayloadMeasure.json')
)
.reply(204);
contextBrokerMock
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.post(
'/v2/entities?options=upsert',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/ngsiv2PayloadMeasure2.json')
'/v2/op/update',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/ngsiv2PayloadMultipleMeasure.json')
)
.reply(204);
});
Expand Down Expand Up @@ -528,16 +520,8 @@ describe('HTTP: NGSIv2 Measure reception ', function () {
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.post(
'/v2/entities?options=upsert',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/ngsiv2PayloadMeasure.json')
)
.reply(204);
contextBrokerMock
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.post(
'/v2/entities?options=upsert',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/ngsiv2PayloadMeasure2.json')
'/v2/op/update',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/ngsiv2PayloadMultipleMeasure2.json')
)
.reply(204);
});
Expand Down
119 changes: 15 additions & 104 deletions test/unit/ngsiv2/HTTP_reveice_measures-test2.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,8 @@ describe('HTTP: Measure reception ', function () {
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.post(
'/v2/entities?options=upsert',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/multipleMeasuresJsonTypes2.json')
)
.reply(204);
contextBrokerMock
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.post(
'/v2/entities?options=upsert',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/multipleMeasuresJsonTypes.json')
'/v2/op/update',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/multipleMeasuresJsonTypes3.json')
)
.reply(204);
});
Expand Down Expand Up @@ -215,17 +207,8 @@ describe('HTTP: Measure reception ', function () {
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.post(
'/v2/entities?options=upsert',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/timeInstantMeasures.json')
)
.reply(204);

contextBrokerMock
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.post(
'/v2/entities?options=upsert',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/timeInstantMeasures2.json')
'/v2/op/update',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/timeInstantMultipleMeasures.json')
)
.reply(204);

Expand Down Expand Up @@ -295,27 +278,9 @@ describe('HTTP: Measure reception ', function () {
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.post(
'/v2/entities?options=upsert',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/timeInstantMeasures1b.json')
)
.reply(204);

contextBrokerMock
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.post(
'/v2/entities?options=upsert',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/timeInstantMeasures2b.json')
)
.reply(204);

contextBrokerMock
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.post(
'/v2/entities?options=upsert'
'/v2/op/update'
// FIXME: Mock about h atttribute + current timestamp
//utils.readExampleFile('./test/unit/ngsiv2/contextRequests/timeInstantMeasures.json')
//utils.readExampleFile('./test/unit/ngsiv2/contextRequests/timeInstantMultipleMeasures1b.json')
)
.reply(204);

Expand Down Expand Up @@ -385,25 +350,7 @@ describe('HTTP: Measure reception ', function () {
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.post(
'/v2/entities?options=upsert',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/timeInstantMeasures1b.json')
)
.reply(204);

contextBrokerMock
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.post(
'/v2/entities?options=upsert',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/timeInstantMeasures2b.json')
)
.reply(204);

contextBrokerMock
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.post(
'/v2/entities?options=upsert'
'/v2/op/update'
// FIXME: Mock about h atttribute + current timestamp
AlvaroVega marked this conversation as resolved.
Show resolved Hide resolved
//utils.readExampleFile('./test/unit/ngsiv2/contextRequests/timeInstantMeasures.json')
)
Expand Down Expand Up @@ -442,7 +389,8 @@ describe('HTTP: Measure reception ', function () {
TimeInstant: '2023-03-23T23:33:33Z'
},
{
h: '111111'
h: '111111',
TimeInstant: '2024-03-23T23:33:33Z'
}
],
headers: {
Expand Down Expand Up @@ -475,27 +423,8 @@ describe('HTTP: Measure reception ', function () {
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.post(
'/v2/entities?options=upsert',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/timeInstantMeasures1b.json')
)
.reply(204);

contextBrokerMock
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.post(
'/v2/entities?options=upsert',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/timeInstantMeasures2b.json')
)
.reply(204);

contextBrokerMock
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.post(
'/v2/entities?options=upsert'
// FIXME: Mock about h atttribute + current timestamp
//utils.readExampleFile('./test/unit/ngsiv2/contextRequests/timeInstantMeasures.json')
'/v2/op/update',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/timeInstantMultipleMeasures1b.json')
)
.reply(204);

Expand Down Expand Up @@ -561,17 +490,8 @@ describe('HTTP: Measure reception ', function () {
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.post(
'/v2/entities?options=upsert',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/timeInstantMeasures.json')
)
.reply(204);

contextBrokerMock
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.post(
'/v2/entities?options=upsert',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/timeInstantMeasures2.json')
'/v2/op/update',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/timeInstantMultipleMeasures.json')
)
.reply(204);

Expand Down Expand Up @@ -627,17 +547,8 @@ describe('HTTP: Measure reception ', function () {
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.post(
'/v2/entities?options=upsert',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/unprovisionedDevice2.json')
)
.reply(204);

contextBrokerUnprovMock
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.post(
'/v2/entities?options=upsert',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/unprovisionedDevice.json')
'/v2/op/update',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/unprovisionedDeviceMultiple.json')
)
.reply(204);

Expand Down
12 changes: 2 additions & 10 deletions test/unit/ngsiv2/MQTT_receive_ngsiv2_measures-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,16 +266,8 @@ describe('MQTT: NGSIv2 Measure reception ', function () {
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.post(
'/v2/entities?options=upsert',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/ngsiv2PayloadMeasure.json')
)
.reply(204);
contextBrokerMock
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.post(
'/v2/entities?options=upsert',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/ngsiv2PayloadMeasure2.json')
'/v2/op/update',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/ngsiv2PayloadMultipleMeasure.json')
)
.reply(204);
});
Expand Down
Loading
Loading