Skip to content

Commit

Permalink
Task/include entity id and entity type as measures for ngsi measures (#…
Browse files Browse the repository at this point in the history
…804)

* Include entity id and entity type as measure value when measure is an ngsi payload

* update CNR

* remove comment

* do not access to value

* update tests

* include ngsi_id and ngsi_type as measure for payloadtypes

* apply just to payload ngsi
  • Loading branch information
AlvaroVega authored Jan 29, 2024
1 parent b06caf7 commit add77eb
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 31 deletions.
2 changes: 1 addition & 1 deletion CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1 +1 @@

- Include entity id and entity type as measure value when measure is an ngsi payload
66 changes: 37 additions & 29 deletions lib/commonBindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,42 +113,50 @@ function extractAttributes(device, current, payloadType) {
for (const entity of arrayEntities) {
const valuesEntity = [];
for (const k in entity) {
// skip id and type
if (entity.hasOwnProperty(k) && !['id', 'type'].includes(k)) {
if (payloadType.toLowerCase() === constants.PAYLOAD_NGSIv2) {
if (entity.hasOwnProperty(k)) {
if (['id', 'type'].includes(k)) {
// Include ngsi id and type as measures
valuesEntity.push({
name: k,
type: guessType(k, device, entity[k].type),
value: entity[k].value,
metadata: entity[k].metadata ? entity[k].metadata : undefined
name: constants.NGSI + k,
type: guessType(k, device, null),
value: entity[k]
});
} else if (payloadType.toLowerCase() === constants.PAYLOAD_NGSILD) {
const ent = {
name: k
};
if (k.toLowerCase() === '@context') {
ent.type = '@context';
ent.value = entity[k];
} else {
if (entity[k].type) {
ent.type = guessType(k, device, entity[k].type);
if (['property', 'geoproperty'].includes(entity[k].type.toLowerCase())) {
ent.value = entity[k].value;
} else if (entity[k].type.toLowerCase() === 'relationship') {
ent.value = entity[k].object;
} else {
if (payloadType.toLowerCase() === constants.PAYLOAD_NGSIv2) {
valuesEntity.push({
name: k,
type: guessType(k, device, entity[k].type),
value: entity[k].value,
metadata: entity[k].metadata ? entity[k].metadata : undefined
});
} else if (payloadType.toLowerCase() === constants.PAYLOAD_NGSILD) {
const ent = {
name: k
};
if (k.toLowerCase() === '@context') {
ent.type = '@context';
ent.value = entity[k];
} else {
if (entity[k].type) {
ent.type = guessType(k, device, entity[k].type);
if (['property', 'geoproperty'].includes(entity[k].type.toLowerCase())) {
ent.value = entity[k].value;
} else if (entity[k].type.toLowerCase() === 'relationship') {
ent.value = entity[k].object;
}
}
}
// Add other stuff as metadata
for (const key in entity[k]) {
if (!['type', 'value', 'object'].includes(key.toLowerCase())) {
if (!ent.metadata) {
ent.metadata = {};
// Add other stuff as metadata
for (const key in entity[k]) {
if (!['type', 'value', 'object'].includes(key.toLowerCase())) {
if (!ent.metadata) {
ent.metadata = {};
}
ent.metadata[key] = { value: entity[k][key] };
}
ent.metadata[key] = { value: entity[k][key] };
}
}
valuesEntity.push(ent);
}
valuesEntity.push(ent);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module.exports = {

PAYLOAD_NGSIv2: 'ngsiv2',
PAYLOAD_NGSILD: 'ngsild',
NGSI: 'ngsi_',

DATE_FORMAT: "yyyymmdd'T'HHMMss'Z'",

Expand Down
8 changes: 8 additions & 0 deletions test/unit/ngsiv2/contextRequests/ngsildPayloadMeasure.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
{
"id": "Second MQTT Device",
"type": "AnMQTTDevice",
"ngsi_id": {
"type": "string",
"value": "urn:ngsi-ld:ParkingSpot:santander:daoiz_velarde_1_5:3"
},
"ngsi_type": {
"type": "string",
"value": "ParkingSpot"
},
"status": {
"type": "Property",
"value": "free",
Expand Down
8 changes: 8 additions & 0 deletions test/unit/ngsiv2/contextRequests/ngsildPayloadMeasure2.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
{
"id": "Second MQTT Device",
"type": "AnMQTTDevice",
"ngsi_id": {
"type": "string",
"value": "urn:ngsi-ld:ParkingSpot:santander:reyes_magos_1_1:1"
},
"ngsi_type": {
"type": "string",
"value": "ParkingSpot"
},
"status": {
"type": "Property",
"value": "free",
Expand Down
10 changes: 9 additions & 1 deletion test/unit/ngsiv2/contextRequests/ngsiv2PayloadMeasure.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
{
"id":"Second MQTT Device",
"type":"AnMQTTDevice",
"type":"AnMQTTDevice",
"ngsi_id": {
"type": "string",
"value": "urn:ngsiv2:Streetlight:Streetlight-Mylightpoint-2"
},
"ngsi_type": {
"type": "string",
"value": "Streetlight"
},
"name": {
"type": "Text",
"value": "MyLightPoint-test1"
Expand Down
8 changes: 8 additions & 0 deletions test/unit/ngsiv2/contextRequests/ngsiv2PayloadMeasure2.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
{
"id": "Second MQTT Device",
"type": "AnMQTTDevice",
"ngsi_id": {
"type": "string",
"value": "urn:ngsiv2:Streetlight:Streetlight-Mylightpoint-3"
},
"ngsi_type": {
"type": "string",
"value": "Streetlight"
},
"name": {
"type": "Text",
"value": "MyLightPoint-test2"
Expand Down

0 comments on commit add77eb

Please sign in to comment.