iotagent-node-lib 2.20.0 → 2.23.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 (68) hide show
  1. package/.nyc_output/6e3d7795-bf8c-4a50-bd2f-f8221f27aeae.json +1 -0
  2. package/.nyc_output/processinfo/6e3d7795-bf8c-4a50-bd2f-f8221f27aeae.json +1 -0
  3. package/.nyc_output/processinfo/index.json +1 -1
  4. package/.readthedocs.yml +3 -1
  5. package/CHANGES_NEXT_RELEASE +1 -0
  6. package/README.md +2 -2
  7. package/config +0 -0
  8. package/doc/Contribution.md +3 -3
  9. package/doc/advanced-topics.md +19 -8
  10. package/doc/api.md +14 -3
  11. package/doc/expressionLanguage.md +17 -0
  12. package/doc/northboundinteractions.md +40 -33
  13. package/doc/operations.md +8 -5
  14. package/doc/requirements.txt +4 -0
  15. package/{docs → doc}/roadmap.md +21 -6
  16. package/doc/usermanual.md +4 -4
  17. package/docker/Mosquitto/Dockerfile +28 -12
  18. package/docker/Mosquitto/Dockerfile.debian +38 -0
  19. package/docker/Mosquitto/Dockerfile.debian~ +23 -0
  20. package/docker/Mosquitto/README.md +8 -7
  21. package/docker/Mosquitto/startMosquitto.sh +8 -0
  22. package/lib/fiware-iotagent-lib.js +1 -0
  23. package/lib/jexlTranformsMap.js +3 -1
  24. package/lib/model/Group.js +2 -1
  25. package/lib/model/dbConn.js +4 -0
  26. package/lib/plugins/expressionPlugin.js +56 -21
  27. package/lib/plugins/multiEntity.js +43 -49
  28. package/lib/plugins/multiEntity.js_avg2 +343 -0
  29. package/lib/plugins/pluginUtils.js +16 -0
  30. package/lib/services/commands/commandService.js +29 -2
  31. package/lib/services/common/iotManagerService.js +2 -1
  32. package/lib/services/devices/deviceRegistryMemory.js +13 -2
  33. package/lib/services/devices/deviceRegistryMongoDB.js +15 -7
  34. package/lib/services/devices/deviceService.js +52 -16
  35. package/lib/services/groups/groupRegistryMongoDB.js +13 -12
  36. package/lib/services/ngsi/entities-NGSI-LD.js +13 -4
  37. package/lib/services/ngsi/entities-NGSI-v2.js +64 -13
  38. package/lib/services/ngsi/ngsiService.js +3 -3
  39. package/lib/services/northBound/contextServer-NGSI-LD.js +20 -1
  40. package/lib/services/northBound/contextServer-NGSI-v2.js +39 -30
  41. package/lib/services/northBound/contextServerUtils.js +10 -10
  42. package/lib/services/northBound/deviceProvisioningServer.js +4 -1
  43. package/lib/templates/createDevice.json +13 -2
  44. package/lib/templates/createDeviceLax.json +2 -3
  45. package/lib/templates/updateDevice.json +13 -2
  46. package/package.json +26 -26
  47. package/test/unit/examples/deviceProvisioningRequests/provisionMinimumDevice4.json +14 -0
  48. package/test/unit/mongodb/mongoDBUtils.js +2 -2
  49. package/test/unit/mongodb/mongodb-group-registry-test.js +1 -1
  50. package/test/unit/mongodb/mongodb-registry-test.js +2 -3
  51. package/test/unit/ngsi-ld/examples/contextAvailabilityRequests/registerIoTAgentCommands.json +2 -1
  52. package/test/unit/ngsi-ld/examples/contextRequests/updateContextLanguageProperties1.json +15 -0
  53. package/test/unit/ngsi-ld/lazyAndCommands/command-test.js +315 -1
  54. package/test/unit/ngsi-ld/ngsiService/languageProperties-test.js +112 -0
  55. package/test/unit/ngsi-mixed/provisioning/ngsi-versioning-test.js +1 -1
  56. package/test/unit/ngsiv2/examples/contextRequests/createMinimumProvisionedDevice4.json +8 -0
  57. package/test/unit/ngsiv2/examples/contextRequests/updateContextCommandStatus2.json +6 -0
  58. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin35.json +2 -0
  59. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin36.json +12 -0
  60. package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin17.json +27 -0
  61. package/test/unit/ngsiv2/expressions/jexlBasedTransformations-test.js +72 -9
  62. package/test/unit/ngsiv2/lazyAndCommands/polling-commands-test.js +151 -0
  63. package/test/unit/ngsiv2/plugins/multientity-plugin_test.js +63 -0
  64. package/test/unit/ngsiv2/plugins/multientity-plugin_test.js_avg2 +1224 -0
  65. package/test/unit/ngsiv2/provisioning/device-provisioning-api_test.js +60 -0
  66. package/test/unit/ngsiv2/provisioning/updateProvisionedDevices-test.js +2 -1
  67. package/.nyc_output/76bc24ff-5fac-4b5a-997d-de2799342eb0.json +0 -1
  68. package/.nyc_output/processinfo/76bc24ff-5fac-4b5a-997d-de2799342eb0.json +0 -1
