iotagent-node-lib 3.4.4 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/doc/admin.md +1 -13
- package/doc/api.md +116 -18
- package/doc/devel/architecture.md +0 -12
- package/doc/index.md +1 -1
- package/doc/roadmap.md +22 -10
- package/lib/commonConfig.js +0 -11
- package/lib/model/Device.js +2 -1
- package/lib/model/Group.js +2 -1
- package/lib/model/dbConn.js +22 -11
- package/lib/plugins/expressionPlugin.js +0 -5
- package/lib/plugins/jexlParser.js +15 -31
- package/lib/services/common/genericMiddleware.js +14 -2
- package/lib/services/common/iotManagerService.js +2 -1
- package/lib/services/devices/deviceRegistryMongoDB.js +3 -1
- package/lib/services/devices/deviceService.js +16 -21
- package/lib/services/devices/devices-NGSI-LD.js +5 -98
- package/lib/services/devices/devices-NGSI-mixed.js +0 -14
- package/lib/services/devices/devices-NGSI-v2.js +3 -0
- package/lib/services/groups/groupRegistryMemory.js +0 -25
- package/lib/services/groups/groupRegistryMongoDB.js +20 -19
- package/lib/services/groups/groupService.js +3 -14
- package/lib/services/ngsi/entities-NGSI-LD.js +81 -6
- package/lib/services/ngsi/entities-NGSI-v2.js +303 -698
- package/lib/services/ngsi/ngsiUtils.js +0 -30
- package/lib/services/northBound/deviceProvisioningServer.js +6 -3
- package/lib/templates/createDevice.json +4 -0
- package/lib/templates/createDeviceLax.json +4 -0
- package/lib/templates/deviceGroup.json +4 -0
- package/lib/templates/updateDevice.json +4 -0
- package/lib/templates/updateDeviceLax.json +4 -0
- package/package.json +6 -2
- package/test/functional/README.md +378 -0
- package/test/functional/config-test.js +70 -0
- package/test/functional/functional-tests-runner.js +126 -0
- package/test/functional/functional-tests.js +241 -0
- package/test/functional/testCases.js +2944 -0
- package/test/functional/testUtils.js +251 -0
- package/test/tools/utils.js +25 -0
- package/test/unit/mongodb/mongodb-connectionoptions-test.js +35 -22
- package/test/unit/ngsi-ld/examples/contextRequests/createProvisionedDeviceWithGroupAndStatic2.json +3 -34
- package/test/unit/ngsi-ld/examples/contextRequests/updateContextAliasPlugin6.json +8 -1
- package/test/unit/ngsi-ld/examples/contextRequests/updateContextAliasPlugin7.json +1 -4
- package/test/unit/ngsi-ld/examples/contextRequests/updateContextAliasPlugin8.json +1 -6
- package/test/unit/ngsi-ld/general/contextBrokerOAuthSecurityAccess-test.js +67 -87
- package/test/unit/ngsi-ld/lazyAndCommands/command-test.js +7 -13
- package/test/unit/ngsi-ld/lazyAndCommands/merge-patch-test.js +43 -43
- package/test/unit/ngsi-ld/lazyAndCommands/polling-commands-test.js +19 -29
- package/test/unit/ngsi-ld/ngsiService/languageProperties-test.js +0 -1
- package/test/unit/ngsi-ld/ngsiService/subscriptions-test.js +35 -46
- package/test/unit/ngsi-ld/plugins/alias-plugin_test.js +8 -9
- package/test/unit/ngsi-ld/provisioning/device-provisioning-api_test.js +96 -221
- package/test/unit/ngsi-ld/provisioning/device-registration_test.js +18 -27
- package/test/unit/ngsi-ld/provisioning/device-update-registration_test.js +8 -16
- package/test/unit/ngsi-ld/provisioning/updateProvisionedDevices-test.js +0 -13
- package/test/unit/ngsiv2/examples/contextRequests/updateContextAliasPlugin8.json +4 -4
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin29b.json +8 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin30.json +1 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin32.json +0 -6
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin34.json +8 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin34b.json +14 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin35.json +1 -11
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin36b.json +13 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin37.json +8 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin41.json +1 -11
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin10b.json +37 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin11.json +0 -4
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin15.json +0 -4
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin16.json +0 -4
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin25.json +4 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin4.json +0 -3
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin5.json +10 -12
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin6.json +0 -4
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin7.json +1 -5
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin8.json +8 -12
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityTimestampPlugin2.json +0 -4
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityTimestampPlugin3.json +0 -8
- package/test/unit/ngsiv2/examples/contextRequests/updateContextStaticAttributesMetadata.json +7 -1
- package/test/unit/ngsiv2/expressions/jexlBasedTransformations-test.js +898 -28
- package/test/unit/ngsiv2/ngsiService/active-devices-test.js +0 -4
- package/test/unit/ngsiv2/ngsiService/staticAttributes-test.js +267 -0
- package/test/unit/ngsiv2/plugins/alias-plugin_test.js +19 -21
- package/test/unit/ngsiv2/plugins/multientity-plugin_test.js +21 -24
- package/test/unit/ngsiv2/provisioning/device-group-utils-test.js +1 -21
- package/test/unit/ngsiv2/provisioning/device-provisioning-api_test.js +4 -6
- package/test/unit/ngsi-ld/ngsiService/autocast-test.js +0 -438
- package/test/unit/ngsi-ld/ngsiService/geoproperties-test.js +0 -381
- package/test/unit/ngsi-ld/provisioning/singleConfigurationMode-test.js +0 -311
- package/test/unit/ngsiv2/ngsiService/autocast-test.js +0 -325
- package/test/unit/ngsiv2/ngsiService/geoproperties-test.js +0 -427
- package/test/unit/ngsiv2/plugins/compress-timestamp-plugin_test.js +0 -217
- package/test/unit/ngsiv2/plugins/timestamp-processing-plugin_test.js +0 -119
- package/test/unit/ngsiv2/provisioning/singleConfigurationMode-test.js +0 -309
|
@@ -30,13 +30,11 @@
|
|
|
30
30
|
const request = require('../../request-shim');
|
|
31
31
|
const alarms = require('../common/alarmManagement');
|
|
32
32
|
const errors = require('../../errors');
|
|
33
|
-
const utils = require('../northBound/restUtils');
|
|
34
33
|
const pluginUtils = require('../../plugins/pluginUtils');
|
|
35
34
|
const config = require('../../commonConfig');
|
|
36
35
|
const constants = require('../../constants');
|
|
37
36
|
const jexlParser = require('../../plugins/jexlParser');
|
|
38
37
|
const expressionPlugin = require('../../plugins/expressionPlugin');
|
|
39
|
-
const compressTimestampPlugin = require('../../plugins/compressTimestamp');
|
|
40
38
|
const moment = require('moment-timezone');
|
|
41
39
|
const NGSIUtils = require('./ngsiUtils');
|
|
42
40
|
const logger = require('logops');
|
|
@@ -94,83 +92,6 @@ function formatGeoAttrs(attr) {
|
|
|
94
92
|
return obj;
|
|
95
93
|
}
|
|
96
94
|
|
|
97
|
-
/**
|
|
98
|
-
* Adds timestamp to ngsiv2 payload entities accoding to timezone, and an optional timestampvalue.
|
|
99
|
-
*
|
|
100
|
-
* @param {Object} payload NGSIv2 payload with one or more entities
|
|
101
|
-
* @param String timezone TimeZone value (optional)
|
|
102
|
-
* @param String timestampValue Timestamp value (optional). If not provided current timestamp is used
|
|
103
|
-
* @param Boolean skipMetadataAtt An optional flag to indicate if timestamp should be added to each metadata attribute. Default is false
|
|
104
|
-
* @return {Object} NGSIv2 payload entities with timestamp
|
|
105
|
-
*/
|
|
106
|
-
function addTimestampNgsi2(payload, timezone, timestampValue) {
|
|
107
|
-
function addTimestampEntity(entity, timezone, timestampValue) {
|
|
108
|
-
const timestamp = {
|
|
109
|
-
type: constants.TIMESTAMP_TYPE_NGSI2
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
if (timestampValue) {
|
|
113
|
-
timestamp.value = timestampValue;
|
|
114
|
-
} else if (!timezone) {
|
|
115
|
-
timestamp.value = new Date().toISOString();
|
|
116
|
-
} else {
|
|
117
|
-
timestamp.value = moment().tz(timezone).format('YYYY-MM-DD[T]HH:mm:ss.SSSZ');
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
function addMetadata(attribute) {
|
|
121
|
-
let timestampFound = false;
|
|
122
|
-
|
|
123
|
-
if (!attribute.metadata) {
|
|
124
|
-
attribute.metadata = {};
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
for (let i = 0; i < attribute.metadata.length; i++) {
|
|
128
|
-
if (attribute.metadata[i] === constants.TIMESTAMP_ATTRIBUTE) {
|
|
129
|
-
if (
|
|
130
|
-
attribute.metadata[constants.TIMESTAMP_ATTRIBUTE].type === constants.TIMESTAMP_TYPE_NGSI2 &&
|
|
131
|
-
attribute.metadata[constants.TIMESTAMP_ATTRIBUTE].value === timestamp.value
|
|
132
|
-
) {
|
|
133
|
-
timestampFound = true;
|
|
134
|
-
break;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
if (!timestampFound) {
|
|
140
|
-
attribute.metadata[constants.TIMESTAMP_ATTRIBUTE] = timestamp;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return attribute;
|
|
144
|
-
}
|
|
145
|
-
let keyCount = 0;
|
|
146
|
-
for (const key in entity) {
|
|
147
|
-
/* eslint-disable-next-line no-prototype-builtins */
|
|
148
|
-
if (entity.hasOwnProperty(key) && key !== 'id' && key !== 'type') {
|
|
149
|
-
addMetadata(entity[key]);
|
|
150
|
-
keyCount += 1;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
// Add timestamp just to entity with attrs: multientity plugin could
|
|
154
|
-
// create empty entities just with id and type.
|
|
155
|
-
if (keyCount > 0) {
|
|
156
|
-
entity[constants.TIMESTAMP_ATTRIBUTE] = timestamp;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
return entity;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
if (payload instanceof Array) {
|
|
163
|
-
for (let i = 0; i < payload.length; i++) {
|
|
164
|
-
if (!utils.isTimestampedNgsi2(payload[i])) {
|
|
165
|
-
payload[i] = addTimestampEntity(payload[i], timezone, timestampValue);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
return payload;
|
|
170
|
-
}
|
|
171
|
-
return addTimestampEntity(payload, timezone, timestampValue);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
95
|
/**
|
|
175
96
|
* Generate an operation handler for NGSIv2-based operations (query and update). The handler takes care of identifiying
|
|
176
97
|
* the errors and calling the appropriate callback with a success or a failure depending on how the operation ended.
|
|
@@ -328,689 +249,373 @@ function sendQueryValueNgsi2(entityName, attributes, typeInformation, token, cal
|
|
|
328
249
|
* array should comply to the NGSIv2's attribute format.
|
|
329
250
|
*
|
|
330
251
|
* @param {String} entityName Name of the entity to register.
|
|
331
|
-
* @param {Array}
|
|
252
|
+
* @param {Array} measures measure array containing the values to update.
|
|
332
253
|
* @param {Object} typeInformation Configuration information for the device.
|
|
333
254
|
* @param {String} token User token to identify against the PEP Proxies (optional).
|
|
334
255
|
*/
|
|
335
|
-
function sendUpdateValueNgsi2(entityName,
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
attributes.forEach(function (attribute) {
|
|
347
|
-
if (attribute.name !== 'id' && attribute.name !== 'type') {
|
|
348
|
-
attributesWithoutIdType.push(attribute);
|
|
256
|
+
function sendUpdateValueNgsi2(entityName, measures, typeInformation, token, callback) {
|
|
257
|
+
//aux function used to builf JEXL context.
|
|
258
|
+
//it returns a flat object from an Attr array
|
|
259
|
+
function reduceAttrToPlainObject(attrs, initObj = {}) {
|
|
260
|
+
if (attrs !== undefined && Array.isArray(attrs)) {
|
|
261
|
+
return attrs.reduce((result, item) => {
|
|
262
|
+
result[item.name] = item.value;
|
|
263
|
+
return result;
|
|
264
|
+
}, initObj);
|
|
265
|
+
} else {
|
|
266
|
+
return initObj;
|
|
349
267
|
}
|
|
350
|
-
});
|
|
351
|
-
attributes = attributesWithoutIdType;
|
|
352
|
-
|
|
353
|
-
const payload = {
|
|
354
|
-
entities: [
|
|
355
|
-
{
|
|
356
|
-
// CB entity id should be always a String
|
|
357
|
-
id: String(entityName)
|
|
358
|
-
}
|
|
359
|
-
]
|
|
360
|
-
};
|
|
361
|
-
|
|
362
|
-
let url = '/v2/op/update';
|
|
363
|
-
|
|
364
|
-
if (typeInformation && typeInformation.type) {
|
|
365
|
-
// CB entity type should be always a String
|
|
366
|
-
payload.entities[0].type = String(typeInformation.type);
|
|
367
268
|
}
|
|
368
269
|
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
let
|
|
270
|
+
let entities = {}; //{entityName:{entityType:[attrs]}} //SubGoal Populate entoties data striucture
|
|
271
|
+
let jexlctxt = {}; //will store the whole context (not just for JEXL)
|
|
272
|
+
let payload = {}; //will store the final payload
|
|
273
|
+
let timestamp = { type: constants.TIMESTAMP_TYPE_NGSI2 }; //timestamp scafold-attr for insertions.
|
|
274
|
+
let plainMeasures = null; //will contain measures POJO
|
|
275
|
+
let idTypeSSSList = pluginUtils.getIdTypeServSubServiceFromDevice(typeInformation);
|
|
372
276
|
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
}
|
|
277
|
+
//Make a clone and overwrite
|
|
278
|
+
typeInformation = JSON.parse(JSON.stringify(typeInformation));
|
|
376
279
|
|
|
280
|
+
//Check mandatory information: type
|
|
377
281
|
if (!typeInformation || !typeInformation.type) {
|
|
378
282
|
callback(new errors.TypeNotFound(null, entityName));
|
|
379
283
|
return;
|
|
380
284
|
}
|
|
381
285
|
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
let measureAttrsForCtxt = [];
|
|
286
|
+
//Make a copy of measures in an plain object: plainMeasures
|
|
287
|
+
plainMeasures = reduceAttrToPlainObject(measures);
|
|
385
288
|
|
|
386
|
-
//
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
289
|
+
//Build the initital JEXL Context
|
|
290
|
+
//All the measures (avoid references make another copy instead)
|
|
291
|
+
jexlctxt = reduceAttrToPlainObject(measures);
|
|
292
|
+
//All the static
|
|
293
|
+
jexlctxt = reduceAttrToPlainObject(typeInformation.staticAttributes, jexlctxt);
|
|
294
|
+
//id type Service and Subservice
|
|
295
|
+
jexlctxt = reduceAttrToPlainObject(idTypeSSSList, jexlctxt);
|
|
296
|
+
|
|
297
|
+
//Managing timestamp (mustInsertTimeInstant flag to decide if we should insert Timestamp later on)
|
|
298
|
+
const mustInsertTimeInstant =
|
|
299
|
+
typeInformation.timestamp !== undefined
|
|
300
|
+
? typeInformation.timestamp
|
|
301
|
+
: config.getConfig().timestamp !== undefined
|
|
302
|
+
? config.getConfig().timestamp
|
|
303
|
+
: false;
|
|
304
|
+
|
|
305
|
+
if (mustInsertTimeInstant) {
|
|
306
|
+
//remove TimeInstant from measures
|
|
307
|
+
measures = measures.filter((item) => item.name !== constants.TIMESTAMP_ATTRIBUTE);
|
|
308
|
+
|
|
309
|
+
if (plainMeasures[constants.TIMESTAMP_ATTRIBUTE]) {
|
|
310
|
+
//if it comes from a measure
|
|
311
|
+
if (moment(plainMeasures[constants.TIMESTAMP_ATTRIBUTE], moment.ISO_8601, true).isValid()) {
|
|
312
|
+
timestamp.value = plainMeasures[constants.TIMESTAMP_ATTRIBUTE];
|
|
403
313
|
} else {
|
|
404
|
-
callback(new errors.
|
|
405
|
-
return;
|
|
314
|
+
callback(new errors.BadTimestamp(null, entityName));
|
|
406
315
|
}
|
|
316
|
+
} else if (!typeInformation.timezone) {
|
|
317
|
+
timestamp.value = new Date().toISOString();
|
|
318
|
+
jexlctxt[constants.TIMESTAMP_ATTRIBUTE] = timestamp.value;
|
|
319
|
+
} else {
|
|
320
|
+
timestamp.value = moment().tz(typeInformation.timezone).format('YYYY-MM-DD[T]HH:mm:ss.SSSZ');
|
|
321
|
+
jexlctxt[constants.TIMESTAMP_ATTRIBUTE] = timestamp.value;
|
|
407
322
|
}
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
logger.debug(
|
|
326
|
+
context,
|
|
327
|
+
'sendUpdateValueNgsi2 called with: entityName=%s, measures=%j, typeInformation=%j, initial jexlContext=%j, timestamp=%j with value=%j',
|
|
328
|
+
entityName,
|
|
329
|
+
plainMeasures,
|
|
330
|
+
typeInformation,
|
|
331
|
+
jexlctxt,
|
|
332
|
+
mustInsertTimeInstant,
|
|
333
|
+
timestamp.value
|
|
334
|
+
);
|
|
335
|
+
|
|
336
|
+
//Now we can calculate the EntityName of primary entity
|
|
337
|
+
let entityNameCalc = null;
|
|
338
|
+
if (typeInformation.entityNameExp !== undefined && typeInformation.entityNameExp !== '') {
|
|
339
|
+
try {
|
|
340
|
+
logger.debug(context, 'sendUpdateValueNgsi2 entityNameExp %j', typeInformation.entityNameExp);
|
|
341
|
+
entityNameCalc = expressionPlugin.applyExpression(typeInformation.entityNameExp, jexlctxt, typeInformation);
|
|
342
|
+
} catch (e) {
|
|
343
|
+
logger.debug(
|
|
344
|
+
context,
|
|
345
|
+
'Error evaluating expression for entityName: %j with context: %j',
|
|
346
|
+
typeInformation.entityNameExp,
|
|
347
|
+
jexlctxt
|
|
348
|
+
);
|
|
429
349
|
}
|
|
430
|
-
}
|
|
431
|
-
let selectedAttrs = [];
|
|
432
|
-
if (typeof typeInformation.explicitAttrs === 'string') {
|
|
433
|
-
// explicitAttrs is a jexlExpression
|
|
434
|
-
// This ctxt should include all possible attrs
|
|
435
|
-
const attributesCtxt = [];
|
|
436
|
-
if (typeInformation.static) {
|
|
437
|
-
typeInformation.static.forEach(function (att) {
|
|
438
|
-
attributesCtxt.push(att);
|
|
439
|
-
});
|
|
440
|
-
}
|
|
441
|
-
// Measures
|
|
442
|
-
for (let i = 0; i < attributes.length; i++) {
|
|
443
|
-
if (attributes[i].name && attributes[i].type) {
|
|
444
|
-
const measureAttr = {
|
|
445
|
-
name: attributes[i].name,
|
|
446
|
-
value: attributes[i].value,
|
|
447
|
-
type: attributes[i].type
|
|
448
|
-
};
|
|
449
|
-
attributesCtxt.push(measureAttr);
|
|
450
|
-
// check measureAttr by object_id -> if in active
|
|
451
|
-
let j = 0;
|
|
452
|
-
let found = false;
|
|
453
|
-
while (j < typeInformation.active.length && !found) {
|
|
454
|
-
if (attributes[i].name === typeInformation.active[j].object_id) {
|
|
455
|
-
let measureAttrByObjectId = {
|
|
456
|
-
name: typeInformation.active[j].name,
|
|
457
|
-
value: attributes[i].value,
|
|
458
|
-
type: attributes[i].type
|
|
459
|
-
};
|
|
460
|
-
attributesCtxt.push(measureAttrByObjectId);
|
|
461
|
-
found = true;
|
|
462
|
-
}
|
|
463
|
-
j++;
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
// This context is just to calculate explicitAttrs when is an expression
|
|
350
|
+
}
|
|
468
351
|
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
352
|
+
entityName = entityNameCalc ? entityNameCalc : entityName;
|
|
353
|
+
//enrich JEXL context
|
|
354
|
+
jexlctxt['entity_name'] = entityName;
|
|
355
|
+
|
|
356
|
+
let preprocessedAttr = [];
|
|
357
|
+
//Add Raw Static, Lazy, Command and Actives attr attributes
|
|
358
|
+
if (typeInformation && typeInformation.staticAttributes) {
|
|
359
|
+
preprocessedAttr = preprocessedAttr.concat(typeInformation.staticAttributes);
|
|
360
|
+
}
|
|
361
|
+
if (typeInformation && typeInformation.lazy) {
|
|
362
|
+
preprocessedAttr = preprocessedAttr.concat(typeInformation.lazy);
|
|
363
|
+
}
|
|
364
|
+
if (typeInformation && typeInformation.active) {
|
|
365
|
+
preprocessedAttr = preprocessedAttr.concat(typeInformation.active);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
//Proccess every proto Attribute to populate entities data steuture
|
|
369
|
+
entities[entityName] = {};
|
|
370
|
+
entities[entityName][typeInformation.type] = [];
|
|
371
|
+
|
|
372
|
+
for (let currentAttr of preprocessedAttr) {
|
|
373
|
+
let hitted = false; //any measure, expressiom or value hit the attr (avoid propagate "silent attr" with null values )
|
|
374
|
+
let attrEntityName = entityName;
|
|
375
|
+
let attrEntityType = typeInformation.type;
|
|
376
|
+
let valueExpression = null;
|
|
377
|
+
//manage active attr without object__id (name by default)
|
|
378
|
+
currentAttr.object_id = currentAttr.object_id ? currentAttr.object_id : currentAttr.name;
|
|
379
|
+
//Enrich the attr (skip, hit, value, meta-timeInstant)
|
|
380
|
+
currentAttr.skipValue = currentAttr.skipValue ? currentAttr.skipValue : null;
|
|
381
|
+
|
|
382
|
+
//determine AttrEntityName for multientity
|
|
383
|
+
if (
|
|
384
|
+
currentAttr.entity_name !== null &&
|
|
385
|
+
currentAttr.entity_name !== undefined &&
|
|
386
|
+
currentAttr.entity_name !== '' &&
|
|
387
|
+
typeof currentAttr.entity_name == 'string'
|
|
388
|
+
) {
|
|
493
389
|
try {
|
|
494
|
-
logger.debug(context,
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
} else if (res === false) {
|
|
504
|
-
// like explicitAttrs == false
|
|
505
|
-
// selectAttrs should be measures and defined attributes
|
|
506
|
-
typeInformation.active.forEach((attr) => {
|
|
507
|
-
selectedAttrs.push(attr.name);
|
|
508
|
-
selectedAttrs.push(attr.object_id);
|
|
509
|
-
});
|
|
510
|
-
for (let i = 0; i < attributes.length; i++) {
|
|
511
|
-
selectedAttrs.push(attributes[i].name);
|
|
512
|
-
}
|
|
513
|
-
} else {
|
|
514
|
-
selectedAttrs = res; // TBD: Check ensure is an array of strings
|
|
515
|
-
}
|
|
516
|
-
if (selectedAttrs.length === 0) {
|
|
517
|
-
// implies do nothing
|
|
518
|
-
logger.info(
|
|
519
|
-
context,
|
|
520
|
-
'sendUpdateValueNgsi2 none selectedAttrs with %j and ctxt %j',
|
|
521
|
-
typeInformation.explicitAttrs,
|
|
522
|
-
ctxt
|
|
523
|
-
);
|
|
524
|
-
return callback(null);
|
|
390
|
+
logger.debug(context,
|
|
391
|
+
'Evaluating attribute: %j, for entity_name(exp):%j, with ctxt: %j',
|
|
392
|
+
currentAttr.name,
|
|
393
|
+
currentAttr.entity_name,
|
|
394
|
+
jexlctxt
|
|
395
|
+
);
|
|
396
|
+
attrEntityName = jexlParser.applyExpression(currentAttr.entity_name, jexlctxt, typeInformation);
|
|
397
|
+
if (!attrEntityName) {
|
|
398
|
+
attrEntityName = currentAttr.entity_name;
|
|
525
399
|
}
|
|
526
400
|
} catch (e) {
|
|
527
|
-
|
|
401
|
+
logger.debug(
|
|
402
|
+
context,
|
|
403
|
+
'Exception evaluating entityNameExp:%j, with jexlctxt: %j',
|
|
404
|
+
currentAttr.entity_name,
|
|
405
|
+
jexlctxt
|
|
406
|
+
);
|
|
407
|
+
attrEntityName = currentAttr.entity_name;
|
|
528
408
|
}
|
|
529
|
-
|
|
530
|
-
typeInformation.active.forEach((attr) => {
|
|
531
|
-
if (selectedAttrs.includes(attr.name)) {
|
|
532
|
-
selectedAttrs.push(attr.object_id);
|
|
533
|
-
}
|
|
534
|
-
// Check if selectedAttrs includes an attribute with format {object_id: xxxx}
|
|
535
|
-
if (selectedAttrs.includes({ object_id: attr.object_id })) {
|
|
536
|
-
selectedAttrs.push(attr.object_id);
|
|
537
|
-
}
|
|
538
|
-
});
|
|
539
|
-
} else if (typeInformation.explicitAttrs && typeof typeInformation.explicitAttrs === 'boolean') {
|
|
540
|
-
// explicitAtts is true => Add just measures which are defined in active attributes
|
|
541
|
-
// and active attributes with expressions
|
|
542
|
-
// and TimeInstant
|
|
543
|
-
selectedAttrs = ['TimeInstant'];
|
|
544
|
-
typeInformation.active.forEach((attr) => {
|
|
545
|
-
// Measures
|
|
546
|
-
if (attr.expression !== undefined) {
|
|
547
|
-
selectedAttrs.push(attr.name);
|
|
548
|
-
selectedAttrs.push(attr.object_id);
|
|
549
|
-
} else {
|
|
550
|
-
// check if active attr is receiving a measure
|
|
551
|
-
let i = 0;
|
|
552
|
-
let found = false;
|
|
553
|
-
while (i < attributes.length && !found) {
|
|
554
|
-
if (attributes[i].name && attributes[i].type) {
|
|
555
|
-
if (attributes[i].name === attr.object_id || attributes[i].name === attr.name) {
|
|
556
|
-
selectedAttrs.push(attr.name);
|
|
557
|
-
selectedAttrs.push(attr.object_id);
|
|
558
|
-
found = true;
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
i++;
|
|
562
|
-
}
|
|
563
|
-
}
|
|
564
|
-
});
|
|
565
409
|
}
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
};
|
|
576
|
-
// ensure payload has attr with proper object_id
|
|
577
|
-
if (attr && attr.object_id) {
|
|
578
|
-
payload.entities[0][attributes[i].name].object_id = attr.object_id;
|
|
579
|
-
}
|
|
580
|
-
const metadata = NGSIUtils.getMetaData(typeInformation, attributes[i].name, attributes[i].metadata);
|
|
581
|
-
if (metadata) {
|
|
582
|
-
payload.entities[0][attributes[i].name].metadata = metadata;
|
|
583
|
-
}
|
|
584
|
-
} else if (attributes[i].name && !selectedAttrs.includes(attributes[i].name) && attributes[i].type) {
|
|
585
|
-
const att = {
|
|
586
|
-
name: attributes[i].name,
|
|
587
|
-
type: attributes[i].type,
|
|
588
|
-
value: attributes[i].value
|
|
589
|
-
};
|
|
590
|
-
measureAttrsForCtxt.push(att);
|
|
591
|
-
}
|
|
410
|
+
|
|
411
|
+
//determine AttrEntityType for multientity
|
|
412
|
+
if (
|
|
413
|
+
currentAttr.entity_type !== null &&
|
|
414
|
+
currentAttr.entity_type !== undefined &&
|
|
415
|
+
currentAttr.entity_type !== '' &&
|
|
416
|
+
typeof currentAttr.entity_type === 'string'
|
|
417
|
+
) {
|
|
418
|
+
attrEntityType = currentAttr.entity_type;
|
|
592
419
|
}
|
|
593
|
-
logger.debug(
|
|
594
|
-
context,
|
|
595
|
-
'sendUpdateValueNgsi2 pre-initial explicitAttrs payload=%j selectedAttrs=%j',
|
|
596
|
-
payload,
|
|
597
|
-
selectedAttrs
|
|
598
|
-
);
|
|
599
|
-
let selectedAttrsByObjectId = selectedAttrs
|
|
600
|
-
.filter((o) => o !== undefined && o.object_id)
|
|
601
|
-
.map(function (el) {
|
|
602
|
-
return el.object_id;
|
|
603
|
-
});
|
|
604
420
|
|
|
605
|
-
//
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
};
|
|
421
|
+
//PRE POPULATE CONTEXT
|
|
422
|
+
jexlctxt[currentAttr.name] = plainMeasures[currentAttr.object_id];
|
|
423
|
+
|
|
424
|
+
//determine Value
|
|
425
|
+
if (currentAttr.value !== undefined) {
|
|
426
|
+
//static attributes already have a value
|
|
427
|
+
hitted = true;
|
|
428
|
+
valueExpression = currentAttr.value;
|
|
429
|
+
} else if (plainMeasures[currentAttr.object_id] !== undefined) {
|
|
430
|
+
//we have got a meaure for that Attr
|
|
431
|
+
//actives ¿lazis?
|
|
432
|
+
hitted = true;
|
|
433
|
+
valueExpression = plainMeasures[currentAttr.object_id];
|
|
434
|
+
}
|
|
435
|
+
//remove measures that has been shadowed by an alias (some may be left and managed later)
|
|
436
|
+
//Maybe we must filter object_id if there is name == object_id
|
|
437
|
+
measures = measures.filter((item) => item.name !== currentAttr.object_id && item.name !== currentAttr.name);
|
|
438
|
+
|
|
439
|
+
if (
|
|
440
|
+
currentAttr.expression !== undefined &&
|
|
441
|
+
currentAttr.expression !== '' &&
|
|
442
|
+
typeof currentAttr.expression == 'string'
|
|
443
|
+
) {
|
|
444
|
+
try {
|
|
445
|
+
hitted = true;
|
|
446
|
+
valueExpression = jexlParser.applyExpression(currentAttr.expression, jexlctxt, typeInformation);
|
|
447
|
+
//we fallback to null if anything unexpecte happend
|
|
448
|
+
if (valueExpression === null || valueExpression === undefined || Number.isNaN(valueExpression)) {
|
|
449
|
+
valueExpression = null;
|
|
635
450
|
}
|
|
636
|
-
})
|
|
451
|
+
} catch (e) {
|
|
452
|
+
valueExpression = null;
|
|
453
|
+
}
|
|
454
|
+
logger.debug(context,
|
|
455
|
+
'Evaluated attr: %j, with expression: %j, and ctxt: %j resulting: %j',
|
|
456
|
+
currentAttr.name,
|
|
457
|
+
currentAttr.expression,
|
|
458
|
+
jexlctxt,
|
|
459
|
+
valueExpression
|
|
460
|
+
);
|
|
637
461
|
}
|
|
638
|
-
} // END check explicitAttrs
|
|
639
|
-
logger.debug(context, 'sendUpdateValueNgsi2 initial payload=%j', payload);
|
|
640
|
-
|
|
641
|
-
const currentEntity = payload.entities[0];
|
|
642
462
|
|
|
643
|
-
|
|
644
|
-
|
|
463
|
+
currentAttr.hitted = hitted;
|
|
464
|
+
currentAttr.value = valueExpression;
|
|
645
465
|
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
let attributesCtxt = [...attsArrayFiltered]; // just copy
|
|
654
|
-
if (typeInformation.static) {
|
|
655
|
-
typeInformation.static.forEach(function (att) {
|
|
656
|
-
attributesCtxt.push(att);
|
|
657
|
-
});
|
|
658
|
-
}
|
|
659
|
-
if (measureAttrsForCtxt) {
|
|
660
|
-
measureAttrsForCtxt.forEach(function (att) {
|
|
661
|
-
attributesCtxt.push(att);
|
|
662
|
-
});
|
|
663
|
-
}
|
|
664
|
-
attributesCtxt = attributesCtxt.concat(idTypeSSSList);
|
|
665
|
-
let ctxt = expressionPlugin.extractContext(attributesCtxt, typeInformation);
|
|
666
|
-
logger.debug(context, 'sendUpdateValueNgsi2 initial ctxt %j ', ctxt);
|
|
466
|
+
//add TimeInstant to attr metadata
|
|
467
|
+
if (mustInsertTimeInstant) {
|
|
468
|
+
if (!currentAttr.metadata) {
|
|
469
|
+
currentAttr.metadata = {};
|
|
470
|
+
}
|
|
471
|
+
currentAttr.metadata[constants.TIMESTAMP_ATTRIBUTE] = timestamp;
|
|
472
|
+
}
|
|
667
473
|
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
const m = currentEntity[k];
|
|
679
|
-
delete currentEntity[k];
|
|
680
|
-
currentEntity[k] = m; // put into the end of currentEntity
|
|
681
|
-
}
|
|
682
|
-
});
|
|
474
|
+
//store de New Attributte in entity data structure
|
|
475
|
+
if (hitted === true) {
|
|
476
|
+
if (entities[attrEntityName] === undefined) {
|
|
477
|
+
entities[attrEntityName] = {};
|
|
478
|
+
}
|
|
479
|
+
if (entities[attrEntityName][attrEntityType] === undefined) {
|
|
480
|
+
entities[attrEntityName][attrEntityType] = [];
|
|
481
|
+
}
|
|
482
|
+
//store de New Attributte
|
|
483
|
+
entities[attrEntityName][attrEntityType].push(currentAttr);
|
|
683
484
|
}
|
|
485
|
+
|
|
486
|
+
//RE-Populate de JEXLcontext (except for null or NaN we preffer undefined)
|
|
487
|
+
jexlctxt[currentAttr.name] = valueExpression;
|
|
684
488
|
}
|
|
685
489
|
|
|
686
|
-
//
|
|
687
|
-
|
|
490
|
+
//now we can compute explicit (Bool or Array) with the complete JexlContext
|
|
491
|
+
let explicit = false;
|
|
492
|
+
if (typeof typeInformation.explicitAttrs === 'string') {
|
|
688
493
|
try {
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
payload.entities[0].id = entityName;
|
|
694
|
-
ctxt['entity_name'] = entityName;
|
|
695
|
-
} catch (e) {
|
|
494
|
+
explicit = jexlParser.applyExpression(typeInformation.explicitAttrs, jexlctxt, typeInformation);
|
|
495
|
+
if (explicit instanceof Array && mustInsertTimeInstant) {
|
|
496
|
+
explicit.push(constants.TIMESTAMP_ATTRIBUTE);
|
|
497
|
+
}
|
|
696
498
|
logger.debug(
|
|
697
499
|
context,
|
|
698
|
-
'
|
|
699
|
-
typeInformation.
|
|
700
|
-
|
|
500
|
+
'Calculated explicitAttrs with expression: %j and ctxt: %j resulting: %j',
|
|
501
|
+
typeInformation.explicitAttrs,
|
|
502
|
+
jexlctxt,
|
|
503
|
+
explicit
|
|
701
504
|
);
|
|
505
|
+
} catch (e) {
|
|
506
|
+
// nothing to do: exception is already logged at info level
|
|
702
507
|
}
|
|
508
|
+
} else if (typeof typeInformation.explicitAttrs == 'boolean') {
|
|
509
|
+
explicit = typeInformation.explicitAttrs;
|
|
703
510
|
}
|
|
704
511
|
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
let attr;
|
|
713
|
-
let newAttr = payload.entities[0][j];
|
|
714
|
-
if (typeInformation.active) {
|
|
715
|
-
attr = typeInformation.active.find((obj) => {
|
|
716
|
-
return obj.object_id === j;
|
|
717
|
-
});
|
|
718
|
-
}
|
|
719
|
-
if (!attr) {
|
|
720
|
-
if (typeInformation.lazy) {
|
|
721
|
-
attr = typeInformation.lazy.find((obj) => {
|
|
722
|
-
return obj.object_id === j;
|
|
723
|
-
});
|
|
512
|
+
//more mesures may be added to the attribute list (unnhandled/left mesaures) l
|
|
513
|
+
if (explicit === false && Object.keys(measures).length > 0) {
|
|
514
|
+
//add Timestamp to measures if needed
|
|
515
|
+
if (mustInsertTimeInstant) {
|
|
516
|
+
for (let currentMeasure of measures) {
|
|
517
|
+
if (!currentMeasure.metadata) {
|
|
518
|
+
currentMeasure.metadata = {};
|
|
724
519
|
}
|
|
520
|
+
currentMeasure.metadata[constants.TIMESTAMP_ATTRIBUTE] = timestamp;
|
|
725
521
|
}
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
});
|
|
731
|
-
}
|
|
732
|
-
}
|
|
733
|
-
if (attr && attr.name) {
|
|
734
|
-
if (['id', 'type'].includes(attr.name)) {
|
|
735
|
-
// invalid mapping
|
|
736
|
-
logger.debug(
|
|
737
|
-
context,
|
|
738
|
-
'sendUpdateValueNgsi2 invalid mapping for attr=%j newAttr=%j',
|
|
739
|
-
attr,
|
|
740
|
-
newAttr
|
|
741
|
-
);
|
|
742
|
-
if (!['id', 'type'].includes(attr.object_id)) {
|
|
743
|
-
delete payload.entities[0][attr.object_id];
|
|
744
|
-
}
|
|
745
|
-
attr = undefined; // stop processing attr
|
|
746
|
-
newAttr = undefined;
|
|
747
|
-
} else {
|
|
748
|
-
ctxt[attr.name] = payload.entities[0][j].value;
|
|
749
|
-
}
|
|
750
|
-
}
|
|
751
|
-
logger.debug(
|
|
752
|
-
context,
|
|
753
|
-
'sendUpdateValueNgsi2 procesing j=%j attr=%j ctxt=%j newAttr=%j ',
|
|
754
|
-
j,
|
|
755
|
-
attr,
|
|
756
|
-
ctxt,
|
|
757
|
-
newAttr
|
|
758
|
-
);
|
|
759
|
-
if (attr && attr.type) {
|
|
760
|
-
newAttr.type = attr.type;
|
|
761
|
-
}
|
|
522
|
+
//If just measures in the principal entity we missed the Timestamp.
|
|
523
|
+
}
|
|
524
|
+
entities[entityName][typeInformation.type] = entities[entityName][typeInformation.type].concat(measures);
|
|
525
|
+
}
|
|
762
526
|
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
logger.debug(
|
|
766
|
-
context,
|
|
767
|
-
'sendUpdateValueNgsi2 apply expression=%j over ctxt=%j and device=%j',
|
|
768
|
-
attr.expression,
|
|
769
|
-
ctxt,
|
|
770
|
-
typeInformation
|
|
771
|
-
);
|
|
772
|
-
let res = null;
|
|
773
|
-
try {
|
|
774
|
-
if (expressionPlugin.contextAvailable(attr.expression, ctxt, typeInformation)) {
|
|
775
|
-
res = expressionPlugin.applyExpression(attr.expression, ctxt, typeInformation);
|
|
776
|
-
if (
|
|
777
|
-
// By default undefined is handled like null: should not progress
|
|
778
|
-
// Some op results (like nonexistent * 2) are a kind of null with a number type
|
|
779
|
-
// but NaN value
|
|
780
|
-
(attr.skipValue === undefined &&
|
|
781
|
-
(res === null || (typeof res === 'number' && isNaN(res)))) ||
|
|
782
|
-
(attr.skipValue !== undefined &&
|
|
783
|
-
(res === attr.skipValue ||
|
|
784
|
-
(typeof res === 'number' && isNaN(res) && attr.skipValue === null)))
|
|
785
|
-
) {
|
|
786
|
-
logger.debug(
|
|
787
|
-
context,
|
|
788
|
-
'sendUpdateValueNgsi2 skip value=%j for res=%j with expression=%j',
|
|
789
|
-
attr.skipValue,
|
|
790
|
-
res,
|
|
791
|
-
attr.expression
|
|
792
|
-
);
|
|
793
|
-
delete payload.entities[0][j]; // remove measure attr
|
|
794
|
-
attr = undefined; // stop process attr
|
|
795
|
-
}
|
|
796
|
-
} else {
|
|
797
|
-
logger.info(
|
|
798
|
-
context,
|
|
799
|
-
'sendUpdateValueNgsi2 no context available for apply expression=%j',
|
|
800
|
-
attr.expression
|
|
801
|
-
);
|
|
802
|
-
res = newAttr.value; // keep newAttr value
|
|
803
|
-
}
|
|
804
|
-
} catch (e) {
|
|
805
|
-
logger.error(context, 'sendUpdateValueNgsi2 apply expression exception=%j', e);
|
|
806
|
-
if (attr && attr.name) {
|
|
807
|
-
res = ctxt[attr.name];
|
|
808
|
-
}
|
|
809
|
-
}
|
|
810
|
-
// jexl expression plugin
|
|
811
|
-
newAttr.value = res;
|
|
527
|
+
//PRE-PROCESSING FINISHED
|
|
528
|
+
//Explicit ATTRS and SKIPVALUES will be managed while we build NGSI payload
|
|
812
529
|
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
if (attr && attr.name && ctxt[attr.name] !== undefined) {
|
|
816
|
-
ctxt[attr.name] = newAttr.value;
|
|
817
|
-
}
|
|
818
|
-
}
|
|
530
|
+
//Get ready to build and send NGSI payload (entities-->payload)
|
|
531
|
+
payload.actionType = 'append';
|
|
819
532
|
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
context,
|
|
831
|
-
'sendUpdateValueNgsi2 MULTI no context available for apply expression=%j',
|
|
832
|
-
attr.entity_name
|
|
833
|
-
);
|
|
834
|
-
newEntityName = attr.entity_name;
|
|
835
|
-
}
|
|
836
|
-
newEntityName = newEntityName ? newEntityName : attr.entity_name;
|
|
837
|
-
} catch (e) {
|
|
838
|
-
logger.error(context, 'sendUpdateValueNgsi2 MULTI apply expression exception=%j', e);
|
|
839
|
-
newEntityName = attr.entity_name;
|
|
840
|
-
}
|
|
841
|
-
logger.debug(
|
|
842
|
-
context,
|
|
843
|
-
'sendUpdateValueNgsi2 MULTI apply expression=%j result=%j payload=%j',
|
|
844
|
-
attr.entity_name,
|
|
845
|
-
newEntityName,
|
|
846
|
-
payload
|
|
847
|
-
);
|
|
848
|
-
}
|
|
849
|
-
// CB entity id and type should be always a String
|
|
850
|
-
let newEntity = {
|
|
851
|
-
id: newEntityName ? String(newEntityName) : String(payload.entities[0].id),
|
|
852
|
-
type: attr.entity_type ? String(attr.entity_type) : String(payload.entities[0].type)
|
|
853
|
-
};
|
|
854
|
-
// Check if there is already a newEntity created
|
|
855
|
-
const alreadyEntity = payload.entities.find((entity) => {
|
|
856
|
-
return entity.id === newEntity.id && entity.type === newEntity.type;
|
|
857
|
-
});
|
|
858
|
-
if (alreadyEntity) {
|
|
859
|
-
// Use alreadyEntity
|
|
860
|
-
alreadyEntity[attr.name] = newAttr;
|
|
861
|
-
} else {
|
|
862
|
-
// Add newEntity to payload.entities
|
|
863
|
-
newEntity[attr.name] = newAttr;
|
|
864
|
-
if (
|
|
865
|
-
'timestamp' in typeInformation && typeInformation.timestamp !== undefined
|
|
866
|
-
? typeInformation.timestamp
|
|
867
|
-
: config.getConfig().timestamp !== undefined
|
|
868
|
-
? config.getConfig().timestamp
|
|
869
|
-
: timestampValue !== undefined
|
|
870
|
-
) {
|
|
871
|
-
newEntity = addTimestampNgsi2(newEntity, typeInformation.timezone, timestampValue);
|
|
872
|
-
logger.debug(context, 'sendUpdateValueNgsi2 timestamped newEntity=%j', newEntity);
|
|
873
|
-
}
|
|
874
|
-
payload.entities.push(newEntity);
|
|
875
|
-
}
|
|
876
|
-
if (attr && attr.name) {
|
|
877
|
-
if (attr.name !== j) {
|
|
878
|
-
logger.debug(
|
|
879
|
-
context,
|
|
880
|
-
'sendUpdateValueNgsi2 MULTI remove measure attr=%j keep alias j=%j from %j',
|
|
881
|
-
j,
|
|
882
|
-
attr,
|
|
883
|
-
payload
|
|
884
|
-
);
|
|
885
|
-
delete payload.entities[0][j];
|
|
886
|
-
}
|
|
887
|
-
}
|
|
888
|
-
// if (attr && (attr.entity_type || attr.entity_name))
|
|
889
|
-
} else {
|
|
890
|
-
// Not a multientity attr
|
|
891
|
-
if (attr && attr.name) {
|
|
892
|
-
payload.entities[0][attr.name] = newAttr;
|
|
893
|
-
if (attr.name !== j) {
|
|
894
|
-
delete payload.entities[0][j]; // keep alias name, remove measure name
|
|
895
|
-
}
|
|
896
|
-
}
|
|
897
|
-
if (newAttr && newAttr.type === constants.TIMESTAMP_TYPE_NGSI2 && newAttr.value) {
|
|
898
|
-
const extendedTime = compressTimestampPlugin.fromBasicToExtended(newAttr.value);
|
|
899
|
-
if (extendedTime) {
|
|
900
|
-
// TBD: there is not flag about compressTimestamp in iotagent-node-lib,
|
|
901
|
-
// but there is one in agents
|
|
902
|
-
newAttr.value = extendedTime;
|
|
903
|
-
}
|
|
904
|
-
}
|
|
905
|
-
if (j === constants.TIMESTAMP_ATTRIBUTE) {
|
|
906
|
-
if (newAttr && newAttr.type === constants.TIMESTAMP_TYPE_NGSI2 && newAttr.value) {
|
|
907
|
-
timestampValue = newAttr.value;
|
|
908
|
-
logger.debug(
|
|
909
|
-
context,
|
|
910
|
-
'sendUpdateValueNgsi2 newAttr is TimeInstant and new payload=%j',
|
|
911
|
-
payload
|
|
912
|
-
);
|
|
913
|
-
}
|
|
914
|
-
}
|
|
533
|
+
payload.entities = [];
|
|
534
|
+
for (let ename in entities) {
|
|
535
|
+
for (let etype in entities[ename]) {
|
|
536
|
+
let e = {};
|
|
537
|
+
e.id = String(ename);
|
|
538
|
+
e.type = String(etype);
|
|
539
|
+
//extract attributes
|
|
540
|
+
let isEmpty = true;
|
|
541
|
+
for (let attr of entities[ename][etype]) {
|
|
542
|
+
//Handling id/type measures, skip, hit & explicit (condition)
|
|
915
543
|
if (
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
544
|
+
attr.name !== 'id' &&
|
|
545
|
+
attr.name !== 'type' &&
|
|
546
|
+
(attr.value !== attr.skipValue || attr.skipValue === undefined) &&
|
|
547
|
+
(attr.hitted || attr.hitted === undefined) && //undefined is for pure measures
|
|
548
|
+
(typeof explicit === 'boolean' || //true and false already handled
|
|
549
|
+
(explicit instanceof Array && //check the array version
|
|
550
|
+
(explicit.includes(attr.name) ||
|
|
551
|
+
explicit.some(
|
|
552
|
+
(item) => attr.object_id !== undefined && item.object_id === attr.object_id
|
|
553
|
+
))))
|
|
921
554
|
) {
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
);
|
|
925
|
-
if (extendedTime) {
|
|
926
|
-
newAttr.metadata[constants.TIMESTAMP_ATTRIBUTE].value = extendedTime;
|
|
927
|
-
}
|
|
928
|
-
}
|
|
929
|
-
}
|
|
930
|
-
} // if (j !== 'id' || j !== 'type')
|
|
931
|
-
|
|
932
|
-
// final attr loop
|
|
933
|
-
logger.debug(
|
|
934
|
-
context,
|
|
935
|
-
'sendUpdateValueNgsi2 after procesing attr=%j current entity=%j current payload=%j',
|
|
936
|
-
j,
|
|
937
|
-
currentEntity,
|
|
938
|
-
payload
|
|
939
|
-
);
|
|
940
|
-
}
|
|
941
|
-
// for attr loop
|
|
942
|
-
|
|
943
|
-
// Add timestamp to paylaod
|
|
944
|
-
if (
|
|
945
|
-
'timestamp' in typeInformation && typeInformation.timestamp !== undefined
|
|
946
|
-
? typeInformation.timestamp
|
|
947
|
-
: config.getConfig().timestamp !== undefined
|
|
948
|
-
? config.getConfig().timestamp
|
|
949
|
-
: timestampValue !== undefined
|
|
950
|
-
) {
|
|
951
|
-
if (timestampValue) {
|
|
952
|
-
// timeInstant is provided as measure
|
|
953
|
-
if (payload.entities.length > 0) {
|
|
954
|
-
for (let n = 0; n < payload.entities.length; n++) {
|
|
955
|
-
// include metadata with TimeInstant in attrs when TimeInstant is provided as measure in all entities
|
|
956
|
-
payload.entities[n] = addTimestampNgsi2(
|
|
957
|
-
payload.entities[n],
|
|
958
|
-
typeInformation.timezone,
|
|
959
|
-
timestampValue
|
|
960
|
-
);
|
|
555
|
+
isEmpty = false;
|
|
556
|
+
e[attr.name] = { type: attr.type, value: attr.value, metadata: attr.metadata };
|
|
961
557
|
}
|
|
962
558
|
}
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
if (!utils.isTimestampedNgsi2(payload.entities[n])) {
|
|
967
|
-
// legacy check needed?
|
|
968
|
-
payload.entities[n] = addTimestampNgsi2(payload.entities[n], typeInformation.timezone);
|
|
969
|
-
// jshint maxdepth:5
|
|
970
|
-
} else if (!utils.IsValidTimestampedNgsi2(payload.entities[n])) {
|
|
971
|
-
// legacy check needed?
|
|
972
|
-
logger.error(context, 'Invalid timestamp:%s', JSON.stringify(payload.entities[0]));
|
|
973
|
-
callback(new errors.BadTimestamp(payload.entities));
|
|
974
|
-
return;
|
|
559
|
+
if (!isEmpty) {
|
|
560
|
+
if (mustInsertTimeInstant) {
|
|
561
|
+
e[constants.TIMESTAMP_ATTRIBUTE] = timestamp;
|
|
975
562
|
}
|
|
563
|
+
payload.entities.push(e);
|
|
976
564
|
}
|
|
977
565
|
}
|
|
978
566
|
}
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
for (const key in payload.entities[m]) {
|
|
983
|
-
// purge object_id from payload
|
|
984
|
-
if (payload.entities[m][key] && payload.entities[m][key].object_id) {
|
|
985
|
-
delete payload.entities[m][key].object_id;
|
|
986
|
-
}
|
|
987
|
-
}
|
|
988
|
-
payload.entities[m] = NGSIUtils.castJsonNativeAttributes(payload.entities[m]); // native types
|
|
989
|
-
}
|
|
990
|
-
logger.debug(context, 'sendUpdateValueNgsi2 payload with native types and without object_id=%j', payload);
|
|
991
|
-
|
|
567
|
+
|
|
568
|
+
let url = '/v2/op/update';
|
|
569
|
+
let options = NGSIUtils.createRequestObject(url, typeInformation, token);
|
|
992
570
|
options.json = payload;
|
|
993
571
|
|
|
994
|
-
// Prevent to update an entity with an empty payload
|
|
572
|
+
// Prevent to update an entity with an empty payload: more than id and type
|
|
995
573
|
if (
|
|
996
574
|
Object.keys(options.json).length > 0 &&
|
|
997
575
|
(options.json.entities.length > 1 ||
|
|
998
|
-
(options.json.entities.length === 1 && Object.keys(options.json.entities[0]).length > 2))
|
|
576
|
+
(options.json.entities.length === 1 && Object.keys(options.json.entities[0]).length > 2))
|
|
999
577
|
) {
|
|
1000
578
|
// Final check: (to keep tests unchanged) before do CB requests
|
|
1001
579
|
// one entity -> request /v2/entities/ + entityName + /atts ?type=typeInformation.type
|
|
1002
580
|
// multi entities -> request /v2/op/update
|
|
1003
581
|
// Note that the options object is prepared for the second case (multi entity), so we "patch" it
|
|
1004
582
|
// only in the first case
|
|
1005
|
-
|
|
583
|
+
|
|
584
|
+
//Multientity more than one name o more than one type at primary entity
|
|
585
|
+
let multientity = Object.keys(entities).length > 1 || Object.keys(entities[entityName]).length > 1;
|
|
586
|
+
|
|
587
|
+
if (!multientity) {
|
|
1006
588
|
// recreate options object to use single entity update
|
|
1007
589
|
url = '/v2/entities?options=upsert';
|
|
1008
590
|
options = NGSIUtils.createRequestObject(url, typeInformation, token);
|
|
1009
|
-
|
|
591
|
+
delete payload.actionType;
|
|
592
|
+
|
|
593
|
+
let entityAttrs = payload.entities[0];
|
|
594
|
+
const transformedObject = {};
|
|
595
|
+
for (let attrname in entityAttrs) {
|
|
596
|
+
let attr = entityAttrs[attrname];
|
|
597
|
+
transformedObject[attrname] = {
|
|
598
|
+
type: attr.type,
|
|
599
|
+
value: attr.value,
|
|
600
|
+
metadata: attr.metadata
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
transformedObject.id = entityAttrs.id;
|
|
604
|
+
transformedObject.type = entityAttrs.type;
|
|
605
|
+
options.json = transformedObject;
|
|
1010
606
|
options.method = 'POST';
|
|
1011
607
|
} // else: keep current options object created for a batch update
|
|
1012
|
-
|
|
1013
|
-
|
|
608
|
+
|
|
609
|
+
//Send the NGSI request
|
|
610
|
+
logger.debug(context,
|
|
611
|
+
'Updating device value in the Context Broker at: %j',
|
|
612
|
+
options.url
|
|
613
|
+
);
|
|
614
|
+
logger.debug(context,
|
|
615
|
+
'Using the following NGSI v2 request: %j',
|
|
616
|
+
options
|
|
617
|
+
);
|
|
618
|
+
|
|
1014
619
|
request(
|
|
1015
620
|
options,
|
|
1016
621
|
generateNGSI2OperationHandler('update', entityName, typeInformation, token, options, callback)
|
|
@@ -1018,19 +623,19 @@ function sendUpdateValueNgsi2(entityName, attributes, typeInformation, token, ca
|
|
|
1018
623
|
} else {
|
|
1019
624
|
logger.debug(
|
|
1020
625
|
context,
|
|
1021
|
-
'Not updating device value in the Context Broker at
|
|
626
|
+
'Not updating device value in the Context Broker at: %j, due to empty payload: %j',
|
|
1022
627
|
options.url,
|
|
1023
|
-
|
|
628
|
+
options
|
|
1024
629
|
);
|
|
1025
630
|
callback(null);
|
|
1026
631
|
}
|
|
1027
632
|
}
|
|
1028
633
|
|
|
1029
634
|
exports.sendQueryValue = sendQueryValueNgsi2;
|
|
1030
|
-
exports.sendUpdateValue = function (entityName,
|
|
1031
|
-
NGSIUtils.applyMiddlewares(NGSIUtils.updateMiddleware,
|
|
1032
|
-
return sendUpdateValueNgsi2(entityName,
|
|
635
|
+
exports.sendUpdateValue = function (entityName, measures, typeInformation, token, callback) {
|
|
636
|
+
NGSIUtils.applyMiddlewares(NGSIUtils.updateMiddleware, measures, typeInformation, () => {
|
|
637
|
+
return sendUpdateValueNgsi2(entityName, measures, typeInformation, token, callback);
|
|
1033
638
|
});
|
|
1034
639
|
};
|
|
1035
|
-
|
|
640
|
+
|
|
1036
641
|
exports.formatGeoAttrs = formatGeoAttrs;
|