@iobroker/socket-classes 0.4.10 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -0
- package/lib/socketCommands.js +377 -158
- package/lib/socketCommandsAdmin.js +326 -251
- package/package.json +1 -1
|
@@ -63,42 +63,6 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
63
63
|
this.onThresholdChanged = onThresholdChanged;
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
_fixAdminUI(obj) {
|
|
67
|
-
if (obj && obj.common && !obj.common.adminUI) {
|
|
68
|
-
if (obj.common.noConfig) {
|
|
69
|
-
obj.common.adminUI = obj.common.adminUI || {};
|
|
70
|
-
obj.common.adminUI.config = 'none';
|
|
71
|
-
} else if (obj.common.jsonConfig) {
|
|
72
|
-
obj.common.adminUI = obj.common.adminUI || {};
|
|
73
|
-
obj.common.adminUI.config = 'json';
|
|
74
|
-
} else if (obj.common.materialize) {
|
|
75
|
-
obj.common.adminUI = obj.common.adminUI || {};
|
|
76
|
-
obj.common.adminUI.config = 'materialize';
|
|
77
|
-
} else {
|
|
78
|
-
obj.common.adminUI = obj.common.adminUI || {};
|
|
79
|
-
obj.common.adminUI.config = 'html';
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (obj.common.jsonCustom) {
|
|
83
|
-
obj.common.adminUI = obj.common.adminUI || {};
|
|
84
|
-
obj.common.adminUI.custom = 'json';
|
|
85
|
-
} else if (obj.common.supportCustoms) {
|
|
86
|
-
obj.common.adminUI = obj.common.adminUI || {};
|
|
87
|
-
obj.common.adminUI.custom = 'json';
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
if (obj.common.materializeTab && obj.common.adminTab) {
|
|
91
|
-
obj.common.adminUI = obj.common.adminUI || {};
|
|
92
|
-
obj.common.adminUI.tab = 'materialize';
|
|
93
|
-
} else if (obj.common.adminTab) {
|
|
94
|
-
obj.common.adminUI = obj.common.adminUI || {};
|
|
95
|
-
obj.common.adminUI.tab = 'html';
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
obj.common.adminUI && this.adapter.log.debug(`Please add to "${obj._id.replace(/\.\d+$/, '')}" common.adminUI=${JSON.stringify(obj.common.adminUI)}`);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
66
|
async updateRatings(uuid) {
|
|
103
67
|
if (!uuid) {
|
|
104
68
|
const obj = await this.adapter.getForeignObjectAsync('system.meta.uuid');
|
|
@@ -131,7 +95,7 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
131
95
|
|
|
132
96
|
_readInstanceConfig(id, user, isTab, configs) {
|
|
133
97
|
return new Promise(resolve =>
|
|
134
|
-
this.adapter.getForeignObject('system.adapter.' + id, {user}, (
|
|
98
|
+
this.adapter.getForeignObject('system.adapter.' + id, {user}, (error, obj) => {
|
|
135
99
|
if (obj && obj.common) {
|
|
136
100
|
const instance = id.split('.').pop();
|
|
137
101
|
const config = {
|
|
@@ -172,25 +136,30 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
172
136
|
}
|
|
173
137
|
}
|
|
174
138
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
139
|
+
try {
|
|
140
|
+
this.adapter.sendToHost(host, command, message, res => {
|
|
141
|
+
if (!message && SocketCommandsAdmin.ALLOW_CACHE.includes(command)) {
|
|
142
|
+
this.cache[hash] = {ts: Date.now(), res: JSON.stringify(res)};
|
|
143
|
+
|
|
144
|
+
this.cacheGB = this.cacheGB || setInterval(() => {
|
|
145
|
+
const commands = Object.keys(this.cache);
|
|
146
|
+
commands.forEach(cmd => {
|
|
147
|
+
if (Date.now() - this.cache[cmd].ts > 500) {
|
|
148
|
+
delete this.cache[cmd];
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
if (!commands.length) {
|
|
152
|
+
clearInterval(this.cacheGB);
|
|
153
|
+
this.cacheGB = null;
|
|
184
154
|
}
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
});
|
|
155
|
+
}, 2000);
|
|
156
|
+
}
|
|
157
|
+
typeof callback === 'function' && setImmediate(() => callback(res));
|
|
158
|
+
});
|
|
159
|
+
} catch (error) {
|
|
160
|
+
this.adapter.log.error('[sendToHost] ERROR: ' + error.toString());
|
|
161
|
+
typeof callback === 'function' && setImmediate(() => callback({ error }));
|
|
162
|
+
}
|
|
194
163
|
}
|
|
195
164
|
|
|
196
165
|
// remove this function when js.controller 4.x will be mainstream
|
|
@@ -209,13 +178,13 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
209
178
|
}
|
|
210
179
|
return response.data;
|
|
211
180
|
})
|
|
212
|
-
.catch(
|
|
213
|
-
if (
|
|
214
|
-
throw new Error((
|
|
215
|
-
} else if (
|
|
181
|
+
.catch(error => {
|
|
182
|
+
if (error.response) {
|
|
183
|
+
throw new Error((error.response.data && error.response.data.error) || error.response.data || error.response.status);
|
|
184
|
+
} else if (error.request) {
|
|
216
185
|
throw new Error('no response');
|
|
217
186
|
} else {
|
|
218
|
-
throw
|
|
187
|
+
throw error;
|
|
219
188
|
}
|
|
220
189
|
});
|
|
221
190
|
}
|
|
@@ -265,22 +234,22 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
265
234
|
return this.adapter.setForeignObjectAsync('system.licenses', systemLicenses)
|
|
266
235
|
.then(() => licenses);
|
|
267
236
|
})
|
|
268
|
-
.catch(
|
|
237
|
+
.catch(error => {
|
|
269
238
|
// if password is invalid
|
|
270
|
-
if (
|
|
239
|
+
if (error.message.includes('Authentication required') || error.message.includes('Cannot decode password')) {
|
|
271
240
|
// clear existing licenses if exist
|
|
272
241
|
if (systemLicenses && systemLicenses.native && systemLicenses.native.licenses && systemLicenses.native.licenses.length) {
|
|
273
242
|
systemLicenses.native.licenses = [];
|
|
274
243
|
systemLicenses.native.readTime = new Date().toISOString();
|
|
275
244
|
return this.adapter.setForeignObjectAsync('system.licenses', systemLicenses)
|
|
276
245
|
.then(() => {
|
|
277
|
-
throw
|
|
246
|
+
throw error;
|
|
278
247
|
});
|
|
279
248
|
} else {
|
|
280
|
-
throw
|
|
249
|
+
throw error;
|
|
281
250
|
}
|
|
282
251
|
} else {
|
|
283
|
-
throw
|
|
252
|
+
throw error;
|
|
284
253
|
}
|
|
285
254
|
});
|
|
286
255
|
} else {
|
|
@@ -311,7 +280,7 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
311
280
|
setTimeout(() => {
|
|
312
281
|
/*
|
|
313
282
|
if (readAll) {
|
|
314
|
-
this.adapter.getForeignStates('*', (
|
|
283
|
+
this.adapter.getForeignStates('*', (error, res) => {
|
|
315
284
|
this.adapter.log.info('received all states');
|
|
316
285
|
for (const id in res) {
|
|
317
286
|
if (res.hasOwnProperty(id) && JSON.stringify(states[id]) !== JSON.stringify(res[id])) {
|
|
@@ -358,50 +327,66 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
358
327
|
}
|
|
359
328
|
|
|
360
329
|
if (!user.match(/^[-.A-Za-züäößÖÄÜа-яА-Я@+$§0-9=?!&# ]+$/)) {
|
|
361
|
-
|
|
362
|
-
callback('Invalid characters in the name. Only following special characters are allowed: -@+$§=?!&# and letters');
|
|
363
|
-
}
|
|
364
|
-
return;
|
|
330
|
+
return SocketCommands._fixCallback(callback, 'Invalid characters in the name. Only following special characters are allowed: -@+$§=?!&# and letters');
|
|
365
331
|
}
|
|
366
332
|
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
if (
|
|
370
|
-
callback
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
333
|
+
try {
|
|
334
|
+
this.adapter.getForeignObject('system.user.' + user, options, (error, obj) => {
|
|
335
|
+
if (obj) {
|
|
336
|
+
SocketCommands._fixCallback(callback, 'User yet exists');
|
|
337
|
+
} else {
|
|
338
|
+
try {
|
|
339
|
+
this.adapter.setForeignObject('system.user.' + user, {
|
|
340
|
+
type: 'user',
|
|
341
|
+
common: {
|
|
342
|
+
name: user,
|
|
343
|
+
enabled: true,
|
|
344
|
+
groups: []
|
|
345
|
+
}
|
|
346
|
+
}, options, () => {
|
|
347
|
+
try {
|
|
348
|
+
this.adapter.setPassword(user, pw, options, callback);
|
|
349
|
+
} catch (error) {
|
|
350
|
+
this.adapter.log.error('[_addUser] cannot set password: ' + error.toString());
|
|
351
|
+
SocketCommands._fixCallback(callback, error);
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
} catch (error) {
|
|
355
|
+
this.adapter.log.error('[_addUser] cannot save user: ' + error.toString());
|
|
356
|
+
SocketCommands._fixCallback(callback, error);
|
|
379
357
|
}
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
} catch (error) {
|
|
361
|
+
this.adapter.log.error('[_addUser] cannot read user: ' + error.toString());
|
|
362
|
+
SocketCommands._fixCallback(callback, error);
|
|
363
|
+
}
|
|
385
364
|
}
|
|
386
365
|
|
|
387
366
|
_delUser(user, options, callback) {
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
if (
|
|
391
|
-
callback
|
|
392
|
-
}
|
|
393
|
-
} else {
|
|
394
|
-
if (obj.common.dontDelete) {
|
|
395
|
-
if (typeof callback === 'function') {
|
|
396
|
-
callback('Cannot delete user, while is system user');
|
|
397
|
-
}
|
|
367
|
+
try {
|
|
368
|
+
this.adapter.getForeignObject('system.user.' + user, options, (error, obj) => {
|
|
369
|
+
if (error || !obj) {
|
|
370
|
+
SocketCommands._fixCallback(callback, 'User does not exist');
|
|
398
371
|
} else {
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
372
|
+
if (obj.common.dontDelete) {
|
|
373
|
+
SocketCommands._fixCallback(callback, 'Cannot delete user, while is system user');
|
|
374
|
+
} else {
|
|
375
|
+
try {
|
|
376
|
+
this.adapter.delForeignObject('system.user.' + user, options, error =>
|
|
377
|
+
// Remove this user from all groups in web client
|
|
378
|
+
SocketCommands._fixCallback(callback, error));
|
|
379
|
+
} catch (error) {
|
|
380
|
+
this.adapter.log.error('[_delUser] cannot delete user: ' + error.toString());
|
|
381
|
+
SocketCommands._fixCallback(callback, error);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
402
384
|
}
|
|
403
|
-
}
|
|
404
|
-
})
|
|
385
|
+
});
|
|
386
|
+
} catch (error) {
|
|
387
|
+
this.adapter.log.error('[_delUser] cannot read user: ' + error.toString());
|
|
388
|
+
SocketCommands._fixCallback(callback, error);
|
|
389
|
+
}
|
|
405
390
|
}
|
|
406
391
|
|
|
407
392
|
_addGroup(group, desc, acl, options, callback) {
|
|
@@ -424,44 +409,63 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
424
409
|
group = group.substring(0, 1).toLowerCase() + group.substring(1);
|
|
425
410
|
|
|
426
411
|
if (!group.match(/^[-.A-Za-züäößÖÄÜа-яА-Я@+$§0-9=?!&#_ ]+$/)) {
|
|
427
|
-
return
|
|
412
|
+
return SocketCommands._fixCallback(callback, 'Invalid characters in the group name. Only following special characters are allowed: -@+$§=?!&# and letters');
|
|
428
413
|
}
|
|
429
414
|
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
415
|
+
try {
|
|
416
|
+
this.adapter.getForeignObject('system.group.' + group, options, (error, obj) => {
|
|
417
|
+
if (obj) {
|
|
418
|
+
SocketCommands._fixCallback(callback, 'Group yet exists');
|
|
419
|
+
} else {
|
|
420
|
+
obj = {
|
|
421
|
+
_id: 'system.group.' + group,
|
|
422
|
+
type: 'group',
|
|
423
|
+
common: {
|
|
424
|
+
name,
|
|
425
|
+
desc,
|
|
426
|
+
members: [],
|
|
427
|
+
acl
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
try {
|
|
431
|
+
this.adapter.setForeignObject('system.group.' + group, obj, options, error =>
|
|
432
|
+
SocketCommands._fixCallback(callback, error, obj));
|
|
433
|
+
} catch (error) {
|
|
434
|
+
this.adapter.log.error('[_addGroup] cannot write group: ' + error.toString());
|
|
435
|
+
SocketCommands._fixCallback(callback, error);
|
|
442
436
|
}
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
437
|
+
}
|
|
438
|
+
});
|
|
439
|
+
} catch (error) {
|
|
440
|
+
this.adapter.log.error('[_addGroup] cannot read group: ' + error.toString());
|
|
441
|
+
SocketCommands._fixCallback(callback, error);
|
|
442
|
+
}
|
|
448
443
|
}
|
|
449
444
|
|
|
450
445
|
_delGroup(group, options, callback) {
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
if (obj.common.dontDelete) {
|
|
456
|
-
typeof callback === 'function' && callback('Cannot delete group, while is system group');
|
|
446
|
+
try {
|
|
447
|
+
this.adapter.getForeignObject('system.group.' + group, options, (error, obj) => {
|
|
448
|
+
if (error || !obj) {
|
|
449
|
+
SocketCommands._fixCallback(callback, 'Group does not exist');
|
|
457
450
|
} else {
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
451
|
+
if (obj.common.dontDelete) {
|
|
452
|
+
SocketCommands._fixCallback(callback, 'Cannot delete group, while is system group');
|
|
453
|
+
} else {
|
|
454
|
+
try {
|
|
455
|
+
this.adapter.delForeignObject('system.group.' + group, options, error =>
|
|
456
|
+
// Remove this group from all users in web client
|
|
457
|
+
SocketCommands._fixCallback(callback, error));
|
|
458
|
+
} catch (error) {
|
|
459
|
+
this.adapter.log.error('[_delGroup] cannot delete group: ' + error.toString());
|
|
460
|
+
SocketCommands._fixCallback(callback, error);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
462
463
|
}
|
|
463
|
-
}
|
|
464
|
-
})
|
|
464
|
+
});
|
|
465
|
+
} catch (error) {
|
|
466
|
+
this.adapter.log.error('[_delGroup] cannot read group: ' + error.toString());
|
|
467
|
+
SocketCommands._fixCallback(callback, error);
|
|
468
|
+
}
|
|
465
469
|
}
|
|
466
470
|
|
|
467
471
|
static _checkObject(obj, options, flag) {
|
|
@@ -515,27 +519,32 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
515
519
|
callback(null, this.objects);
|
|
516
520
|
}
|
|
517
521
|
} else {
|
|
518
|
-
|
|
519
|
-
this.adapter.
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
socket._acl
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
522
|
+
try {
|
|
523
|
+
this.adapter.getObjectList({include_docs: true}, (error, res) => {
|
|
524
|
+
this.adapter.log.info('received all objects');
|
|
525
|
+
res = res.rows;
|
|
526
|
+
const objects = {};
|
|
527
|
+
|
|
528
|
+
if (socket._acl &&
|
|
529
|
+
socket._acl.user !== 'system.user.admin' &&
|
|
530
|
+
!socket._acl.groups.includes('system.group.administrator')) {
|
|
531
|
+
for (let i = 0; i < res.length; i++) {
|
|
532
|
+
if (SocketCommandsAdmin._checkObject(res[i].doc, socket._acl, 4 /* 'read' */)) {
|
|
533
|
+
objects[res[i].doc._id] = res[i].doc;
|
|
534
|
+
}
|
|
529
535
|
}
|
|
536
|
+
callback(null, objects);
|
|
537
|
+
} else {
|
|
538
|
+
for (let j = 0; j < res.length; j++) {
|
|
539
|
+
objects[res[j].doc._id] = res[j].doc;
|
|
540
|
+
}
|
|
541
|
+
callback(null, objects);
|
|
530
542
|
}
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
callback(null, objects);
|
|
537
|
-
}
|
|
538
|
-
});
|
|
543
|
+
});
|
|
544
|
+
} catch (error) {
|
|
545
|
+
this.adapter.log.error('[_getAllObjects] ERROR: ' + error.toString());
|
|
546
|
+
SocketCommands._fixCallback(callback, error);
|
|
547
|
+
}
|
|
539
548
|
}
|
|
540
549
|
}
|
|
541
550
|
}
|
|
@@ -543,35 +552,40 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
543
552
|
_initCommandsUser() {
|
|
544
553
|
this.commands['addUser'] = (socket, user, pass, callback) => {
|
|
545
554
|
if (this._checkPermissions(socket, 'addUser', callback, user)) {
|
|
546
|
-
this._addUser(user, pass, {user: socket._acl.user}, (
|
|
547
|
-
SocketCommands._fixCallback(callback,
|
|
555
|
+
this._addUser(user, pass, {user: socket._acl.user}, (error, ...args) =>
|
|
556
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
548
557
|
}
|
|
549
558
|
};
|
|
550
559
|
|
|
551
560
|
this.commands['delUser'] = (socket, user, callback) => {
|
|
552
561
|
if (this._checkPermissions(socket, 'delUser', callback, user)) {
|
|
553
|
-
this._delUser(user, {user: socket._acl.user}, (
|
|
554
|
-
SocketCommands._fixCallback(callback,
|
|
562
|
+
this._delUser(user, {user: socket._acl.user}, (error, ...args) =>
|
|
563
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
555
564
|
}
|
|
556
565
|
};
|
|
557
566
|
|
|
558
567
|
this.commands['addGroup'] = (socket, group, desc, acl, callback) => {
|
|
559
568
|
if (this._checkPermissions(socket, 'addGroup', callback, group)) {
|
|
560
|
-
this._addGroup(group, desc, acl, {user: socket._acl.user}, (
|
|
561
|
-
SocketCommands._fixCallback(callback,
|
|
569
|
+
this._addGroup(group, desc, acl, {user: socket._acl.user}, (error, ...args) =>
|
|
570
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
562
571
|
}
|
|
563
572
|
};
|
|
564
573
|
|
|
565
574
|
this.commands['delGroup'] = (socket, group, callback) => {
|
|
566
575
|
if (this._checkPermissions(socket, 'delGroup', callback, group)) {
|
|
567
|
-
this._delGroup(group, {user: socket._acl.user}, (
|
|
568
|
-
SocketCommands._fixCallback(callback,
|
|
576
|
+
this._delGroup(group, {user: socket._acl.user}, (error, ...args) =>
|
|
577
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
569
578
|
}
|
|
570
579
|
};
|
|
571
580
|
this.commands['changePassword'] = (socket, user, pass, callback) => {
|
|
572
581
|
if (user === socket._acl.user || this._checkPermissions(socket, 'changePassword', callback, user)) {
|
|
573
|
-
|
|
574
|
-
|
|
582
|
+
try {
|
|
583
|
+
this.adapter.setPassword(user, pass, {user: socket._acl.user}, (error, ...args) =>
|
|
584
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
585
|
+
} catch (error) {
|
|
586
|
+
this.adapter.log.error('[_changePassword] ERROR: ' + error.toString());
|
|
587
|
+
SocketCommands._fixCallback(callback, error);
|
|
588
|
+
}
|
|
575
589
|
}
|
|
576
590
|
};
|
|
577
591
|
}
|
|
@@ -582,33 +596,38 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
582
596
|
return this.adapter.log.warn('[getHostByIp] Invalid callback');
|
|
583
597
|
}
|
|
584
598
|
if (this._checkPermissions(socket, 'getHostByIp', ip)) {
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
599
|
+
try {
|
|
600
|
+
this.adapter.getObjectView('system', 'host', {}, {user: socket._acl.user}, (error, data) => {
|
|
601
|
+
if (data && data.rows && data.rows.length) {
|
|
602
|
+
for (let i = 0; i < data.rows.length; i++) {
|
|
603
|
+
const obj = data.rows[i].value;
|
|
604
|
+
// if we requested specific name
|
|
605
|
+
if (obj.common.hostname === ip) {
|
|
606
|
+
return callback(ip, obj);
|
|
607
|
+
} else
|
|
608
|
+
// try to find this IP in the list
|
|
609
|
+
if (obj.native.hardware && obj.native.hardware.networkInterfaces) {
|
|
610
|
+
const net = obj.native.hardware.networkInterfaces;
|
|
611
|
+
for (const eth in net) {
|
|
612
|
+
if (!Object.prototype.hasOwnProperty.call(net, eth)) {
|
|
613
|
+
continue;
|
|
614
|
+
}
|
|
615
|
+
for (let j = 0; j < net[eth].length; j++) {
|
|
616
|
+
if (net[eth][j].address === ip) {
|
|
617
|
+
return callback(ip, obj);
|
|
618
|
+
}
|
|
603
619
|
}
|
|
604
620
|
}
|
|
605
621
|
}
|
|
606
622
|
}
|
|
607
623
|
}
|
|
608
|
-
}
|
|
609
624
|
|
|
610
|
-
|
|
611
|
-
|
|
625
|
+
callback(ip, null);
|
|
626
|
+
});
|
|
627
|
+
} catch (error) {
|
|
628
|
+
this.adapter.log.error('[_changePassword] ERROR: ' + error.toString());
|
|
629
|
+
SocketCommands._fixCallback(callback, error);
|
|
630
|
+
}
|
|
612
631
|
}
|
|
613
632
|
};
|
|
614
633
|
|
|
@@ -665,7 +684,7 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
665
684
|
});
|
|
666
685
|
}
|
|
667
686
|
}
|
|
668
|
-
} catch (
|
|
687
|
+
} catch (_error) {
|
|
669
688
|
// push unchecked
|
|
670
689
|
// result.list.push('log/' + transport + '/' + files[f]);
|
|
671
690
|
this.adapter.log.error(`Cannot check file: ${fileName}/${files[f]}`);
|
|
@@ -677,18 +696,18 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
677
696
|
} else {
|
|
678
697
|
result = {error: 'no file loggers'};
|
|
679
698
|
}
|
|
680
|
-
} catch (
|
|
681
|
-
this.adapter.log.error(
|
|
682
|
-
result = {error
|
|
699
|
+
} catch (error) {
|
|
700
|
+
this.adapter.log.error('Cannot read logs: ' + error);
|
|
701
|
+
result = {error};
|
|
683
702
|
}
|
|
684
|
-
|
|
703
|
+
SocketCommands._fixCallback(callback, result.error, result.list);
|
|
685
704
|
}
|
|
686
705
|
}, 500);
|
|
687
706
|
|
|
688
707
|
this._sendToHost(host, 'getLogFiles', null, result => {
|
|
689
708
|
clearTimeout(timeout);
|
|
690
709
|
timeout = null;
|
|
691
|
-
|
|
710
|
+
SocketCommands._fixCallback(callback, result.error, result.list);
|
|
692
711
|
});
|
|
693
712
|
}
|
|
694
713
|
};
|
|
@@ -699,8 +718,13 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
699
718
|
if (this.states && this.states[id]) {
|
|
700
719
|
delete this.states[id];
|
|
701
720
|
}
|
|
702
|
-
|
|
703
|
-
|
|
721
|
+
try {
|
|
722
|
+
this.adapter.delForeignState(id, {user: socket._acl.user}, (error, ...args) =>
|
|
723
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
724
|
+
} catch (error) {
|
|
725
|
+
this.adapter.log.error('[delState] ERROR: ' + error.toString());
|
|
726
|
+
SocketCommands._fixCallback(callback, error);
|
|
727
|
+
}
|
|
704
728
|
}
|
|
705
729
|
};
|
|
706
730
|
|
|
@@ -711,7 +735,12 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
711
735
|
this.adapter.log.debug(`cmdExec on ${host}(${id}): ${cmd}`);
|
|
712
736
|
// remember socket for this ID.
|
|
713
737
|
this.cmdSessions[id] = {socket};
|
|
714
|
-
|
|
738
|
+
try {
|
|
739
|
+
this.adapter.sendToHost(host, 'cmdExec', {data: cmd, id});
|
|
740
|
+
} catch (error) {
|
|
741
|
+
this.adapter.log.error('[cmdExec] ERROR: ' + error.toString());
|
|
742
|
+
SocketCommands._fixCallback(callback, error);
|
|
743
|
+
}
|
|
715
744
|
}
|
|
716
745
|
};
|
|
717
746
|
|
|
@@ -726,54 +755,65 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
726
755
|
this.commands['getRatings'] = (socket, update, callback) => {
|
|
727
756
|
if (update || !this.adapter._ratings) {
|
|
728
757
|
this.updateRatings()
|
|
729
|
-
.then(() =>
|
|
758
|
+
.then(() => SocketCommands._fixCallback(callback, null, this.adapter._ratings));
|
|
730
759
|
} else {
|
|
731
|
-
|
|
760
|
+
SocketCommands._fixCallback(callback, null, this.adapter._ratings);
|
|
732
761
|
}
|
|
733
762
|
};
|
|
734
763
|
|
|
735
764
|
this.commands['getCurrentInstance'] = (socket, callback) => {
|
|
736
|
-
|
|
765
|
+
SocketCommands._fixCallback(callback, null, this.adapter.namespace);
|
|
737
766
|
};
|
|
738
767
|
|
|
739
768
|
this.commands['checkFeatureSupported'] = (socket, feature, callback) => {
|
|
740
|
-
|
|
769
|
+
SocketCommands._fixCallback(callback, null, this.adapter.supportsFeature && this.adapter.supportsFeature(feature));
|
|
741
770
|
};
|
|
742
771
|
|
|
743
772
|
this.commands['decrypt'] = (socket, encryptedText, callback) => {
|
|
744
773
|
if (this.secret) {
|
|
745
|
-
|
|
774
|
+
SocketCommands._fixCallback(callback, null, this.adapter.decrypt(this.secret, encryptedText));
|
|
746
775
|
} else {
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
776
|
+
try {
|
|
777
|
+
this.adapter.getForeignObject('system.config', (error, obj) => {
|
|
778
|
+
if (obj && obj.native && obj.native.secret) {
|
|
779
|
+
this.secret = obj.native.secret;
|
|
780
|
+
SocketCommands._fixCallback(callback, null, this.adapter.decrypt(this.secret, encryptedText));
|
|
781
|
+
} else {
|
|
782
|
+
this.adapter.log.error(`No system.config found: ${error}`);
|
|
783
|
+
SocketCommands._fixCallback(callback, error);
|
|
784
|
+
}
|
|
785
|
+
});
|
|
786
|
+
} catch (error) {
|
|
787
|
+
this.adapter.log.error(`Cannot decrypt: ${error}`);
|
|
788
|
+
SocketCommands._fixCallback(callback, error);
|
|
789
|
+
}
|
|
756
790
|
}
|
|
757
791
|
};
|
|
758
792
|
|
|
759
793
|
this.commands['encrypt'] = (socket, plainText, callback) => {
|
|
760
794
|
if (this.secret) {
|
|
761
|
-
|
|
795
|
+
SocketCommands._fixCallback(callback, null, this.adapter.encrypt(this.secret, plainText));
|
|
762
796
|
} else {
|
|
763
|
-
this.adapter.getForeignObject('system.config', (
|
|
797
|
+
this.adapter.getForeignObject('system.config', (error, obj) => {
|
|
764
798
|
if (obj && obj.native && obj.native.secret) {
|
|
765
799
|
this.secret = obj.native.secret;
|
|
766
|
-
|
|
800
|
+
try {
|
|
801
|
+
const encrypted = this.adapter.encrypt(this.secret, plainText);
|
|
802
|
+
SocketCommands._fixCallback(callback, null, encrypted);
|
|
803
|
+
} catch (error) {
|
|
804
|
+
this.adapter.log.error(`Cannot encrypt: ${error}`);
|
|
805
|
+
SocketCommands._fixCallback(callback, error);
|
|
806
|
+
}
|
|
767
807
|
} else {
|
|
768
|
-
this.adapter.log.error(`No system.config found: ${
|
|
769
|
-
SocketCommands._fixCallback(callback,
|
|
808
|
+
this.adapter.log.error(`No system.config found: ${error}`);
|
|
809
|
+
SocketCommands._fixCallback(callback, error);
|
|
770
810
|
}
|
|
771
811
|
});
|
|
772
812
|
}
|
|
773
813
|
};
|
|
774
814
|
|
|
775
815
|
this.commands['getIsEasyModeStrict'] = (socket, callback) => {
|
|
776
|
-
|
|
816
|
+
SocketCommands._fixCallback(callback, null, this.adapter.config.accessLimit);
|
|
777
817
|
};
|
|
778
818
|
|
|
779
819
|
this.commands['getEasyMode'] = (socket, callback) => {
|
|
@@ -802,10 +842,10 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
802
842
|
});
|
|
803
843
|
});
|
|
804
844
|
} else {
|
|
805
|
-
this.adapter.getObjectView('system', 'instance', {startkey: 'system.adapter.', endkey: 'system.adapter.\u9999'}, {user}, (
|
|
845
|
+
this.adapter.getObjectView('system', 'instance', {startkey: 'system.adapter.', endkey: 'system.adapter.\u9999'}, {user}, (error, doc) => {
|
|
806
846
|
const promises = [];
|
|
807
847
|
const configs = [];
|
|
808
|
-
if (!
|
|
848
|
+
if (!error && doc.rows.length) {
|
|
809
849
|
for (let i = 0; i < doc.rows.length; i++) {
|
|
810
850
|
const obj = doc.rows[i].value;
|
|
811
851
|
if (obj.common.noConfig && !obj.common.adminTab) {
|
|
@@ -835,10 +875,10 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
835
875
|
if (typeof callback === 'function' && this._checkPermissions(socket, 'getObject', callback)) {
|
|
836
876
|
this.adapter.getObjectView('system', 'adapter',
|
|
837
877
|
{startkey: `system.adapter.${adapterName || ''}`, endkey: `system.adapter.${adapterName || '\u9999'}`},
|
|
838
|
-
(
|
|
878
|
+
(error, doc) =>
|
|
839
879
|
{
|
|
840
|
-
if (
|
|
841
|
-
callback(
|
|
880
|
+
if (error) {
|
|
881
|
+
callback(error);
|
|
842
882
|
} else {
|
|
843
883
|
callback(null, doc.rows
|
|
844
884
|
.filter(obj => obj && (!adapterName || (obj.common && obj.common.name === this.adapterName)))
|
|
@@ -862,7 +902,7 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
862
902
|
let timeout = setTimeout(() => {
|
|
863
903
|
if (timeout) {
|
|
864
904
|
timeout = null;
|
|
865
|
-
|
|
905
|
+
SocketCommands._fixCallback(callback, 'updateLicenses timeout');
|
|
866
906
|
}
|
|
867
907
|
}, 7000);
|
|
868
908
|
|
|
@@ -870,14 +910,14 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
870
910
|
if (timeout) {
|
|
871
911
|
clearTimeout(timeout);
|
|
872
912
|
timeout = null;
|
|
873
|
-
|
|
913
|
+
SocketCommands._fixCallback(callback, result.error, result && result.result);
|
|
874
914
|
}
|
|
875
915
|
});
|
|
876
916
|
} else {
|
|
877
917
|
// remove this branch when js-controller 4.x will be mainstream
|
|
878
918
|
this._updateLicenses(login, password)
|
|
879
|
-
.then(licenses =>
|
|
880
|
-
.catch(
|
|
919
|
+
.then(licenses => SocketCommands._fixCallback(callback, null, licenses))
|
|
920
|
+
.catch(error => SocketCommands._fixCallback(callback, error));
|
|
881
921
|
}
|
|
882
922
|
}
|
|
883
923
|
};
|
|
@@ -887,9 +927,9 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
887
927
|
if (this._checkPermissions(socket, 'getObject', callback)) {
|
|
888
928
|
this.adapter.getObjectView('system', 'instance',
|
|
889
929
|
{startkey: `system.adapter.`, endkey: `system.adapter.\u9999`},
|
|
890
|
-
(
|
|
891
|
-
if (
|
|
892
|
-
callback(
|
|
930
|
+
(error, doc) => {
|
|
931
|
+
if (error) {
|
|
932
|
+
callback(error);
|
|
893
933
|
} else {
|
|
894
934
|
// calculate
|
|
895
935
|
const result = {};
|
|
@@ -916,9 +956,9 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
916
956
|
if (this._checkPermissions(socket, 'getObject', callback)) {
|
|
917
957
|
this.adapter.getObjectView('system', 'adapter',
|
|
918
958
|
{startkey: `system.adapter.`, endkey: `system.adapter.\u9999`},
|
|
919
|
-
(
|
|
920
|
-
if (
|
|
921
|
-
callback(
|
|
959
|
+
(error, doc) => {
|
|
960
|
+
if (error) {
|
|
961
|
+
callback(error);
|
|
922
962
|
} else {
|
|
923
963
|
// calculate
|
|
924
964
|
const result = {};
|
|
@@ -954,14 +994,14 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
954
994
|
|
|
955
995
|
this.commands['getCompactSystemConfig'] = (socket, callback) => {
|
|
956
996
|
if (this._checkPermissions(socket, 'getObject', callback)) {
|
|
957
|
-
this.adapter.getForeignObject('system.config', (
|
|
997
|
+
this.adapter.getForeignObject('system.config', (error, obj) => {
|
|
958
998
|
obj = obj || {};
|
|
959
999
|
const secret = obj.native && obj.native.secret;
|
|
960
1000
|
delete obj.native;
|
|
961
1001
|
if (secret) {
|
|
962
1002
|
obj.native = {secret};
|
|
963
1003
|
}
|
|
964
|
-
callback(
|
|
1004
|
+
callback(error, obj);
|
|
965
1005
|
});
|
|
966
1006
|
}
|
|
967
1007
|
};
|
|
@@ -983,9 +1023,9 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
983
1023
|
this.commands['getCompactHosts'] = (socket, callback) => {
|
|
984
1024
|
if (this._checkPermissions(socket, 'getObject', callback)) {
|
|
985
1025
|
this.adapter.getObjectView('system', 'host',
|
|
986
|
-
{startkey: 'system.host.', endkey: 'system.host.\u9999'}, (
|
|
987
|
-
if (
|
|
988
|
-
callback(
|
|
1026
|
+
{startkey: 'system.host.', endkey: 'system.host.\u9999'}, (error, doc) => {
|
|
1027
|
+
if (error) {
|
|
1028
|
+
callback(error);
|
|
989
1029
|
} else {
|
|
990
1030
|
const result = [];
|
|
991
1031
|
doc.rows.map(item => {
|
|
@@ -1028,8 +1068,13 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
1028
1068
|
this.commands['getForeignStates'] = (socket, pattern, callback) => {
|
|
1029
1069
|
if (this._checkPermissions(socket, 'getStates', callback)) {
|
|
1030
1070
|
if (typeof callback === 'function') {
|
|
1031
|
-
|
|
1032
|
-
|
|
1071
|
+
try {
|
|
1072
|
+
this.adapter.getForeignStates(pattern, (error, ...args) =>
|
|
1073
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
1074
|
+
} catch (error) {
|
|
1075
|
+
this.adapter.log.error(`[getForeignStates] ERROR: ${error}`);
|
|
1076
|
+
SocketCommands._fixCallback(callback, error);
|
|
1077
|
+
}
|
|
1033
1078
|
} else {
|
|
1034
1079
|
this.adapter.log.warn('[getForeignStates] Invalid callback')
|
|
1035
1080
|
}
|
|
@@ -1042,9 +1087,14 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
1042
1087
|
options = {};
|
|
1043
1088
|
}
|
|
1044
1089
|
options.user = socket._acl.user;
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1090
|
+
try {
|
|
1091
|
+
// options.recursive = true; // the only difference between delObject and delObjects is this line
|
|
1092
|
+
this.adapter.delForeignObject(id, options, (error, ...args) =>
|
|
1093
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
1094
|
+
} catch (error) {
|
|
1095
|
+
this.adapter.log.error(`[delObject] ERROR: ${error}`);
|
|
1096
|
+
SocketCommands._fixCallback(callback, error);
|
|
1097
|
+
}
|
|
1048
1098
|
}
|
|
1049
1099
|
};
|
|
1050
1100
|
|
|
@@ -1055,8 +1105,13 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
1055
1105
|
}
|
|
1056
1106
|
options.user = socket._acl.user;
|
|
1057
1107
|
options.recursive = true; // the only difference between delObject and delObjects is this line
|
|
1058
|
-
|
|
1059
|
-
|
|
1108
|
+
try {
|
|
1109
|
+
this.adapter.delForeignObject(id, options, (error, ...args) =>
|
|
1110
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
1111
|
+
} catch (error) {
|
|
1112
|
+
this.adapter.log.error(`[delObjects] ERROR: ${error}`);
|
|
1113
|
+
SocketCommands._fixCallback(callback, error);
|
|
1114
|
+
}
|
|
1060
1115
|
}
|
|
1061
1116
|
};
|
|
1062
1117
|
}
|
|
@@ -1072,9 +1127,15 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
1072
1127
|
}
|
|
1073
1128
|
options = options || {};
|
|
1074
1129
|
options.user = socket._acl.user;
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1130
|
+
|
|
1131
|
+
try {
|
|
1132
|
+
const buffer = Buffer.from(data64, 'base64');
|
|
1133
|
+
this.adapter.writeFile(_adapter, fileName, buffer, {user: socket._acl.user}, (error, ...args) =>
|
|
1134
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
1135
|
+
} catch (error) {
|
|
1136
|
+
this.adapter.log.error('[writeFile] Cannot convert data: ' + error.toString());
|
|
1137
|
+
callback && callback('Cannot convert data: ' + error.toString());
|
|
1138
|
+
}
|
|
1078
1139
|
}
|
|
1079
1140
|
};
|
|
1080
1141
|
}
|
|
@@ -1089,8 +1150,13 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
1089
1150
|
|
|
1090
1151
|
this.commands['extendObject'] = (socket, id, obj, callback) => {
|
|
1091
1152
|
if (this._checkPermissions(socket, 'extendObject', callback, id)) {
|
|
1092
|
-
|
|
1093
|
-
|
|
1153
|
+
try {
|
|
1154
|
+
this.adapter.extendForeignObject(id, obj, {user: socket._acl.user}, (error, ...args) =>
|
|
1155
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
1156
|
+
} catch (error) {
|
|
1157
|
+
this.adapter.log.error(`[extendObject] ERROR: ${error}`);
|
|
1158
|
+
SocketCommands._fixCallback(callback, error);
|
|
1159
|
+
}
|
|
1094
1160
|
}
|
|
1095
1161
|
};
|
|
1096
1162
|
|
|
@@ -1101,13 +1167,17 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
1101
1167
|
type = undefined;
|
|
1102
1168
|
}
|
|
1103
1169
|
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1170
|
+
if (typeof callback === 'function') {
|
|
1171
|
+
try {
|
|
1172
|
+
this.adapter.getForeignObjects(pattern, type, {user: socket._acl.user}, (error, ...args) =>
|
|
1173
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
1174
|
+
} catch (error) {
|
|
1175
|
+
this.adapter.log.error(`[extendObject] ERROR: ${error}`);
|
|
1176
|
+
SocketCommands._fixCallback(callback, error);
|
|
1109
1177
|
}
|
|
1110
|
-
}
|
|
1178
|
+
} else {
|
|
1179
|
+
this.adapter.log.warn('[getObjects] Invalid callback');
|
|
1180
|
+
}
|
|
1111
1181
|
}
|
|
1112
1182
|
};
|
|
1113
1183
|
|
|
@@ -1121,8 +1191,13 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
1121
1191
|
options = {};
|
|
1122
1192
|
}
|
|
1123
1193
|
options.user = socket._acl.user;
|
|
1124
|
-
|
|
1125
|
-
|
|
1194
|
+
try {
|
|
1195
|
+
this.adapter.delForeignObject(id, options, (error, ...args) =>
|
|
1196
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
1197
|
+
} catch (error) {
|
|
1198
|
+
this.adapter.log.error(`[delObject] ERROR: ${error}`);
|
|
1199
|
+
SocketCommands._fixCallback(callback, error);
|
|
1200
|
+
}
|
|
1126
1201
|
}
|
|
1127
1202
|
};
|
|
1128
1203
|
}
|