iotagent-node-lib 3.2.0 → 3.4.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.
Files changed (184) hide show
  1. package/.github/ISSUE_TEMPLATE/bug_report.yml +134 -0
  2. package/.github/ISSUE_TEMPLATE/config.yml +16 -0
  3. package/.github/ISSUE_TEMPLATE/feature_request.yml +55 -0
  4. package/.github/advanced-issue-labeler.yml +30 -0
  5. package/.github/workflows/issue-labeler.yml +43 -0
  6. package/README.md +10 -11
  7. package/doc/README.md +16 -0
  8. package/doc/admin.md +565 -0
  9. package/doc/api.md +32 -85
  10. package/doc/deprecated.md +16 -10
  11. package/doc/{architecture.md → devel/architecture.md} +3 -3
  12. package/doc/{Contribution.md → devel/contribution-guidelines.md} +43 -35
  13. package/doc/devel/development.md +1879 -0
  14. package/doc/{northboundinteractions.md → devel/northboundinteractions.md} +18 -33
  15. package/doc/index.md +3 -5
  16. package/doc/requirements.txt +1 -1
  17. package/docker/Mosquitto/Dockerfile +1 -1
  18. package/docker/Mosquitto/README.md +1 -0
  19. package/lib/commonConfig.js +0 -5
  20. package/lib/fiware-iotagent-lib.js +1 -1
  21. package/lib/jexlTranformsMap.js +2 -1
  22. package/lib/model/Device.js +0 -1
  23. package/lib/model/Group.js +0 -1
  24. package/lib/model/dbConn.js +1 -7
  25. package/lib/plugins/jexlParser.js +1 -1
  26. package/lib/request-shim.js +2 -2
  27. package/lib/services/commands/commandService.js +1 -1
  28. package/lib/services/common/genericMiddleware.js +1 -1
  29. package/lib/services/common/iotManagerService.js +0 -1
  30. package/lib/services/devices/deviceRegistryMemory.js +2 -2
  31. package/lib/services/devices/deviceRegistryMongoDB.js +32 -19
  32. package/lib/services/devices/deviceService.js +44 -43
  33. package/lib/services/devices/devices-NGSI-LD.js +14 -2
  34. package/lib/services/devices/devices-NGSI-mixed.js +0 -2
  35. package/lib/services/devices/devices-NGSI-v2.js +23 -104
  36. package/lib/services/groups/groupService.js +1 -1
  37. package/lib/services/ngsi/entities-NGSI-LD.js +3 -3
  38. package/lib/services/ngsi/entities-NGSI-v2.js +28 -19
  39. package/lib/services/northBound/deviceProvisioningServer.js +14 -8
  40. package/lib/templates/createDevice.json +0 -4
  41. package/lib/templates/createDeviceLax.json +0 -4
  42. package/lib/templates/deviceGroup.json +1 -5
  43. package/lib/templates/updateDevice.json +4 -0
  44. package/lib/templates/updateDeviceLax.json +11 -0
  45. package/mkdocs.yml +6 -11
  46. package/package.json +3 -3
  47. package/scripts/legacy_expression_tool/README.md +280 -0
  48. package/scripts/legacy_expression_tool/legacy_expression_tool.py +423 -0
  49. package/scripts/legacy_expression_tool/requirements.txt +3 -0
  50. package/test/unit/examples/deviceProvisioningRequests/provisionMinimumDevice4.json +0 -1
  51. package/test/unit/general/contextBrokerKeystoneSecurityAccess-test.js +5 -15
  52. package/test/unit/mongodb/mongodb-registry-test.js +1 -1
  53. package/test/unit/ngsi-ld/general/contextBrokerOAuthSecurityAccess-test.js +66 -65
  54. package/test/unit/ngsi-ld/general/https-support-test.js +1 -1
  55. package/test/unit/ngsi-ld/lazyAndCommands/command-test.js +8 -7
  56. package/test/unit/ngsi-ld/lazyAndCommands/merge-patch-test.js +31 -30
  57. package/test/unit/ngsi-ld/lazyAndCommands/polling-commands-test.js +12 -11
  58. package/test/unit/ngsi-ld/ngsiService/subscriptions-test.js +41 -39
  59. package/test/unit/ngsi-ld/provisioning/device-provisioning-api_test.js +122 -122
  60. package/test/unit/ngsi-ld/provisioning/device-registration_test.js +28 -28
  61. package/test/unit/ngsi-ld/provisioning/device-update-registration_test.js +18 -17
  62. package/test/unit/ngsi-ld/provisioning/singleConfigurationMode-test.js +7 -7
  63. package/test/unit/ngsi-ld/provisioning/updateProvisionedDevices-test.js +8 -7
  64. package/test/unit/ngsi-mixed/provisioning/ngsi-versioning-test.js +33 -37
  65. package/test/unit/ngsiv2/examples/contextRequests/updateContext.json +2 -0
  66. package/test/unit/ngsiv2/examples/contextRequests/updateContext1.json +3 -1
  67. package/test/unit/ngsiv2/examples/contextRequests/updateContext3WithStatic.json +2 -0
  68. package/test/unit/ngsiv2/examples/contextRequests/updateContext4.json +4 -1
  69. package/test/unit/ngsiv2/examples/contextRequests/updateContext5.json +12 -0
  70. package/test/unit/ngsiv2/examples/contextRequests/updateContext6.json +12 -0
  71. package/test/unit/ngsiv2/examples/contextRequests/updateContextAliasPlugin1.json +2 -0
  72. package/test/unit/ngsiv2/examples/contextRequests/updateContextAliasPlugin2.json +3 -1
  73. package/test/unit/ngsiv2/examples/contextRequests/updateContextAliasPlugin3.json +3 -1
  74. package/test/unit/ngsiv2/examples/contextRequests/updateContextAliasPlugin4.json +3 -1
  75. package/test/unit/ngsiv2/examples/contextRequests/updateContextAliasPlugin5.json +3 -1
  76. package/test/unit/ngsiv2/examples/contextRequests/updateContextAliasPlugin6.json +3 -1
  77. package/test/unit/ngsiv2/examples/contextRequests/updateContextAliasPlugin7.json +3 -1
  78. package/test/unit/ngsiv2/examples/contextRequests/updateContextAliasPlugin8.json +3 -1
  79. package/test/unit/ngsiv2/examples/contextRequests/updateContextAliasPlugin9.json +3 -1
  80. package/test/unit/ngsiv2/examples/contextRequests/updateContextAutocast1.json +2 -0
  81. package/test/unit/ngsiv2/examples/contextRequests/updateContextAutocast2.json +2 -0
  82. package/test/unit/ngsiv2/examples/contextRequests/updateContextAutocast3.json +3 -1
  83. package/test/unit/ngsiv2/examples/contextRequests/updateContextAutocast4.json +3 -1
  84. package/test/unit/ngsiv2/examples/contextRequests/updateContextAutocast5.json +3 -1
  85. package/test/unit/ngsiv2/examples/contextRequests/updateContextAutocast6.json +3 -1
  86. package/test/unit/ngsiv2/examples/contextRequests/updateContextAutocast7.json +3 -1
  87. package/test/unit/ngsiv2/examples/contextRequests/updateContextCommandError.json +3 -1
  88. package/test/unit/ngsiv2/examples/contextRequests/updateContextCommandExpired.json +3 -1
  89. package/test/unit/ngsiv2/examples/contextRequests/updateContextCommandFinish.json +3 -1
  90. package/test/unit/ngsiv2/examples/contextRequests/updateContextCommandStatus.json +2 -0
  91. package/test/unit/ngsiv2/examples/contextRequests/updateContextCommandStatus2.json +2 -0
  92. package/test/unit/ngsiv2/examples/contextRequests/updateContextCompressTimestamp1.json +3 -1
  93. package/test/unit/ngsiv2/examples/contextRequests/updateContextCompressTimestamp2.json +3 -1
  94. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin1.json +2 -12
  95. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin11.json +2 -4
  96. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin12.json +2 -4
  97. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin13.json +3 -1
  98. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin2.json +2 -12
  99. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin29.json +2 -12
  100. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin3.json +2 -4
  101. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin30.json +2 -0
  102. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin31.json +2 -0
  103. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin32.json +2 -0
  104. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin33.json +2 -0
  105. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin34.json +2 -0
  106. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin35.json +2 -0
  107. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin36.json +1 -0
  108. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin4.json +2 -0
  109. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin40.json +1 -1
  110. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin41.json +1 -10
  111. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin5.json +2 -4
  112. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin6.json +2 -4
  113. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin7.json +2 -4
  114. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin8.json +2 -12
  115. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin9.json +2 -4
  116. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionSkip.json +12 -0
  117. package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityJexlExpressionPlugin1.json +1 -1
  118. package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin1.json +1 -1
  119. package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin10.json +1 -1
  120. package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin11.json +1 -1
  121. package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin12.json +1 -1
  122. package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin13.json +1 -1
  123. package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin14.json +1 -1
  124. package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin15.json +1 -1
  125. package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin16.json +1 -1
  126. package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin17.json +1 -1
  127. package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin2.json +1 -1
  128. package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin25.json +2 -6
  129. package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin3.json +1 -1
  130. package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin4.json +1 -1
  131. package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin5.json +1 -1
  132. package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin6.json +1 -1
  133. package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin7.json +1 -1
  134. package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin8.json +1 -1
  135. package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin9.json +1 -1
  136. package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityTimestampPlugin1.json +1 -1
  137. package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityTimestampPlugin2.json +1 -1
  138. package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityTimestampPlugin3.json +1 -1
  139. package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityTimestampPlugin4.json +2 -0
  140. package/test/unit/ngsiv2/examples/contextRequests/updateContextProcessTimestamp.json +2 -0
  141. package/test/unit/ngsiv2/examples/contextRequests/updateContextStaticAttributes.json +2 -0
  142. package/test/unit/ngsiv2/examples/contextRequests/updateContextStaticAttributesMetadata.json +3 -1
  143. package/test/unit/ngsiv2/examples/contextRequests/updateContextTimestamp.json +3 -1
  144. package/test/unit/ngsiv2/examples/contextRequests/updateContextTimestampFalse.json +12 -0
  145. package/test/unit/ngsiv2/examples/contextRequests/updateContextTimestampFalseTimeInstant.json +12 -0
  146. package/test/unit/ngsiv2/examples/contextRequests/updateContextTimestampOverride.json +2 -0
  147. package/test/unit/ngsiv2/examples/contextRequests/updateContextTimestampOverrideWithoutMilis.json +2 -0
  148. package/test/unit/ngsiv2/examples/contextRequests/updateContextTimestampTimezone.json +3 -1
  149. package/test/unit/ngsiv2/expressions/jexlBasedTransformations-test.js +144 -85
  150. package/test/unit/ngsiv2/general/contextBrokerOAuthSecurityAccess-test.js +20 -53
  151. package/test/unit/ngsiv2/general/https-support-test.js +2 -6
  152. package/test/unit/ngsiv2/lazyAndCommands/command-test.js +4 -10
  153. package/test/unit/ngsiv2/lazyAndCommands/polling-commands-test.js +8 -24
  154. package/test/unit/ngsiv2/ngsiService/active-devices-test.js +146 -65
  155. package/test/unit/ngsiv2/ngsiService/autocast-test.js +14 -21
  156. package/test/unit/ngsiv2/ngsiService/staticAttributes-test.js +3 -5
  157. package/test/unit/ngsiv2/ngsiService/subscriptions-test.js +11 -20
  158. package/test/unit/ngsiv2/plugins/alias-plugin_test.js +20 -30
  159. package/test/unit/ngsiv2/plugins/compress-timestamp-plugin_test.js +4 -6
  160. package/test/unit/ngsiv2/plugins/custom-plugin_test.js +1 -2
  161. package/test/unit/ngsiv2/plugins/multientity-plugin_test.js +3 -5
  162. package/test/unit/ngsiv2/plugins/timestamp-processing-plugin_test.js +2 -3
  163. package/test/unit/ngsiv2/provisioning/device-group-api-test.js +2 -3
  164. package/test/unit/ngsiv2/provisioning/device-provisioning-api_test.js +13 -156
  165. package/test/unit/ngsiv2/provisioning/device-registration_test.js +9 -13
  166. package/test/unit/ngsiv2/provisioning/device-update-registration_test.js +4 -10
  167. package/test/unit/ngsiv2/provisioning/singleConfigurationMode-test.js +0 -11
  168. package/test/unit/ngsiv2/provisioning/updateProvisionedDevices-test.js +0 -8
  169. package/test/unit/plugins/capture-provision-inPlugins_test.js +0 -6
  170. package/.nyc_output/33364de2-1199-4ec2-b33c-cae063ef8cc4.json +0 -1
  171. package/.nyc_output/processinfo/33364de2-1199-4ec2-b33c-cae063ef8cc4.json +0 -1
  172. package/.nyc_output/processinfo/index.json +0 -1
  173. package/doc/config-basic-example.js +0 -20
  174. package/doc/development.md +0 -285
  175. package/doc/howto.md +0 -645
  176. package/doc/installationguide.md +0 -370
  177. package/doc/operations.md +0 -127
  178. package/doc/usermanual.md +0 -900
  179. package/lib/plugins/bidirectionalData.js +0 -356
  180. package/test/unit/ngsi-ld/plugins/bidirectional-plugin_test.js +0 -697
  181. package/test/unit/ngsiv2/plugins/bidirectional-plugin_test.js +0 -599
  182. /package/doc/{NorthboundInteractions.postman_collection → devel/NorthboundInteractions.postman_collection} +0 -0
  183. /package/doc/{echo.js → devel/echo.js} +0 -0
  184. /package/doc/{finalResult.js → devel/finalResult.js} +0 -0