@@ -0,0 +1,343 @@
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
+ /* eslint-disable no-prototype-builtins */
27
+ /* eslint-disable array-callback-return */
28
+
29
+ const _ = require('underscore');
30
+ const constants = require('../constants');
31
+ const legacyParser = require('./expressionParser');
32
+ const jexlParser = require('./jexlParser');
33
+ const config = require('../commonConfig');
34
+ /* eslint-disable-next-line no-unused-vars */
35
+ const logger = require('logops');
36
+ /* eslint-disable-next-line no-unused-vars */
37
+ const context = {
38
+ op: 'IoTAgentNGSI.MultiEntityPlugin'
39
+ };
40
+ const utils = require('./pluginUtils');
41
+ /* eslint-disable-next-line no-unused-vars */
42
+ const aliasPlugin = require('./attributeAlias');
43
+
44
+ function checkJexl(typeInformation) {
45
+ if (
46
+ config.getConfig().defaultExpressionLanguage === 'jexl' &&
47
+ typeInformation.expressionLanguage &&
48
+ typeInformation.expressionLanguage !== 'legacy'
49
+ ) {
50
+ return true;
51
+ } else if (config.getConfig().defaultExpressionLanguage === 'jexl' && !typeInformation.expressionLanguage) {
52
+ return true;
53
+ } else if (
54
+ config.getConfig().defaultExpressionLanguage === 'legacy' &&
55
+ typeInformation.expressionLanguage &&
56
+ typeInformation.expressionLanguage === 'jexl'
57
+ ) {
58
+ return true;
59
+ }
60
+ return false;
61
+ }
62
+
63
+ function hasEntityName(item) {
64
+ return item.entity_name;
65
+ }
66
+
67
+ /**
68
+ * Return a list of all the attributes that don't have a multientity option. It considers NGSIv2.
69
+ *
70
+ * @param {Array} originalAttrs Array of original attributes coming from the single-entity device.
71
+ * @param {Array} meAttributes Array of all the multientity attributes.
72
+ * @return {Array} List of all the attrbiutes without multientity flag.
73
+ */
74
+ function filterOutMultientitiesNgsi2(originalAttrs, meAttributes) {
75
+
76
+ function filterByEntityName() {
77
+ return function (item) {
78
+ return (item.entity_name !== undefined);
79
+ };
80
+ }
81
+
82
+ const result = {};
83
+ logger.debug(context, 'filterOutMultientitiesNgsi2 entity=originalAttrs: %j', originalAttrs);
84
+ logger.debug(context, 'filterOutMultientitiesNgsi2 meAttributes: %j', meAttributes);
85
+ const meNamesList = _.pluck(meAttributes, 'name');
86
+ //logger.debug(context, 'filterOutMultientitiesNgsi2 meNamesList: %j', meNamesList);
87
+ const meObjectsList = _.pluck(meAttributes, 'object_id');
88
+ logger.debug(context, 'filterOutMultientitiesNgsi2 meObjectsList: %j', meObjectsList);
89
+ const meEntityNamesList = _.pluck(meAttributes.filter(filterByEntityName()), 'name');
90
+ logger.debug(context, 'filterOutMultientitiesNgsi2 meEntityNamesList: %j', meEntityNamesList);
91
+ let toBeFilteredByObj = [];
92
+
93
+ for (const att in originalAttrs) {
94
+ if (originalAttrs.hasOwnProperty(att)) {
95
+ logger.debug(context, 'filterOutMultientitiesNgsi2 checking att: %j value %j', att, originalAttrs[att]);
96
+ //if (!_.contains(meNamesList, att)) {
97
+ if (!_.contains(meEntityNamesList, att)) {
98
+ result[att] = originalAttrs[att];
99
+ } else {
100
+ // TO DO: should be checked with meAttributes
101
+ // if (!originalAttrs[att].entity_name && !originalAttrs[att].entity_type) {
102
+ // result[att] = originalAttrs[att];
103
+ // }
104
+ }
105
+ if (originalAttrs[att].hasOwnProperty('multi')) {
106
+ let cleanAttributes = _.union([_.clone(originalAttrs[att])], originalAttrs[att].multi);
107
+ delete cleanAttributes[0].multi;
108
+ cleanAttributes = _.map(cleanAttributes, function (val) {
109
+ val['name'] = att;
110
+ return val;
111
+ });
112
+ toBeFilteredByObj = _.union(toBeFilteredByObj, cleanAttributes);
113
+ }
114
+ }
115
+ }
116
+
117
+ for (const att in toBeFilteredByObj) {
118
+ if (!_.contains(meObjectsList, toBeFilteredByObj[att].object_id)) {
119
+ result[toBeFilteredByObj[att].name] = toBeFilteredByObj[att];
120
+ }
121
+ }
122
+ logger.debug(context, 'filterOutMultientitiesNgsi2 result: %j', result);
123
+ return result;
124
+ }
125
+
126
+ /**
127
+ * Generate new Context Elements for each new Entity, with the attributes of the original entity matching its
128
+ * entity_name. It considers Ngsiv2.
129
+ *
130
+ * @param {Object} entity The original entity
131
+ * @param {Array} newEntities List of the new entities that will be generated
132
+ * @param {Array} multiEntityAttributes List of attributes with multientity option
133
+ * @return {Array} List of the new Context Entities
134
+ */
135
+ function generateNewCEsNgsi2(entity, newEntities, multiEntityAttributes) {
136
+ const result = [];
137
+ let newEntityAttributes;
138
+ let newEntityAttributeNames;
139
+ let newEntityAttributeObjectIds;
140
+ function filterByEntityNameandType(entityNameType) {
141
+ return function (item) {
142
+ return (
143
+ item.entity_name === entityNameType.entity_name &&
144
+ (!item.entity_type || item.entity_type === entityNameType.entity_type)
145
+ );
146
+ };
147
+ }
148
+
149
+ function filterByAttributeObjectIds(entity, newEntityAttributeNames, newEntityAttributeObjectIds) {
150
+ const result = {};
151
+ for (const att in entity) {
152
+ if (entity.hasOwnProperty(att)) {
153
+ if (_.contains(newEntityAttributeNames, att)) {
154
+ if (entity[att].object_id && _.contains(newEntityAttributeObjectIds, entity[att].object_id)) {
155
+ result[att] = entity[att];
156
+ delete entity[att].object_id;
157
+ } else if (entity[att].multi) {
158
+ for (const j in entity[att].multi) {
159
+ if (
160
+ entity[att].multi[j].object_id &&
161
+ _.contains(newEntityAttributeObjectIds, entity[att].multi[j].object_id)
162
+ ) {
163
+ result[att] = entity[att].multi[j];
164
+ delete entity[att].multi[j].object_id;
165
+ break; // stop in first ocurrence (#635)
166
+ }
167
+ }
168
+ }
169
+ }
170
+ }
171
+ }
172
+ return result;
173
+ }
174
+
175
+ function filterNew(entity, multientityAttributes, name, type) {
176
+ logger.debug(context, 'filterNew: \n entity %j \n multientityAttribute %j \n name %j type %j',
177
+ entity, multientityAttributes, name, type);
178
+ const result = {};
179
+ for (const att in entity) {
180
+ if (entity.hasOwnProperty(att)) {
181
+ for (const j in multientityAttributes) {
182
+ if ( multientityAttributes[j].object_id && entity[att].object_id &&
183
+ multientityAttributes[j].object_id === entity[att].object_id &&
184
+ (multientityAttributes[j].entity_type ?
185
+ multientityAttributes[j].entity_type === type : true) &&
186
+ multientityAttributes[j].entity_name === name
187
+ ) {
188
+ result[multientityAttributes[j].name] = {
189
+ type: entity[att].type,
190
+ value: entity[att].value
191
+ };
192
+ if (entity[att].metadata) {
193
+ result[multientityAttributes[j].name].metadata = entity[att].metadata;
194
+ }
195
+ }
196
+ if (entity[att].multi){
197
+ for (const k in entity[att].multi) {
198
+ if ( multientityAttributes[j].object_id && entity[att].multi[k].object_id &&
199
+ multientityAttributes[j].object_id === entity[att].multi[k].object_id &&
200
+ (multientityAttributes[j].entity_type ?
201
+ multientityAttributes[j].entity_type === type : true) &&
202
+ multientityAttributes[j].entity_name === name
203
+ ) {
204
+ result[multientityAttributes[j].name] = {
205
+ type: entity[att].multi[k].type,
206
+ value: entity[att].multi[k].value
207
+ };
208
+ if (entity[att].multi[k].metadata) {
209
+ result[multientityAttributes[j].name].metadata = entity[att].multi[k].metadata;
210
+ }
211
+ }
212
+ }
213
+
214
+ }
215
+ }
216
+
217
+ }
218
+ }
219
+ logger.debug(context, 'filterNew result: %j', result);
220
+ return result;
221
+ }
222
+
223
+ // logger.debug(context, 'generateNewCEsNgsi2 entity: %j', entity); // measure
224
+ // entity: {"vol":{"type":"Number","value":0,"object_id":"v"},"id":"sh3","type":"ShareStation"}
225
+ // logger.debug(context, 'generateNewCEsNgsi2 multientityAttributes %j', multiEntityAttributes);
226
+ // multiEntityAttributes: [{"object_id":"v","name":"vol","type":"Number","entity_name":"WeatherStation1","entity_type":"Type1"},{"object_id":"v","name":"vol","type":"Number","entity_name":"WeatherStation1","entity_type":"Type2"},{"object_id":"v","name":"extravol","type":"Number","entity_name":"WeatherStation1","entity_type":"Type2"}]
227
+ //logger.debug(context, 'generateNewCEsNgsi2 newEntities %j', newEntities);
228
+ // newEntities [{"entity_name":"WeatherStation1","entity_type":"Type1"},{"entity_name":"WeatherStation1","entity_type":"Type2"}]
229
+ for (let i = 0; i < newEntities.length; i++) {
230
+ //logger.debug(context, 'generateNewCEsNgsi2 current newEntities: %j', newEntities[i]);
231
+ newEntityAttributeNames = _.pluck(
232
+ multiEntityAttributes.filter(filterByEntityNameandType(newEntities[i])),
233
+ 'name'
234
+ );
235
+ //logger.debug(context, 'generateNewCEsNgsi2 newEntityAttributeNames: %j', newEntityAttributeNames);
236
+ newEntityAttributeObjectIds = _.pluck(
237
+ multiEntityAttributes.filter(filterByEntityNameandType(newEntities[i])),
238
+ 'object_id'
239
+ );
240
+ //logger.debug(context, 'generateNewCEsNgsi2 newEntityAttributeObjectIds: %j', newEntityAttributeObjectIds);
241
+ //newEntityAttributes = filterByAttributeObjectIds(entity, newEntityAttributeNames, newEntityAttributeObjectIds);
242
+ newEntityAttributes = filterNew(entity, multiEntityAttributes, newEntities[i].entity_name, newEntities[i].entity_type);
243
+ //logger.debug(context, 'generateNewCEsNgsi2 newEntityAttributes: %j', newEntityAttributes);
244
+ newEntityAttributes.type = newEntities[i].entity_type;
245
+ newEntityAttributes.id = newEntities[i].entity_name;
246
+ result.push(newEntityAttributes);
247
+ }
248
+ logger.debug(context, 'generateNewCEsNgsi2 result entities %j', result);
249
+ return result;
250
+ }
251
+
252
+ function extractNewEntities(multiEntityAttributes, defaultType) {
253
+ let newEntitieswithDuplicates = multiEntityAttributes.map((elem) => {
254
+ return { entity_name: elem.entity_name, entity_type: elem.entity_type || defaultType };
255
+ });
256
+ let auxOverwriteTree = {};
257
+ for (let entityItem in newEntitieswithDuplicates) {
258
+ if (!auxOverwriteTree[newEntitieswithDuplicates[entityItem].entity_name]) {
259
+ auxOverwriteTree[newEntitieswithDuplicates[entityItem].entity_name] = {};
260
+ }
261
+ auxOverwriteTree[newEntitieswithDuplicates[entityItem].entity_name][
262
+ newEntitieswithDuplicates[entityItem].entity_type
263
+ ] = null;
264
+ }
265
+ let flatNewEntities = [];
266
+ for (let entityItem in auxOverwriteTree) {
267
+ for (let typeItem in auxOverwriteTree[entityItem]) {
268
+ flatNewEntities.push({ entity_name: entityItem, entity_type: typeItem });
269
+ }
270
+ }
271
+ return flatNewEntities;
272
+ }
273
+ /**
274
+ * Propagates the same timestamp used in entity to entities. This is needed given that timestamp processing
275
+ * plugin runs before multientity plugin, so we could have issues as the one described here:
276
+ * https://github.com/telefonicaid/iotagent-node-lib/issues/748
277
+ *
278
+ * Note that this kind of timestamp propagation only works for NGSIv2. That is intentional: NGSIv1 is
279
+ * deprecated so we don't want to spend effort on it.
280
+ *
281
+ * @param entity entity which is the source of timestamp
282
+ * @param entities array to adjust
283
+ *
284
+ */
285
+ function propagateTimestamp(entity, entities) {
286
+ const ts = entity[constants.TIMESTAMP_ATTRIBUTE];
287
+ if (!ts) {
288
+ return;
289
+ }
290
+
291
+ entities.map(function (en) {
292
+ let att;
293
+ // Set timestamp metadata in attributes (except TimeInstant attribute itself)
294
+ for (att in en && att !== constants.TIMESTAMP_ATTRIBUTE) {
295
+ if (en.hasOwnProperty(att) && att !== 'id' && att !== 'type') {
296
+ if (!en[att].metadata) {
297
+ en[att].metadata = {};
298
+ }
299
+ en[att].metadata[constants.TIMESTAMP_ATTRIBUTE] = ts;
300
+ }
301
+ }
302
+ // Set timestamp attribute inm the entity itself
303
+ en[constants.TIMESTAMP_ATTRIBUTE] = ts;
304
+ });
305
+ }
306
+
307
+ function updateAttribute(entity, typeInformation, callback) {
308
+ let parser = legacyParser;
309
+ if (checkJexl(typeInformation)) {
310
+ parser = jexlParser;
311
+ }
312
+ const attsArray = utils.extractAttributesArrayFromNgsi2Entity(entity);
313
+ const ctx = parser.extractContext(attsArray);
314
+
315
+ let entities = [entity];
316
+ if (typeInformation.active) {
317
+ const multiEntityAttributes = typeInformation.active.filter(hasEntityName);
318
+ for (let i in multiEntityAttributes) {
319
+ if (parser.contextAvailable(multiEntityAttributes[i].entity_name, ctx)) {
320
+ let entityName = parser.applyExpression(multiEntityAttributes[i].entity_name, ctx, typeInformation);
321
+ // An entity_name could not be null, but a result or expression could be null
322
+ if (entityName !== null) {
323
+ multiEntityAttributes[i].entity_name = entityName;
324
+ }
325
+ }
326
+ }
327
+ const newEntities = extractNewEntities(multiEntityAttributes, typeInformation.type);
328
+
329
+ if (multiEntityAttributes.length > 0) {
330
+ let resultAttributes = filterOutMultientitiesNgsi2(entity, multiEntityAttributes);
331
+ logger.debug(context, ' filterOutMultientitiesNgsi2 resultAttributes: %j', resultAttributes);
332
+ const newCes = generateNewCEsNgsi2(entity, newEntities, multiEntityAttributes);
333
+ entities = [resultAttributes].concat(newCes);
334
+ logger.debug(context, 'all entities: %j', entities);
335
+ propagateTimestamp(entity, entities);
336
+ } else {
337
+ entities = entity;
338
+ }
339
+ }
340
+ callback(null, entities, typeInformation);
341
+ }
342
+
343
+ exports.update = updateAttribute;
@@ -167,9 +167,25 @@ function identityFilter(entity, typeInformation, callback) {
167
167
  callback(null, entity, typeInformation);
168
168
  }
