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

add metadata to ngsild payload measure + doc #780

Merged
merged 18 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from 17 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
6 changes: 5 additions & 1 deletion docs/usermanual.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,11 @@ Example of these `ngsild` payloads are the following ones:
}
```

Note that array of entities are handled as a multiple measure, each entity is a measure.
Some additional considerations to take into account:

- In the case of array of entities, they are handled as a multiple measure, i.e. each entity is a measure.
- The `type` of the attribute is the one used in the provision of the attribute, not the one in the measure. The exception is the autoprovisioned devices case, in which case the `type` of the attribute is taken from the measure (given the attribute lacks proviosioned type). In this latter case, if the attribute `type` is not included in the measure the [explicit type omission rules for Context Broker](https://github.com/telefonicaid/fiware-orion/blob/master/doc/manuals/orion-api.md#partial-representations) are also taken into account in this case.
- In the case of NGSI-LD, fields different from `type`, `value` or `object` (e.g. `observedAt` in the examples above) are include as NGSI-v2 metadata in the entity corresponding to the measure at Context Broker. Note IOTA doesn't provide the `type` for that metadata, so the Context Broker applies [a default type based in the metadat `value` JSON type](https://github.com/telefonicaid/fiware-orion/blob/master/doc/manuals/orion-api.md#partial-representations).
fgalan marked this conversation as resolved.
Show resolved Hide resolved

##### SOAP-XML Measure reporting

Expand Down
30 changes: 22 additions & 8 deletions lib/commonBindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ function parseMessage(message) {
* Find the attribute given by its name between all the active attributes of the given device, returning its type, or
* null otherwise.
*
* @param {String} attribute Name of the attribute to find.
* @param {Object} device Device object containing all the information about a device.
* @param {String} attribute Name of the attribute to find.
* @param {Object} device Device object containing all the information about a device.
* @param {measureType} type Type of measure attribute according with measure when available (ngsiv2 and ngsild measures)
* @return {String} String identifier of the attribute type.
*/
function guessType(attribute, device) {
function guessType(attribute, device, measureType) {
if (device.active) {
for (let i = 0; i < device.active.length; i++) {
if (device.active[i].name === attribute) {
Expand All @@ -89,7 +90,11 @@ function guessType(attribute, device) {
if (attribute === constants.TIMESTAMP_ATTRIBUTE) {
return constants.TIMESTAMP_TYPE_NGSI2;
}
return constants.DEFAULT_ATTRIBUTE_TYPE;
if (measureType) {
return measureType;
} else {
return constants.DEFAULT_ATTRIBUTE_TYPE;
}
}

function extractAttributes(device, current, payloadType) {
Expand All @@ -113,7 +118,7 @@ function extractAttributes(device, current, payloadType) {
if (payloadType.toLowerCase() === constants.PAYLOAD_NGSIv2) {
valuesEntity.push({
name: k,
type: entity[k].type,
type: guessType(k, device, entity[k].type),
value: entity[k].value,
metadata: entity[k].metadata ? entity[k].metadata : undefined
});
Expand All @@ -126,13 +131,22 @@ function extractAttributes(device, current, payloadType) {
ent.value = entity[k];
} else {
if (entity[k].type) {
ent.type = 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 (let key in entity[k]) {
if (!['type', 'value', 'object'].includes(key.toLowerCase())) {
AlvaroVega marked this conversation as resolved.
Show resolved Hide resolved
if (!ent.metadata) {
ent.metadata = {};
}
ent.metadata[key] = { "value": entity[k][key] };
}
}
}
valuesEntity.push(ent);
}
Expand All @@ -149,7 +163,7 @@ function extractAttributes(device, current, payloadType) {
if (current.hasOwnProperty(k)) {
values.push({
name: k,
type: guessType(k, device),
type: guessType(k, device, null),
value: current[k]
});
}
Expand Down Expand Up @@ -218,7 +232,7 @@ function singleMeasure(apiKey, deviceId, attribute, device, parsedMessage) {
const values = [
{
name: attribute,
type: guessType(attribute, device),
type: guessType(attribute, device, null),
value: parsedMessage[0]
}
];
Expand Down
7 changes: 6 additions & 1 deletion test/unit/ngsiv2/contextRequests/ngsildPayloadMeasure.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
"type": "AnMQTTDevice",
"status": {
"type": "Property",
"value": "free"
"value": "free",
"metadata": {
"observedAt":{
"value": "2018-09-21T12:00:00Z"
}
}
},
"category": {
"type": "Property",
Expand Down
7 changes: 6 additions & 1 deletion test/unit/ngsiv2/contextRequests/ngsildPayloadMeasure2.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
"type": "AnMQTTDevice",
"status": {
"type": "Property",
"value": "free"
"value": "free",
"metadata": {
"observedAt": {
"value": "2012-09-21T12:00:00Z"
}
}
},
"category": {
"type": "Property",
Expand Down
Loading