Skip to content

Commit

Permalink
allow extract multiple measures from ngsi when mqtt binding
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvaroVega committed Nov 13, 2023
1 parent 9af8fdf commit 961e857
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 19 deletions.
54 changes: 36 additions & 18 deletions lib/commonBindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,28 +258,46 @@ function multipleMeasures(apiKey, deviceId, device, messageObj) {

for (let j = 0; j < messageObj.length; j++) {
measure = messageObj[j];
let attributesArray = [];
const values = extractAttributes(device, measure, device.payloadType);
iotAgentLib.update(device.name, device.type, '', values, 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
if (values[0][0]) {
// multi measure is extracted due to payloadType is ngsi
attributesArray = values;
} else {
attributesArray = [values];
}
for (let k in attributesArray) {
config
.getLogger()
.debug(
context,
'Processing multiple measures for device %s with apiKey %s values %j',
deviceId,
apiKey,
attributesArray[k]
);
} else {
config
.getLogger()
.info(
iotAgentLib.update(device.name, device.type, '', attributesArray[k], device, function (error) {
if (error) {
config.getLogger().error(
ctxt,
'Multiple measures for device %s with apiKey %s successfully updated',
deviceId,
apiKey
/*jshint quotmark: double */
"MEASURES-002: Couldn't send the updated values to the Context Broker due to an error: %j",
/*jshint quotmark: single */
error
);
}
finishSouthBoundTransaction(null);
});
} else {
config
.getLogger()
.info(
ctxt,
'Multiple measures for device %s with apiKey %s successfully updated',
deviceId,
apiKey
);
}
finishSouthBoundTransaction(null);
});
}
}
}

Expand Down
125 changes: 124 additions & 1 deletion test/unit/ngsiv2/MQTT_receive_ngsiv2_measures-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
* please contact with::[[email protected]]
*
*/

/* eslint-disable no-unused-vars */

const iotaJson = require('../../../');
Expand Down Expand Up @@ -148,6 +147,130 @@ describe('MQTT: NGSIv2 Measure reception ', function () {
});
});

describe('When a publish multiple NGSIv2 append measure format arrives for the MQTT binding and NGSIV2 is the expected payload type', function () {
const measure = {
actionType: 'APPEND',
entities: [
{
id: 'urn:ngsi-ld:Streetlight:Streetlight-Mylightpoint-2',
type: 'Streetlight',
name: {
type: 'Text',
value: 'MyLightPoint-test1'
},
description: {
type: 'Text',
value: 'testdescription'
},
status: {
type: 'Text',
value: 'connected'
},
dateServiceStarted: {
type: 'DateTime',
value: '2020-06-04T09: 55: 02'
},
locationComment: {
type: 'Text',
value: 'Test1'
},
location: {
type: 'geo:json',
value: {
coordinates: [-87.88429, 41.99499],
type: 'Point'
}
},
address: {
type: 'Text',
value: {
streetAddress: 'MyStreet'
}
},
isRemotelyManaged: {
type: 'Integer',
value: 1
},
installationDate: {
type: 'DateTime',
value: '2022-04-17T02: 30: 04'
}
},
{
id: 'urn:ngsi-ld:Streetlight:Streetlight-Mylightpoint-3',
type: 'Streetlight',
name: {
type: 'Text',
value: 'MyLightPoint-test2'
},
description: {
type: 'Text',
value: 'testdescription'
},
status: {
type: 'Text',
value: 'connected'
},
dateServiceStarted: {
type: 'DateTime',
value: '2022-06-04T09: 55: 02'
},
locationComment: {
type: 'Text',
value: 'Test3'
},
location: {
type: 'geo:json',
value: {
coordinates: [-84.88429, 42.99499],
type: 'Point'
}
},
address: {
type: 'Text',
value: {
streetAddress: 'MyFarStreet'
}
},
isRemotelyManaged: {
type: 'Integer',
value: 3
},
installationDate: {
type: 'DateTime',
value: '2023-04-17T02: 30: 04'
}
}
]
};
beforeEach(function () {
contextBrokerMock
.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')
)
.reply(204);
});
it('should send its value to the Context Broker', function (done) {
mqttClient.publish('json/1234/MQTT_2/attrs', JSON.stringify(measure), null, function (error) {
setTimeout(function () {
contextBrokerMock.done();
done();
}, 100);
});
});
});

describe('When a publish single NGSIv2 entity measure format arrives for the MQTT binding and NGSIV2 is the expected payload type', function () {
const measure = {
id: 'urn:ngsi-ld:Streetlight:Streetlight-Mylightpoint-2',
Expand Down

0 comments on commit 961e857

Please sign in to comment.