diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 8b1378917..a4bcd74d2 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1 +1 @@ - +- Include entity id and entity type as measure value when measure is an ngsi payload diff --git a/lib/commonBindings.js b/lib/commonBindings.js index 8de4334c9..d9cef39d5 100644 --- a/lib/commonBindings.js +++ b/lib/commonBindings.js @@ -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); } } } diff --git a/lib/constants.js b/lib/constants.js index ded407577..908e78e5f 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -32,6 +32,7 @@ module.exports = { PAYLOAD_NGSIv2: 'ngsiv2', PAYLOAD_NGSILD: 'ngsild', + NGSI: 'ngsi_', DATE_FORMAT: "yyyymmdd'T'HHMMss'Z'", diff --git a/test/unit/ngsiv2/contextRequests/ngsildPayloadMeasure.json b/test/unit/ngsiv2/contextRequests/ngsildPayloadMeasure.json index a2da1a2ff..ac54337e9 100644 --- a/test/unit/ngsiv2/contextRequests/ngsildPayloadMeasure.json +++ b/test/unit/ngsiv2/contextRequests/ngsildPayloadMeasure.json @@ -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", diff --git a/test/unit/ngsiv2/contextRequests/ngsildPayloadMeasure2.json b/test/unit/ngsiv2/contextRequests/ngsildPayloadMeasure2.json index 3428dd809..5b0acbff8 100644 --- a/test/unit/ngsiv2/contextRequests/ngsildPayloadMeasure2.json +++ b/test/unit/ngsiv2/contextRequests/ngsildPayloadMeasure2.json @@ -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", diff --git a/test/unit/ngsiv2/contextRequests/ngsiv2PayloadMeasure.json b/test/unit/ngsiv2/contextRequests/ngsiv2PayloadMeasure.json index f86bd9076..990dd2d93 100644 --- a/test/unit/ngsiv2/contextRequests/ngsiv2PayloadMeasure.json +++ b/test/unit/ngsiv2/contextRequests/ngsiv2PayloadMeasure.json @@ -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" diff --git a/test/unit/ngsiv2/contextRequests/ngsiv2PayloadMeasure2.json b/test/unit/ngsiv2/contextRequests/ngsiv2PayloadMeasure2.json index 4609d052c..ee1285330 100644 --- a/test/unit/ngsiv2/contextRequests/ngsiv2PayloadMeasure2.json +++ b/test/unit/ngsiv2/contextRequests/ngsiv2PayloadMeasure2.json @@ -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"