@iobroker/socket-classes 0.4.11 → 0.5.1
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 +361 -154
- package/lib/socketCommandsAdmin.js +341 -253
- 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,27 @@ 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
|
-
const secret = obj.native && obj.native.secret;
|
|
999
|
+
const secret = obj && 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);
|
|
1005
|
+
});
|
|
1006
|
+
}
|
|
1007
|
+
};
|
|
1008
|
+
|
|
1009
|
+
this.commands['getCompactSystemRepositories'] = (socket, callback) => {
|
|
1010
|
+
if (this._checkPermissions(socket, 'getObject', callback)) {
|
|
1011
|
+
this.adapter.getForeignObject('system.repositories', (error, obj) => {
|
|
1012
|
+
obj && obj.native && obj.native.repositories && Object.keys(obj.native.repositories).forEach(name => {
|
|
1013
|
+
if (obj.native.repositories[name].json) {
|
|
1014
|
+
obj.native.repositories[name].json = null;
|
|
1015
|
+
}
|
|
1016
|
+
});
|
|
1017
|
+
callback(error, obj);
|
|
965
1018
|
});
|
|
966
1019
|
}
|
|
967
1020
|
};
|
|
@@ -973,7 +1026,7 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
973
1026
|
const result = {};
|
|
974
1027
|
data && Object.keys(data).forEach(name => result[name] = {
|
|
975
1028
|
version: data[name].version,
|
|
976
|
-
icon: data[name].extIcon
|
|
1029
|
+
icon: data[name].extIcon,
|
|
977
1030
|
});
|
|
978
1031
|
callback(result);
|
|
979
1032
|
});
|
|
@@ -983,9 +1036,9 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
983
1036
|
this.commands['getCompactHosts'] = (socket, callback) => {
|
|
984
1037
|
if (this._checkPermissions(socket, 'getObject', callback)) {
|
|
985
1038
|
this.adapter.getObjectView('system', 'host',
|
|
986
|
-
{startkey: 'system.host.', endkey: 'system.host.\u9999'}, (
|
|
987
|
-
if (
|
|
988
|
-
callback(
|
|
1039
|
+
{startkey: 'system.host.', endkey: 'system.host.\u9999'}, (error, doc) => {
|
|
1040
|
+
if (error) {
|
|
1041
|
+
callback(error);
|
|
989
1042
|
} else {
|
|
990
1043
|
const result = [];
|
|
991
1044
|
doc.rows.map(item => {
|
|
@@ -1028,8 +1081,13 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
1028
1081
|
this.commands['getForeignStates'] = (socket, pattern, callback) => {
|
|
1029
1082
|
if (this._checkPermissions(socket, 'getStates', callback)) {
|
|
1030
1083
|
if (typeof callback === 'function') {
|
|
1031
|
-
|
|
1032
|
-
|
|
1084
|
+
try {
|
|
1085
|
+
this.adapter.getForeignStates(pattern, (error, ...args) =>
|
|
1086
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
1087
|
+
} catch (error) {
|
|
1088
|
+
this.adapter.log.error(`[getForeignStates] ERROR: ${error}`);
|
|
1089
|
+
SocketCommands._fixCallback(callback, error);
|
|
1090
|
+
}
|
|
1033
1091
|
} else {
|
|
1034
1092
|
this.adapter.log.warn('[getForeignStates] Invalid callback')
|
|
1035
1093
|
}
|
|
@@ -1042,9 +1100,14 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
1042
1100
|
options = {};
|
|
1043
1101
|
}
|
|
1044
1102
|
options.user = socket._acl.user;
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1103
|
+
try {
|
|
1104
|
+
// options.recursive = true; // the only difference between delObject and delObjects is this line
|
|
1105
|
+
this.adapter.delForeignObject(id, options, (error, ...args) =>
|
|
1106
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
1107
|
+
} catch (error) {
|
|
1108
|
+
this.adapter.log.error(`[delObject] ERROR: ${error}`);
|
|
1109
|
+
SocketCommands._fixCallback(callback, error);
|
|
1110
|
+
}
|
|
1048
1111
|
}
|
|
1049
1112
|
};
|
|
1050
1113
|
|
|
@@ -1055,8 +1118,13 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
1055
1118
|
}
|
|
1056
1119
|
options.user = socket._acl.user;
|
|
1057
1120
|
options.recursive = true; // the only difference between delObject and delObjects is this line
|
|
1058
|
-
|
|
1059
|
-
|
|
1121
|
+
try {
|
|
1122
|
+
this.adapter.delForeignObject(id, options, (error, ...args) =>
|
|
1123
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
1124
|
+
} catch (error) {
|
|
1125
|
+
this.adapter.log.error(`[delObjects] ERROR: ${error}`);
|
|
1126
|
+
SocketCommands._fixCallback(callback, error);
|
|
1127
|
+
}
|
|
1060
1128
|
}
|
|
1061
1129
|
};
|
|
1062
1130
|
}
|
|
@@ -1072,9 +1140,15 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
1072
1140
|
}
|
|
1073
1141
|
options = options || {};
|
|
1074
1142
|
options.user = socket._acl.user;
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1143
|
+
|
|
1144
|
+
try {
|
|
1145
|
+
const buffer = Buffer.from(data64, 'base64');
|
|
1146
|
+
this.adapter.writeFile(_adapter, fileName, buffer, {user: socket._acl.user}, (error, ...args) =>
|
|
1147
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
1148
|
+
} catch (error) {
|
|
1149
|
+
this.adapter.log.error('[writeFile] Cannot convert data: ' + error.toString());
|
|
1150
|
+
callback && callback('Cannot convert data: ' + error.toString());
|
|
1151
|
+
}
|
|
1078
1152
|
}
|
|
1079
1153
|
};
|
|
1080
1154
|
}
|
|
@@ -1089,8 +1163,13 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
1089
1163
|
|
|
1090
1164
|
this.commands['extendObject'] = (socket, id, obj, callback) => {
|
|
1091
1165
|
if (this._checkPermissions(socket, 'extendObject', callback, id)) {
|
|
1092
|
-
|
|
1093
|
-
|
|
1166
|
+
try {
|
|
1167
|
+
this.adapter.extendForeignObject(id, obj, {user: socket._acl.user}, (error, ...args) =>
|
|
1168
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
1169
|
+
} catch (error) {
|
|
1170
|
+
this.adapter.log.error(`[extendObject] ERROR: ${error}`);
|
|
1171
|
+
SocketCommands._fixCallback(callback, error);
|
|
1172
|
+
}
|
|
1094
1173
|
}
|
|
1095
1174
|
};
|
|
1096
1175
|
|
|
@@ -1101,13 +1180,17 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
1101
1180
|
type = undefined;
|
|
1102
1181
|
}
|
|
1103
1182
|
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1183
|
+
if (typeof callback === 'function') {
|
|
1184
|
+
try {
|
|
1185
|
+
this.adapter.getForeignObjects(pattern, type, {user: socket._acl.user}, (error, ...args) =>
|
|
1186
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
1187
|
+
} catch (error) {
|
|
1188
|
+
this.adapter.log.error(`[extendObject] ERROR: ${error}`);
|
|
1189
|
+
SocketCommands._fixCallback(callback, error);
|
|
1109
1190
|
}
|
|
1110
|
-
}
|
|
1191
|
+
} else {
|
|
1192
|
+
this.adapter.log.warn('[getObjects] Invalid callback');
|
|
1193
|
+
}
|
|
1111
1194
|
}
|
|
1112
1195
|
};
|
|
1113
1196
|
|
|
@@ -1121,8 +1204,13 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
1121
1204
|
options = {};
|
|
1122
1205
|
}
|
|
1123
1206
|
options.user = socket._acl.user;
|
|
1124
|
-
|
|
1125
|
-
|
|
1207
|
+
try {
|
|
1208
|
+
this.adapter.delForeignObject(id, options, (error, ...args) =>
|
|
1209
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
1210
|
+
} catch (error) {
|
|
1211
|
+
this.adapter.log.error(`[delObject] ERROR: ${error}`);
|
|
1212
|
+
SocketCommands._fixCallback(callback, error);
|
|
1213
|
+
}
|
|
1126
1214
|
}
|
|
1127
1215
|
};
|
|
1128
1216
|
}
|