@itentialopensource/adapter-utils 5.3.5 → 5.3.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/lib/brokerHandler.js +69 -26
- package/lib/genericHandler.js +1 -1
- package/package.json +1 -1
- package/refs?service=git-upload-pack +0 -0
package/CHANGELOG.md
CHANGED
package/lib/brokerHandler.js
CHANGED
|
@@ -21,6 +21,7 @@ function getDeviceBrokerArray(allProps) {
|
|
|
21
21
|
log.trace(origin);
|
|
22
22
|
const brokerCallsArr = ['getDevice', 'getDevicesFiltered', 'isAlive', 'getConfig', 'getCount'];
|
|
23
23
|
const deviceBrokerProps = allProps.devicebroker;
|
|
24
|
+
let useSampleProps = false;
|
|
24
25
|
|
|
25
26
|
try {
|
|
26
27
|
const sampleFile = path.join(adapterDir, '/sampleProperties.json');
|
|
@@ -39,6 +40,7 @@ function getDeviceBrokerArray(allProps) {
|
|
|
39
40
|
log.info(`No device broker props found for ${brokerCallsArr[i]}`);
|
|
40
41
|
deviceBrokerProps[brokerCallsArr[i]] = [];
|
|
41
42
|
} else {
|
|
43
|
+
useSampleProps = true;
|
|
42
44
|
// use the sample properties information
|
|
43
45
|
log.info(`Updating device broker props for ${brokerCallsArr[i]} from sample props`);
|
|
44
46
|
deviceBrokerProps[brokerCallsArr[i]] = sampleProps.devicebroker[brokerCallsArr[i]];
|
|
@@ -46,6 +48,15 @@ function getDeviceBrokerArray(allProps) {
|
|
|
46
48
|
}
|
|
47
49
|
}
|
|
48
50
|
|
|
51
|
+
// If sample properties doesn't have enabled flag, set it to false
|
|
52
|
+
if (useSampleProps) {
|
|
53
|
+
if (sampleProps.devicebroker.enabled === true) {
|
|
54
|
+
deviceBrokerProps.enabled = sampleProps.devicebroker.enabled;
|
|
55
|
+
} else {
|
|
56
|
+
deviceBrokerProps.enabled = false;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
49
60
|
log.debug('Device broker array', JSON.stringify(deviceBrokerProps, null, 3));
|
|
50
61
|
return deviceBrokerProps;
|
|
51
62
|
} catch (ex) {
|
|
@@ -206,6 +217,13 @@ class BrokerHandler {
|
|
|
206
217
|
const origin = `${this.myid}-${meth}`;
|
|
207
218
|
log.trace(origin);
|
|
208
219
|
|
|
220
|
+
// verify if enabled is set to true in properties, if it doesn't exist assume it's true
|
|
221
|
+
if (this.allProps.devicebroker.enabled === false) {
|
|
222
|
+
const errorObj = this.requestHandlerInst.formatErrorObject(this.myid, meth, 'Device broker is not enabled in deviceBroker properties', null, null, null, null);
|
|
223
|
+
log.info(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
224
|
+
return callback(null, errorObj);
|
|
225
|
+
}
|
|
226
|
+
|
|
209
227
|
// make sure we are set up for device broker getDevice
|
|
210
228
|
if (!this.allProps.devicebroker || !this.allProps.devicebroker.getDevice || this.allProps.devicebroker.getDevice.length === 0 || !this.allProps.devicebroker.getDevice[0].path) {
|
|
211
229
|
const errorObj = this.requestHandlerInst.formatErrorObject(this.myid, meth, 'Missing Properties devicebroker.getDevice.path', null, null, null, null);
|
|
@@ -293,11 +311,21 @@ class BrokerHandler {
|
|
|
293
311
|
* @param {getCallback} callback - a callback function to return the result
|
|
294
312
|
* (appliances) or the error
|
|
295
313
|
*/
|
|
296
|
-
getDevicesFiltered(options,
|
|
314
|
+
getDevicesFiltered(options, callupdatedOptions, callback) {
|
|
297
315
|
const meth = 'brokerHandler-getDevicesFiltered';
|
|
298
316
|
const origin = `${this.myid}-${meth}`;
|
|
299
317
|
log.trace(origin);
|
|
300
318
|
|
|
319
|
+
// If no options are passed set to an empty object
|
|
320
|
+
const updatedOptions = options || {};
|
|
321
|
+
|
|
322
|
+
// verify if enabled is set to true in properties, if it doesn't exist assume it's true
|
|
323
|
+
if (this.allProps.devicebroker.enabled === false) {
|
|
324
|
+
const errorObj = this.requestHandlerInst.formatErrorObject(this.myid, meth, 'Device broker is not enabled in deviceBroker properties', null, null, null, null);
|
|
325
|
+
log.info(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
326
|
+
return callback(null, errorObj);
|
|
327
|
+
}
|
|
328
|
+
|
|
301
329
|
// make sure we are set up for device broker getDevicesFiltered
|
|
302
330
|
if (!this.allProps.devicebroker || !this.allProps.devicebroker.getDevicesFiltered || this.allProps.devicebroker.getDevicesFiltered.length === 0 || !this.allProps.devicebroker.getDevicesFiltered[0].path) {
|
|
303
331
|
const errorObj = this.requestHandlerInst.formatErrorObject(this.myid, meth, 'Missing Properties devicebroker.getDevicesFiltered.path', null, null, null);
|
|
@@ -305,33 +333,27 @@ class BrokerHandler {
|
|
|
305
333
|
return callback(null, errorObj);
|
|
306
334
|
}
|
|
307
335
|
|
|
308
|
-
|
|
309
|
-
if (options === undefined || options === null || options === '' || options.length === 0) {
|
|
310
|
-
const errorObj = this.requestHandlerInst.formatErrorObject(this.myid, meth, 'Missing Data', ['options'], null, null, null);
|
|
311
|
-
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
312
|
-
return callback(null, errorObj);
|
|
313
|
-
}
|
|
314
|
-
log.debug(`Device Filter Options: ${JSON.stringify(options)}`);
|
|
336
|
+
log.debug(`Device Filter updatedOptions: ${JSON.stringify(updatedOptions)}`);
|
|
315
337
|
|
|
316
|
-
if (!(
|
|
338
|
+
if (!(updatedOptions.start === undefined || updatedOptions.start === null) && (updatedOptions.limit === undefined || updatedOptions.limit === null)) {
|
|
317
339
|
const errorObj = this.requestHandlerInst.formatErrorObject(this.myid, meth, 'Cannot specify start without limit.', null, null, null);
|
|
318
340
|
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
319
341
|
return callback(null, errorObj);
|
|
320
342
|
}
|
|
321
343
|
|
|
322
|
-
if ((
|
|
344
|
+
if ((updatedOptions.start === undefined || updatedOptions.start === null) && !(updatedOptions.limit === undefined || updatedOptions.limit === null)) {
|
|
323
345
|
const errorObj = this.requestHandlerInst.formatErrorObject(this.myid, meth, 'Cannot specify limit without start.', null, null, null);
|
|
324
346
|
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
325
347
|
return callback(null, errorObj);
|
|
326
348
|
}
|
|
327
349
|
|
|
328
|
-
if (typeof
|
|
350
|
+
if (typeof updatedOptions.start === 'string' || typeof updatedOptions.limit === 'string') {
|
|
329
351
|
const errorObj = this.requestHandlerInst.formatErrorObject(this.myid, meth, 'Start and Limit must be numbers.', null, null, null);
|
|
330
352
|
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
331
353
|
return callback(null, errorObj);
|
|
332
354
|
}
|
|
333
355
|
|
|
334
|
-
if (
|
|
356
|
+
if (updatedOptions.start < 0 || updatedOptions.limit <= 0) {
|
|
335
357
|
const errorObj = this.requestHandlerInst.formatErrorObject(this.myid, meth, 'Limit and/or Start value is too low.', null, null, null);
|
|
336
358
|
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
337
359
|
return callback(null, errorObj);
|
|
@@ -351,7 +373,7 @@ class BrokerHandler {
|
|
|
351
373
|
if (res) {
|
|
352
374
|
entityTypeCached = true;
|
|
353
375
|
// Retrieve devices from cache
|
|
354
|
-
return this.requestHandlerInst.retrieveEntitiesCache('Device',
|
|
376
|
+
return this.requestHandlerInst.retrieveEntitiesCache('Device', updatedOptions, (callRet, callErr) => {
|
|
355
377
|
if (callErr) {
|
|
356
378
|
log.error(callErr);
|
|
357
379
|
const errorObj = this.requestHandlerInst.formatErrorObject(this.myid, meth, callErr, null, null, null);
|
|
@@ -369,18 +391,18 @@ class BrokerHandler {
|
|
|
369
391
|
}
|
|
370
392
|
|
|
371
393
|
try {
|
|
372
|
-
// const nextToken =
|
|
373
|
-
// const maxResults =
|
|
394
|
+
// const nextToken = updatedOptions.start;
|
|
395
|
+
// const maxResults = updatedOptions.limit;
|
|
374
396
|
|
|
375
397
|
// set up the filter of Device Names
|
|
376
398
|
let filterName = [];
|
|
377
|
-
if (
|
|
399
|
+
if (updatedOptions && updatedOptions.filter && updatedOptions.filter.name) {
|
|
378
400
|
// when this hack is removed, remove the lint ignore above
|
|
379
|
-
if (Array.isArray(
|
|
401
|
+
if (Array.isArray(updatedOptions.filter.name)) {
|
|
380
402
|
// eslint-disable-next-line prefer-destructuring
|
|
381
|
-
filterName =
|
|
403
|
+
filterName = updatedOptions.filter.name;
|
|
382
404
|
} else {
|
|
383
|
-
filterName = [
|
|
405
|
+
filterName = [updatedOptions.filter.name];
|
|
384
406
|
}
|
|
385
407
|
}
|
|
386
408
|
|
|
@@ -390,11 +412,11 @@ class BrokerHandler {
|
|
|
390
412
|
const callProps = this.allProps.devicebroker.getDevicesFiltered[i];
|
|
391
413
|
callProps.headers = {
|
|
392
414
|
...(callProps.headers || {}),
|
|
393
|
-
...(
|
|
415
|
+
...(callupdatedOptions.headers || {})
|
|
394
416
|
};
|
|
395
417
|
callPromises.push(
|
|
396
418
|
new Promise((resolve, reject) => {
|
|
397
|
-
this.requestHandlerInst.iapMakeGenericCall(
|
|
419
|
+
this.requestHandlerInst.iapMakeGenericCall(callupdatedOptions, 'getDevicesFiltered', callProps, [{ fake: 'fakedata' }], filterName, (callRet, callErr) => {
|
|
398
420
|
// return an error
|
|
399
421
|
if (callErr) {
|
|
400
422
|
console.debug(`in cache iap call failed with error ${callErr}`);
|
|
@@ -420,7 +442,7 @@ class BrokerHandler {
|
|
|
420
442
|
});
|
|
421
443
|
|
|
422
444
|
// sort by name property
|
|
423
|
-
if (
|
|
445
|
+
if (updatedOptions && updatedOptions.sort) {
|
|
424
446
|
myResult.sort((a, b) => {
|
|
425
447
|
if (a.name > b.name) {
|
|
426
448
|
return 1;
|
|
@@ -433,14 +455,14 @@ class BrokerHandler {
|
|
|
433
455
|
}
|
|
434
456
|
|
|
435
457
|
// pagination: get entities limit*start to limit*(start+1) - 1
|
|
436
|
-
if (myResult.length !== 0 &&
|
|
437
|
-
const end = Math.min(myResult.length,
|
|
438
|
-
if (
|
|
458
|
+
if (myResult.length !== 0 && updatedOptions && updatedOptions.start >= 0 && updatedOptions.limit > 0) {
|
|
459
|
+
const end = Math.min(myResult.length, updatedOptions.limit * (updatedOptions.start + 1));
|
|
460
|
+
if (updatedOptions.limit * updatedOptions.start >= end) {
|
|
439
461
|
const errorObj = this.requestHandlerInst.formatErrorObject(this.myid, meth, 'Page number too large for limit size.', null, null, null);
|
|
440
462
|
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
441
463
|
return callback(null, errorObj);
|
|
442
464
|
}
|
|
443
|
-
myResult = myResult.slice(
|
|
465
|
+
myResult = myResult.slice(updatedOptions.limit * updatedOptions.start, end);
|
|
444
466
|
}
|
|
445
467
|
log.debug(`${origin}: Found #${myResult.length} devices.`);
|
|
446
468
|
log.debug(`Devices: ${JSON.stringify(myResult)}`);
|
|
@@ -471,6 +493,13 @@ class BrokerHandler {
|
|
|
471
493
|
const origin = `${this.myid}-${meth}`;
|
|
472
494
|
log.trace(origin);
|
|
473
495
|
|
|
496
|
+
// verify if enabled is set to true in properties, if it doesn't exist assume it's true
|
|
497
|
+
if (this.allProps.devicebroker.enabled === false) {
|
|
498
|
+
const errorObj = this.requestHandlerInst.formatErrorObject(this.myid, meth, 'Device broker is not enabled in deviceBroker properties', null, null, null, null);
|
|
499
|
+
log.info(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
500
|
+
return callback(null, errorObj);
|
|
501
|
+
}
|
|
502
|
+
|
|
474
503
|
// make sure we are set up for device broker isAlive
|
|
475
504
|
if (!this.allProps.devicebroker || !this.allProps.devicebroker.isAlive || this.allProps.devicebroker.isAlive.length === 0 || !this.allProps.devicebroker.isAlive[0].path) {
|
|
476
505
|
const errorObj = this.requestHandlerInst.formatErrorObject(this.myid, meth, 'Missing Properties devicebroker.isAlive.path', null, null, null);
|
|
@@ -567,6 +596,13 @@ class BrokerHandler {
|
|
|
567
596
|
const origin = `${this.myid}-${meth}`;
|
|
568
597
|
log.trace(origin);
|
|
569
598
|
|
|
599
|
+
// verify if enabled is set to true in properties, if it doesn't exist assume it's true
|
|
600
|
+
if (this.allProps.devicebroker.enabled === false) {
|
|
601
|
+
const errorObj = this.requestHandlerInst.formatErrorObject(this.myid, meth, 'Device broker is not enabled in deviceBroker properties', null, null, null, null);
|
|
602
|
+
log.info(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
603
|
+
return callback(null, errorObj);
|
|
604
|
+
}
|
|
605
|
+
|
|
570
606
|
// make sure we are set up for device broker getConfig
|
|
571
607
|
if (!this.allProps.devicebroker || !this.allProps.devicebroker.getConfig || this.allProps.devicebroker.getConfig.length === 0 || !this.allProps.devicebroker.getConfig[0].path) {
|
|
572
608
|
const errorObj = this.requestHandlerInst.formatErrorObject(this.myid, meth, 'Missing Properties devicebroker.getConfig.path', null, null, null);
|
|
@@ -667,6 +703,13 @@ class BrokerHandler {
|
|
|
667
703
|
const origin = `${this.myid}-${meth}`;
|
|
668
704
|
log.trace(origin);
|
|
669
705
|
|
|
706
|
+
// verify if enabled is set to true in properties, if it doesn't exist assume it's true
|
|
707
|
+
if (this.allProps.devicebroker.enabled === false) {
|
|
708
|
+
const errorObj = this.requestHandlerInst.formatErrorObject(this.myid, meth, 'Device broker is not enabled in deviceBroker properties', null, null, null, null);
|
|
709
|
+
log.info(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
710
|
+
return callback(null, errorObj);
|
|
711
|
+
}
|
|
712
|
+
|
|
670
713
|
// if caching get data from the cache
|
|
671
714
|
if (this.allProps.cache.enabled) { // a bit redundant but just in case errors in cacheHandler
|
|
672
715
|
let entityTypeCached = false;
|
package/lib/genericHandler.js
CHANGED
|
@@ -227,7 +227,7 @@ class GenericHandler {
|
|
|
227
227
|
}
|
|
228
228
|
|
|
229
229
|
// Does not support AWS adapters due to the auth call above
|
|
230
|
-
if (metadata && metadata.pagination) {
|
|
230
|
+
if (metadata && metadata.pagination && metadata.pagination.requestLocation && metadata.pagination.limitVar && metadata.pagination.offsetVar && metadata.pagination.incrementBy) {
|
|
231
231
|
return this.expandedGenericAdapterRequestPaginated(metadata.pagination, action, reqObj, returnF, meth, (returnData, returnError) => {
|
|
232
232
|
if (returnError) {
|
|
233
233
|
/* HERE IS WHERE YOU CAN ALTER THE ERROR MESSAGE */
|
package/package.json
CHANGED
|
Binary file
|