@@ -1,356 +0,0 @@
1
- /*
2
- * Copyright 2016 Telefonica Investigación y Desarrollo, S.A.U
3
- *
4
- * This file is part of fiware-iotagent-lib
5
- *
6
- * fiware-iotagent-lib is free software: you can redistribute it and/or
7
- * modify it under the terms of the GNU Affero General Public License as
8
- * published by the Free Software Foundation, either version 3 of the License,
9
- * or (at your option) any later version.
10
- *
11
- * fiware-iotagent-lib is distributed in the hope that it will be useful,
12
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
- * See the GNU Affero General Public License for more details.
15
- *
16
- * You should have received a copy of the GNU Affero General Public
17
- * License along with fiware-iotagent-lib.
18
- * If not, see http://www.gnu.org/licenses/.
19
- *
20
- * For those usages not covered by the GNU Affero General Public License
21
- * please contact with::daniel.moranjimenez@telefonica.com
22
- *
23
- * Modified by: Daniel Calvo - ATOS Research & Innovation
24
- */
25
-
26
- const async = require('async');
27
- const apply = async.apply;
28
- const _ = require('underscore');
29
- const parser = require('./expressionPlugin');
30
- const logger = require('logops');
31
- const config = require('../commonConfig');
32
- const subscriptions = require('../services/ngsi/subscriptionService');
33
- const deviceService = require('../services/devices/deviceService');
34
- const context = {
35
- op: 'IoTAgentNGSI.BidirectionalPlugin'
36
- };
37
-
38
- /**
39
- * Extract a list of all the bidirectional attributes (those containing reverse expressions) from a device object.
40
- * If the device belongs to a Configuration Group, its attributes are also considered.
41
- *
42
- * @param {Object} device Device data object.
43
- * @param {Object} group Configuration Group data object.
44
- */
45
- function extractBidirectionalAttributes(device, group, callback) {
46
- let attributeList;
47
-
48
- function isBidirectional(item) {
49
- return item.reverse;
50
- }
51
-
52
- if (device.active) {
53
- attributeList = device.active.filter(isBidirectional);
54
- } else {
55
- attributeList = [];
56
- }
57
-
58
- if (group && group.attributes) {
59
- attributeList = attributeList.concat(group.attributes.filter(isBidirectional));
60
- }
61
-
62
- logger.debug(context, 'Extracting attribute list');
63
-
64
- callback(null, attributeList);
65
- }
66
-
67
- /**
68
- * Extract all the variables that exists in the collection of reverse attribute expressions of an attribute.
69
- *
70
- * @param {Object} item Attribute with a collection of reverse expressions.
71
- * @return {Array} List of variables in all the collection of reverse expressions.
72
- */
73
- function extractVariables(item) {
74
- let variables;
75
-
76
- function extractFromExpression(value) {
77
- if (value.expression) {
78
- return parser.extractVariables(value.expression);
79
- }
80
- return [];
81
- }
82
-
83
- if (item.reverse) {
84
- variables = _.uniq(_.flatten(item.reverse.map(extractFromExpression)));
85
- }
86
-
87
- logger.debug(context, 'Extracted variables: %j', variables);
88
-
89
- return variables;
90
- }
91
-
92
- /**
93
- * Send a subscription for each reverse attribute defined for a device.
94
- *
95
- * @param {Object} device Device data object.
96
- * @param {Array} attributeList List of active attributes for subscription.
97
- */
98
- function sendSubscriptions(device, attributeList, callback) {
99
- function sendSingleSubscriptionNgsi2(item, innerCb) {
100
- const variables = extractVariables(item);
101
-
102
- subscriptions.subscribe(device, [item.name], variables, function handleSubscription(error, subId) {
103
- if (error) {
104
- innerCb(error);
105
- } else {
106
- innerCb(null, {
107
- id: subId.substr(subId.lastIndexOf('/') + 1),
108
- triggers: [item.name]
109
- });
110
- }
111
- });
112
- }
113
-
114
- logger.debug(context, 'Sending bidirectionality subscriptions for device [%s]', device.id);
115
-
116
- async.map(attributeList, sendSingleSubscriptionNgsi2, callback);
117
- }
118
-
119
- /**
120
- * Add the list of generated subscription IDs to the device object.
121
- *
122
- * @param {Array} subscriptionMaps List of subscription IDs to be saved.
123
- * @param {Object} device Device data object.
124
- * @return {Object} Modified device object.
125
- */
126
- function updateDeviceWithSubscriptionIds(subscriptionMaps, device) {
127
- if (!device.subscriptions) {
128
- device.subscriptions = [];
129
- }
130
-
131
- device.subscriptions = device.subscriptions.concat(subscriptionMaps);
132
-
133
- return device;
134
- }
135
-
136
- /**
137
- * Middleware to handle incoming Configuration group provisions. Should check for the existence of reverse active
138
- * attributes and create subscriptions for the modifciation of those values.
139
- *
140
- * @param {Object} newGroup Configuration Group data object.
141
- */
142
- function handleGroupProvision(newGroup, callback) {
143
- callback(null, newGroup);
144
- }
145
-
146
- /**
147
- * Get a list of all the reverse transformations of a device that can be processed with the information reported by
148
- * the incoming notification.
149
- *
150
- * @param {Array} list List of attributes to be checked
151
- * @param {Array} values List of reported attributes (with their name, type and value).
152
- */
153
- function getReverseTransformations(list, values, callback) {
154
- const availableData = _.pluck(values, 'name');
155
- let transformations = [];
156
-
157
- function getVariable(expression) {
158
- return parser.extractVariables(expression);
159
- }
160
-
161
- if (list && list.length > 0) {
162
- for (let i = 0; i < list.length; i++) {
163
- if (list[i].reverse && list[i].reverse.length > 0) {
164
- const expressions = _.pluck(list[i].reverse, 'expression');
165
- const variables = _.uniq(_.flatten(expressions.map(getVariable)));
166
-
167
- if (_.difference(variables, availableData).length === 0) {
168
- transformations = transformations.concat(list[i].reverse);
169
- }
170
- }
171
- }
172
- }
173
-
174
- logger.debug(context, 'Got the following transformations: %j', transformations);
175
-
176
- callback(null, transformations);
177
- }
178
-
179
- /**
180
- * Apply a list of transformations to the reported values,
181
- * generating a new array of values with the additional data.
182
- * For NGSI-v2 this consists of value transformations only
183
- *
184
- * @param {Array} values List of reported attributes (with their name, type and value).
185
- * @param {Object} typeInformation Provisioning information about the device represented by the entity.
186
- * @param {Array} transformations List of transformations to apply (with their name, type and expression).
187
- */
188
- function processTransformationsNGSIv2(values, typeInformation, transformations, callback) {
189
- let cleanedExpression;
190
- const ctx = parser.extractContext(values);
191
- let resultTransformations = [];
192
-
193
- for (let j = 0; j < 2; j++) {
194
- if (transformations[j]) {
195
- resultTransformations = resultTransformations.concat(transformations[j]);
196
- }
197
- }
198
-
199
- for (let i = 0; i < resultTransformations.length; i++) {
200
- cleanedExpression = resultTransformations[i].expression;
201
-
202
- values.push({
203
- name: resultTransformations[i].object_id,
204
- type: resultTransformations[i].type,
205
- value: parser.parse(cleanedExpression, ctx, 'String', typeInformation)
206
- });
207
- }
208
-
209
- callback(null, values);
210
- }
211
-
212
- /**
213
- * Apply a list of transformations to the reported values,
214
- * generating a new array of values with the additional data.
215
- * For NGSI-LD the output includes value, metadata and datasetId
216
- *
217
- * @param {Array} values List of reported attributes (with their name, type and value).
218
- * @param {Object} typeInformation Provisioning information about the device represented by the entity.
219
- * @param {Array} transformations List of transformations to apply (with their name, type and expression).
220
- */
221
- function processTransformationsNGSILD(values, typeInformation, transformations, callback) {
222
- let cleanedExpression;
223
- const defaultValues = [];
224
- const datasetIds = {};
225
-
226
- // Split incoming values into those with and without datasetId
227
- values.forEach((value) => {
228
- if (value.datasetId) {
229
- datasetIds[value.datasetId] = datasetIds[value.datasetId] || [];
230
- datasetIds[value.datasetId].push(value);
231
- } else {
232
- defaultValues.push(value);
233
- }
234
- });
235
-
236
- let resultTransformations = [];
237
-
238
- for (let j = 0; j < 2; j++) {
239
- if (transformations[j]) {
240
- resultTransformations = resultTransformations.concat(transformations[j]);
241
- }
242
- }
243
-
244
- // Process Transformations using the default datasetId
245
- // This is the direct equivalent of the NGSI-v2 function
246
- if (!_.isEmpty(defaultValues)) {
247
- for (let i = 0; i < resultTransformations.length; i++) {
248
- cleanedExpression = resultTransformations[i].expression;
249
- values.push({
250
- name: resultTransformations[i].object_id,
251
- type: resultTransformations[i].type,
252
- metadata: {},
253
- value: parser.parse(cleanedExpression, parser.extractContext(defaultValues), 'String', typeInformation)
254
- });
255
- }
256
- }
257
-
258
- // Process Transformations using an explicit datasetId
259
- // There is no equivalent with NGSI-v2
260
- _.keys(datasetIds).forEach((datasetId) => {
261
- for (let i = 0; i < resultTransformations.length; i++) {
262
- cleanedExpression = resultTransformations[i].expression;
263
- values.push({
264
- name: resultTransformations[i].object_id,
265
- type: resultTransformations[i].type,
266
- datasetId,
267
- metadata: {},
268
- value: parser.parse(
269
- cleanedExpression,
270
- parser.extractContext(datasetIds[datasetId]),
271
- 'String',
272
- typeInformation
273
- )
274
- });
275
- }
276
- });
277
- callback(null, values);
278
- }
279
-
280
- /**
281
- * Apply a list of transformations to the reported values, generating a new array of values with the additional data.
282
- *
283
- * @param {Array} values List of reported attributes (with their name, type and value).
284
- * @param {Object} typeInformation Provisioning information about the device represented by the entity.
285
- * @param {Array} transformations List of transformations to apply (with their name, type and expression).
286
- */
287
- function processTransformations(values, typeInformation, transformations, callback) {
288
- if (config.checkNgsiLD({})) {
289
- processTransformationsNGSILD(values, typeInformation, transformations, callback);
290
- } else {
291
- processTransformationsNGSIv2(values, typeInformation, transformations, callback);
292
- }
293
- }
294
-
295
- /**
296
- * Middleware to handle incoming Configuration group provisions. Should check for the existence of reverse active
297
- * attributes and create subscriptions for the modifciation of those values.
298
- *
299
- * @param {Object} device Device data object.
300
- */
301
- function handleDeviceProvision(device, callback) {
302
- deviceService.findConfigurationGroup(device, function (error, group) {
303
- if (error) {
304
- callback(error);
305
- } else {
306
- async.waterfall(
307
- [apply(extractBidirectionalAttributes, device, group), apply(sendSubscriptions, device)],
308
- function (error, subscriptionMaps) {
309
- if (error) {
310
- callback(error);
311
- } else {
312
- device = updateDeviceWithSubscriptionIds(subscriptionMaps, device);
313
- callback(null, device);
314
- }
315
- }
316
- );
317
- }
318
- });
319
- }
320
-
321
- /**
322
- * Handles an incoming notification, modifying the reported values if the device has any bidirectional expression
323
- * defined for its active attributes.
324
- *
325
- * @param {Object} device Device data object.
326
- * @param {Array} values List of notified values.
327
- */
328
- function handleNotification(device, values, callback) {
329
- deviceService.findConfigurationGroup(device, function (error, group) {
330
- const deviceAttributes = device.active || [];
331
- const groupAttributes = (group && group.attributes) || [];
332
-
333
- if (deviceAttributes.length > 0 || groupAttributes.length > 0) {
334
- logger.debug(context, 'Processing active attributes notification');
335
-
336
- async.waterfall(
337
- [
338
- apply(async.series, [
339
- apply(getReverseTransformations, deviceAttributes, values),
340
- apply(getReverseTransformations, groupAttributes, values)
341
- ]),
342
- apply(processTransformations, values, device)
343
- ],
344
- function (error, results) {
345
- callback(error, device, results);
346
- }
347
- );
348
- } else {
349
- callback(null, device, values);
350
- }
351
- });
352
- }
353
-
354
- exports.deviceProvision = handleDeviceProvision;
355
- exports.groupProvision = handleGroupProvision;
356
- exports.notification = handleNotification;