169
169
 
170
+ /**
171
+ * Creates an array of attributes from an device including id, type, service and subservice
172
+ * @param {Object} device
173
+ * @return {Object} Array of id, type, service and subservice extracted from device
174
+ */
175
+ function getIdTypeServSubServiceFromDevice(typeInformation) {
176
+ let attrList = [
177
+ { name: 'id', type: 'String', value: typeInformation.id },
178
+ { name: 'type', type: 'String', value: typeInformation.type },
179
+ { name: 'service', type: 'String', value: typeInformation.service },
180
+ { name: 'subservice', type: 'String', value: typeInformation.subservice }
181
+ ];
182
+ return attrList;
183
+ }
184
+
170
185
  exports.createProcessAttribute = createProcessAttribute;
171
186
  exports.createUpdateFilter = createUpdateFilter;
172
187
  exports.createQueryFilter = createQueryFilter;
173
188
  exports.identityFilter = identityFilter;
174
189
  exports.createNgsi2Entity = createNgsi2Entity;
175
190
  exports.extractAttributesArrayFromNgsi2Entity = extractAttributesArrayFromNgsi2Entity;
191
+ exports.getIdTypeServSubServiceFromDevice = getIdTypeServSubServiceFromDevice;
@@ -29,10 +29,12 @@ const config = require('../../commonConfig');
29
29
  const constants = require('../../constants');
