@iobroker/adapter-react-v5 2.1.1 → 2.1.4

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/Connection.js DELETED
@@ -1,3337 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports["default"] = exports.PROGRESS = exports.ERRORS = void 0;
7
-
8
- var _propTypes = _interopRequireDefault(require("prop-types"));
9
-
10
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
11
-
12
- function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
13
-
14
- function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
15
-
16
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
17
-
18
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
19
-
20
- function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
21
-
22
- function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
23
-
24
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
25
-
26
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
27
-
28
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
29
-
30
- /** Possible progress states. */
31
- var PROGRESS = {
32
- /** The socket is connecting. */
33
- CONNECTING: 0,
34
-
35
- /** The socket is successfully connected. */
36
- CONNECTED: 1,
37
-
38
- /** All objects are loaded. */
39
- OBJECTS_LOADED: 2,
40
-
41
- /** The socket is ready for use. */
42
- READY: 3
43
- };
44
- exports.PROGRESS = PROGRESS;
45
- var PERMISSION_ERROR = 'permissionError';
46
- var NOT_CONNECTED = 'notConnectedError';
47
- var TIMEOUT_FOR_ADMIN4 = 1300;
48
- var ERRORS = {
49
- PERMISSION_ERROR: PERMISSION_ERROR,
50
- NOT_CONNECTED: NOT_CONNECTED
51
- };
52
- exports.ERRORS = ERRORS;
53
-
54
- function fixAdminUI(obj) {
55
- if (obj && obj.common && !obj.common.adminUI) {
56
- if (obj.common.noConfig) {
57
- obj.common.adminUI = obj.common.adminUI || {};
58
- obj.common.adminUI.config = 'none';
59
- } else if (obj.common.jsonConfig) {
60
- obj.common.adminUI = obj.common.adminUI || {};
61
- obj.common.adminUI.config = 'json';
62
- } else if (obj.common.materialize) {
63
- obj.common.adminUI = obj.common.adminUI || {};
64
- obj.common.adminUI.config = 'materialize';
65
- } else {
66
- obj.common.adminUI = obj.common.adminUI || {};
67
- obj.common.adminUI.config = 'html';
68
- }
69
-
70
- if (obj.common.jsonCustom) {
71
- obj.common.adminUI = obj.common.adminUI || {};
72
- obj.common.adminUI.custom = 'json';
73
- } else if (obj.common.supportCustoms) {
74
- obj.common.adminUI = obj.common.adminUI || {};
75
- obj.common.adminUI.custom = 'json';
76
- }
77
-
78
- if (obj.common.materializeTab && obj.common.adminTab) {
79
- obj.common.adminUI = obj.common.adminUI || {};
80
- obj.common.adminUI.tab = 'materialize';
81
- } else if (obj.common.adminTab) {
82
- obj.common.adminUI = obj.common.adminUI || {};
83
- obj.common.adminUI.tab = 'html';
84
- }
85
-
86
- obj.common.adminUI && console.debug("Please add to \"".concat(obj._id.replace(/\.\d+$/, ''), "\" common.adminUI=").concat(JSON.stringify(obj.common.adminUI)));
87
- }
88
-
89
- return obj;
90
- }
91
-
92
- var Connection = /*#__PURE__*/function () {
93
- /**
94
- * @param {import('./types').ConnectionProps} props
95
- */
96
- function Connection(props) {
97
- var _this = this;
98
-
99
- _classCallCheck(this, Connection);
100
-
101
- props = props || {
102
- protocol: window.location.protocol,
103
- host: window.location.hostname
104
- };
105
- this.props = props;
106
- this.autoSubscribes = this.props.autoSubscribes || [];
107
- this.autoSubscribeLog = this.props.autoSubscribeLog;
108
- this.props.protocol = this.props.protocol || window.location.protocol;
109
- this.props.host = this.props.host || window.location.hostname;
110
- this.props.port = this.props.port || (window.location.port === '3000' ? Connection.isWeb() ? 8082 : 8081 : window.location.port);
111
- this.props.ioTimeout = Math.max(this.props.ioTimeout || 20000, 20000);
112
- this.props.cmdTimeout = Math.max(this.props.cmdTimeout || 5000, 5000); // breaking change. Do not load all objects by default is true
113
-
114
- this.doNotLoadAllObjects = this.props.doNotLoadAllObjects === undefined ? true : this.props.doNotLoadAllObjects;
115
- this.doNotLoadACL = this.props.doNotLoadACL === undefined ? true : this.props.doNotLoadACL;
116
- /** @type {Record<string, ioBroker.State>} */
117
-
118
- this.states = {};
119
- this.objects = null;
120
- this.acl = null;
121
- this.firstConnect = true;
122
- this.waitForRestart = false;
123
- /** @type {ioBroker.Languages} */
124
-
125
- this.systemLang = 'en';
126
- this.connected = false;
127
- this._waitForFirstConnection = new Promise(function (resolve) {
128
- _this._waitForFirstConnectionResolve = resolve;
129
- });
130
- /** @type {Record<string, { reg: RegExp; cbs: ioBroker.StateChangeHandler[]}>} */
131
-
132
- this.statesSubscribes = {}; // subscribe for states
133
-
134
- /** @type {Record<string, { reg: RegExp; cbs: import('./types').ObjectChangeHandler[]}>} */
135
-
136
- this.objectsSubscribes = {}; // subscribe for objects
137
-
138
- this.onProgress = this.props.onProgress || function () {};
139
-
140
- this.onError = this.props.onError || function (err) {
141
- console.error(err);
142
- };
143
-
144
- this.loaded = false;
145
- this.loadTimer = null;
146
- this.loadCounter = 0;
147
- this.admin5only = this.props.admin5only || false;
148
- /** @type {((connected: boolean) => void)[]} */
149
-
150
- this.onConnectionHandlers = [];
151
- /** @type {((message: string) => void)[]} */
152
-
153
- this.onLogHandlers = [];
154
- /** @type {Record<string, Promise<any>>} */
155
-
156
- this._promises = {};
157
- this.startSocket();
158
- }
159
- /**
160
- * Checks if this connection is running in a web adapter and not in an admin.
161
- * @returns {boolean} True if running in a web adapter or in a socketio adapter.
162
- */
163
-
164
-
165
- _createClass(Connection, [{
166
- key: "startSocket",
167
- value:
168
- /**
169
- * Starts the socket.io connection.
170
- * @returns {void}
171
- */
172
- function startSocket() {
173
- var _this2 = this;
174
-
175
- // if socket io is not yet loaded
176
- if (typeof window.io === 'undefined') {
177
- // if in index.html the onLoad function not defined
178
- if (typeof window.registerSocketOnLoad !== 'function') {
179
- // poll if loaded
180
- this.scriptLoadCounter = this.scriptLoadCounter || 0;
181
- this.scriptLoadCounter++;
182
-
183
- if (this.scriptLoadCounter < 30) {
184
- // wait till the script loaded
185
- setTimeout(function () {
186
- return _this2.startSocket();
187
- }, 100);
188
- return;
189
- } else {
190
- window.alert('Cannot load socket.io.js!');
191
- }
192
- } else {
193
- // register on load
194
- window.registerSocketOnLoad(function () {
195
- return _this2.startSocket();
196
- });
197
- }
198
-
199
- return;
200
- } else {
201
- // socket was initialized, do not repeat
202
- if (this._socket) {
203
- return;
204
- }
205
- }
206
-
207
- var host = this.props.host;
208
- var port = this.props.port;
209
- var protocol = this.props.protocol.replace(':', ''); // if web adapter, socket io could be on other port or even host
210
-
211
- if (window.socketUrl) {
212
- var parts = window.socketUrl.split(':');
213
- host = parts[0] || host;
214
- port = parts[1] || port;
215
-
216
- if (host.includes('://')) {
217
- parts = host.split('://');
218
- protocol = parts[0];
219
- host = parts[1];
220
- }
221
- }
222
-
223
- var url = port ? "".concat(protocol, "://").concat(host, ":").concat(port) : "".concat(protocol, "://").concat(host);
224
- this._socket = window.io.connect(url, {
225
- query: 'ws=true',
226
- name: this.props.name,
227
- timeout: this.props.ioTimeout
228
- });
229
-
230
- this._socket.on('connect', function (noTimeout) {
231
- // If the user is not admin it takes some time to install the handlers, because all rights must be checked
232
- if (noTimeout !== true) {
233
- setTimeout(function () {
234
- return _this2.getVersion().then(function (info) {
235
- var _info$version$split = info.version.split('.'),
236
- _info$version$split2 = _slicedToArray(_info$version$split, 3),
237
- major = _info$version$split2[0],
238
- minor = _info$version$split2[1],
239
- patch = _info$version$split2[2];
240
-
241
- var v = parseInt(major, 10) * 10000 + parseInt(minor, 10) * 100 + parseInt(patch, 10);
242
-
243
- if (v < 40102) {
244
- _this2._authTimer = null; // possible this is old version of admin
245
-
246
- _this2.onPreConnect(false, false);
247
- } else {
248
- _this2._socket.emit('authenticate', function (isOk, isSecure) {
249
- return _this2.onPreConnect(isOk, isSecure);
250
- });
251
- }
252
- });
253
- }, 500);
254
- } else {
255
- // iobroker websocket waits, till all handlers are installed
256
- _this2._socket.emit('authenticate', function (isOk, isSecure) {
257
- return _this2.onPreConnect(isOk, isSecure);
258
- });
259
- }
260
- });
261
-
262
- this._socket.on('reconnect', function () {
263
- _this2.onProgress(PROGRESS.READY);
264
-
265
- _this2.connected = true;
266
-
267
- if (_this2.waitForRestart) {
268
- window.location.reload(false);
269
- } else {
270
- _this2._subscribe(true);
271
-
272
- _this2.onConnectionHandlers.forEach(function (cb) {
273
- return cb(true);
274
- });
275
- }
276
- });
277
-
278
- this._socket.on('disconnect', function () {
279
- _this2.connected = false;
280
- _this2.subscribed = false;
281
-
282
- _this2.onProgress(PROGRESS.CONNECTING);
283
-
284
- _this2.onConnectionHandlers.forEach(function (cb) {
285
- return cb(false);
286
- });
287
- });
288
-
289
- this._socket.on('reauthenticate', function () {
290
- return _this2.authenticate();
291
- });
292
-
293
- this._socket.on('log', function (message) {
294
- _this2.props.onLog && _this2.props.onLog(message);
295
-
296
- _this2.onLogHandlers.forEach(function (cb) {
297
- return cb(message);
298
- });
299
- });
300
-
301
- this._socket.on('error', function (err) {
302
- var _err = err || '';
303
-
304
- if (typeof _err.toString !== 'function') {
305
- _err = JSON.stringify(_err);
306
- console.error("Received strange error: ".concat(_err));
307
- }
308
-
309
- _err = _err.toString();
310
-
311
- if (_err.includes('User not authorized')) {
312
- _this2.authenticate();
313
- } else {
314
- window.alert("Socket Error: ".concat(err));
315
- }
316
- });
317
-
318
- this._socket.on('connect_error', function (err) {
319
- return console.error("Connect error: ".concat(err));
320
- });
321
-
322
- this._socket.on('permissionError', function (err) {
323
- return _this2.onError({
324
- message: 'no permission',
325
- operation: err.operation,
326
- type: err.type,
327
- id: err.id || ''
328
- });
329
- });
330
-
331
- this._socket.on('objectChange', function (id, obj) {
332
- return setTimeout(function () {
333
- return _this2.objectChange(id, obj);
334
- }, 0);
335
- });
336
-
337
- this._socket.on('stateChange', function (id, state) {
338
- return setTimeout(function () {
339
- return _this2.stateChange(id, state);
340
- }, 0);
341
- });
342
-
343
- this._socket.on('cmdStdout', function (id, text) {
344
- return _this2.onCmdStdoutHandler && _this2.onCmdStdoutHandler(id, text);
345
- });
346
-
347
- this._socket.on('cmdStderr', function (id, text) {
348
- return _this2.onCmdStderrHandler && _this2.onCmdStderrHandler(id, text);
349
- });
350
-
351
- this._socket.on('cmdExit', function (id, exitCode) {
352
- return _this2.onCmdExitHandler && _this2.onCmdExitHandler(id, exitCode);
353
- });
354
- }
355
- /**
356
- * Called internally.
357
- * @private
358
- * @param {boolean} isOk
359
- * @param {boolean} isSecure
360
- */
361
-
362
- }, {
363
- key: "onPreConnect",
364
- value: function onPreConnect(isOk, isSecure) {
365
- var _this3 = this;
366
-
367
- if (this._authTimer) {
368
- clearTimeout(this._authTimer);
369
- this._authTimer = null;
370
- }
371
-
372
- this.connected = true;
373
- this.isSecure = isSecure;
374
-
375
- if (this.waitForRestart) {
376
- window.location.reload(false);
377
- } else {
378
- if (this.firstConnect) {
379
- // retry strategy
380
- this.loadTimer = setTimeout(function () {
381
- _this3.loadTimer = null;
382
- _this3.loadCounter++;
383
-
384
- if (_this3.loadCounter < 10) {
385
- _this3.onConnect();
386
- }
387
- }, 1000);
388
-
389
- if (!this.loaded) {
390
- this.onConnect();
391
- }
392
- } else {
393
- this.onProgress(PROGRESS.READY);
394
- }
395
-
396
- this._subscribe(true);
397
-
398
- this.onConnectionHandlers.forEach(function (cb) {
399
- return cb(true);
400
- });
401
- }
402
-
403
- if (this._waitForFirstConnectionResolve) {
404
- this._waitForFirstConnectionResolve();
405
-
406
- this._waitForFirstConnectionResolve = null;
407
- }
408
- }
409
- /**
410
- * Checks if the socket is connected.
411
- * @returns {boolean} true if connected.
412
- */
413
-
414
- }, {
415
- key: "isConnected",
416
- value: function isConnected() {
417
- return this.connected;
418
- }
419
- /**
420
- * Checks if the socket is connected.
421
- * @returns {Promise<void>} Promise resolves if once connected.
422
- */
423
-
424
- }, {
425
- key: "waitForFirstConnection",
426
- value: function waitForFirstConnection() {
427
- return this._waitForFirstConnection;
428
- }
429
- /**
430
- * Called internally.
431
- * @private
432
- */
433
-
434
- }, {
435
- key: "_getUserPermissions",
436
- value: function _getUserPermissions(cb) {
437
- if (this.doNotLoadACL) {
438
- return cb && cb();
439
- } else {
440
- this._socket.emit('getUserPermissions', cb);
441
- }
442
- }
443
- /**
444
- * Called internally.
445
- * @private
446
- */
447
-
448
- }, {
449
- key: "onConnect",
450
- value: function onConnect() {
451
- var _this4 = this;
452
-
453
- this._getUserPermissions(function (err, acl) {
454
- if (err) {
455
- return _this4.onError('Cannot read user permissions: ' + err);
456
- } else if (!_this4.doNotLoadACL) {
457
- if (_this4.loaded) {
458
- return;
459
- }
460
-
461
- _this4.loaded = true;
462
- clearTimeout(_this4.loadTimer);
463
- _this4.loadTimer = null;
464
-
465
- _this4.onProgress(PROGRESS.CONNECTED);
466
-
467
- _this4.firstConnect = false;
468
- _this4.acl = acl;
469
- } // Read system configuration
470
-
471
-
472
- return (_this4.admin5only && !window.vendorPrefix ? _this4.getCompactSystemConfig() : _this4.getSystemConfig()).then(function (data) {
473
- if (_this4.doNotLoadACL) {
474
- if (_this4.loaded) {
475
- return undefined;
476
- }
477
-
478
- _this4.loaded = true;
479
- clearTimeout(_this4.loadTimer);
480
- _this4.loadTimer = null;
481
-
482
- _this4.onProgress(PROGRESS.CONNECTED);
483
-
484
- _this4.firstConnect = false;
485
- }
486
-
487
- _this4.systemConfig = data;
488
-
489
- if (_this4.systemConfig && _this4.systemConfig.common) {
490
- _this4.systemLang = _this4.systemConfig.common.language;
491
- } else {
492
- _this4.systemLang = window.navigator.userLanguage || window.navigator.language;
493
-
494
- if (_this4.systemLang !== 'en' && _this4.systemLang !== 'de' && _this4.systemLang !== 'ru') {
495
- _this4.systemConfig.common.language = 'en';
496
- _this4.systemLang = 'en';
497
- }
498
- }
499
-
500
- _this4.props.onLanguage && _this4.props.onLanguage(_this4.systemLang);
501
-
502
- if (!_this4.doNotLoadAllObjects) {
503
- return _this4.getObjects().then(function () {
504
- _this4.onProgress(PROGRESS.READY);
505
-
506
- _this4.props.onReady && _this4.props.onReady(_this4.objects);
507
- });
508
- } else {
509
- _this4.objects = _this4.admin5only ? {} : {
510
- 'system.config': data
511
- };
512
-
513
- _this4.onProgress(PROGRESS.READY);
514
-
515
- _this4.props.onReady && _this4.props.onReady(_this4.objects);
516
- }
517
-
518
- return undefined;
519
- })["catch"](function (e) {
520
- return _this4.onError('Cannot read system config: ' + e);
521
- });
522
- });
523
- }
524
- /**
525
- * Called internally.
526
- * @private
527
- */
528
-
529
- }, {
530
- key: "authenticate",
531
- value: function authenticate() {
532
- if (window.location.search.includes('&href=')) {
533
- window.location = "".concat(window.location.protocol, "//").concat(window.location.host).concat(window.location.pathname).concat(window.location.search).concat(window.location.hash);
534
- } else {
535
- window.location = "".concat(window.location.protocol, "//").concat(window.location.host).concat(window.location.pathname, "?login&href=").concat(window.location.search).concat(window.location.hash);
536
- }
537
- }
538
- /**
539
- * Subscribe to changes of the given state.
540
- * @param {string} id The ioBroker state ID.
541
- * @param {ioBroker.StateChangeHandler} cb The callback.
542
- */
543
-
544
- /**
545
- * Subscribe to changes of the given state.
546
- * @param {string} id The ioBroker state ID.
547
- * @param {boolean} binary Set to true if the given state is binary and requires Base64 decoding.
548
- * @param {ioBroker.StateChangeHandler} cb The callback.
549
- */
550
-
551
- }, {
552
- key: "subscribeState",
553
- value: function subscribeState(id, binary, cb) {
554
- if (typeof binary === 'function') {
555
- cb = binary;
556
- binary = false;
557
- }
558
-
559
- if (!this.statesSubscribes[id]) {
560
- var reg = id.replace(/\./g, '\\.').replace(/\*/g, '.*').replace(/\(/g, '\\(').replace(/\)/g, '\\)').replace(/\+/g, '\\+').replace(/\[/g, '\\[');
561
-
562
- if (reg.indexOf('*') === -1) {
563
- reg += '$';
564
- }
565
-
566
- this.statesSubscribes[id] = {
567
- reg: new RegExp(reg),
568
- cbs: []
569
- };
570
- this.statesSubscribes[id].cbs.push(cb);
571
-
572
- if (this.connected) {
573
- this._socket.emit('subscribe', id);
574
- }
575
- } else {
576
- !this.statesSubscribes[id].cbs.includes(cb) && this.statesSubscribes[id].cbs.push(cb);
577
- }
578
-
579
- if (typeof cb === 'function' && this.connected) {
580
- if (binary) {
581
- this.getBinaryState(id).then(function (base64) {
582
- return cb(id, base64);
583
- })["catch"](function (e) {
584
- return console.error("Cannot getForeignStates \"".concat(id, "\": ").concat(JSON.stringify(e)));
585
- });
586
- } else {
587
- if (Connection.isWeb()) {
588
- this._socket.emit('getStates', id, function (err, states) {
589
- err && console.error("Cannot getForeignStates \"".concat(id, "\": ").concat(JSON.stringify(err)));
590
- states && Object.keys(states).forEach(function (id) {
591
- return cb(id, states[id]);
592
- });
593
- });
594
- } else {
595
- this._socket.emit('getForeignStates', id, function (err, states) {
596
- err && console.error("Cannot getForeignStates \"".concat(id, "\": ").concat(JSON.stringify(err)));
597
- states && Object.keys(states).forEach(function (id) {
598
- return cb(id, states[id]);
599
- });
600
- });
601
- }
602
- }
603
- }
604
- }
605
- /**
606
- * Unsubscribes all callbacks from changes of the given state.
607
- * @param {string} id The ioBroker state ID.
608
- */
609
-
610
- /**
611
- * Unsubscribes the given callback from changes of the given state.
612
- * @param {string} id The ioBroker state ID.
613
- * @param {ioBroker.StateChangeHandler} cb The callback.
614
- */
615
-
616
- }, {
617
- key: "unsubscribeState",
618
- value: function unsubscribeState(id, cb) {
619
- if (this.statesSubscribes[id]) {
620
- if (cb) {
621
- var pos = this.statesSubscribes[id].cbs.indexOf(cb);
622
- pos !== -1 && this.statesSubscribes[id].cbs.splice(pos, 1);
623
- } else {
624
- this.statesSubscribes[id].cbs = [];
625
- }
626
-
627
- if (!this.statesSubscribes[id].cbs || !this.statesSubscribes[id].cbs.length) {
628
- delete this.statesSubscribes[id];
629
- this.connected && this._socket.emit('unsubscribe', id);
630
- }
631
- }
632
- }
633
- /**
634
- * Subscribe to changes of the given object.
635
- * @param {string} id The ioBroker object ID.
636
- * @param {import('./types').ObjectChangeHandler} cb The callback.
637
- * @returns {Promise<void>}
638
- */
639
-
640
- }, {
641
- key: "subscribeObject",
642
- value: function subscribeObject(id, cb) {
643
- if (!this.objectsSubscribes[id]) {
644
- var reg = id.replace(/\./g, '\\.').replace(/\*/g, '.*');
645
-
646
- if (!reg.includes('*')) {
647
- reg += '$';
648
- }
649
-
650
- this.objectsSubscribes[id] = {
651
- reg: new RegExp(reg),
652
- cbs: []
653
- };
654
- this.objectsSubscribes[id].cbs.push(cb);
655
- this.connected && this._socket.emit('subscribeObjects', id);
656
- } else {
657
- !this.objectsSubscribes[id].cbs.includes(cb) && this.objectsSubscribes[id].cbs.push(cb);
658
- }
659
-
660
- return Promise.resolve();
661
- }
662
- /**
663
- * Unsubscribes all callbacks from changes of the given object.
664
- * @param {string} id The ioBroker object ID.
665
- * @returns {Promise<void>}
666
- */
667
-
668
- /**
669
- * Unsubscribes the given callback from changes of the given object.
670
- * @param {string} id The ioBroker object ID.
671
- * @param {import('./types').ObjectChangeHandler} cb The callback.
672
- * @returns {Promise<void>}
673
- */
674
-
675
- }, {
676
- key: "unsubscribeObject",
677
- value: function unsubscribeObject(id, cb) {
678
- if (this.objectsSubscribes[id]) {
679
- if (cb) {
680
- var pos = this.objectsSubscribes[id].cbs.indexOf(cb);
681
- pos !== -1 && this.objectsSubscribes[id].cbs.splice(pos, 1);
682
- } else {
683
- this.objectsSubscribes[id].cbs = [];
684
- }
685
-
686
- if (this.connected && (!this.objectsSubscribes[id].cbs || !this.objectsSubscribes[id].cbs.length)) {
687
- delete this.objectsSubscribes[id];
688
- this.connected && this._socket.emit('unsubscribeObjects', id);
689
- }
690
- }
691
-
692
- return Promise.resolve();
693
- }
694
- /**
695
- * Called internally.
696
- * @private
697
- * @param {string} id
698
- * @param {ioBroker.Object | null | undefined} obj
699
- */
700
-
701
- }, {
702
- key: "objectChange",
703
- value: function objectChange(id, obj) {
704
- var _this5 = this;
705
-
706
- // update main.objects cache
707
- if (!this.objects) {
708
- return;
709
- }
710
- /** @type {import("./types").OldObject} */
711
-
712
-
713
- var oldObj;
714
- var changed = false;
715
-
716
- if (obj) {
717
- if (obj._rev && this.objects[id]) {
718
- this.objects[id]._rev = obj._rev;
719
- }
720
-
721
- if (this.objects[id]) {
722
- oldObj = {
723
- _id: id,
724
- type: this.objects[id].type
725
- };
726
- }
727
-
728
- if (!this.objects[id] || JSON.stringify(this.objects[id]) !== JSON.stringify(obj)) {
729
- this.objects[id] = obj;
730
- changed = true;
731
- }
732
- } else if (this.objects[id]) {
733
- oldObj = {
734
- _id: id,
735
- type: this.objects[id].type
736
- };
737
- delete this.objects[id];
738
- changed = true;
739
- }
740
-
741
- Object.keys(this.objectsSubscribes).forEach(function (_id) {
742
- if (_id === id || _this5.objectsSubscribes[_id].reg.test(id)) {
743
- //@ts-ignore
744
- _this5.objectsSubscribes[_id].cbs.forEach(function (cb) {
745
- return cb(id, obj, oldObj);
746
- });
747
- }
748
- });
749
-
750
- if (changed && this.props.onObjectChange) {
751
- this.props.onObjectChange(id, obj);
752
- }
753
- }
754
- /**
755
- * Called internally.
756
- * @private
757
- * @param {string} id
758
- * @param {ioBroker.State | null | undefined} state
759
- */
760
-
761
- }, {
762
- key: "stateChange",
763
- value: function stateChange(id, state) {
764
- for (var task in this.statesSubscribes) {
765
- if (this.statesSubscribes.hasOwnProperty(task) && this.statesSubscribes[task].reg.test(id)) {
766
- this.statesSubscribes[task].cbs.forEach(function (cb) {
767
- return cb(id, state);
768
- });
769
- }
770
- }
771
- }
772
- /**
773
- * Gets all states.
774
- * @param {boolean} disableProgressUpdate don't call onProgress() when done
775
- * @returns {Promise<Record<string, ioBroker.State>>}
776
- */
777
-
778
- }, {
779
- key: "getStates",
780
- value: function getStates(disableProgressUpdate) {
781
- var _this6 = this;
782
-
783
- if (!this.connected) {
784
- return Promise.reject(NOT_CONNECTED);
785
- }
786
-
787
- return new Promise(function (resolve, reject) {
788
- return _this6._socket.emit('getStates', function (err, res) {
789
- _this6.states = res; //@ts-ignore
790
-
791
- !disableProgressUpdate && _this6.onProgress(PROGRESS.STATES_LOADED);
792
- return err ? reject(err) : resolve(_this6.states);
793
- });
794
- });
795
- }
796
- /**
797
- * Gets the given state.
798
- * @param {string} id The state ID.
799
- * @returns {Promise<ioBroker.State | null | undefined>}
800
- */
801
-
802
- }, {
803
- key: "getState",
804
- value: function getState(id) {
805
- var _this7 = this;
806
-
807
- if (!this.connected) {
808
- return Promise.reject(NOT_CONNECTED);
809
- }
810
-
811
- return new Promise(function (resolve, reject) {
812
- return _this7._socket.emit('getState', id, function (err, state) {
813
- return err ? reject(err) : resolve(state);
814
- });
815
- });
816
- }
817
- /**
818
- * Gets the given binary state.
819
- * @param {string} id The state ID.
820
- * @returns {Promise<Buffer | undefined>}
821
- */
822
-
823
- }, {
824
- key: "getBinaryState",
825
- value: function getBinaryState(id) {
826
- var _this8 = this;
827
-
828
- if (!this.connected) {
829
- return Promise.reject(NOT_CONNECTED);
830
- } // the data will come in base64
831
-
832
-
833
- return new Promise(function (resolve, reject) {
834
- return _this8._socket.emit('getBinaryState', id, function (err, state) {
835
- return err ? reject(err) : resolve(state);
836
- });
837
- });
838
- }
839
- /**
840
- * Sets the given binary state.
841
- * @param {string} id The state ID.
842
- * @param {string} base64 The Base64 encoded binary data.
843
- * @returns {Promise<void>}
844
- */
845
-
846
- }, {
847
- key: "setBinaryState",
848
- value: function setBinaryState(id, base64) {
849
- var _this9 = this;
850
-
851
- if (!this.connected) {
852
- return Promise.reject(NOT_CONNECTED);
853
- } // the data will come in base64
854
-
855
-
856
- return new Promise(function (resolve, reject) {
857
- return _this9._socket.emit('setBinaryState', id, base64, function (err) {
858
- return err ? reject(err) : resolve();
859
- });
860
- });
861
- }
862
- /**
863
- * Sets the given state value.
864
- * @param {string} id The state ID.
865
- * @param {string | number | boolean | ioBroker.State | ioBroker.SettableState | null} val The state value.
866
- * @returns {Promise<void>}
867
- */
868
-
869
- }, {
870
- key: "setState",
871
- value: function setState(id, val) {
872
- var _this10 = this;
873
-
874
- if (!this.connected) {
875
- return Promise.reject(NOT_CONNECTED);
876
- }
877
-
878
- return new Promise(function (resolve, reject) {
879
- return _this10._socket.emit('setState', id, val, function (err) {
880
- return err ? reject(err) : resolve();
881
- });
882
- });
883
- }
884
- /**
885
- * Gets all objects.
886
- * @param {(objects?: Record<string, ioBroker.Object>) => void} update Callback that is executed when all objects are retrieved.
887
- * @returns {void}
888
- */
889
-
890
- /**
891
- * Gets all objects.
892
- * @param {boolean} update Set to true to retrieve all objects from the server (instead of using the local cache).
893
- * @param {boolean} disableProgressUpdate don't call onProgress() when done
894
- * @returns {Promise<Record<string, ioBroker.Object>> | undefined}
895
- */
896
-
897
- }, {
898
- key: "getObjects",
899
- value: function getObjects(update, disableProgressUpdate) {
900
- var _this11 = this;
901
-
902
- if (!this.connected) {
903
- return Promise.reject(NOT_CONNECTED);
904
- } else {
905
- return new Promise(function (resolve, reject) {
906
- if (!update && _this11.objects) {
907
- return resolve(_this11.objects);
908
- }
909
-
910
- _this11._socket.emit(Connection.isWeb() ? 'getObjects' : 'getAllObjects', function (err, res) {
911
- _this11.objects = res;
912
- disableProgressUpdate && _this11.onProgress(PROGRESS.OBJECTS_LOADED);
913
- err ? reject(err) : resolve(_this11.objects);
914
- });
915
- });
916
- }
917
- }
918
- /**
919
- * Called internally.
920
- * @private
921
- * @param {boolean} isEnable
922
- */
923
-
924
- }, {
925
- key: "_subscribe",
926
- value: function _subscribe(isEnable) {
927
- var _this12 = this;
928
-
929
- if (isEnable && !this.subscribed) {
930
- this.subscribed = true;
931
- this.autoSubscribes.forEach(function (id) {
932
- return _this12._socket.emit('subscribeObjects', id);
933
- }); // re subscribe objects
934
-
935
- Object.keys(this.objectsSubscribes).forEach(function (id) {
936
- return _this12._socket.emit('subscribeObjects', id);
937
- }); // re-subscribe logs
938
-
939
- this.autoSubscribeLog && this._socket.emit('requireLog', true); // re subscribe states
940
-
941
- Object.keys(this.statesSubscribes).forEach(function (id) {
942
- return _this12._socket.emit('subscribe', id);
943
- });
944
- } else if (!isEnable && this.subscribed) {
945
- this.subscribed = false; // un-subscribe objects
946
-
947
- this.autoSubscribes.forEach(function (id) {
948
- return _this12._socket.emit('unsubscribeObjects', id);
949
- });
950
- Object.keys(this.objectsSubscribes).forEach(function (id) {
951
- return _this12._socket.emit('unsubscribeObjects', id);
952
- }); // un-subscribe logs
953
-
954
- this.autoSubscribeLog && this._socket.emit('requireLog', false); // un-subscribe states
955
-
956
- Object.keys(this.statesSubscribes).forEach(function (id) {
957
- return _this12._socket.emit('unsubscribe', id);
958
- });
959
- }
960
- }
961
- /**
962
- * Requests log updates.
963
- * @param {boolean} isEnabled Set to true to get logs.
964
- * @returns {Promise<void>}
965
- */
966
-
967
- }, {
968
- key: "requireLog",
969
- value: function requireLog(isEnabled) {
970
- var _this13 = this;
971
-
972
- if (!this.connected) {
973
- return Promise.reject(NOT_CONNECTED);
974
- }
975
-
976
- return new Promise(function (resolve, reject) {
977
- return _this13._socket.emit('requireLog', isEnabled, function (err) {
978
- return err ? reject(err) : resolve();
979
- });
980
- });
981
- }
982
- /**
983
- * Deletes the given object.
984
- * @param {string} id The object ID.
985
- * @param {boolean} maintenance Force deletion of non conform IDs.
986
- * @returns {Promise<void>}
987
- */
988
-
989
- }, {
990
- key: "delObject",
991
- value: function delObject(id, maintenance) {
992
- var _this14 = this;
993
-
994
- if (!this.connected) {
995
- return Promise.reject(NOT_CONNECTED);
996
- }
997
-
998
- return new Promise(function (resolve, reject) {
999
- return _this14._socket.emit('delObject', id, {
1000
- maintenance: !!maintenance
1001
- }, function (err) {
1002
- return err ? reject(err) : resolve();
1003
- });
1004
- });
1005
- }
1006
- /**
1007
- * Deletes the given object and all its children.
1008
- * @param {string} id The object ID.
1009
- * @param {boolean} maintenance Force deletion of non conform IDs.
1010
- * @returns {Promise<void>}
1011
- */
1012
-
1013
- }, {
1014
- key: "delObjects",
1015
- value: function delObjects(id, maintenance) {
1016
- var _this15 = this;
1017
-
1018
- if (!this.connected) {
1019
- return Promise.reject(NOT_CONNECTED);
1020
- }
1021
-
1022
- return new Promise(function (resolve, reject) {
1023
- return _this15._socket.emit('delObjects', id, {
1024
- maintenance: !!maintenance
1025
- }, function (err) {
1026
- return err ? reject(err) : resolve();
1027
- });
1028
- });
1029
- }
1030
- /**
1031
- * Sets the object.
1032
- * @param {string} id The object ID.
1033
- * @param {ioBroker.SettableObject} obj The object.
1034
- * @returns {Promise<void>}
1035
- */
1036
-
1037
- }, {
1038
- key: "setObject",
1039
- value: function setObject(id, obj) {
1040
- var _this16 = this;
1041
-
1042
- if (!this.connected) {
1043
- return Promise.reject(NOT_CONNECTED);
1044
- }
1045
-
1046
- if (!obj) {
1047
- return Promise.reject('Null object is not allowed');
1048
- }
1049
-
1050
- obj = JSON.parse(JSON.stringify(obj));
1051
-
1052
- if (obj.hasOwnProperty('from')) {
1053
- delete obj.from;
1054
- }
1055
-
1056
- if (obj.hasOwnProperty('user')) {
1057
- delete obj.user;
1058
- }
1059
-
1060
- if (obj.hasOwnProperty('ts')) {
1061
- delete obj.ts;
1062
- }
1063
-
1064
- return new Promise(function (resolve, reject) {
1065
- return _this16._socket.emit('setObject', id, obj, function (err) {
1066
- return err ? reject(err) : resolve();
1067
- });
1068
- });
1069
- }
1070
- /**
1071
- * Gets the object with the given id from the server.
1072
- * @param {string} id The object ID.
1073
- * @returns {ioBroker.GetObjectPromise} The object.
1074
- */
1075
-
1076
- }, {
1077
- key: "getObject",
1078
- value: function getObject(id) {
1079
- var _this17 = this;
1080
-
1081
- if (!this.connected) {
1082
- return Promise.reject(NOT_CONNECTED);
1083
- }
1084
-
1085
- return new Promise(function (resolve, reject) {
1086
- return _this17._socket.emit('getObject', id, function (err, obj) {
1087
- return err ? reject(err) : resolve(obj);
1088
- });
1089
- });
1090
- }
1091
- /**
1092
- * Get all adapter instances.
1093
- * @param {boolean} [update] Force update.
1094
- * @returns {Promise<ioBroker.Object[]>}
1095
- */
1096
-
1097
- /**
1098
- * Get all instances of the given adapter.
1099
- * @param {string} adapter The name of the adapter.
1100
- * @param {boolean} [update] Force update.
1101
- * @returns {Promise<ioBroker.Object[]>}
1102
- */
1103
-
1104
- }, {
1105
- key: "getAdapterInstances",
1106
- value: function getAdapterInstances(adapter, update) {
1107
- var _this18 = this;
1108
-
1109
- if (Connection.isWeb()) {
1110
- return Promise.reject('Allowed only in admin');
1111
- }
1112
-
1113
- if (typeof adapter === 'boolean') {
1114
- update = adapter;
1115
- adapter = '';
1116
- }
1117
-
1118
- adapter = adapter || '';
1119
-
1120
- if (!update && this._promises['instances_' + adapter]) {
1121
- return this._promises['instances_' + adapter];
1122
- }
1123
-
1124
- if (!this.connected) {
1125
- return Promise.reject(NOT_CONNECTED);
1126
- }
1127
-
1128
- this._promises['instances_' + adapter] = new Promise(function (resolve, reject) {
1129
- var timeout = setTimeout(function () {
1130
- timeout = null;
1131
-
1132
- _this18.getObjectView("system.adapter.".concat(adapter, "."), "system.adapter.".concat(adapter, ".\u9999"), 'instance').then(function (items) {
1133
- return resolve(Object.keys(items).map(function (id) {
1134
- return fixAdminUI(items[id]);
1135
- }));
1136
- })["catch"](function (e) {
1137
- return reject(e);
1138
- });
1139
- }, TIMEOUT_FOR_ADMIN4);
1140
-
1141
- _this18._socket.emit('getAdapterInstances', adapter, function (err, instances) {
1142
- if (timeout) {
1143
- clearTimeout(timeout);
1144
- timeout = null;
1145
- return err ? reject(err) : resolve(instances);
1146
- }
1147
- });
1148
- });
1149
- return this._promises['instances_' + adapter];
1150
- }
1151
- /**
1152
- * Get all adapters.
1153
- * @param {boolean} [update] Force update.
1154
- * @returns {Promise<ioBroker.Object[]>}
1155
- */
1156
-
1157
- /**
1158
- * Get adapters with the given name.
1159
- * @param {string} adapter The name of the adapter.
1160
- * @param {boolean} [update] Force update.
1161
- * @returns {Promise<ioBroker.Object[]>}
1162
- */
1163
-
1164
- }, {
1165
- key: "getAdapters",
1166
- value: function getAdapters(adapter, update) {
1167
- var _this19 = this;
1168
-
1169
- if (Connection.isWeb()) {
1170
- return Promise.reject('Allowed only in admin');
1171
- }
1172
-
1173
- if (typeof adapter === 'boolean') {
1174
- update = adapter;
1175
- adapter = '';
1176
- }
1177
-
1178
- adapter = adapter || '';
1179
-
1180
- if (!update && this._promises['adapter_' + adapter]) {
1181
- return this._promises['adapter_' + adapter];
1182
- }
1183
-
1184
- if (!this.connected) {
1185
- return Promise.reject(NOT_CONNECTED);
1186
- }
1187
-
1188
- this._promises['adapter_' + adapter] = new Promise(function (resolve, reject) {
1189
- var timeout = setTimeout(function () {
1190
- timeout = null;
1191
-
1192
- _this19.getObjectView("system.adapter.".concat(adapter, "."), "system.adapter.".concat(adapter, ".\u9999"), 'adapter').then(function (items) {
1193
- return resolve(Object.keys(items).map(function (id) {
1194
- return fixAdminUI(items[id]);
1195
- }));
1196
- })["catch"](function (e) {
1197
- return reject(e);
1198
- });
1199
- }, TIMEOUT_FOR_ADMIN4);
1200
-
1201
- _this19._socket.emit('getAdapters', adapter, function (err, adapters) {
1202
- if (timeout) {
1203
- clearTimeout(timeout);
1204
- timeout = null;
1205
- return err ? reject(err) : resolve(adapters);
1206
- }
1207
- });
1208
- });
1209
- return this._promises['adapter_' + adapter];
1210
- }
1211
- /**
1212
- * Called internally.
1213
- * @private
1214
- * @param {any[]} objs
1215
- * @param {(err?: any) => void} cb
1216
- */
1217
-
1218
- }, {
1219
- key: "_renameGroups",
1220
- value: function _renameGroups(objs, cb) {
1221
- var _this20 = this;
1222
-
1223
- if (!objs || !objs.length) {
1224
- cb && cb();
1225
- } else {
1226
- var obj = objs.pop();
1227
- var oldId = obj._id;
1228
- obj._id = obj.newId;
1229
- delete obj.newId;
1230
- this.setObject(obj._id, obj).then(function () {
1231
- return _this20.delObject(oldId);
1232
- }).then(function () {
1233
- return setTimeout(function () {
1234
- return _this20._renameGroups(objs, cb);
1235
- }, 0);
1236
- })["catch"](function (err) {
1237
- return cb && cb(err);
1238
- });
1239
- }
1240
- }
1241
- /**
1242
- * Rename a group.
1243
- * @param {string} id The id.
1244
- * @param {string} newId The new id.
1245
- * @param {string | { [lang in ioBroker.Languages]?: string; }} newName The new name.
1246
- */
1247
-
1248
- }, {
1249
- key: "renameGroup",
1250
- value: function renameGroup(id, newId, newName) {
1251
- var _this21 = this;
1252
-
1253
- if (Connection.isWeb()) {
1254
- return Promise.reject('Allowed only in admin');
1255
- }
1256
-
1257
- return this.getGroups(true).then(function (groups) {
1258
- if (groups.length) {
1259
- // find all elements
1260
- var groupsToRename = groups.filter(function (group) {
1261
- return group._id.startsWith(id + '.');
1262
- });
1263
- groupsToRename.forEach(function (group) {
1264
- return group.newId = newId + group._id.substring(id.length);
1265
- });
1266
- return new Promise(function (resolve, reject) {
1267
- return _this21._renameGroups(groupsToRename, function (err) {
1268
- return err ? reject(err) : resolve();
1269
- });
1270
- }).then(function () {
1271
- var obj = groups.find(function (group) {
1272
- return group._id === id;
1273
- });
1274
-
1275
- if (obj) {
1276
- obj._id = newId;
1277
-
1278
- if (newName !== undefined) {
1279
- obj.common = obj.common || {};
1280
- obj.common.name = newName;
1281
- }
1282
-
1283
- return _this21.setObject(obj._id, obj).then(function () {
1284
- return _this21.delObject(id);
1285
- });
1286
- }
1287
- });
1288
- }
1289
- });
1290
- }
1291
- /**
1292
- * Sends a message to a specific instance or all instances of some specific adapter.
1293
- * @param {string} instance The instance to send this message to.
1294
- * @param {string} [command] Command name of the target instance.
1295
- * @param {ioBroker.MessagePayload} [data] The message data to send.
1296
- * @returns {Promise<ioBroker.Message | undefined>}
1297
- */
1298
-
1299
- }, {
1300
- key: "sendTo",
1301
- value: function sendTo(instance, command, data) {
1302
- var _this22 = this;
1303
-
1304
- if (!this.connected) {
1305
- return Promise.reject(NOT_CONNECTED);
1306
- }
1307
-
1308
- return new Promise(function (resolve) {
1309
- return _this22._socket.emit('sendTo', instance, command, data, function (result) {
1310
- return resolve(result);
1311
- });
1312
- });
1313
- }
1314
- /**
1315
- * Extend an object and create it if it might not exist.
1316
- * @param {string} id The id.
1317
- * @param {ioBroker.PartialObject} obj The object.
1318
- */
1319
-
1320
- }, {
1321
- key: "extendObject",
1322
- value: function extendObject(id, obj) {
1323
- var _this23 = this;
1324
-
1325
- if (!this.connected) {
1326
- return Promise.reject(NOT_CONNECTED);
1327
- }
1328
-
1329
- obj = JSON.parse(JSON.stringify(obj));
1330
-
1331
- if (obj.hasOwnProperty('from')) {
1332
- delete obj.from;
1333
- }
1334
-
1335
- if (obj.hasOwnProperty('user')) {
1336
- delete obj.user;
1337
- }
1338
-
1339
- if (obj.hasOwnProperty('ts')) {
1340
- delete obj.ts;
1341
- }
1342
-
1343
- return new Promise(function (resolve, reject) {
1344
- return _this23._socket.emit('extendObject', id, obj, function (err) {
1345
- return err ? reject(err) : resolve();
1346
- });
1347
- });
1348
- }
1349
- /**
1350
- * Register a handler for log messages.
1351
- * @param {(message: string) => void} handler The handler.
1352
- */
1353
-
1354
- }, {
1355
- key: "registerLogHandler",
1356
- value: function registerLogHandler(handler) {
1357
- !this.onLogHandlers.includes(handler) && this.onLogHandlers.push(handler);
1358
- }
1359
- /**
1360
- * Unregister a handler for log messages.
1361
- * @param {(message: string) => void} handler The handler.
1362
- */
1363
-
1364
- }, {
1365
- key: "unregisterLogHandler",
1366
- value: function unregisterLogHandler(handler) {
1367
- var pos = this.onLogHandlers.indexOf(handler);
1368
- pos !== -1 && this.onLogHandlers.splice(pos, 1);
1369
- }
1370
- /**
1371
- * Register a handler for the connection state.
1372
- * @param {(connected: boolean) => void} handler The handler.
1373
- */
1374
-
1375
- }, {
1376
- key: "registerConnectionHandler",
1377
- value: function registerConnectionHandler(handler) {
1378
- !this.onConnectionHandlers.includes(handler) && this.onConnectionHandlers.push(handler);
1379
- }
1380
- /**
1381
- * Unregister a handler for the connection state.
1382
- * @param {(connected: boolean) => void} handler The handler.
1383
- */
1384
-
1385
- }, {
1386
- key: "unregisterConnectionHandler",
1387
- value: function unregisterConnectionHandler(handler) {
1388
- var pos = this.onConnectionHandlers.indexOf(handler);
1389
- pos !== -1 && this.onConnectionHandlers.splice(pos, 1);
1390
- }
1391
- /**
1392
- * Set the handler for standard output of a command.
1393
- * @param {(id: string, text: string) => void} handler The handler.
1394
- */
1395
-
1396
- }, {
1397
- key: "registerCmdStdoutHandler",
1398
- value: function registerCmdStdoutHandler(handler) {
1399
- this.onCmdStdoutHandler = handler;
1400
- }
1401
- /**
1402
- * Unset the handler for standard output of a command.
1403
- * @param {(id: string, text: string) => void} handler The handler.
1404
- */
1405
-
1406
- }, {
1407
- key: "unregisterCmdStdoutHandler",
1408
- value: function unregisterCmdStdoutHandler(handler) {
1409
- this.onCmdStdoutHandler = null;
1410
- }
1411
- /**
1412
- * Set the handler for standard error of a command.
1413
- * @param {(id: string, text: string) => void} handler The handler.
1414
- */
1415
-
1416
- }, {
1417
- key: "registerCmdStderrHandler",
1418
- value: function registerCmdStderrHandler(handler) {
1419
- this.onCmdStderrHandler = handler;
1420
- }
1421
- /**
1422
- * Unset the handler for standard error of a command.
1423
- * @param {(id: string, text: string) => void} handler The handler.
1424
- */
1425
-
1426
- }, {
1427
- key: "unregisterCmdStderrHandler",
1428
- value: function unregisterCmdStderrHandler(handler) {
1429
- this.onCmdStderrHandler = null;
1430
- }
1431
- /**
1432
- * Set the handler for exit of a command.
1433
- * @param {(id: string, exitCode: number) => void} handler The handler.
1434
- */
1435
-
1436
- }, {
1437
- key: "registerCmdExitHandler",
1438
- value: function registerCmdExitHandler(handler) {
1439
- this.onCmdExitHandler = handler;
1440
- }
1441
- /**
1442
- * Unset the handler for exit of a command.
1443
- * @param {(id: string, exitCode: number) => void} handler The handler.
1444
- */
1445
-
1446
- }, {
1447
- key: "unregisterCmdExitHandler",
1448
- value: function unregisterCmdExitHandler(handler) {
1449
- this.onCmdExitHandler = null;
1450
- }
1451
- /**
1452
- * Get all enums with the given name.
1453
- * @param {string} [_enum] The name of the enum
1454
- * @param {boolean} [update] Force update.
1455
- * @returns {Promise<Record<string, ioBroker.Object>>}
1456
- */
1457
-
1458
- }, {
1459
- key: "getEnums",
1460
- value: function getEnums(_enum, update) {
1461
- var _this24 = this;
1462
-
1463
- if (!update && this._promises['enums_' + (_enum || 'all')]) {
1464
- return this._promises['enums_' + (_enum || 'all')];
1465
- }
1466
-
1467
- if (!this.connected) {
1468
- return Promise.reject(NOT_CONNECTED);
1469
- }
1470
-
1471
- this._promises['enums_' + (_enum || 'all')] = new Promise(function (resolve, reject) {
1472
- _this24._socket.emit('getObjectView', 'system', 'enum', {
1473
- startkey: 'enum.' + (_enum || ''),
1474
- endkey: 'enum.' + (_enum ? _enum + '.' : '') + "\u9999"
1475
- }, function (err, res) {
1476
- if (!err && res) {
1477
- var _res = {};
1478
-
1479
- for (var i = 0; i < res.rows.length; i++) {
1480
- if (_enum && res.rows[i].id === 'enum.' + _enum) {
1481
- continue;
1482
- }
1483
-
1484
- _res[res.rows[i].id] = res.rows[i].value;
1485
- }
1486
-
1487
- resolve(_res);
1488
- } else {
1489
- reject(err);
1490
- }
1491
- });
1492
- });
1493
- return this._promises['enums_' + (_enum || 'all')];
1494
- }
1495
- /**
1496
- * Query a predefined object view.
1497
- * @param {string} start The start ID.
1498
- * @param {string} end The end ID.
1499
- * @param {string} type The type of object.
1500
- * @returns {Promise<Record<string, ioBroker.Object>>}
1501
- */
1502
-
1503
- }, {
1504
- key: "getObjectView",
1505
- value: function getObjectView(start, end, type) {
1506
- var _this25 = this;
1507
-
1508
- if (!this.connected) {
1509
- return Promise.reject(NOT_CONNECTED);
1510
- }
1511
-
1512
- start = start || '';
1513
- end = end || "\u9999";
1514
- return new Promise(function (resolve, reject) {
1515
- _this25._socket.emit('getObjectView', 'system', type, {
1516
- startkey: start,
1517
- endkey: end
1518
- }, function (err, res) {
1519
- if (!err) {
1520
- var _res = {};
1521
-
1522
- if (res && res.rows) {
1523
- for (var i = 0; i < res.rows.length; i++) {
1524
- _res[res.rows[i].id] = res.rows[i].value;
1525
- }
1526
- }
1527
-
1528
- resolve(_res);
1529
- } else {
1530
- reject(err);
1531
- }
1532
- });
1533
- });
1534
- }
1535
- /**
1536
- * Get the stored certificates.
1537
- * @param {boolean} [update] Force update.
1538
- * @returns {Promise<{name: string; type: 'public' | 'private' | 'chained'}[]>}
1539
- */
1540
-
1541
- }, {
1542
- key: "getCertificates",
1543
- value: function getCertificates(update) {
1544
- if (Connection.isWeb()) {
1545
- return Promise.reject('Allowed only in admin');
1546
- }
1547
-
1548
- if (this._promises.cert && !update) {
1549
- return this._promises.cert;
1550
- }
1551
-
1552
- if (!this.connected) {
1553
- return Promise.reject(NOT_CONNECTED);
1554
- }
1555
-
1556
- this._promises.cert = this.getObject('system.certificates').then(function (res) {
1557
- var certs = [];
1558
-
1559
- if (res && res["native"] && res["native"].certificates) {
1560
- Object.keys(res["native"].certificates).forEach(function (c) {
1561
- var cert = res["native"].certificates[c];
1562
-
1563
- if (!cert) {
1564
- return;
1565
- }
1566
-
1567
- var _cert = {
1568
- name: c,
1569
- type: ''
1570
- }; // If it is filename, it could be everything
1571
-
1572
- if (cert.length < 700 && (cert.indexOf('/') !== -1 || cert.indexOf('\\') !== -1)) {
1573
- if (c.toLowerCase().includes('private')) {
1574
- _cert.type = 'private';
1575
- } else if (cert.toLowerCase().includes('private')) {
1576
- _cert.type = 'private';
1577
- } else if (c.toLowerCase().includes('public')) {
1578
- _cert.type = 'public';
1579
- } else if (cert.toLowerCase().includes('public')) {
1580
- _cert.type = 'public';
1581
- }
1582
-
1583
- certs.push(_cert);
1584
- } else {
1585
- _cert.type = cert.substring(0, '-----BEGIN RSA PRIVATE KEY'.length) === '-----BEGIN RSA PRIVATE KEY' || cert.substring(0, '-----BEGIN PRIVATE KEY'.length) === '-----BEGIN PRIVATE KEY' ? 'private' : 'public';
1586
-
1587
- if (_cert.type === 'public') {
1588
- var m = cert.split('-----END CERTIFICATE-----');
1589
-
1590
- if (m.filter(function (t) {
1591
- return t.replace(/\r\n|\r|\n/, '').trim();
1592
- }).length > 1) {
1593
- _cert.type = 'chained';
1594
- }
1595
- }
1596
-
1597
- certs.push(_cert);
1598
- }
1599
- });
1600
- }
1601
-
1602
- return certs;
1603
- });
1604
- return this._promises.cert;
1605
- }
1606
- /**
1607
- * Get the logs from a host (only for admin connection).
1608
- * @param {string} host
1609
- * @param {number} [linesNumber]
1610
- * @returns {Promise<string[]>}
1611
- */
1612
-
1613
- }, {
1614
- key: "getLogs",
1615
- value: function getLogs(host, linesNumber) {
1616
- var _this26 = this;
1617
-
1618
- if (Connection.isWeb()) {
1619
- return Promise.reject('Allowed only in admin');
1620
- }
1621
-
1622
- if (!this.connected) {
1623
- return Promise.reject(NOT_CONNECTED);
1624
- }
1625
-
1626
- return new Promise(function (resolve) {
1627
- return _this26._socket.emit('sendToHost', host, 'getLogs', linesNumber || 200, function (lines) {
1628
- return resolve(lines);
1629
- });
1630
- });
1631
- }
1632
- /**
1633
- * Get the log files (only for admin connection).
1634
- * @returns {Promise<string[]>}
1635
- */
1636
-
1637
- }, {
1638
- key: "getLogsFiles",
1639
- value: function getLogsFiles(host) {
1640
- var _this27 = this;
1641
-
1642
- if (Connection.isWeb()) {
1643
- return Promise.reject('Allowed only in admin');
1644
- }
1645
-
1646
- if (!this.connected) {
1647
- return Promise.reject(NOT_CONNECTED);
1648
- }
1649
-
1650
- return new Promise(function (resolve, reject) {
1651
- return _this27._socket.emit('readLogs', host, function (err, files) {
1652
- return err ? reject(err) : resolve(files);
1653
- });
1654
- });
1655
- }
1656
- /**
1657
- * Delete the logs from a host (only for admin connection).
1658
- * @param {string} host
1659
- * @returns {Promise<void>}
1660
- */
1661
-
1662
- }, {
1663
- key: "delLogs",
1664
- value: function delLogs(host) {
1665
- var _this28 = this;
1666
-
1667
- if (Connection.isWeb()) {
1668
- return Promise.reject('Allowed only in admin');
1669
- }
1670
-
1671
- if (!this.connected) {
1672
- return Promise.reject(NOT_CONNECTED);
1673
- }
1674
-
1675
- return new Promise(function (resolve, reject) {
1676
- return _this28._socket.emit('sendToHost', host, 'delLogs', null, function (error) {
1677
- return error ? reject(error) : resolve();
1678
- });
1679
- });
1680
- }
1681
- /**
1682
- * Read the meta items.
1683
- * @returns {Promise<ioBroker.Object[]>}
1684
- */
1685
-
1686
- }, {
1687
- key: "readMetaItems",
1688
- value: function readMetaItems() {
1689
- var _this29 = this;
1690
-
1691
- if (!this.connected) {
1692
- return Promise.reject(NOT_CONNECTED);
1693
- }
1694
-
1695
- return new Promise(function (resolve, reject) {
1696
- return _this29._socket.emit('getObjectView', 'system', 'meta', {
1697
- startkey: '',
1698
- endkey: "\u9999"
1699
- }, function (err, objs) {
1700
- return err ? reject(err) : resolve(objs.rows && objs.rows.map(function (obj) {
1701
- return obj.value;
1702
- }));
1703
- });
1704
- });
1705
- }
1706
- /**
1707
- * Read the directory of an adapter.
1708
- * @param {string} adapter The adapter name.
1709
- * @param {string} fileName The directory name.
1710
- * @returns {Promise<ioBroker.ReadDirResult[]>}
1711
- */
1712
-
1713
- }, {
1714
- key: "readDir",
1715
- value: function readDir(adapter, fileName) {
1716
- var _this30 = this;
1717
-
1718
- if (!this.connected) {
1719
- return Promise.reject(NOT_CONNECTED);
1720
- }
1721
-
1722
- return new Promise(function (resolve, reject) {
1723
- return _this30._socket.emit('readDir', adapter, fileName, function (err, files) {
1724
- return err ? reject(err) : resolve(files);
1725
- });
1726
- });
1727
- }
1728
- /**
1729
- * Read a file of an adapter.
1730
- * @param {string} adapter The adapter name.
1731
- * @param {string} fileName The file name.
1732
- * @param {boolean} base64 If it must be a base64 format
1733
- * @returns {Promise<string>}
1734
- */
1735
-
1736
- }, {
1737
- key: "readFile",
1738
- value: function readFile(adapter, fileName, base64) {
1739
- var _this31 = this;
1740
-
1741
- if (!this.connected) {
1742
- return Promise.reject(NOT_CONNECTED);
1743
- }
1744
-
1745
- return new Promise(function (resolve, reject) {
1746
- if (!base64) {
1747
- _this31._socket.emit('readFile', adapter, fileName, function (err, data, type) {
1748
- //@ts-ignore
1749
- err ? reject(err) : resolve({
1750
- data: data,
1751
- type: type
1752
- });
1753
- });
1754
- } else {
1755
- _this31._socket.emit('readFile64', adapter, fileName, base64, function (err, data) {
1756
- return err ? reject(err) : resolve(data);
1757
- });
1758
- }
1759
- });
1760
- }
1761
- /**
1762
- * Write a file of an adapter.
1763
- * @param {string} adapter The adapter name.
1764
- * @param {string} fileName The file name.
1765
- * @param {Buffer | string} data The data (if it's a Buffer, it will be converted to Base64).
1766
- * @returns {Promise<void>}
1767
- */
1768
-
1769
- }, {
1770
- key: "writeFile64",
1771
- value: function writeFile64(adapter, fileName, data) {
1772
- var _this32 = this;
1773
-
1774
- if (!this.connected) {
1775
- return Promise.reject(NOT_CONNECTED);
1776
- }
1777
-
1778
- return new Promise(function (resolve, reject) {
1779
- if (typeof data === 'string') {
1780
- _this32._socket.emit('writeFile', adapter, fileName, data, function (err) {
1781
- return err ? reject(err) : resolve();
1782
- });
1783
- } else {
1784
- var base64 = btoa(new Uint8Array(data).reduce(function (data, _byte) {
1785
- return data + String.fromCharCode(_byte);
1786
- }, ''));
1787
-
1788
- _this32._socket.emit('writeFile64', adapter, fileName, base64, function (err) {
1789
- return err ? reject(err) : resolve();
1790
- });
1791
- }
1792
- });
1793
- }
1794
- /**
1795
- * Rename a file or folder of an adapter.
1796
- *
1797
- * All files in folder will be renamed too.
1798
- * @param {string} adapter The adapter name.
1799
- * @param {string} oldName The file name of the file to be renamed.
1800
- * @param {string} newName The new file name.
1801
- * @returns {Promise<void>}
1802
- */
1803
-
1804
- }, {
1805
- key: "rename",
1806
- value: function rename(adapter, oldName, newName) {
1807
- var _this33 = this;
1808
-
1809
- if (!this.connected) {
1810
- return Promise.reject(NOT_CONNECTED);
1811
- }
1812
-
1813
- return new Promise(function (resolve, reject) {
1814
- return _this33._socket.emit('rename', adapter, oldName, newName, function (err) {
1815
- return err ? reject(err) : resolve();
1816
- });
1817
- });
1818
- }
1819
- /**
1820
- * Delete a file of an adapter.
1821
- * @param {string} adapter The adapter name.
1822
- * @param {string} fileName The file name.
1823
- * @returns {Promise<void>}
1824
- */
1825
-
1826
- }, {
1827
- key: "deleteFile",
1828
- value: function deleteFile(adapter, fileName) {
1829
- var _this34 = this;
1830
-
1831
- if (!this.connected) {
1832
- return Promise.reject(NOT_CONNECTED);
1833
- }
1834
-
1835
- return new Promise(function (resolve, reject) {
1836
- return _this34._socket.emit('unlink', adapter, fileName, function (err) {
1837
- return err ? reject(err) : resolve();
1838
- });
1839
- });
1840
- }
1841
- /**
1842
- * Delete a folder of an adapter.
1843
- * All files in folder will be deleted.
1844
- * @param {string} adapter The adapter name.
1845
- * @param {string} folderName The folder name.
1846
- * @returns {Promise<void>}
1847
- */
1848
-
1849
- }, {
1850
- key: "deleteFolder",
1851
- value: function deleteFolder(adapter, folderName) {
1852
- var _this35 = this;
1853
-
1854
- if (!this.connected) {
1855
- return Promise.reject(NOT_CONNECTED);
1856
- }
1857
-
1858
- return new Promise(function (resolve, reject) {
1859
- return _this35._socket.emit('deleteFolder', adapter, folderName, function (err) {
1860
- return err ? reject(err) : resolve();
1861
- });
1862
- });
1863
- }
1864
- /**
1865
- * Get the list of all hosts.
1866
- * @param {boolean} [update] Force update.
1867
- * @returns {Promise<ioBroker.Object[]>}
1868
- */
1869
-
1870
- }, {
1871
- key: "getHosts",
1872
- value: function getHosts(update) {
1873
- var _this36 = this;
1874
-
1875
- if (Connection.isWeb()) {
1876
- return Promise.reject('Allowed only in admin');
1877
- }
1878
-
1879
- if (!update && this._promises.hosts) {
1880
- return this._promises.hosts;
1881
- }
1882
-
1883
- if (!this.connected) {
1884
- return Promise.reject(NOT_CONNECTED);
1885
- }
1886
-
1887
- this._promises.hosts = new Promise(function (resolve, reject) {
1888
- return _this36._socket.emit('getObjectView', 'system', 'host', {
1889
- startkey: 'system.host.',
1890
- endkey: "system.host.\u9999"
1891
- }, function (err, doc) {
1892
- if (err) {
1893
- reject(err);
1894
- } else {
1895
- resolve(doc.rows.map(function (item) {
1896
- return item.value;
1897
- }));
1898
- }
1899
- });
1900
- });
1901
- return this._promises.hosts;
1902
- }
1903
- /**
1904
- * Get the list of all users.
1905
- * @param {boolean} [update] Force update.
1906
- * @returns {Promise<ioBroker.Object[]>}
1907
- */
1908
-
1909
- }, {
1910
- key: "getUsers",
1911
- value: function getUsers(update) {
1912
- var _this37 = this;
1913
-
1914
- if (Connection.isWeb()) {
1915
- return Promise.reject('Allowed only in admin');
1916
- }
1917
-
1918
- if (!update && this._promises.users) {
1919
- return this._promises.users;
1920
- }
1921
-
1922
- if (!this.connected) {
1923
- return Promise.reject(NOT_CONNECTED);
1924
- }
1925
-
1926
- this._promises.users = new Promise(function (resolve, reject) {
1927
- return _this37._socket.emit('getObjectView', 'system', 'user', {
1928
- startkey: 'system.user.',
1929
- endkey: "system.user.\u9999"
1930
- }, function (err, doc) {
1931
- if (err) {
1932
- reject(err);
1933
- } else {
1934
- resolve(doc.rows.map(function (item) {
1935
- return item.value;
1936
- }));
1937
- }
1938
- });
1939
- });
1940
- return this._promises.users;
1941
- }
1942
- /**
1943
- * Get the list of all groups.
1944
- * @param {boolean} [update] Force update.
1945
- * @returns {Promise<ioBroker.Object[]>}
1946
- */
1947
-
1948
- }, {
1949
- key: "getGroups",
1950
- value: function getGroups(update) {
1951
- var _this38 = this;
1952
-
1953
- if (!update && this._promises.groups) {
1954
- return this._promises.groups;
1955
- }
1956
-
1957
- if (!this.connected) {
1958
- return Promise.reject(NOT_CONNECTED);
1959
- }
1960
-
1961
- this._promises.groups = new Promise(function (resolve, reject) {
1962
- return _this38._socket.emit('getObjectView', 'system', 'group', {
1963
- startkey: 'system.group.',
1964
- endkey: "system.group.\u9999"
1965
- }, function (err, doc) {
1966
- if (err) {
1967
- reject(err);
1968
- } else {
1969
- resolve(doc.rows.map(function (item) {
1970
- return item.value;
1971
- }));
1972
- }
1973
- });
1974
- });
1975
- return this._promises.groups;
1976
- }
1977
- /**
1978
- * Get the host information.
1979
- * @param {string} host
1980
- * @param {boolean} [update] Force update.
1981
- * @param {number} [timeoutMs] optional read timeout.
1982
- * @returns {Promise<any>}
1983
- */
1984
-
1985
- }, {
1986
- key: "getHostInfo",
1987
- value: function getHostInfo(host, update, timeoutMs) {
1988
- var _this39 = this;
1989
-
1990
- if (Connection.isWeb()) {
1991
- return Promise.reject('Allowed only in admin');
1992
- }
1993
-
1994
- if (!host.startsWith('system.host.')) {
1995
- host += 'system.host.' + host;
1996
- }
1997
-
1998
- if (!update && this._promises['hostInfo' + host]) {
1999
- return this._promises['hostInfo' + host];
2000
- }
2001
-
2002
- if (!this.connected) {
2003
- return Promise.reject(NOT_CONNECTED);
2004
- }
2005
-
2006
- this._promises['hostInfo' + host] = new Promise(function (resolve, reject) {
2007
- var timeout = setTimeout(function () {
2008
- if (timeout) {
2009
- timeout = null;
2010
- reject('getHostInfo timeout');
2011
- }
2012
- }, timeoutMs || _this39.props.cmdTimeout);
2013
-
2014
- _this39._socket.emit('sendToHost', host, 'getHostInfo', null, function (data) {
2015
- if (timeout) {
2016
- clearTimeout(timeout);
2017
- timeout = null;
2018
-
2019
- if (data === PERMISSION_ERROR) {
2020
- reject('May not read "getHostInfo"');
2021
- } else if (!data) {
2022
- reject('Cannot read "getHostInfo"');
2023
- } else {
2024
- resolve(data);
2025
- }
2026
- }
2027
- });
2028
- });
2029
- return this._promises['hostInfo' + host];
2030
- }
2031
- /**
2032
- * Get the host information (short version).
2033
- * @param {string} host
2034
- * @param {boolean} [update] Force update.
2035
- * @param {number} [timeoutMs] optional read timeout.
2036
- * @returns {Promise<any>}
2037
- */
2038
-
2039
- }, {
2040
- key: "getHostInfoShort",
2041
- value: function getHostInfoShort(host, update, timeoutMs) {
2042
- var _this40 = this;
2043
-
2044
- if (Connection.isWeb()) {
2045
- return Promise.reject('Allowed only in admin');
2046
- }
2047
-
2048
- if (!host.startsWith('system.host.')) {
2049
- host += 'system.host.' + host;
2050
- }
2051
-
2052
- if (!update && this._promises['hostInfoShort' + host]) {
2053
- return this._promises['hostInfoShort' + host];
2054
- }
2055
-
2056
- if (!this.connected) {
2057
- return Promise.reject(NOT_CONNECTED);
2058
- }
2059
-
2060
- this._promises['hostInfoShort' + host] = new Promise(function (resolve, reject) {
2061
- var timeout = setTimeout(function () {
2062
- if (timeout) {
2063
- timeout = null;
2064
- reject('hostInfoShort timeout');
2065
- }
2066
- }, timeoutMs || _this40.props.cmdTimeout);
2067
-
2068
- _this40._socket.emit('sendToHost', host, 'getHostInfoShort', null, function (data) {
2069
- if (timeout) {
2070
- clearTimeout(timeout);
2071
- timeout = null;
2072
-
2073
- if (data === PERMISSION_ERROR) {
2074
- reject('May not read "getHostInfoShort"');
2075
- } else if (!data) {
2076
- reject('Cannot read "getHostInfoShort"');
2077
- } else {
2078
- resolve(data);
2079
- }
2080
- }
2081
- });
2082
- });
2083
- return this._promises['hostInfoShort' + host];
2084
- }
2085
- /**
2086
- * Get the repository.
2087
- * @param {string} host
2088
- * @param {any} [args]
2089
- * @param {boolean} [update] Force update.
2090
- * @param {number} [timeoutMs] timeout in ms.
2091
- * @returns {Promise<any>}
2092
- */
2093
-
2094
- }, {
2095
- key: "getRepository",
2096
- value: function getRepository(host, args, update, timeoutMs) {
2097
- var _this41 = this;
2098
-
2099
- if (Connection.isWeb()) {
2100
- return Promise.reject('Allowed only in admin');
2101
- }
2102
-
2103
- if (!update && this._promises.repo) {
2104
- return this._promises.repo;
2105
- }
2106
-
2107
- if (!this.connected) {
2108
- return Promise.reject(NOT_CONNECTED);
2109
- }
2110
-
2111
- if (!host.startsWith('system.host.')) {
2112
- host += 'system.host.' + host;
2113
- }
2114
-
2115
- this._promises.repo = new Promise(function (resolve, reject) {
2116
- var timeout = setTimeout(function () {
2117
- if (timeout) {
2118
- timeout = null;
2119
- reject('getRepository timeout');
2120
- }
2121
- }, timeoutMs || _this41.props.cmdTimeout);
2122
-
2123
- _this41._socket.emit('sendToHost', host, 'getRepository', args, function (data) {
2124
- if (timeout) {
2125
- clearTimeout(timeout);
2126
- timeout = null;
2127
-
2128
- if (data === PERMISSION_ERROR) {
2129
- reject('May not read "getRepository"');
2130
- } else if (!data) {
2131
- reject('Cannot read "getRepository"');
2132
- } else {
2133
- resolve(data);
2134
- }
2135
- }
2136
- });
2137
- });
2138
- return this._promises.repo;
2139
- }
2140
- /**
2141
- * Get the installed.
2142
- * @param {string} host
2143
- * @param {boolean} [update] Force update.
2144
- * @param {number} [cmdTimeout] timeout in ms (optional)
2145
- * @returns {Promise<any>}
2146
- */
2147
-
2148
- }, {
2149
- key: "getInstalled",
2150
- value: function getInstalled(host, update, cmdTimeout) {
2151
- var _this42 = this;
2152
-
2153
- if (Connection.isWeb()) {
2154
- return Promise.reject('Allowed only in admin');
2155
- }
2156
-
2157
- this._promises.installed = this._promises.installed || {};
2158
-
2159
- if (!update && this._promises.installed[host]) {
2160
- return this._promises.installed[host];
2161
- }
2162
-
2163
- if (!this.connected) {
2164
- return Promise.reject(NOT_CONNECTED);
2165
- }
2166
-
2167
- if (!host.startsWith('system.host.')) {
2168
- host += 'system.host.' + host;
2169
- }
2170
-
2171
- this._promises.installed[host] = new Promise(function (resolve, reject) {
2172
- var timeout = setTimeout(function () {
2173
- if (timeout) {
2174
- timeout = null;
2175
- reject('getInstalled timeout');
2176
- }
2177
- }, cmdTimeout || _this42.props.cmdTimeout);
2178
-
2179
- _this42._socket.emit('sendToHost', host, 'getInstalled', null, function (data) {
2180
- if (timeout) {
2181
- clearTimeout(timeout);
2182
- timeout = null;
2183
-
2184
- if (data === PERMISSION_ERROR) {
2185
- reject('May not read "getInstalled"');
2186
- } else if (!data) {
2187
- reject('Cannot read "getInstalled"');
2188
- } else {
2189
- resolve(data);
2190
- }
2191
- }
2192
- });
2193
- });
2194
- return this._promises.installed[host];
2195
- }
2196
- /**
2197
- * Execute a command on a host.
2198
- * @param {string} host The host name.
2199
- * @param {string} cmd The command.
2200
- * @param {string} cmdId The command ID.
2201
- * @param {number} cmdTimeout Timeout of command in ms
2202
- * @returns {Promise<void>}
2203
- */
2204
-
2205
- }, {
2206
- key: "cmdExec",
2207
- value: function cmdExec(host, cmd, cmdId, cmdTimeout) {
2208
- var _this43 = this;
2209
-
2210
- if (Connection.isWeb()) {
2211
- return Promise.reject('Allowed only in admin');
2212
- }
2213
-
2214
- if (!this.connected) {
2215
- return Promise.reject(NOT_CONNECTED);
2216
- }
2217
-
2218
- if (!host.startsWith(host)) {
2219
- host += 'system.host.' + host;
2220
- }
2221
-
2222
- return new Promise(function (resolve, reject) {
2223
- var timeout = cmdTimeout && setTimeout(function () {
2224
- if (timeout) {
2225
- timeout = null;
2226
- reject('cmdExec timeout');
2227
- }
2228
- }, cmdTimeout);
2229
-
2230
- _this43._socket.emit('cmdExec', host, cmdId, cmd, null, function (err) {
2231
- if (!cmdTimeout || timeout) {
2232
- timeout && clearTimeout(timeout);
2233
- timeout = null;
2234
-
2235
- if (err) {
2236
- reject(err);
2237
- } else {
2238
- resolve();
2239
- }
2240
- }
2241
- });
2242
- });
2243
- }
2244
- /**
2245
- * Checks if a given feature is supported.
2246
- * @param {string} feature The feature to check.
2247
- * @param {boolean} [update] Force update.
2248
- * @returns {Promise<any>}
2249
- */
2250
-
2251
- }, {
2252
- key: "checkFeatureSupported",
2253
- value: function checkFeatureSupported(feature, update) {
2254
- var _this44 = this;
2255
-
2256
- if (!update && this._promises['supportedFeatures_' + feature]) {
2257
- return this._promises['supportedFeatures_' + feature];
2258
- }
2259
-
2260
- if (!this.connected) {
2261
- return Promise.reject(NOT_CONNECTED);
2262
- }
2263
-
2264
- this._promises['supportedFeatures_' + feature] = new Promise(function (resolve, reject) {
2265
- return _this44._socket.emit('checkFeatureSupported', feature, function (err, features) {
2266
- err ? reject(err) : resolve(features);
2267
- });
2268
- });
2269
- return this._promises['supportedFeatures_' + feature];
2270
- }
2271
- /**
2272
- * Read the base settings of a given host.
2273
- * @param {string} host
2274
- * @returns {Promise<any>}
2275
- */
2276
-
2277
- }, {
2278
- key: "readBaseSettings",
2279
- value: function readBaseSettings(host) {
2280
- var _this45 = this;
2281
-
2282
- if (Connection.isWeb()) {
2283
- return Promise.reject('Allowed only in admin');
2284
- }
2285
-
2286
- return this.checkFeatureSupported('CONTROLLER_READWRITE_BASE_SETTINGS').then(function (result) {
2287
- if (result) {
2288
- if (!_this45.connected) {
2289
- return Promise.reject(NOT_CONNECTED);
2290
- }
2291
-
2292
- return new Promise(function (resolve, reject) {
2293
- var timeout = setTimeout(function () {
2294
- if (timeout) {
2295
- timeout = null;
2296
- reject('readBaseSettings timeout');
2297
- }
2298
- }, _this45.props.cmdTimeout);
2299
-
2300
- if (host.startsWith('system.host.')) {
2301
- host = host.replace(/^system\.host\./, '');
2302
- }
2303
-
2304
- _this45._socket.emit('sendToHost', host, 'readBaseSettings', null, function (data) {
2305
- if (timeout) {
2306
- clearTimeout(timeout);
2307
- timeout = null;
2308
-
2309
- if (data === PERMISSION_ERROR) {
2310
- reject('May not read "BaseSettings"');
2311
- } else if (!data) {
2312
- reject('Cannot read "BaseSettings"');
2313
- } else {
2314
- resolve(data);
2315
- }
2316
- }
2317
- });
2318
- });
2319
- } else {
2320
- return Promise.reject('Not supported');
2321
- }
2322
- });
2323
- }
2324
- /**
2325
- * Write the base settings of a given host.
2326
- * @param {string} host
2327
- * @param {any} config
2328
- * @returns {Promise<any>}
2329
- */
2330
-
2331
- }, {
2332
- key: "writeBaseSettings",
2333
- value: function writeBaseSettings(host, config) {
2334
- var _this46 = this;
2335
-
2336
- if (Connection.isWeb()) {
2337
- return Promise.reject('Allowed only in admin');
2338
- }
2339
-
2340
- return this.checkFeatureSupported('CONTROLLER_READWRITE_BASE_SETTINGS').then(function (result) {
2341
- if (result) {
2342
- if (!_this46.connected) {
2343
- return Promise.reject(NOT_CONNECTED);
2344
- }
2345
-
2346
- return new Promise(function (resolve, reject) {
2347
- var timeout = setTimeout(function () {
2348
- if (timeout) {
2349
- timeout = null;
2350
- reject('writeBaseSettings timeout');
2351
- }
2352
- }, _this46.props.cmdTimeout);
2353
-
2354
- _this46._socket.emit('sendToHost', host, 'writeBaseSettings', config, function (data) {
2355
- if (timeout) {
2356
- clearTimeout(timeout);
2357
- timeout = null;
2358
-
2359
- if (data === PERMISSION_ERROR) {
2360
- reject('May not write "BaseSettings"');
2361
- } else if (!data) {
2362
- reject('Cannot write "BaseSettings"');
2363
- } else {
2364
- resolve(data);
2365
- }
2366
- }
2367
- });
2368
- });
2369
- } else {
2370
- return Promise.reject('Not supported');
2371
- }
2372
- });
2373
- }
2374
- /**
2375
- * Send command to restart the iobroker on host
2376
- * @param {string} host
2377
- * @returns {Promise<any>}
2378
- */
2379
-
2380
- }, {
2381
- key: "restartController",
2382
- value: function restartController(host) {
2383
- var _this47 = this;
2384
-
2385
- if (Connection.isWeb()) {
2386
- return Promise.reject('Allowed only in admin');
2387
- }
2388
-
2389
- return new Promise(function (resolve, reject) {
2390
- _this47._socket.emit('sendToHost', host, 'restartController', null, function (error) {
2391
- error ? reject(error) : resolve(true);
2392
- });
2393
- });
2394
- }
2395
- /**
2396
- * Read statistics information from host
2397
- * @param {string} host
2398
- * @param {string} typeOfDiag one of none, normal, no-city, extended
2399
- * @returns {Promise<any>}
2400
- */
2401
-
2402
- }, {
2403
- key: "getDiagData",
2404
- value: function getDiagData(host, typeOfDiag) {
2405
- var _this48 = this;
2406
-
2407
- if (Connection.isWeb()) {
2408
- return Promise.reject('Allowed only in admin');
2409
- }
2410
-
2411
- return new Promise(function (resolve) {
2412
- _this48._socket.emit('sendToHost', host, 'getDiagData', typeOfDiag, function (result) {
2413
- return resolve(result);
2414
- });
2415
- });
2416
- }
2417
- /**
2418
- * Read all states (which might not belong to this adapter) which match the given pattern.
2419
- * @param {string} pattern
2420
- * @returns {ioBroker.GetStatesPromise}
2421
- */
2422
-
2423
- }, {
2424
- key: "getForeignStates",
2425
- value: function getForeignStates(pattern) {
2426
- var _this49 = this;
2427
-
2428
- if (!this.connected) {
2429
- return Promise.reject(NOT_CONNECTED);
2430
- }
2431
-
2432
- if (Connection.isWeb()) {
2433
- return new Promise(function (resolve, reject) {
2434
- return _this49._socket.emit('getStates', pattern || '*', function (err, states) {
2435
- return err ? reject(err) : resolve(states);
2436
- });
2437
- });
2438
- } else {
2439
- return new Promise(function (resolve, reject) {
2440
- return _this49._socket.emit('getForeignStates', pattern || '*', function (err, states) {
2441
- return err ? reject(err) : resolve(states);
2442
- });
2443
- });
2444
- }
2445
- }
2446
- /**
2447
- * Get foreign objects by pattern, by specific type and resolve their enums.
2448
- * @param {string} pattern
2449
- * @param {string} [type]
2450
- * @returns {ioBroker.GetObjectsPromise}
2451
- */
2452
-
2453
- }, {
2454
- key: "getForeignObjects",
2455
- value: function getForeignObjects(pattern, type) {
2456
- var _this50 = this;
2457
-
2458
- if (!this.connected) {
2459
- return Promise.reject(NOT_CONNECTED);
2460
- }
2461
-
2462
- return new Promise(function (resolve, reject) {
2463
- return _this50._socket.emit('getForeignObjects', pattern || '*', type, function (err, states) {
2464
- return err ? reject(err) : resolve(states);
2465
- });
2466
- });
2467
- }
2468
- /**
2469
- * Gets the system configuration.
2470
- * @param {boolean} [update] Force update.
2471
- * @returns {Promise<ioBroker.OtherObject>}
2472
- */
2473
-
2474
- }, {
2475
- key: "getSystemConfig",
2476
- value: function getSystemConfig(update) {
2477
- if (!update && this._promises.systemConfig) {
2478
- return this._promises.systemConfig;
2479
- }
2480
-
2481
- if (!this.connected) {
2482
- return Promise.reject(NOT_CONNECTED);
2483
- }
2484
-
2485
- this._promises.systemConfig = this.getObject('system.config').then(function (systemConfig) {
2486
- //@ts-ignore
2487
- systemConfig = systemConfig || {}; //@ts-ignore
2488
-
2489
- systemConfig.common = systemConfig.common || {}; //@ts-ignore
2490
-
2491
- systemConfig["native"] = systemConfig["native"] || {};
2492
- return systemConfig;
2493
- });
2494
- return this._promises.systemConfig;
2495
- }
2496
- /**
2497
- * Sets the system configuration.
2498
- * @param {ioBroker.SettableObjectWorker<ioBroker.OtherObject>} obj
2499
- * @returns {Promise<ioBroker.SettableObjectWorker<ioBroker.OtherObject>>}
2500
- */
2501
-
2502
- }, {
2503
- key: "setSystemConfig",
2504
- value: function setSystemConfig(obj) {
2505
- var _this51 = this;
2506
-
2507
- return this.setObject('system.config', obj).then(function () {
2508
- return _this51._promises.systemConfig = Promise.resolve(obj);
2509
- });
2510
- }
2511
- /**
2512
- * Get the raw socket.io socket.
2513
- * @returns {any}
2514
- */
2515
-
2516
- }, {
2517
- key: "getRawSocket",
2518
- value: function getRawSocket() {
2519
- return this._socket;
2520
- }
2521
- /**
2522
- * Get the history of a given state.
2523
- * @param {string} id
2524
- * @param {ioBroker.GetHistoryOptions} options
2525
- * @returns {Promise<ioBroker.GetHistoryResult>}
2526
- */
2527
-
2528
- }, {
2529
- key: "getHistory",
2530
- value: function getHistory(id, options) {
2531
- var _this52 = this;
2532
-
2533
- if (!this.connected) {
2534
- return Promise.reject(NOT_CONNECTED);
2535
- }
2536
-
2537
- return new Promise(function (resolve, reject) {
2538
- return _this52._socket.emit('getHistory', id, options, function (err, values) {
2539
- return err ? reject(err) : resolve(values);
2540
- });
2541
- });
2542
- }
2543
- /**
2544
- * Get the history of a given state.
2545
- * @param {string} id
2546
- * @param {ioBroker.GetHistoryOptions} options
2547
- * @returns {Promise<{values: ioBroker.GetHistoryResult; sesionId: string; stepIgnore: number}>}
2548
- */
2549
-
2550
- }, {
2551
- key: "getHistoryEx",
2552
- value: function getHistoryEx(id, options) {
2553
- var _this53 = this;
2554
-
2555
- if (!this.connected) {
2556
- return Promise.reject(NOT_CONNECTED);
2557
- }
2558
-
2559
- return new Promise(function (resolve, reject) {
2560
- return _this53._socket.emit('getHistory', id, options, function (err, values, stepIgnore, sessionId) {
2561
- return err ? reject(err) : resolve({
2562
- values: values,
2563
- sessionId: sessionId,
2564
- stepIgnore: stepIgnore
2565
- });
2566
- });
2567
- });
2568
- }
2569
- /**
2570
- * Change the password of the given user.
2571
- * @param {string} user
2572
- * @param {string} password
2573
- * @returns {Promise<void>}
2574
- */
2575
-
2576
- }, {
2577
- key: "changePassword",
2578
- value: function changePassword(user, password) {
2579
- var _this54 = this;
2580
-
2581
- if (Connection.isWeb()) {
2582
- return Promise.reject('Allowed only in admin');
2583
- }
2584
-
2585
- return new Promise(function (resolve, reject) {
2586
- return _this54._socket.emit('changePassword', user, password, function (err) {
2587
- return err ? reject(err) : resolve();
2588
- });
2589
- });
2590
- }
2591
- /**
2592
- * Get the IP addresses of the given host.
2593
- * @param {string} host
2594
- * @param {boolean} [update] Force update.
2595
- * @returns {Promise<string[]>}
2596
- */
2597
-
2598
- }, {
2599
- key: "getIpAddresses",
2600
- value: function getIpAddresses(host, update) {
2601
- if (Connection.isWeb()) {
2602
- return Promise.reject('Allowed only in admin');
2603
- }
2604
-
2605
- if (!host.startsWith('system.host.')) {
2606
- host = 'system.host.' + host;
2607
- }
2608
-
2609
- if (!update && this._promises['IPs_' + host]) {
2610
- return this._promises['IPs_' + host];
2611
- }
2612
-
2613
- this._promises['IPs_' + host] = this.getObject(host).then(function (obj) {
2614
- return obj && obj.common ? obj.common.address || [] : [];
2615
- });
2616
- return this._promises['IPs_' + host];
2617
- }
2618
- /**
2619
- * Get the IP addresses with interface names of the given host or find host by IP.
2620
- * @param {string} ipOrHostName
2621
- * @param {boolean} [update] Force update.
2622
- * @returns {Promise<any[<name, address, family>]>}
2623
- */
2624
-
2625
- }, {
2626
- key: "getHostByIp",
2627
- value: function getHostByIp(ipOrHostName, update) {
2628
- var _this55 = this;
2629
-
2630
- if (Connection.isWeb()) {
2631
- return Promise.reject('Allowed only in admin');
2632
- }
2633
-
2634
- if (ipOrHostName.startsWith('system.host.')) {
2635
- ipOrHostName = ipOrHostName.replace(/^system\.host\./, '');
2636
- }
2637
-
2638
- if (!update && this._promises['rIPs_' + ipOrHostName]) {
2639
- return this._promises['rIPs_' + ipOrHostName];
2640
- }
2641
-
2642
- this._promises['rIPs_' + ipOrHostName] = new Promise(function (resolve) {
2643
- return _this55._socket.emit('getHostByIp', ipOrHostName, function (ip, host) {
2644
- var _host$native, _host$native$hardware;
2645
-
2646
- var IPs4 = [{
2647
- name: '[IPv4] 0.0.0.0 - Listen on all IPs',
2648
- address: '0.0.0.0',
2649
- family: 'ipv4'
2650
- }];
2651
- var IPs6 = [{
2652
- name: '[IPv6] :: - Listen on all IPs',
2653
- address: '::',
2654
- family: 'ipv6'
2655
- }];
2656
-
2657
- if ((_host$native = host["native"]) !== null && _host$native !== void 0 && (_host$native$hardware = _host$native.hardware) !== null && _host$native$hardware !== void 0 && _host$native$hardware.networkInterfaces) {
2658
- for (var eth in host["native"].hardware.networkInterfaces) {
2659
- if (!host["native"].hardware.networkInterfaces.hasOwnProperty(eth)) {
2660
- continue;
2661
- }
2662
-
2663
- for (var num = 0; num < host["native"].hardware.networkInterfaces[eth].length; num++) {
2664
- if (host["native"].hardware.networkInterfaces[eth][num].family !== 'IPv6') {
2665
- IPs4.push({
2666
- name: "[".concat(host["native"].hardware.networkInterfaces[eth][num].family, "] ").concat(host["native"].hardware.networkInterfaces[eth][num].address, " - ").concat(eth),
2667
- address: host["native"].hardware.networkInterfaces[eth][num].address,
2668
- family: 'ipv4'
2669
- });
2670
- } else {
2671
- IPs6.push({
2672
- name: "[".concat(host["native"].hardware.networkInterfaces[eth][num].family, "] ").concat(host["native"].hardware.networkInterfaces[eth][num].address, " - ").concat(eth),
2673
- address: host["native"].hardware.networkInterfaces[eth][num].address,
2674
- family: 'ipv6'
2675
- });
2676
- }
2677
- }
2678
- }
2679
- }
2680
-
2681
- for (var i = 0; i < IPs6.length; i++) {
2682
- IPs4.push(IPs6[i]);
2683
- }
2684
-
2685
- resolve(IPs4);
2686
- });
2687
- });
2688
- return this._promises['rIPs_' + ipOrHostName];
2689
- }
2690
- /**
2691
- * Encrypt a text
2692
- * @param {string} text
2693
- * @returns {Promise<string>}
2694
- */
2695
-
2696
- }, {
2697
- key: "encrypt",
2698
- value: function encrypt(text) {
2699
- var _this56 = this;
2700
-
2701
- if (Connection.isWeb()) {
2702
- return Promise.reject('Allowed only in admin');
2703
- }
2704
-
2705
- return new Promise(function (resolve, reject) {
2706
- return _this56._socket.emit('encrypt', text, function (err, text) {
2707
- return err ? reject(err) : resolve(text);
2708
- });
2709
- });
2710
- }
2711
- /**
2712
- * Decrypt a text
2713
- * @param {string} encryptedText
2714
- * @returns {Promise<string>}
2715
- */
2716
-
2717
- }, {
2718
- key: "decrypt",
2719
- value: function decrypt(encryptedText) {
2720
- var _this57 = this;
2721
-
2722
- if (Connection.isWeb()) {
2723
- return Promise.reject('Allowed only in admin');
2724
- }
2725
-
2726
- return new Promise(function (resolve, reject) {
2727
- return _this57._socket.emit('decrypt', encryptedText, function (err, text) {
2728
- return err ? reject(err) : resolve(text);
2729
- });
2730
- });
2731
- }
2732
- /**
2733
- * Gets the version.
2734
- * @returns {Promise<{version: string; serverName: string}>}
2735
- */
2736
-
2737
- }, {
2738
- key: "getVersion",
2739
- value: function getVersion() {
2740
- var _this58 = this;
2741
-
2742
- this._promises.version = this._promises.version || new Promise(function (resolve, reject) {
2743
- return _this58._socket.emit('getVersion', function (err, version, serverName) {
2744
- // support of old socket.io
2745
- if (err && !version && typeof err === 'string' && err.match(/\d+\.\d+\.\d+/)) {
2746
- resolve({
2747
- version: err,
2748
- serverName: 'socketio'
2749
- });
2750
- } else {
2751
- return err ? reject(err) : resolve({
2752
- version: version,
2753
- serverName: serverName
2754
- });
2755
- }
2756
- });
2757
- });
2758
- return this._promises.version;
2759
- }
2760
- /**
2761
- * Gets the web server name.
2762
- * @returns {Promise<string>}
2763
- */
2764
-
2765
- }, {
2766
- key: "getWebServerName",
2767
- value: function getWebServerName() {
2768
- var _this59 = this;
2769
-
2770
- this._promises.webName = this._promises.webName || new Promise(function (resolve, reject) {
2771
- return _this59._socket.emit('getAdapterName', function (err, name) {
2772
- return err ? reject(err) : resolve(name);
2773
- });
2774
- });
2775
- return this._promises.webName;
2776
- }
2777
- /**
2778
- * Gets the admin version.
2779
- * @deprecated use getVersion()
2780
- * @returns {Promise<{version: string; serverName: string}>}
2781
- */
2782
-
2783
- }, {
2784
- key: "getAdminVersion",
2785
- value: function getAdminVersion() {
2786
- console.log('Deprecated: use getVersion');
2787
- return this.getVersion();
2788
- }
2789
- /**
2790
- * Change access rights for file
2791
- * @param {string} [adapter] adapter name
2792
- * @param {string} [filename] file name with full path. it could be like vis.0/*
2793
- * @param {object} [options] like {mode: 0x644}
2794
- * @returns {Promise<{entries: array}>}
2795
- */
2796
-
2797
- }, {
2798
- key: "chmodFile",
2799
- value: function chmodFile(adapter, filename, options) {
2800
- var _this60 = this;
2801
-
2802
- if (Connection.isWeb()) {
2803
- return Promise.reject('Allowed only in admin');
2804
- }
2805
-
2806
- if (!this.connected) {
2807
- return Promise.reject(NOT_CONNECTED);
2808
- }
2809
-
2810
- return new Promise(function (resolve, reject) {
2811
- return _this60._socket.emit('chmodFile', adapter, filename, options, function (err, entries, id) {
2812
- return err ? reject(err) : resolve({
2813
- entries: entries,
2814
- id: id
2815
- });
2816
- });
2817
- });
2818
- }
2819
- /**
2820
- * Change owner or/and owner group for file
2821
- * @param {string} [adapter] adapter name
2822
- * @param {string} [filename] file name with full path. it could be like vis.0/*
2823
- * @param {object} [options] like {owner: 'newOwner', ownerGroup: 'newGroup'}
2824
- * @returns {Promise<{entries: array}>}
2825
- */
2826
-
2827
- }, {
2828
- key: "chownFile",
2829
- value: function chownFile(adapter, filename, options) {
2830
- var _this61 = this;
2831
-
2832
- if (Connection.isWeb()) {
2833
- return Promise.reject('Allowed only in admin');
2834
- }
2835
-
2836
- if (!this.connected) {
2837
- return Promise.reject(NOT_CONNECTED);
2838
- }
2839
-
2840
- return new Promise(function (resolve, reject) {
2841
- return _this61._socket.emit('chownFile', adapter, filename, options, function (err, entries, id) {
2842
- return err ? reject(err) : resolve({
2843
- entries: entries,
2844
- id: id
2845
- });
2846
- });
2847
- });
2848
- }
2849
- /**
2850
- * Check if the file exists
2851
- * @param {string} [adapter] adapter name
2852
- * @param {string} [filename] file name with full path. it could be like vis.0/*
2853
- * @returns {Promise<boolean>}
2854
- */
2855
-
2856
- }, {
2857
- key: "fileExists",
2858
- value: function fileExists(adapter, filename) {
2859
- var _this62 = this;
2860
-
2861
- if (!this.connected) {
2862
- return Promise.reject(NOT_CONNECTED);
2863
- }
2864
-
2865
- return new Promise(function (resolve, reject) {
2866
- return _this62._socket.emit('fileExists', adapter, filename, function (err, exists) {
2867
- return err ? reject(err) : resolve(exists);
2868
- });
2869
- });
2870
- }
2871
- /**
2872
- * Get the alarm notifications from a host (only for admin connection).
2873
- * @param {string} host
2874
- * @param {string} [category] - optional
2875
- * @returns {Promise<any>}
2876
- */
2877
-
2878
- }, {
2879
- key: "getNotifications",
2880
- value: function getNotifications(host, category) {
2881
- var _this63 = this;
2882
-
2883
- if (Connection.isWeb()) {
2884
- return Promise.reject('Allowed only in admin');
2885
- }
2886
-
2887
- if (!this.connected) {
2888
- return Promise.reject(NOT_CONNECTED);
2889
- }
2890
-
2891
- return new Promise(function (resolve) {
2892
- return _this63._socket.emit('sendToHost', host, 'getNotifications', {
2893
- category: category
2894
- }, function (notifications) {
2895
- return resolve(notifications);
2896
- });
2897
- });
2898
- }
2899
- /**
2900
- * Clear the alarm notifications on a host (only for admin connection).
2901
- * @param {string} host
2902
- * @param {string} [category] - optional
2903
- * @returns {Promise<any>}
2904
- */
2905
-
2906
- }, {
2907
- key: "clearNotifications",
2908
- value: function clearNotifications(host, category) {
2909
- var _this64 = this;
2910
-
2911
- if (Connection.isWeb()) {
2912
- return Promise.reject('Allowed only in admin');
2913
- }
2914
-
2915
- if (!this.connected) {
2916
- return Promise.reject(NOT_CONNECTED);
2917
- }
2918
-
2919
- return new Promise(function (resolve) {
2920
- return _this64._socket.emit('sendToHost', host, 'clearNotifications', {
2921
- category: category
2922
- }, function (notifications) {
2923
- return resolve(notifications);
2924
- });
2925
- });
2926
- }
2927
- /**
2928
- * Read if only easy mode is allowed (only for admin connection).
2929
- * @returns {Promise<boolean>}
2930
- */
2931
-
2932
- }, {
2933
- key: "getIsEasyModeStrict",
2934
- value: function getIsEasyModeStrict() {
2935
- var _this65 = this;
2936
-
2937
- if (Connection.isWeb()) {
2938
- return Promise.reject('Allowed only in admin');
2939
- }
2940
-
2941
- if (!this.connected) {
2942
- return Promise.reject(NOT_CONNECTED);
2943
- }
2944
-
2945
- return new Promise(function (resolve, reject) {
2946
- return _this65._socket.emit('getIsEasyModeStrict', function (error, isStrict) {
2947
- return error ? reject(error) : resolve(isStrict);
2948
- });
2949
- });
2950
- }
2951
- /**
2952
- * Read easy mode configuration (only for admin connection).
2953
- * @returns {Promise<any>}
2954
- */
2955
-
2956
- }, {
2957
- key: "getEasyMode",
2958
- value: function getEasyMode() {
2959
- var _this66 = this;
2960
-
2961
- if (Connection.isWeb()) {
2962
- return Promise.reject('Allowed only in admin');
2963
- }
2964
-
2965
- if (!this.connected) {
2966
- return Promise.reject(NOT_CONNECTED);
2967
- }
2968
-
2969
- return new Promise(function (resolve, reject) {
2970
- return _this66._socket.emit('getEasyMode', function (error, config) {
2971
- return error ? reject(error) : resolve(config);
2972
- });
2973
- });
2974
- }
2975
- /**
2976
- * Read current user
2977
- * @returns {Promise<string>}
2978
- */
2979
-
2980
- }, {
2981
- key: "getCurrentUser",
2982
- value: function getCurrentUser() {
2983
- var _this67 = this;
2984
-
2985
- if (!this.connected) {
2986
- return Promise.reject(NOT_CONNECTED);
2987
- }
2988
-
2989
- return new Promise(function (resolve) {
2990
- return _this67._socket.emit('authEnabled', function (isSecure, user) {
2991
- return resolve(user);
2992
- });
2993
- });
2994
- }
2995
- }, {
2996
- key: "getCurrentSession",
2997
- value: function getCurrentSession(cmdTimeout) {
2998
- if (!this.connected) {
2999
- return Promise.reject(NOT_CONNECTED);
3000
- }
3001
-
3002
- return new Promise(function (resolve, reject) {
3003
- var controller = new AbortController();
3004
- var timeout = setTimeout(function () {
3005
- if (timeout) {
3006
- timeout = null;
3007
- controller.abort();
3008
- reject('getCurrentSession timeout');
3009
- }
3010
- }, cmdTimeout || 5000);
3011
- return fetch('./session', {
3012
- signal: controller.signal
3013
- }).then(function (res) {
3014
- return res.json();
3015
- }).then(function (json) {
3016
- if (timeout) {
3017
- clearTimeout(timeout);
3018
- timeout = null;
3019
- resolve(json);
3020
- }
3021
- })["catch"](function (e) {
3022
- reject('getCurrentSession: ' + e);
3023
- });
3024
- });
3025
- }
3026
- /**
3027
- * Read adapter ratings
3028
- * @returns {Promise<any>}
3029
- */
3030
-
3031
- }, {
3032
- key: "getRatings",
3033
- value: function getRatings(update) {
3034
- var _this68 = this;
3035
-
3036
- if (Connection.isWeb()) {
3037
- return Promise.reject('Allowed only in admin');
3038
- }
3039
-
3040
- if (!this.connected) {
3041
- return Promise.reject(NOT_CONNECTED);
3042
- }
3043
-
3044
- return new Promise(function (resolve, reject) {
3045
- return _this68._socket.emit('getRatings', update, function (err, ratings) {
3046
- return err ? reject(err) : resolve(ratings);
3047
- });
3048
- });
3049
- }
3050
- /**
3051
- * Read current web, socketio or admin namespace, like admin.0
3052
- * @returns {Promise<string>}
3053
- */
3054
-
3055
- }, {
3056
- key: "getCurrentInstance",
3057
- value: function getCurrentInstance() {
3058
- var _this69 = this;
3059
-
3060
- if (!this.connected) {
3061
- return Promise.reject(NOT_CONNECTED);
3062
- }
3063
-
3064
- this._promises.currentInstance = this._promises.currentInstance || new Promise(function (resolve, reject) {
3065
- return _this69._socket.emit('getCurrentInstance', function (err, namespace) {
3066
- return err ? reject(err) : resolve(namespace);
3067
- });
3068
- });
3069
- return this._promises.currentInstance;
3070
- } // returns very optimized information for adapters to minimize connection load
3071
-
3072
- }, {
3073
- key: "getCompactAdapters",
3074
- value: function getCompactAdapters(update) {
3075
- var _this70 = this;
3076
-
3077
- if (Connection.isWeb()) {
3078
- return Promise.reject('Allowed only in admin');
3079
- }
3080
-
3081
- if (!update && this._promises.compactAdapters) {
3082
- return this._promises.compactAdapters;
3083
- }
3084
-
3085
- if (!this.connected) {
3086
- return Promise.reject(NOT_CONNECTED);
3087
- }
3088
-
3089
- this._promises.compactAdapters = new Promise(function (resolve, reject) {
3090
- return _this70._socket.emit('getCompactAdapters', function (err, adapters) {
3091
- return err ? reject(err) : resolve(adapters);
3092
- });
3093
- });
3094
- return this._promises.compactAdapters;
3095
- } // returns very optimized information for adapters to minimize connection load
3096
-
3097
- }, {
3098
- key: "getCompactInstances",
3099
- value: function getCompactInstances(update) {
3100
- var _this71 = this;
3101
-
3102
- if (Connection.isWeb()) {
3103
- return Promise.reject('Allowed only in admin');
3104
- }
3105
-
3106
- if (!update && this._promises.compactInstances) {
3107
- return this._promises.compactInstances;
3108
- }
3109
-
3110
- if (!this.connected) {
3111
- return Promise.reject(NOT_CONNECTED);
3112
- }
3113
-
3114
- this._promises.compactInstances = new Promise(function (resolve, reject) {
3115
- return _this71._socket.emit('getCompactInstances', function (err, instances) {
3116
- return err ? reject(err) : resolve(instances);
3117
- });
3118
- });
3119
- return this._promises.compactInstances;
3120
- } // returns very optimized information for adapters to minimize connection load
3121
- // reads only version of installed adapter
3122
-
3123
- }, {
3124
- key: "getCompactInstalled",
3125
- value: function getCompactInstalled(host, update, cmdTimeout) {
3126
- var _this72 = this;
3127
-
3128
- if (Connection.isWeb()) {
3129
- return Promise.reject('Allowed only in admin');
3130
- }
3131
-
3132
- this._promises.installedCompact = this._promises.installedCompact || {};
3133
-
3134
- if (!update && this._promises.installedCompact[host]) {
3135
- return this._promises.installedCompact[host];
3136
- }
3137
-
3138
- if (!this.connected) {
3139
- return Promise.reject(NOT_CONNECTED);
3140
- }
3141
-
3142
- if (!host.startsWith('system.host.')) {
3143
- host += 'system.host.' + host;
3144
- }
3145
-
3146
- this._promises.installedCompact[host] = new Promise(function (resolve, reject) {
3147
- var timeout = setTimeout(function () {
3148
- if (timeout) {
3149
- timeout = null;
3150
- reject('getCompactInstalled timeout');
3151
- }
3152
- }, cmdTimeout || _this72.props.cmdTimeout);
3153
-
3154
- _this72._socket.emit('getCompactInstalled', host, function (data) {
3155
- if (timeout) {
3156
- clearTimeout(timeout);
3157
- timeout = null;
3158
-
3159
- if (data === PERMISSION_ERROR) {
3160
- reject('May not read "getCompactInstalled"');
3161
- } else if (!data) {
3162
- reject('Cannot read "getCompactInstalled"');
3163
- } else {
3164
- resolve(data);
3165
- }
3166
- }
3167
- });
3168
- });
3169
- return this._promises.installedCompact[host];
3170
- } // returns very optimized information for adapters to minimize connection load
3171
-
3172
- }, {
3173
- key: "getCompactSystemConfig",
3174
- value: function getCompactSystemConfig(update) {
3175
- var _this73 = this;
3176
-
3177
- if (!update && this._promises.systemConfigCommon) {
3178
- return this._promises.systemConfigCommon;
3179
- }
3180
-
3181
- if (!this.connected) {
3182
- return Promise.reject(NOT_CONNECTED);
3183
- }
3184
-
3185
- this._promises.systemConfigCommon = new Promise(function (resolve, reject) {
3186
- return _this73._socket.emit('getCompactSystemConfig', function (err, systemConfig) {
3187
- return err ? reject(err) : resolve(systemConfig);
3188
- });
3189
- });
3190
- return this._promises.systemConfigCommon;
3191
- }
3192
- /**
3193
- * Get the repository in compact form (only version and icon).
3194
- * @param {string} host
3195
- * @param {boolean} [update] Force update.
3196
- * @param {number} [timeoutMs] timeout in ms.
3197
- * @returns {Promise<any>}
3198
- */
3199
-
3200
- }, {
3201
- key: "getCompactRepository",
3202
- value: function getCompactRepository(host, update, timeoutMs) {
3203
- var _this74 = this;
3204
-
3205
- if (Connection.isWeb()) {
3206
- return Promise.reject('Allowed only in admin');
3207
- }
3208
-
3209
- if (!update && this._promises.repoCompact) {
3210
- return this._promises.repoCompact;
3211
- }
3212
-
3213
- if (!this.connected) {
3214
- return Promise.reject(NOT_CONNECTED);
3215
- }
3216
-
3217
- if (!host.startsWith('system.host.')) {
3218
- host += 'system.host.' + host;
3219
- }
3220
-
3221
- this._promises.repoCompact = new Promise(function (resolve, reject) {
3222
- var timeout = setTimeout(function () {
3223
- if (timeout) {
3224
- timeout = null;
3225
- reject('getCompactRepository timeout');
3226
- }
3227
- }, timeoutMs || _this74.props.cmdTimeout);
3228
-
3229
- _this74._socket.emit('getCompactRepository', host, function (data) {
3230
- if (timeout) {
3231
- clearTimeout(timeout);
3232
- timeout = null;
3233
-
3234
- if (data === PERMISSION_ERROR) {
3235
- reject('May not read "getCompactRepository"');
3236
- } else if (!data) {
3237
- reject('Cannot read "getCompactRepository"');
3238
- } else {
3239
- resolve(data);
3240
- }
3241
- }
3242
- });
3243
- });
3244
- return this._promises.repoCompact;
3245
- }
3246
- /**
3247
- * Get the list of all hosts in compact form (only _id, common.name, common.icon, common.color, native.hardware.networkInterfaces)
3248
- * @param {boolean} [update] Force update.
3249
- * @returns {Promise<ioBroker.Object[]>}
3250
- */
3251
-
3252
- }, {
3253
- key: "getCompactHosts",
3254
- value: function getCompactHosts(update) {
3255
- var _this75 = this;
3256
-
3257
- if (Connection.isWeb()) {
3258
- return Promise.reject('Allowed only in admin');
3259
- }
3260
-
3261
- if (!update && this._promises.hostsCompact) {
3262
- return this._promises.hostsCompact;
3263
- }
3264
-
3265
- if (!this.connected) {
3266
- return Promise.reject(NOT_CONNECTED);
3267
- }
3268
-
3269
- this._promises.hostsCompact = new Promise(function (resolve, reject) {
3270
- return _this75._socket.emit('getCompactHosts', function (err, hosts) {
3271
- return err ? reject(err) : resolve(hosts);
3272
- });
3273
- });
3274
- return this._promises.hostsCompact;
3275
- }
3276
- /**
3277
- * Get uuid
3278
- * @returns {Promise<ioBroker.Object[]>}
3279
- */
3280
-
3281
- }, {
3282
- key: "getUuid",
3283
- value: function getUuid() {
3284
- if (this._promises.uuid) {
3285
- return this._promises.uuid;
3286
- }
3287
-
3288
- if (!this.connected) {
3289
- return Promise.reject(NOT_CONNECTED);
3290
- }
3291
-
3292
- this._promises.uuid = this.getObject('system.meta.uuid') //@ts-ignore
3293
- .then(function (obj) {
3294
- var _obj$native;
3295
-
3296
- return obj === null || obj === void 0 ? void 0 : (_obj$native = obj["native"]) === null || _obj$native === void 0 ? void 0 : _obj$native.uuid;
3297
- });
3298
- return this._promises.uuid;
3299
- }
3300
- /**
3301
- * Logout current user
3302
- * @returns {Promise<null>}
3303
- */
3304
-
3305
- }, {
3306
- key: "logout",
3307
- value: function logout() {
3308
- var _this76 = this;
3309
-
3310
- if (!this.connected) {
3311
- return Promise.reject(NOT_CONNECTED);
3312
- }
3313
-
3314
- return new Promise(function (resolve, reject) {
3315
- return _this76._socket.emit('logout', function (err) {
3316
- return err ? reject(err) : resolve(null);
3317
- });
3318
- });
3319
- }
3320
- }], [{
3321
- key: "isWeb",
3322
- value: function isWeb() {
3323
- return window.adapterName === 'material' || window.adapterName === 'vis' || window.socketUrl !== undefined;
3324
- }
3325
- }]);
3326
-
3327
- return Connection;
3328
- }();
3329
-
3330
- Connection.Connection = {
3331
- onLog: _propTypes["default"].func,
3332
- onReady: _propTypes["default"].func,
3333
- onProgress: _propTypes["default"].func
3334
- };
3335
- var _default = Connection;
3336
- exports["default"] = _default;
3337
- //# sourceMappingURL=Connection.js.map