Skip to content

Commit

Permalink
check if timestamp is a mapped attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvaroVega committed Apr 3, 2024
1 parent 697ca68 commit 61b3c23
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions lib/services/ngsi/entities-NGSI-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ function sendUpdateValueNgsi2(entityName, measures, typeInformation, token, call
let jexlctxt = {}; //will store the whole context (not just for JEXL)
let payload = {}; //will store the final payload
let timestamp = { type: constants.TIMESTAMP_TYPE_NGSI2 }; //timestamp scafold-attr for insertions.
let timestampAttr = null;
let plainMeasures = null; //will contain measures POJO
let idTypeSSSList = pluginUtils.getIdTypeServSubServiceFromDevice(typeInformation);

Expand Down Expand Up @@ -308,15 +309,14 @@ function sendUpdateValueNgsi2(entityName, measures, typeInformation, token, call
jexlctxt = reduceAttrToPlainObject(idTypeSSSList, jexlctxt);

//Managing timestamp (mustInsertTimeInstant flag to decide if we should insert Timestamp later on)
const mustInsertTimeInstant =
typeInformation.timestamp !== undefined
? typeInformation.timestamp
: false;
const mustInsertTimeInstant = typeInformation.timestamp !== undefined ? typeInformation.timestamp : false;

if (mustInsertTimeInstant) {
//remove TimeInstant from measures
measures = measures.filter((item) => item.name !== constants.TIMESTAMP_ATTRIBUTE);

if (typeInformation && typeInformation.active) {
timestampAttr = typeInformation.active.filter((item) => item.name === constants.TIMESTAMP_ATTRIBUTE);
}
if (plainMeasures[constants.TIMESTAMP_ATTRIBUTE]) {
//if it comes from a measure
if (moment(plainMeasures[constants.TIMESTAMP_ATTRIBUTE], moment.ISO_8601, true).isValid()) {
Expand All @@ -327,6 +327,16 @@ function sendUpdateValueNgsi2(entityName, measures, typeInformation, token, call
);
return;
}
} else if (timestampAttr && timestampAttr.length > 0 && plainMeasures[timestampAttr[0]['object_id']]) {
// Maybe TimeInstant is a mapped attribute
if (moment(plainMeasures[timestampAttr[0]['object_id']], moment.ISO_8601, true).isValid()) {
timestamp.value = plainMeasures[timestampAttr[0]['object_id']];
} else {
callback(
new errors.BadTimestamp(plainMeasures[timestampAttr[0]['object_id']], entityName, typeInformation)
);
return;
}
} else if (!typeInformation.timezone) {
timestamp.value = new Date().toISOString();
jexlctxt[constants.TIMESTAMP_ATTRIBUTE] = timestamp.value;
Expand Down

0 comments on commit 61b3c23

Please sign in to comment.