30
30
  const ngsiService = require('../ngsi/ngsiService');
31
31
  const deviceService = require('../devices/deviceService');
32
+ const pluginUtils = require('../../plugins/pluginUtils');
32
33
  let daemonId;
33
34
  const context = {
34
35
  op: 'IoTAgentNGSI.CommandService'
35
36
  };
37
+ const expressionPlugin = require('../../plugins/expressionPlugin');
36
38
 
37
39
  function listCommands(service, subservice, deviceId, callback) {
38
40
  logger.debug(context, 'Listing all commands for device [%s]', deviceId);
@@ -41,11 +43,35 @@ function listCommands(service, subservice, deviceId, callback) {
41
43
  }
42
44
 
43
45
  function addCommand(service, subservice, deviceId, command, callback) {
44
- logger.debug(context, 'Adding command [%s] to the queue for device [%s]', command.name, deviceId);
45
-
46
+ logger.debug(context, 'Adding command [%j] to the queue for device [%s]', command, deviceId);
46
47
  config.getCommandRegistry().add(service, subservice, deviceId, command, callback);
47
48
  }
48
49
 
50
+ function addCommandDevice(service, subservice, device, command, callback) {
51
+ logger.debug(context, 'Adding command [%j] to the queue for device [%j]', command, device);
52
+ let deviceCmd;
53
+ if (device && device.commands) {
54
+ deviceCmd = device.commands.find((c) => c.name === command.name);
55
+ }
56
+ if (deviceCmd && deviceCmd.expression) {
57
+ let parser = expressionPlugin;
58
+ // The context for the JEXL expression should be the ID, TYPE, S, SS
59
+ let attrList = pluginUtils.getIdTypeServSubServiceFromDevice(device);
60
+ attrList = device.staticAttributes ? attrList.concat(device.staticAttributes) : attrList.concat([]);
61
+ let ctxt = parser.extractContext(attrList, device);
62
+ logger.debug(context, 'attrList [%j] for device %j', attrList, device);
63
+ // expression result will be the full command payload
64
+ let cmdValueRes = null;
65
+ try {
66
+ cmdValueRes = parser.applyExpression(deviceCmd.expression, ctxt, device);
67
+ } catch (e) {
68
+ // nothing to do
69
+ }
70
+ command.value = cmdValueRes ? cmdValueRes : deviceCmd.expression;
71
+ }
72
+ config.getCommandRegistry().add(service, subservice, device.id, command, callback);
73
+ }
74
+
49
75
  function updateCommand(service, subservice, deviceId, name, value, callback) {
50
76
  logger.debug(context, 'Updating command [%s] for device [%s] with value [%s]', name, deviceId, value);
51
77
 
@@ -147,6 +173,7 @@ function stopExpirationDaemon(callback) {
147
173
 
148
174
  exports.list = intoTrans(context, listCommands);
149
175
  exports.add = addCommand;
176
+ exports.addCmd = addCommandDevice;
150
177
  exports.update = updateCommand;
151
178
  exports.remove = removeCommand;
152
179
  exports.start = startExpirationDaemon;
@@ -60,7 +60,8 @@ function register(callback) {
60
60
  explicitAttrs: service.explicitAttrs,
61
61
  expressionLanguage: service.expressionLanguage,
62
62
  defaultEntityNameConjunction: service.defaultEntityNameConjunction,
63
- ngsiVersion: service.ngsiVersion
63
+ ngsiVersion: service.ngsiVersion,
64
+ entityNameExp: service.entityNameExp
64
65
  };
65
66
  }
66
67
 
@@ -149,13 +149,19 @@ function getDevice(id, service, subservice, callback) {
149
149
  }
150
150
  }
151
151
 
152
- function getByName(name, service, subservice, callback) {
152
+ function getByNameAndType(name, type, service, subservice, callback) {
153
153
  const devices = _.values(registeredDevices[service]);
154
154
  let device;
155
155
 
156
156
  for (let i = 0; i < devices.length; i++) {
157
157
  if (devices[i].name === name) {
158
- device = devices[i];
158
+ if (type) {
159
+ if (devices[i].type === type) {
160
+ device = devices[i];
161
+ }
162
+ } else {
163
+ device = devices[i];
164
+ }
159
165
  }
160
166
  }
161
167
 
@@ -166,6 +172,10 @@ function getByName(name, service, subservice, callback) {
166
172
  }
167
173
  }
168
174
 
175
+ function getByName(name, service, subservice, callback) {
176
+ getByNameAndType(name, null, service, subservice, callback);
177
+ }
178
+
169
179
  function update(device, callback) {
170
180
  registeredDevices[device.service][device.id] = deepClone(device);
171
181
  callback(null, device);
@@ -208,4 +218,5 @@ exports.list = listDevices;
208
218
  exports.get = getDevice;
209
219
  exports.getSilently = getDevice;
210
220
  exports.getByName = getByName;
221
+ exports.getByNameAndType = getByNameAndType;
211
222
  exports.clear = clear;
@@ -243,15 +243,18 @@ function getDevice(id, service, subservice, callback) {
243
243
  });
244
244
  }
245
245
 
246
- function getByName(name, service, servicepath, callback) {
246
+ function getByNameAndType(name, type, service, servicepath, callback) {
247
247
  context = fillService(context, { service, subservice: servicepath });
248
- logger.debug(context, 'Looking for device with name [%s].', name);
249
-
250
- const query = Device.model.findOne({
251
- name,
252
- service,
248
+ let optionsQuery = {
249
+ name: name,
250
+ service: service,
253
251
  subservice: servicepath
254
- });
252
+ };
253
+ if (type) {
254
+ optionsQuery.type = type;
255
+ }
256
+ logger.debug(context, 'Looking for device with [%j].', optionsQuery);
257
+ const query = Device.model.findOne(optionsQuery);
255
258
 
256
259
  query.select({ __v: 0 });
257
260
 
@@ -270,6 +273,10 @@ function getByName(name, service, servicepath, callback) {
270
273
  });
271
274
  }
272
275
 
276
+ function getByName(name, service, servicepath, callback) {
277
+ getByNameAndType(name, null, service, servicepath, callback);
278
+ }
279
+
273
280
  /**
274
281
  * Updates the given device into the database.
275
282
  * updated.
@@ -361,4 +368,5 @@ exports.list = alarmsInt(constants.MONGO_ALARM, listDevices);
361
368
  exports.get = alarmsInt(constants.MONGO_ALARM, getDevice);
362
369
  exports.getSilently = getDevice;
363
370
  exports.getByName = alarmsInt(constants.MONGO_ALARM, getByName);
371
+ exports.getByNameAndType = alarmsInt(constants.MONGO_ALARM, getByNameAndType);
364
372
  exports.clear = alarmsInt(constants.MONGO_ALARM, clear);
@@ -38,6 +38,8 @@ const logger = require('logops');
38
38
  const config = require('../../commonConfig');
39
39
  const registrationUtils = require('./registrationUtils');
40
40
  const subscriptions = require('../ngsi/subscriptionService');
41
+ const expressionPlugin = require('./../../plugins/expressionPlugin');
42
+ const pluginUtils = require('./../../plugins/pluginUtils');
41
43
  const _ = require('underscore');
42
44
  const context = {
43
45
  op: 'IoTAgentNGSI.DeviceService'
@@ -214,7 +216,13 @@ function findConfigurationGroup(deviceObj, callback) {
214
216
  } else {
215
217
  config
216
218
  .getGroupRegistry()
217
- .findTypeSilently(deviceObj.service, deviceObj.subservice, deviceObj.type, deviceObj.apikey, handlerGroupFindByType);
219
+ .findTypeSilently(
220
+ deviceObj.service,
221
+ deviceObj.subservice,
222
+ deviceObj.type,
223
+ deviceObj.apikey,
224
+ handlerGroupFindByType
225
+ );
218
226
  }
219
227
  }
220
228
 
@@ -274,22 +282,32 @@ function registerDevice(deviceObj, callback) {
274
282
  }
275
283
 
276
284
  if (!deviceData.name) {
277
- let conjunction;
278
- if (configuration && configuration.defaultEntityNameConjunction !== undefined) {
279
- conjunction = configuration.defaultEntityNameConjunction;
285
+ if (configuration && configuration.entityNameExp !== undefined) {
286
+ // Add device ID, TYPE, S, SS to the context for the JEXL expression
287
+ let attrList = pluginUtils.getIdTypeServSubServiceFromDevice(deviceData);
288
+ attrList = deviceData.staticAttributes ? attrList.concat(deviceData.staticAttributes) : attrList;
289
+ let ctxt = expressionPlugin.extractContext(attrList, deviceData);
290
+ let entityName = expressionPlugin.applyExpression(configuration.entityNameExp, ctxt, deviceData);
291
+ deviceData.name = entityName ? entityName : configuration.entityNameExp;
280
292
  } else {
281
- conjunction = config.getConfig().defaultEntityNameConjunction;
282
- }
283
- deviceData.name = deviceData.type + conjunction + deviceData.id;
284
- if (config.checkNgsiLD(configuration)) {
285
- deviceData.name = 'urn:ngsi-ld:' + deviceData.type + conjunction + deviceData.id;
293
+ let conjunction;
294
+ if (configuration && configuration.defaultEntityNameConjunction !== undefined) {
295
+ conjunction = configuration.defaultEntityNameConjunction;
296
+ } else {
297
+ conjunction = config.getConfig().defaultEntityNameConjunction;
298
+ }
299
+ deviceData.name = deviceData.type + conjunction + deviceData.id;
300
+
301
+ if (config.checkNgsiLD(configuration)) {
302
+ deviceData.name = 'urn:ngsi-ld:' + deviceData.type + conjunction + deviceData.id;
303
+ }
304
+ logger.debug(
305
+ context,
306
+ 'Device name not found, falling back to deviceType%sdeviceId [%s]',
307
+ conjunction,
308
+ deviceData.name
309
+ );
286
310
  }
287
- logger.debug(
288
- context,
289
- 'Device name not found, falling back to deviceType%sdeviceId [%s]',
290
- conjunction,
291
- deviceData.name
292
- );
293
311
  }
294
312
 
295
313
  if (!configuration && config.getConfig().types[deviceData.type]) {
@@ -513,6 +531,18 @@ function getDeviceByName(deviceName, service, subservice, callback) {
513
531
  config.getRegistry().getByName(deviceName, service, subservice, callback);
514
532
  }
515
533
 
534
+ /**
535
+ * Retrieve a device from the registry based on its entity name and type
536
+ *
537
+ * @param {String} deviceName Name of the entity associated to a device.
538
+ * @param {String} deviceType Type of the entity associated to a device.
539
+ * @param {String} service Service the device belongs to.
540
+ * @param {String} subservice Division inside the service.
541
+ */
542
+ function getDeviceByNameAndType(deviceName, deviceType, service, subservice, callback) {
543
+ config.getRegistry().getByNameAndType(deviceName, deviceType, service, subservice, callback);
544
+ }
545
+
516
546
  /**
517
547
  * Retrieve a device from the registry based on the value of a given attribute.
518
548
  *
@@ -581,7 +611,12 @@ function findOrCreate(deviceId, group, callback) {
581
611
  callback(error, device, group);
582
612
  });
583
613
  } else {
584
- logger.info(context, 'Device %j not provisioned due autoprovision is disabled by its conf %j', newDevice, group);
614
+ logger.info(
615
+ context,
616
+ 'Device %j not provisioned due autoprovision is disabled by its conf %j',
617
+ newDevice,
618
+ group
619
+ );
585
620
  callback(new errors.DeviceNotFound(deviceId));
586
621
  }
587
622
  } else {
@@ -632,6 +667,7 @@ exports.getDevice = intoTrans(context, checkRegistry)(getDevice);
632
667
  exports.getDeviceSilently = intoTrans(context, checkRegistry)(getDeviceSilently);
633
668
  exports.getDevicesByAttribute = intoTrans(context, checkRegistry)(getDevicesByAttribute);
634
669
  exports.getDeviceByName = intoTrans(context, checkRegistry)(getDeviceByName);
670
+ exports.getDeviceByNameAndType = intoTrans(context, checkRegistry)(getDeviceByNameAndType);
635
671
  exports.register = intoTrans(context, registerDevice);
636
672
  exports.updateRegister = intoTrans(context, updateRegisterDevice);
637
673
  exports.unregister = intoTrans(context, unregisterDevice);