-
Notifications
You must be signed in to change notification settings - Fork 88
/
entities-NGSI-v2.js
1034 lines (975 loc) · 44.5 KB
/
entities-NGSI-v2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2020 Telefonica Investigación y Desarrollo, S.A.U
*
* This file is part of fiware-iotagent-lib
*
* fiware-iotagent-lib is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* fiware-iotagent-lib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with fiware-iotagent-lib.
* If not, see http://www.gnu.org/licenses/.
*
* For those usages not covered by the GNU Affero General Public License
* please contact with::daniel.moranjimenez@telefonica.com
*
* Modified by: Federico M. Facca - Martel Innovate
* Modified by: Daniel Calvo - ATOS Research & Innovation
* Modified by: Jason Fox - FIWARE Foundation
*/
/* eslint-disable consistent-return */
const request = require('../../request-shim');
const alarms = require('../common/alarmManagement');
const errors = require('../../errors');
const utils = require('../northBound/restUtils');
const pluginUtils = require('../../plugins/pluginUtils');
const config = require('../../commonConfig');
const constants = require('../../constants');
const jexlParser = require('../../plugins/jexlParser');
const expressionPlugin = require('../../plugins/expressionPlugin');
const compressTimestampPlugin = require('../../plugins/compressTimestamp');
const moment = require('moment-timezone');
const NGSIUtils = require('./ngsiUtils');
const logger = require('logops');
const context = {
op: 'IoTAgentNGSI.Entities-v2'
};
/**
* Amends an NGSIv2 Geoattribute from String to GeoJSON format
*
* @param {Object} attr Attribute to be analyzed
* @return {Object} GeoJSON version of the attribute
*/
function formatGeoAttrs(attr) {
const obj = attr;
if (attr.type) {
switch (attr.type.toLowerCase()) {
// GeoProperties
case 'geo:json':
// FIXME: #1012
// case 'geoproperty':
// case 'point':
// case 'geo:point':
obj.type = 'geo:json';
obj.value = NGSIUtils.getLngLats('Point', attr.value);
break;
// FIXME: #1012
// case 'linestring':
// case 'geo:linestring':
// obj.type = 'geo:json';
// obj.value = NGSIUtils.getLngLats('LineString', attr.value);
// break;
// case 'polygon':
// case 'geo:polygon':
// obj.type = 'geo:json';
// obj.value = NGSIUtils.getLngLats('Polygon', attr.value);
// break;
// case 'multipoint':
// case 'geo:multipoint':
// obj.type = 'geo:json';
// obj.value = NGSIUtils.getLngLats('MultiPoint', attr.value);
// break;
// case 'multilinestring':
// case 'geo:multilinestring':
// obj.type = 'geo:json';
// obj.value = NGSIUtils.getLngLats('MultiLineString', attr.value);
// break;
// case 'multipolygon':
// case 'geo:multipolygon':
// obj.type = 'geo:json';
// obj.value = NGSIUtils.getLngLats('MultiPolygon', attr.value);
// break;
}
}
return obj;
}
/**
* Adds timestamp to ngsiv2 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 addTimestampNgsi2(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);
}
/**
* Generate an operation handler for NGSIv2-based operations (query and update). The handler takes care of identifiying
* the errors and calling the appropriate callback with a success or a failure depending on how the operation ended.
*
* Most of the parameters are passed for debugging purposes mainly.
*
* @param {String} operationName Name of the NGSI operation being performed.
* @param {String} entityName Name of the entity that was the target of the operation.
* @param {Object} typeInformation Information about the device the entity represents.
* @param {String} token Security token used to access the entity.
* @param {Object} options Object holding all the information about the HTTP request.
* @return {Function} The generated handler.
*/
function generateNGSI2OperationHandler(operationName, entityName, typeInformation, token, options, callback) {
return function (error, response, body) {
if (error) {
logger.error(context, 'Error found executing ' + operationName + ' action in Context Broker: %s', error);
alarms.raise(constants.ORION_ALARM, error);
callback(error);
} else if (body && body.orionError) {
logger.debug(
context,
'Orion error found executing ' + operationName + ' action in Context Broker: %j',
body.orionError
);
callback(new errors.BadRequest(body.orionError.details));
} else if (response && operationName === 'update' && response.statusCode === 204) {
logger.info(context, 'Received the following response from the CB: Value updated successfully\n');
alarms.release(constants.ORION_ALARM);
callback(null, body);
} else if (response && operationName === 'query' && body !== undefined && response.statusCode === 200) {
logger.debug(
context,
'Received the following response from the CB:\n\n%s\n\n',
JSON.stringify(body, null, 4)
);
logger.debug(context, 'Value queried successfully');
alarms.release(constants.ORION_ALARM);
callback(null, body);
} else if (response && operationName === 'query' && response.statusCode === 204) {
logger.info(
context,
'Received the following response from the CB:\n\n%s\n\n',
JSON.stringify(body, null, 4)
);
logger.error(
context,
'Operation ' +
operationName +
' bad status code from the CB: 204.' +
'A query operation must always return a body'
);
callback(new errors.BadAnswer(response.statusCode, operationName));
} else if (response && (response.statusCode === 403 || response.statusCode === 401)) {
logger.debug(context, 'Access forbidden executing ' + operationName + ' operation');
callback(
new errors.AccessForbidden(
token,
options.headers['fiware-service'],
options.headers['fiware-servicepath']
)
);
} else if (response && body && response.statusCode === 404) {
logger.info(
context,
'Received the following response from the CB:\n\n%s\n\n',
JSON.stringify(body, null, 4)
);
logger.error(context, 'Operation ' + operationName + ' error connecting to the Context Broker: %j', body);
let errorField = body.error;
if (body.description) {
errorField += ':' + body.description;
}
if (errorField !== undefined) {
callback(new errors.DeviceNotFound(entityName));
} else {
callback(new errors.EntityGenericError(entityName, typeInformation.type, body));
}
} else {
logger.debug(context, 'Unknown error executing ' + operationName + ' operation');
if (!(body instanceof Array || body instanceof Object)) {
body = JSON.parse(body);
}
callback(new errors.EntityGenericError(entityName, typeInformation.type, body, response.statusCode));
}
};
}
/**
* Makes a query to the Device's entity in the context broker using NGSIv2, with the list
* of attributes given by the 'attributes' array.
*
* @param {String} entityName Name of the entity to query.
* @param {Array} attributes Attribute array containing the names of the attributes to query.
* @param {Object} typeInformation Configuration information for the device.
* @param {String} token User token to identify against the PEP Proxies (optional).
*/
function sendQueryValueNgsi2(entityName, attributes, typeInformation, token, callback) {
let url = '/v2/entities/' + entityName + '/attrs';
if (attributes && attributes.length > 0) {
let attributesQueryParam = '';
for (let i = 0; i < attributes.length; i++) {
attributesQueryParam = attributesQueryParam + attributes[i];
if (i < attributes.length - 1) {
attributesQueryParam = attributesQueryParam + ',';
}
}
url = url + '?attrs=' + attributesQueryParam;
}
if (typeInformation.type) {
if (attributes && attributes.length > 0) {
url += '&type=' + typeInformation.type;
} else {
url += '?type=' + typeInformation.type;
}
}
const options = NGSIUtils.createRequestObject(url, typeInformation, token);
options.method = 'GET';
if (!typeInformation || !typeInformation.type) {
callback(new errors.TypeNotFound(null, entityName));
return;
}
logger.debug(context, 'Querying values of the device in the Context Broker at [%s]', options.url);
logger.debug(context, 'Using the following request:\n\n%s\n\n', JSON.stringify(options, null, 4));
request(
options,
generateNGSI2OperationHandler('query', entityName, typeInformation, token, options, function (error, result) {
if (error) {
callback(error);
} else {
NGSIUtils.applyMiddlewares(NGSIUtils.queryMiddleware, result, typeInformation, callback);
}
})
);
}
/**
* Makes an update in the Device's entity in the context broker, with the values given in the 'attributes' array. This
* array should comply to the NGSIv2's attribute format.
*
* @param {String} entityName Name of the entity to register.
* @param {Array} attributes Attribute array containing the values to update.
* @param {Object} typeInformation Configuration information for the device.
* @param {String} token User token to identify against the PEP Proxies (optional).
*/
function sendUpdateValueNgsi2(entityName, attributes, typeInformation, token, callback) {
logger.debug(
context,
'sendUpdateValueNgsi2 called with: entityName=%s attributes=%j typeInformation=%j',
entityName,
attributes,
typeInformation
);
const payload = {
entities: [
{
id: entityName
}
]
};
let url = '/v2/op/update';
if (typeInformation && typeInformation.type) {
payload.entities[0].type = typeInformation.type;
}
if (config.getConfig().appendMode === false) {
payload.actionType = 'update';
} else {
payload.actionType = 'append';
}
let options = NGSIUtils.createRequestObject(url, typeInformation, token);
if (typeInformation && typeInformation.staticAttributes) {
attributes = attributes.concat(typeInformation.staticAttributes);
}
if (!typeInformation || !typeInformation.type) {
callback(new errors.TypeNotFound(null, entityName));
return;
}
let idTypeSSSList = pluginUtils.getIdTypeServSubServiceFromDevice(typeInformation);
logger.debug(context, 'sendUpdateValueNgsi2 idTypeSSS are %j ', idTypeSSSList);
let measureAttrsForCtxt = [];
// Check explicitAttrs: adds all final needed attributes to payload
if (
typeInformation.explicitAttrs === undefined ||
(typeof typeInformation.explicitAttrs === 'boolean' && !typeInformation.explicitAttrs)
// explicitAttrs is not defined => default case: all attrs should be included
) {
// This loop adds all measure values (attributes) into payload entities (entity[0])
for (let i = 0; i < attributes.length; i++) {
if (attributes[i].name && attributes[i].type) {
payload.entities[0][attributes[i].name] = {
value: attributes[i].value,
type: attributes[i].type
};
const metadata = NGSIUtils.getMetaData(typeInformation, attributes[i].name, attributes[i].metadata);
if (metadata) {
payload.entities[0][attributes[i].name].metadata = metadata;
}
} else {
callback(new errors.BadRequest(null, entityName));
return;
}
}
logger.debug(context, 'sendUpdateValueNgsi2 pre-initial non-explicitAttrs payload=%j', payload);
// Loop for add attrs from type.information.active (and lazys?) into payload entities (entity[0])
if (typeInformation.active) {
typeInformation.active.forEach((attr) => {
if (attr.expression) {
if (attr.object_id) {
payload.entities[0][attr.object_id] = {
value: payload.entities[0][attr.object_id]
? payload.entities[0][attr.object_id].value
: undefined,
type: attr.type,
object_id: attr.object_id
};
} else {
payload.entities[0][attr.name] = {
value: payload.entities[0][attr.name] ? payload.entities[0][attr.name].value : undefined,
type: attr.type
};
}
}
});
}
} else {
let selectedAttrs = [];
if (typeof typeInformation.explicitAttrs === 'string') {
// explicitAttrs is a jexlExpression
// This ctxt should include all possible attrs
const attributesCtxt = [];
if (typeInformation.static) {
typeInformation.static.forEach(function (att) {
attributesCtxt.push(att);
});
}
// Measures
for (let i = 0; i < attributes.length; i++) {
if (attributes[i].name && attributes[i].type) {
const measureAttr = {
name: attributes[i].name,
value: attributes[i].value,
type: attributes[i].type
};
attributesCtxt.push(measureAttr);
// check measureAttr by object_id -> if in active
let j = 0;
let found = false;
while (j < typeInformation.active.length && !found) {
if (attributes[i].name === typeInformation.active[j].object_id) {
let measureAttrByObjectId = {
name: typeInformation.active[j].name,
value: attributes[i].value,
type: attributes[i].type
};
attributesCtxt.push(measureAttrByObjectId);
found = true;
}
j++;
}
}
}
// This context is just to calculate explicitAttrs when is an expression
let ctxt = expressionPlugin.extractContext(attributesCtxt.concat(idTypeSSSList));
// typeInformation.active all attrs with expressions
if (typeInformation.active) {
typeInformation.active.forEach(function (att) {
if (att.expression !== undefined) {
let expandedAttr = {
name: att.name,
value: att.expression,
type: att.type
};
attributesCtxt.push(expandedAttr);
if (att.object_id !== undefined) {
let expandedAttrByObjectId = {
name: att.object_id,
value: att.expression,
type: att.type
};
attributesCtxt.push(expandedAttrByObjectId);
}
ctxt = expressionPlugin.extractContext(attributesCtxt.concat(idTypeSSSList));
}
});
}
// calculate expression for explicitAttrs
try {
logger.debug(context, 'sendUpdateValueNgsi2 selectedAttrs ctxt %j', ctxt);
let res = jexlParser.applyExpression(typeInformation.explicitAttrs, ctxt, typeInformation);
if (res === true) {
// like explicitAttrs == true
// selectAttrs should be measures which are defined attributes
typeInformation.active.forEach((attr) => {
selectedAttrs.push(attr.name);
selectedAttrs.push(attr.object_id);
});
} else if (res === false) {
// like explicitAttrs == false
// selectAttrs should be measures and defined attributes
typeInformation.active.forEach((attr) => {
selectedAttrs.push(attr.name);
selectedAttrs.push(attr.object_id);
});
for (let i = 0; i < attributes.length; i++) {
selectedAttrs.push(attributes[i].name);
}
} else {
selectedAttrs = res; // TBD: Check ensure is an array of strings
}
if (selectedAttrs.length === 0) {
// implies do nothing
logger.info(
context,
'sendUpdateValueNgsi2 none selectedAttrs with %j and ctxt %j',
typeInformation.explicitAttrs,
ctxt
);
return callback(null);
}
} catch (e) {
// nothing to do: exception is already logged at info level
}
typeInformation.active.forEach((attr) => {
if (selectedAttrs.includes(attr.name)) {
selectedAttrs.push(attr.object_id);
}
// Check if selectedAttrs includes an attribute with format {object_id: xxxx}
if (selectedAttrs.includes({ object_id: attr.object_id })) {
selectedAttrs.push(attr.object_id);
}
});
} else if (typeInformation.explicitAttrs && typeof typeInformation.explicitAttrs === 'boolean') {
// explicitAtts is true => Add just measures which are defined in active attributes
// and active attributes with expressions
// and TimeInstant
selectedAttrs = ['TimeInstant'];
typeInformation.active.forEach((attr) => {
// Measures
if (attr.expression !== undefined) {
selectedAttrs.push(attr.name);
selectedAttrs.push(attr.object_id);
} else {
// check if active attr is receiving a measure
let i = 0;
let found = false;
while (i < attributes.length && !found) {
if (attributes[i].name && attributes[i].type) {
if (attributes[i].name === attr.object_id || attributes[i].name === attr.name) {
selectedAttrs.push(attr.name);
selectedAttrs.push(attr.object_id);
found = true;
}
}
i++;
}
}
});
}
// This loop adds selected measured values (attributes) into payload entities (entity[0])
for (let i = 0; i < attributes.length; i++) {
if (attributes[i].name && selectedAttrs.includes(attributes[i].name) && attributes[i].type) {
const attr = typeInformation.active.find((obj) => {
return obj.name === attributes[i].name;
});
payload.entities[0][attributes[i].name] = {
value: attributes[i].value,
type: attributes[i].type
};
// ensure payload has attr with proper object_id
if (attr && attr.object_id) {
payload.entities[0][attributes[i].name].object_id = attr.object_id;
}
const metadata = NGSIUtils.getMetaData(typeInformation, attributes[i].name, attributes[i].metadata);
if (metadata) {
payload.entities[0][attributes[i].name].metadata = metadata;
}
} else if (attributes[i].name && !selectedAttrs.includes(attributes[i].name) && attributes[i].type) {
const att = {
name: attributes[i].name,
type: attributes[i].type,
value: attributes[i].value
};
measureAttrsForCtxt.push(att);
}
}
logger.debug(
context,
'sendUpdateValueNgsi2 pre-initial explicitAttrs payload=%j selectedAttrs=%j',
payload,
selectedAttrs
);
let selectedAttrsByObjectId = selectedAttrs
.filter((o) => o !== undefined && o.object_id)
.map(function (el) {
return el.object_id;
});
// Loop for add seleted attrs from type.information.active into pyaload entities (entity[0])
if (typeInformation.active) {
typeInformation.active.forEach((attr) => {
if (selectedAttrs.includes(attr.name)) {
if (attr.object_id) {
payload.entities[0][attr.object_id] = {
value: payload.entities[0][attr.object_id]
? payload.entities[0][attr.object_id].value
: payload.entities[0][attr.name]
? payload.entities[0][attr.name].value
: undefined,
type: attr.type,
object_id: attr.object_id
};
} else {
payload.entities[0][attr.name] = {
value: payload.entities[0][attr.name] ? payload.entities[0][attr.name].value : undefined,
type: attr.type
};
}
} else if (attr.object_id !== undefined && selectedAttrsByObjectId.includes(attr.object_id)) {
payload.entities[0][attr.object_id] = {
value: payload.entities[0][attr.object_id]
? payload.entities[0][attr.object_id].value
: payload.entities[0][attr.name]
? payload.entities[0][attr.name].value
: undefined,
type: attr.type,
object_id: attr.object_id
};
}
});
}
} // END check explicitAttrs
logger.debug(context, 'sendUpdateValueNgsi2 initial payload=%j', payload);
const currentEntity = payload.entities[0];
// Prepare attributes for expresionPlugin
const attsArray = pluginUtils.extractAttributesArrayFromNgsi2Entity(currentEntity);
// Exclude processing all attr expressions when current attr is of type 'commandStatus' or 'commandResult'
let attsArrayFiltered = [];
if (attsArray) {
attsArrayFiltered = attsArray.filter((obj) => {
return ![constants.COMMAND_STATUS, constants.COMMAND_RESULT].includes(obj.type);
});
}
let attributesCtxt = [...attsArrayFiltered]; // just copy
if (typeInformation.static) {
typeInformation.static.forEach(function (att) {
attributesCtxt.push(att);
});
}
if (measureAttrsForCtxt) {
measureAttrsForCtxt.forEach(function (att) {
attributesCtxt.push(att);
});
}
attributesCtxt = attributesCtxt.concat(idTypeSSSList);
let ctxt = expressionPlugin.extractContext(attributesCtxt, typeInformation);
logger.debug(context, 'sendUpdateValueNgsi2 initial ctxt %j ', ctxt);
// Sort currentEntity to get first attrs without expressions (checking attrs in typeInformation.active)
// attributes without expressions should be processed before
logger.debug(context, 'sendUpdateValueNgsi2 currentEntity %j ', currentEntity);
if (typeInformation.active && typeInformation.active.length > 0) {
for (const k in currentEntity) {
typeInformation.active.forEach(function (att) {
if (
(att.object_id && att.object_id === k && att.expression) ||
(att.name && att.name === k && att.expression)
) {
const m = currentEntity[k];
delete currentEntity[k];
currentEntity[k] = m; // put into the end of currentEntity
}
});
}
}
// Evaluate entityNameExp with a context including measures
if (typeInformation.entityNameExp !== undefined && typeInformation.entityNameExp !== '') {
try {
logger.debug(context, 'sendUpdateValueNgsi2 entityNameExp %j ', typeInformation.entityNameExp);
entityName = expressionPlugin.applyExpression(typeInformation.entityNameExp, ctxt, typeInformation);
payload.entities[0].id = entityName;
ctxt['entity_name'] = entityName;
} catch (e) {
logger.debug(
context,
'Error evaluating expression for entityName: %s with context: %s',
typeInformation.entityNameExp,
ctxt
);
}
}
logger.debug(context, 'sendUpdateValueNgsi2 currentEntity sorted %j ', currentEntity);
let timestampValue = undefined;
// Loop for each final attribute to apply alias, multientity and expressions
for (const j in currentEntity) {
// discard id and type
if (j !== 'id' || j !== 'type') {
// Apply Mapping Alias: object_id in attributes are in typeInformation.active
let attr;
let newAttr = payload.entities[0][j];
if (typeInformation.active) {
attr = typeInformation.active.find((obj) => {
return obj.object_id === j;
});
}
if (!attr) {
if (typeInformation.lazy) {
attr = typeInformation.lazy.find((obj) => {
return obj.object_id === j;
});
}
}
if (!attr) {
if (typeInformation.active) {
attr = typeInformation.active.find((obj) => {
return obj.name === j;
});
}
}
if (attr && attr.name) {
if (['id', 'type'].includes(attr.name)) {
// invalid mapping
logger.debug(
context,
'sendUpdateValueNgsi2 invalid mapping for attr=%j newAttr=%j',
attr,
newAttr
);
delete payload.entities[0][attr.object_id];
attr = undefined; // stop processing attr
newAttr = undefined;
} else {
ctxt[attr.name] = payload.entities[0][j].value;
}
}
logger.debug(
context,
'sendUpdateValueNgsi2 procesing j=%j attr=%j ctxt=%j newAttr=%j ',
j,
attr,
ctxt,
newAttr
);
if (attr && attr.type) {
newAttr.type = attr.type;
}
// Apply expression
if (attr && attr.expression) {
logger.debug(
context,
'sendUpdateValueNgsi2 apply expression=%j over ctxt=%j and device=%j',
attr.expression,
ctxt,
typeInformation
);
let res = null;
try {
if (expressionPlugin.contextAvailable(attr.expression, ctxt, typeInformation)) {
res = expressionPlugin.applyExpression(attr.expression, ctxt, typeInformation);
if (
// By default undefined is equivalent to null: should not progress
(attr.skipValue === undefined && res === null) ||
(attr.skipValue !== undefined && res === attr.skipValue)
) {
logger.debug(
context,
'sendUpdateValueNgsi2 skip value=%j for res=%j with expression=%j',
attr.skipValue,
res,
attr.expression
);
delete payload.entities[0][j]; // remove measure attr
attr = undefined; // stop process attr
}
} else {
logger.info(
context,
'sendUpdateValueNgsi2 no context available for apply expression=%j',
attr.expression
);
res = newAttr.value; // keep newAttr value
}
} catch (e) {
logger.error(context, 'sendUpdateValueNgsi2 apply expression exception=%j', e);
if (attr && attr.name) {
res = ctxt[attr.name];
}
}
// jexl expression plugin
newAttr.value = res;
logger.debug(context, 'sendUpdateValueNgsi2 apply expression result=%j newAttr=%j', res, newAttr);
// update current context with value
if (attr && attr.name && ctxt[attr.name] !== undefined) {
ctxt[attr.name] = newAttr.value;
}
}
// Apply Multientity: entity_type and entity_name in attributes are in typeInformation.active
if (attr && (attr.entity_type || attr.entity_name)) {
// Create a newEntity for this attribute
let newEntityName = null;
if (attr.entity_name) {
try {
if (expressionPlugin.contextAvailable(attr.entity_name, ctxt, typeInformation)) {
newEntityName = expressionPlugin.applyExpression(attr.entity_name, ctxt, typeInformation);
} else {
logger.info(
context,
'sendUpdateValueNgsi2 MULTI no context available for apply expression=%j',
attr.entity_name
);
newEntityName = attr.entity_name;
}
newEntityName = newEntityName ? newEntityName : attr.entity_name;
} catch (e) {
logger.error(context, 'sendUpdateValueNgsi2 MULTI apply expression exception=%j', e);
newEntityName = attr.entity_name;
}
logger.debug(
context,
'sendUpdateValueNgsi2 MULTI apply expression=%j result=%j payload=%j',
attr.entity_name,
newEntityName,
payload
);
}
let newEntity = {
id: newEntityName ? newEntityName : payload.entities[0].id,
type: attr.entity_type ? attr.entity_type : payload.entities[0].type
};
// Check if there is already a newEntity created
const alreadyEntity = payload.entities.find((entity) => {
return entity.id === newEntity.id && entity.type === newEntity.type;
});
if (alreadyEntity) {
// Use alreadyEntity
alreadyEntity[attr.name] = newAttr;
} else {
// Add newEntity to payload.entities
newEntity[attr.name] = newAttr;
if (
'timestamp' in typeInformation && typeInformation.timestamp !== undefined
? typeInformation.timestamp
: config.getConfig().timestamp !== undefined
? config.getConfig().timestamp
: timestampValue !== undefined
) {
newEntity = addTimestampNgsi2(newEntity, typeInformation.timezone, timestampValue);
logger.debug(context, 'sendUpdateValueNgsi2 timestamped newEntity=%j', newEntity);
}
payload.entities.push(newEntity);
}
if (attr && attr.name) {
if (attr.name !== j) {
logger.debug(
context,
'sendUpdateValueNgsi2 MULTI remove measure attr=%j keep alias j=%j from %j',
j,
attr,
payload
);
delete payload.entities[0][j];
}
}
// if (attr && (attr.entity_type || attr.entity_name))
} else {
// Not a multientity attr
if (attr && attr.name) {
payload.entities[0][attr.name] = newAttr;
if (attr.name !== j) {
delete payload.entities[0][j]; // keep alias name, remove measure name
}
}
if (newAttr && newAttr.type === constants.TIMESTAMP_TYPE_NGSI2 && newAttr.value) {
const extendedTime = compressTimestampPlugin.fromBasicToExtended(newAttr.value);
if (extendedTime) {
// TBD: there is not flag about compressTimestamp in iotagent-node-lib,
// but there is one in agents
newAttr.value = extendedTime;
}
}
if (j === constants.TIMESTAMP_ATTRIBUTE) {
if (newAttr && newAttr.type === constants.TIMESTAMP_TYPE_NGSI2 && newAttr.value) {
timestampValue = newAttr.value;
logger.debug(
context,
'sendUpdateValueNgsi2 newAttr is TimeInstant and new payload=%j',
payload
);
}
}
if (
newAttr &&
newAttr.metadata &&
newAttr.metadata[constants.TIMESTAMP_ATTRIBUTE] &&
newAttr.metadata[constants.TIMESTAMP_ATTRIBUTE].type === constants.TIMESTAMP_TYPE_NGSI2 &&
newAttr.metadata[constants.TIMESTAMP_ATTRIBUTE].value
) {
const extendedTime = compressTimestampPlugin.fromBasicToExtended(
newAttr.metadata[constants.TIMESTAMP_ATTRIBUTE].value
);
if (extendedTime) {
newAttr.metadata[constants.TIMESTAMP_ATTRIBUTE].value = extendedTime;
}
}
}
} // if (j !== 'id' || j !== 'type')
// final attr loop
logger.debug(
context,
'sendUpdateValueNgsi2 after procesing attr=%j current entity=%j current payload=%j',
j,
currentEntity,
payload
);
}
// for attr loop
// Add timestamp to paylaod
if (
'timestamp' in typeInformation && typeInformation.timestamp !== undefined
? typeInformation.timestamp
: config.getConfig().timestamp !== undefined
? config.getConfig().timestamp
: timestampValue !== undefined
) {
if (timestampValue) {
// timeInstant is provided as measure
if (payload.entities.length > 0) {
for (let n = 0; n < payload.entities.length; n++) {
// include metadata with TimeInstant in attrs when TimeInstant is provided as measure in all entities
payload.entities[n] = addTimestampNgsi2(
payload.entities[n],
typeInformation.timezone,
timestampValue
);
}
}
} else {
// jshint maxdepth:5
for (let n = 0; n < payload.entities.length; n++) {
if (!utils.isTimestampedNgsi2(payload.entities[n])) {
// legacy check needed?
payload.entities[n] = addTimestampNgsi2(payload.entities[n], typeInformation.timezone);
// jshint maxdepth:5
} else if (!utils.IsValidTimestampedNgsi2(payload.entities[n])) {
// legacy check needed?
logger.error(context, 'Invalid timestamp:%s', JSON.stringify(payload.entities[0]));
callback(new errors.BadTimestamp(payload.entities));
return;
}
}
}
}
logger.debug(context, 'sendUpdateValueNgsi2 ending payload=%j', payload);
for (let m = 0; m < payload.entities.length; m++) {
for (const key in payload.entities[m]) {
// purge object_id from payload
if (payload.entities[m][key] && payload.entities[m][key].object_id) {
delete payload.entities[m][key].object_id;
}
}
payload.entities[m] = NGSIUtils.castJsonNativeAttributes(payload.entities[m]); // native types
}
logger.debug(context, 'sendUpdateValueNgsi2 payload with native types and without object_id=%j', payload);
options.json = payload;
// Prevent to update an entity with an empty payload
if (
Object.keys(options.json).length > 0 &&
(options.json.entities.length > 1 ||
(options.json.entities.length === 1 && Object.keys(options.json.entities[0]).length > 2)) // more than id and type
) {
// Final check: (to keep tests unchanged) before do CB requests
// one entity -> request /v2/entities/ + entityName + /atts ?type=typeInformation.type
// multi entities -> request /v2/op/update
// Note that the options object is prepared for the second case (multi entity), so we "patch" it
// only in the first case
if (options.json.entities.length === 1) {
// recreate options object to use single entity update
url = '/v2/entities';
if (config.getConfig().appendMode === false) {
url += '/' + entityName + '/attrs';
if (typeInformation && typeInformation.type) {
url += '?type=' + typeInformation.type;
}
} else {
// appendMode === true
url += '?options=upsert';
}
options = NGSIUtils.createRequestObject(url, typeInformation, token);