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

Send update from the scratch #1513

Merged
merged 39 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
c781cd8
version of sendUpdate from the scratch. WIP (Not all test passes)
mrutid Oct 9, 2023
ac23f2b
remove IDE files
mrutid Oct 9, 2023
af8cd02
add TimeInstant constant usage and support for object_id explicit attrs
mrutid Oct 9, 2023
a09e03c
clean up explicitAttrs condition
mrutid Oct 9, 2023
99d277c
Merge branch 'master' into mru_sendUpdatefromthescratch
mrutid Oct 9, 2023
c45c875
remove addTimestamp helper
mrutid Oct 9, 2023
13beb64
remove atributes param (add mesares)
mrutid Oct 9, 2023
062fade
rearrange defaults and clean up
mrutid Oct 9, 2023
4a4a6cb
encapsulate attr to pojo mangement
mrutid Oct 9, 2023
882ce96
use Reduce properly y minos refactors
mrutid Oct 9, 2023
175f7b2
refactor: some logs, comments and cleaning
mrutid Oct 10, 2023
34d19c5
Fix TEST CASES JEXL
mrutid Oct 10, 2023
166f771
Some fixes for JEXL suite
mrutid Oct 10, 2023
2abf03d
Merge remote-tracking branch 'origin/task/remove_autocast2' into mru_…
mrutid Oct 10, 2023
ba61993
Fix Test alias plugin
mrutid Oct 10, 2023
ce40b92
remove set log level debug
mrutid Oct 10, 2023
faf7e42
fix silent entities in Multientity TEST
mrutid Oct 10, 2023
75c66f9
FIX TEST Multientity and active devices
mrutid Oct 10, 2023
e271601
manage default behavior for active with missing object_id
mrutid Oct 10, 2023
5f0966e
Merge branch 'master' into mru_sendUpdatefromthescratch
mrutid Oct 10, 2023
d1cf27b
lint fix
mrutid Oct 10, 2023
50c87e4
remove DEBUG level from test
mrutid Oct 10, 2023
e76e323
remove compressed TimeInstant test (unused/unwanted feature)
mrutid Oct 10, 2023
347e4c5
fix lint
mrutid Oct 10, 2023
a48510d
fixing wrong expect
mrutid Oct 10, 2023
1159830
Refactor
mrutid Oct 11, 2023
74e8c6c
FIX TEST
mrutid Oct 11, 2023
f06315d
add deepequal to utils
mrutid Oct 11, 2023
7a5c10f
remove debug artifact from package.json
mrutid Oct 11, 2023
6f6e8d1
remove leftovers (DEBUG)
mrutid Oct 11, 2023
b8c1fee
fix test. The were expecting silent entities
mrutid Oct 11, 2023
3fc9c6f
inprove TimeInstant (as a measure) management
mrutid Oct 11, 2023
c852e5d
remove files with no test
mrutid Oct 11, 2023
fa3ebd9
inprove TimeInstant measure 2
mrutid Oct 11, 2023
e33206b
Update CHANGES_NEXT_RELEASE
mrutid Oct 11, 2023
ca325d2
Merge branch 'master' into mru_sendUpdatefromthescratch
mrutid Oct 11, 2023
1002a7d
Merge branch 'master' into mru_sendUpdatefromthescratch
mrutid Oct 11, 2023
d960343
rearrange and improve log (DEBUG)
mrutid Oct 16, 2023
f35f688
Merge branch 'mru_sendUpdatefromthescratch' of https://github.com/tel…
mrutid Oct 16, 2023
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
2 changes: 2 additions & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- Large refactor of IOTA Lib code to make it simpler
- Remove: time compression support
- Remove: autocast (including env var IOTA_AUTOCAST) (#1498)
- Fix: use but not store timestamp and explicitAttrs from group with autoprovisioned devices (#1504, partially)
- Fix: MongoDB connection authentication (user and password were not actually used) (#1510)
Expand Down
6 changes: 3 additions & 3 deletions lib/plugins/jexlParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,16 @@ function applyExpression(expression, context, typeInformation) {
// Delete null values from context. Related:
// https://github.com/telefonicaid/iotagent-node-lib/issues/1440
// https://github.com/TomFrost/Jexl/issues/133
deleteNulls(context);
deleteNullsAndNaN(context);
const result = parse(expression, context);
logger.debug(logContext, 'applyExpression "[%j]" over "[%j]" result "[%j]" ', expression, context, result);
const expressionResult = result !== undefined ? result : expression;
return expressionResult;
}

function deleteNulls(object) {
function deleteNullsAndNaN(object) {
for (let key in object) {
if (object[key] === null) {
if (object[key] === null || Number.isNaN(object[key])) {
delete object[key];
}
}
Expand Down
84 changes: 80 additions & 4 deletions lib/services/ngsi/entities-NGSI-LD.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,87 @@ const _ = require('underscore');
const context = {
op: 'IoTAgentNGSI-LD'
};
const NGSIv2 = require('./entities-NGSI-v2');
const NGSIUtils = require('./ngsiUtils');

const NGSI_LD_URN = 'urn:ngsi-ld:';

/**
* Adds timestamp to ngsi payload entities accoding to timezone, and an optional timestampvalue.
*
* @param {Object} payload NGSIv2 payload with one or more entities
* @param String timezone TimeZone value (optional)
* @param String timestampValue Timestamp value (optional). If not provided current timestamp is used
* @param Boolean skipMetadataAtt An optional flag to indicate if timestamp should be added to each metadata attribute. Default is false
* @return {Object} NGSIv2 payload entities with timestamp
*/
function addTimestamp(payload, timezone, timestampValue) {
function addTimestampEntity(entity, timezone, timestampValue) {
const timestamp = {
type: constants.TIMESTAMP_TYPE_NGSI2
};

if (timestampValue) {
timestamp.value = timestampValue;
} else if (!timezone) {
timestamp.value = new Date().toISOString();
} else {
timestamp.value = moment().tz(timezone).format('YYYY-MM-DD[T]HH:mm:ss.SSSZ');
}

function addMetadata(attribute) {
let timestampFound = false;

if (!attribute.metadata) {
attribute.metadata = {};
}

for (let i = 0; i < attribute.metadata.length; i++) {
if (attribute.metadata[i] === constants.TIMESTAMP_ATTRIBUTE) {
if (
attribute.metadata[constants.TIMESTAMP_ATTRIBUTE].type === constants.TIMESTAMP_TYPE_NGSI2 &&
attribute.metadata[constants.TIMESTAMP_ATTRIBUTE].value === timestamp.value
) {
timestampFound = true;
break;
}
}
}

if (!timestampFound) {
attribute.metadata[constants.TIMESTAMP_ATTRIBUTE] = timestamp;
}

return attribute;
}
let keyCount = 0;
for (const key in entity) {
/* eslint-disable-next-line no-prototype-builtins */
if (entity.hasOwnProperty(key) && key !== 'id' && key !== 'type') {
addMetadata(entity[key]);
keyCount += 1;
}
}
// Add timestamp just to entity with attrs: multientity plugin could
// create empty entities just with id and type.
if (keyCount > 0) {
entity[constants.TIMESTAMP_ATTRIBUTE] = timestamp;
}

return entity;
}

if (payload instanceof Array) {
for (let i = 0; i < payload.length; i++) {
if (!utils.isTimestampedNgsi2(payload[i])) {
payload[i] = addTimestampEntity(payload[i], timezone, timestampValue);
}
}

return payload;
}
return addTimestampEntity(payload, timezone, timestampValue);
}

/**
* Amends an NGSIv2 attribute to NGSI-LD format
* All native JSON types are respected and cast as Property values
Expand Down Expand Up @@ -843,7 +919,7 @@ function sendUpdateValueNgsiLD(entityName, attributes, typeInformation, token, c
? config.getConfig().timestamp
: timestampValue !== undefined
) {
newEntity = NGSIv2.addTimestamp(newEntity, typeInformation.timezone, timestampValue);
newEntity = addTimestamp(newEntity, typeInformation.timezone, timestampValue);
logger.debug(context, 'sendUpdateValueNgsiLD \n timestamped newEntity=%j', newEntity);
}
payload.push(newEntity);
Expand Down Expand Up @@ -927,14 +1003,14 @@ function sendUpdateValueNgsiLD(entityName, attributes, typeInformation, token, c
// timeInstant is provided as measure
if (Object.keys(payload[0]).length > 1) {
// include metadata with TimeInstant in attrs when TimeInstant is provided as measure in all entities
payload[0] = NGSIv2.addTimestamp(payload[0], typeInformation.timezone, timestampValue);
payload[0] = addTimestamp(payload[0], typeInformation.timezone, timestampValue);
}
} else {
// jshint maxdepth:5
for (let n = 0; n < payload.length; n++) {
if (!utils.isTimestampedNgsi2(payload[n])) {
// legacy check needed?
payload[n] = NGSIv2.addTimestamp(payload[n], typeInformation.timezone);
payload[n] = addTimestamp(payload[n], typeInformation.timezone);
// jshint maxdepth:5
} else if (!utils.IsValidTimestampedNgsi2(payload[n])) {
// legacy check needed?
Expand Down
Loading
Loading