@iobroker/socket-classes 1.4.3 → 1.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -978,6 +978,12 @@ Read all instances of the given adapter, or all instances of all adapters if ada
978
978
  -->
979
979
 
980
980
  ## Changelog
981
+ ### 1.4.6 (2023-10-19)
982
+ * (bluefox) Added `publishInstanceMessageAll` command
983
+
984
+ ### 1.4.4 (2023-10-11)
985
+ * (bluefox) Caught errors by subscribe/unsubscribe
986
+
981
987
  ### 1.4.3 (2023-10-07)
982
988
  * (foxriver76) do not await the subscribes anymore
983
989
 
@@ -140,27 +140,24 @@ class SocketAdmin extends SocketCommon {
140
140
  stateChange(id, state) {
141
141
  this.commands.stateChange(id, state);
142
142
 
143
- if (this.server && this.server.sockets) {
144
- const sockets = this.server.sockets.sockets || this.server.sockets.connected;
145
-
143
+ const sockets = this.getSocketsList();
144
+ if (sockets) {
146
145
  sockets.forEach(socket =>
147
146
  this.commands.publish(socket, 'stateChange', id, state));
148
147
  }
149
148
  }
150
149
 
151
150
  fileChange(id, fileName, size) {
152
- if (this.server && this.server.sockets) {
153
- const sockets = this.server.sockets.sockets || this.server.sockets.connected;
154
-
151
+ const sockets = this.getSocketsList();
152
+ if (sockets) {
155
153
  sockets.forEach(socket =>
156
154
  this.commands.publishFile(socket, id, fileName, size));
157
155
  }
158
156
  }
159
157
 
160
158
  repoUpdated() {
161
- if (this.server && this.server.sockets) {
162
- const sockets = this.server.sockets.sockets || this.server.sockets.connected;
163
-
159
+ const sockets = this.getSocketsList();
160
+ if (sockets) {
164
161
  sockets.forEach(socket =>
165
162
  this.__updateSession(socket));
166
163
 
@@ -169,9 +166,8 @@ class SocketAdmin extends SocketCommon {
169
166
  }
170
167
 
171
168
  objectChange(id, obj) {
172
- if (this.server && this.server.sockets) {
173
- const sockets = this.server.sockets.sockets || this.server.sockets.connected;
174
-
169
+ const sockets = this.getSocketsList();
170
+ if (sockets) {
175
171
  sockets.forEach(socket =>
176
172
  this.commands.publish(socket, 'objectChange', id, obj));
177
173
  }
@@ -186,13 +182,14 @@ class SocketAdmin extends SocketCommon {
186
182
  }
187
183
 
188
184
  sendCommand(obj) {
189
- if (this.commands.sendCommand(obj) && this.server && this.server.sockets) {
190
- const sockets = this.server.sockets.sockets || this.server.sockets.connected;
185
+ if (this.commands.sendCommand(obj)) {
186
+ const sockets = this.getSocketsList();
187
+ if (sockets) {
188
+ sockets.forEach(socket =>
189
+ this.__updateSession(socket));
191
190
 
192
- sockets.forEach(socket =>
193
- this.__updateSession(socket));
194
-
195
- this.server.sockets.emit(obj.command, obj.message.id, obj.message.data);
191
+ this.server.sockets.emit(obj.command, obj.message.id, obj.message.data);
192
+ }
196
193
  }
197
194
  }
198
195
 
@@ -317,28 +317,19 @@ class SocketCommands {
317
317
  if (this.subscribes[type][key] === undefined) {
318
318
  this.subscribes[type][key] = 1;
319
319
  if (type === 'stateChange') {
320
- try {
321
- this.adapter.subscribeForeignStatesAsync(pattern, options);
322
- } catch (e) {
323
- this.adapter.log.error(`Cannot subscribe "${pattern}": ${e.message}`);
324
- }
320
+ this.adapter.subscribeForeignStatesAsync(pattern, options)
321
+ .catch(e => this.adapter.log.error(`Cannot subscribe "${pattern}": ${e.message}`));
325
322
  } else if (type === 'objectChange') {
326
- try {
327
- this.adapter.subscribeForeignObjectsAsync(pattern, options);
328
- } catch (e) {
329
- this.adapter.log.error(`Cannot subscribe "${pattern}": ${e.message}`);
330
- }
323
+ this.adapter.subscribeForeignObjectsAsync(pattern, options)
324
+ .catch(e => this.adapter.log.error(`Cannot subscribe "${pattern}": ${e.message}`));
331
325
  } else if (type === 'log') {
332
326
  if (!this.logEnabled && this.adapter.requireLog) {
333
327
  this.logEnabled = true;
334
328
  this.adapter.requireLog(true, options);
335
329
  }
336
330
  } else if (type === 'fileChange' && this.adapter.subscribeForeignFiles) {
337
- try {
338
- this.adapter.subscribeForeignFiles(pattern, patternFile, options);
339
- } catch (e) {
340
- this.adapter.log.error(`Cannot subscribe "${pattern}": ${e.message}`);
341
- }
331
+ this.adapter.subscribeForeignFiles(pattern, patternFile, options)
332
+ .catch(e => this.adapter.log.error(`Cannot subscribe "${pattern}": ${e.message}`));
342
333
  }
343
334
  } else {
344
335
  this.subscribes[type][key]++;
@@ -377,28 +368,19 @@ class SocketCommands {
377
368
  this.subscribes[type][key]--;
378
369
  if (this.subscribes[type][key] <= 0) {
379
370
  if (type === 'stateChange') {
380
- try {
381
- this.adapter.unsubscribeForeignStatesAsync(pattern, options);
382
- } catch (e) {
383
- this.adapter.log.error(`Cannot unsubscribe "${pattern}": ${e.message}`);
384
- }
371
+ this.adapter.unsubscribeForeignStatesAsync(pattern, options)
372
+ .catch(e => this.adapter.log.error(`Cannot unsubscribe "${pattern}": ${e.message}`));
385
373
  } else if (type === 'objectChange') {
386
- try {
387
- this.adapter.unsubscribeForeignObjectsAsync(pattern, options);
388
- } catch (e) {
389
- this.adapter.log.error(`Cannot unsubscribe "${pattern}": ${e.message}`);
390
- }
374
+ this.adapter.unsubscribeForeignObjectsAsync(pattern, options)
375
+ .catch(e => this.adapter.log.error(`Cannot unsubscribe "${pattern}": ${e.message}`));
391
376
  } else if (type === 'log') {
392
377
  if (this.logEnabled && this.adapter.requireLog) {
393
378
  this.logEnabled = false;
394
379
  this.adapter.requireLog(false, options);
395
380
  }
396
381
  } else if (type === 'fileChange' && this.adapter.unsubscribeForeignFiles) {
397
- try {
398
- this.adapter.unsubscribeForeignFiles(pattern, patternFile, options);
399
- } catch (e) {
400
- this.adapter.log.error(`Cannot unsubscribe "${pattern}": ${e.message}`);
401
- }
382
+ this.adapter.unsubscribeForeignFiles(pattern, patternFile, options)
383
+ .catch(e => this.adapter.log.error(`Cannot unsubscribe "${pattern}": ${e.message}`));
402
384
  }
403
385
  delete this.subscribes[type][pattern];
404
386
  }
@@ -415,28 +397,19 @@ class SocketCommands {
415
397
  this.subscribes[type][key]--;
416
398
  if (this.subscribes[type][key] <= 0) {
417
399
  if (type === 'stateChange') {
418
- try {
419
- this.adapter.unsubscribeForeignStatesAsync(pattern, options);
420
- } catch (e) {
421
- this.adapter.log.error(`Cannot unsubscribe "${pattern}": ${e.message}`);
422
- }
400
+ this.adapter.unsubscribeForeignStatesAsync(pattern, options)
401
+ .catch(e => this.adapter.log.error(`Cannot unsubscribe "${pattern}": ${e.message}`));
423
402
  } else if (type === 'objectChange') {
424
- try {
425
- this.adapter.unsubscribeForeignObjectsAsync(pattern, options);
426
- } catch (e) {
427
- this.adapter.log.error(`Cannot unsubscribe "${pattern}": ${e.message}`);
428
- }
403
+ this.adapter.unsubscribeForeignObjectsAsync(pattern, options)
404
+ .catch(e => this.adapter.log.error(`Cannot unsubscribe "${pattern}": ${e.message}`));
429
405
  } else if (type === 'log') {
430
406
  if (this.adapter.requireLog && this.logEnabled) {
431
407
  this.logEnabled = false;
432
408
  this.adapter.requireLog(false, options);
433
409
  }
434
410
  } else if (type === 'fileChange' && this.adapter.unsubscribeForeignFiles) {
435
- try {
436
- this.adapter.unsubscribeForeignFiles(pattern, patternFile, options);
437
- } catch (e) {
438
- this.adapter.log.error(`Cannot unsubscribe "${pattern}": ${e.message}`);
439
- }
411
+ this.adapter.unsubscribeForeignFiles(pattern, patternFile, options)
412
+ .catch(e => this.adapter.log.error(`Cannot unsubscribe "${pattern}": ${e.message}`));
440
413
  }
441
414
  delete this.subscribes[type][key];
442
415
  }
@@ -444,17 +417,11 @@ class SocketCommands {
444
417
  } else {
445
418
  for (const pattern of Object.keys(this.subscribes[type])) {
446
419
  if (type === 'stateChange') {
447
- try {
448
- this.adapter.unsubscribeForeignStatesAsync(pattern, options);
449
- } catch (e) {
450
- this.adapter.log.error(`Cannot unsubscribe "${pattern}": ${e.message}`);
451
- }
420
+ this.adapter.unsubscribeForeignStatesAsync(pattern, options)
421
+ .catch(e => this.adapter.log.error(`Cannot unsubscribe "${pattern}": ${e.message}`));
452
422
  } else if (type === 'objectChange') {
453
- try {
454
- this.adapter.unsubscribeForeignObjectsAsync(pattern, options);
455
- } catch (e) {
456
- this.adapter.log.error(`Cannot unsubscribe "${pattern}": ${e.message}`);
457
- }
423
+ this.adapter.unsubscribeForeignObjectsAsync(pattern, options)
424
+ .catch(e => this.adapter.log.error(`Cannot unsubscribe "${pattern}": ${e.message}`));
458
425
  } else if (type === 'log') {
459
426
  // console.log((socket._name || socket.id) + ' requireLog false');
460
427
  if (this.adapter.requireLog && this.logEnabled) {
@@ -463,11 +430,8 @@ class SocketCommands {
463
430
  }
464
431
  } else if (type === 'fileChange' && this.adapter.unsubscribeForeignFiles) {
465
432
  const [id, fileName] = pattern.split('####');
466
- try {
467
- this.adapter.unsubscribeForeignFiles(id, fileName, options);
468
- } catch (e) {
469
- this.adapter.log.error(`Cannot unsubscribe "${pattern}": ${e.message}`);
470
- }
433
+ this.adapter.unsubscribeForeignFiles(id, fileName, options)
434
+ .catch(e => this.adapter.log.error(`Cannot unsubscribe "${pattern}": ${e.message}`));
471
435
  }
472
436
  }
473
437
 
@@ -496,17 +460,11 @@ class SocketCommands {
496
460
  if (this.subscribes[type][pattern] === undefined) {
497
461
  this.subscribes[type][pattern] = 1;
498
462
  if (type === 'stateChange') {
499
- try {
500
- this.adapter.subscribeForeignStatesAsync(pattern, options);
501
- } catch (e) {
502
- this.adapter.log.error(`Cannot subscribe "${pattern}": ${e.message}`);
503
- }
463
+ this.adapter.subscribeForeignStatesAsync(pattern, options)
464
+ .catch(e => this.adapter.log.error(`Cannot subscribe "${pattern}": ${e.message}`));
504
465
  } else if (type === 'objectChange') {
505
- try {
506
- this.adapter.subscribeForeignObjectsAsync(pattern, options);
507
- } catch (e) {
508
- this.adapter.log.error(`Cannot subscribe "${pattern}": ${e.message}`);
509
- }
466
+ this.adapter.subscribeForeignObjectsAsync(pattern, options)
467
+ .catch(e => this.adapter.log.error(`Cannot subscribe "${pattern}": ${e.message}`));
510
468
  } else if (type === 'log') {
511
469
  if (this.adapter.requireLog && !this.logEnabled) {
512
470
  this.logEnabled = true;
@@ -514,11 +472,8 @@ class SocketCommands {
514
472
  }
515
473
  } else if (type === 'fileChange' && this.adapter.subscribeForeignFiles) {
516
474
  const [id, fileName] = pattern.split('####');
517
- try {
518
- this.adapter.subscribeForeignFiles(id, fileName, options);
519
- } catch (e) {
520
- this.adapter.log.error(`Cannot subscribe "${pattern}": ${e.message}`);
521
- }
475
+ this.adapter.subscribeForeignFiles(id, fileName, options)
476
+ .catch(e => this.adapter.log.error(`Cannot subscribe "${pattern}": ${e.message}`));
522
477
  }
523
478
  } else {
524
479
  this.subscribes[type][pattern]++;
@@ -550,17 +505,11 @@ class SocketCommands {
550
505
  this.subscribes[type][pattern]--;
551
506
  if (this.subscribes[type][pattern] <= 0) {
552
507
  if (type === 'stateChange') {
553
- try {
554
- this.adapter.unsubscribeForeignStatesAsync(pattern, options);
555
- } catch (e) {
556
- this.adapter.log.error(`Cannot unsubscribe "${pattern}": ${e.message}`);
557
- }
508
+ this.adapter.unsubscribeForeignStatesAsync(pattern, options)
509
+ .catch(e => this.adapter.log.error(`Cannot unsubscribe "${pattern}": ${e.message}`));
558
510
  } else if (type === 'objectChange') {
559
- try {
560
- this.adapter.unsubscribeForeignObjectsAsync(pattern, options);
561
- } catch (e) {
562
- this.adapter.log.error(`Cannot unsubscribe "${pattern}": ${e.message}`);
563
- }
511
+ this.adapter.unsubscribeForeignObjectsAsync(pattern, options)
512
+ .catch(e => this.adapter.log.error(`Cannot unsubscribe "${pattern}": ${e.message}`));
564
513
  } else if (type === 'log') {
565
514
  if (this.adapter.requireLog && !this.logEnabled) {
566
515
  this.logEnabled = true;
@@ -568,11 +517,8 @@ class SocketCommands {
568
517
  }
569
518
  } else if (type === 'fileChange' && this.adapter.unsubscribeForeignFiles) {
570
519
  const [id, fileName] = pattern.split('####');
571
- try {
572
- this.adapter.unsubscribeForeignFiles(id, fileName, options);
573
- } catch (e) {
574
- this.adapter.log.error(`Cannot unsubscribe "${pattern}": ${e.message}`);
575
- }
520
+ this.adapter.unsubscribeForeignFiles(id, fileName, options)
521
+ .catch(e => this.adapter.log.error(`Cannot unsubscribe "${pattern}": ${e.message}`));
576
522
  }
577
523
  delete this.subscribes[type][pattern];
578
524
  }
@@ -326,13 +326,17 @@ class SocketCommandsAdmin extends SocketCommands {
326
326
  this.eventsThreshold.timeActivated = 0;
327
327
  this.adapter.log.info('Subscribe to all states again');
328
328
 
329
- setTimeout(() => {
329
+ setTimeout(async () => {
330
330
  this.onThresholdChanged && this.onThresholdChanged(false);
331
- this.adapter.unsubscribeForeignStates('system.adapter.*');
331
+ try {
332
+ await this.adapter.unsubscribeForeignStatesAsync('system.adapter.*');
333
+ } catch (e) {
334
+ this.adapter.log.error(`Cannot unsubscribe "system.adapter.*": ${e.message}`);
335
+ }
332
336
 
333
337
  for (const pattern of Object.keys(this.subscribes.stateChange)) {
334
338
  try {
335
- this.adapter.subscribeForeignStatesAsync(pattern);
339
+ await this.adapter.subscribeForeignStatesAsync(pattern);
336
340
  } catch (e) {
337
341
  this.adapter.log.error(`Cannot subscribe "${pattern}": ${e.message}`);
338
342
  }
@@ -345,7 +349,7 @@ class SocketCommandsAdmin extends SocketCommands {
345
349
  if (!this.eventsThreshold.active) {
346
350
  this.eventsThreshold.active = true;
347
351
 
348
- setTimeout(() => {
352
+ setTimeout(async () => {
349
353
  this.adapter.log.info(
350
354
  `Unsubscribe from all states, except system's, because over ${this.eventsThreshold.repeatSeconds} seconds the number of events is over ${this.eventsThreshold.value} (in last second ${this.eventsThreshold.count})`
351
355
  );
@@ -355,13 +359,17 @@ class SocketCommandsAdmin extends SocketCommands {
355
359
 
356
360
  for (const pattern of Object.keys(this.subscribes.stateChange)) {
357
361
  try {
358
- this.adapter.unsubscribeForeignStatesAsync(pattern);
362
+ await this.adapter.unsubscribeForeignStatesAsync(pattern);
359
363
  } catch (e) {
360
364
  this.adapter.log.error(`Cannot unsubscribe "${pattern}": ${e.message}`);
361
365
  }
362
366
  }
363
367
 
364
- this.adapter.subscribeForeignStates('system.adapter.*');
368
+ try {
369
+ await this.adapter.subscribeForeignStatesAsync('system.adapter.*');
370
+ } catch (e) {
371
+ this.adapter.log.error(`Cannot subscribe "system.adapter.*": ${e.message}`);
372
+ }
365
373
  }, 100);
366
374
  }
367
375
  }
@@ -198,9 +198,10 @@ class SocketCommon {
198
198
  _unsubscribeAll() {
199
199
  if (this.server && this.server.ioBroker) {
200
200
  // this could be an object or array
201
- const sockets = this.server.sockets.sockets || this.server.sockets.connected;
202
-
203
- Object.keys(sockets).forEach(i => this.commands.unsubscribeSocket(sockets[i]));
201
+ const sockets = this.getSocketsList();
202
+ if (sockets) {
203
+ Object.keys(sockets).forEach(i => this.commands.unsubscribeSocket(sockets[i]));
204
+ }
204
205
  } else if (this.server && this.server.sockets) {
205
206
  for (const socket in this.server.sockets) {
206
207
  if (Object.prototype.hasOwnProperty.call(this.server.sockets, socket)) {
@@ -383,10 +384,9 @@ class SocketCommon {
383
384
 
384
385
  if (this.server) {
385
386
  const clientsArray = [];
386
- if (this.server.sockets) {
387
+ const sockets = this.getSocketsList();
388
+ if (sockets) {
387
389
  // this could be an object or array
388
- const sockets = this.server.sockets.sockets || this.server.sockets.connected;
389
-
390
390
  Object.keys(sockets).forEach(i => clientsArray.push(sockets[i]._name || 'noname'));
391
391
  }
392
392
  const text = `[${clientsArray.length}]${clientsArray.join(', ')}`;
@@ -423,15 +423,33 @@ class SocketCommon {
423
423
  return this.commands.publishFile(socket, id, fileName, size);
424
424
  }
425
425
 
426
+ getSocketsList() {
427
+ if (this.server && this.server.sockets) {
428
+ // this could be an object or array
429
+ return this.server.sockets.sockets || this.server.sockets.connected;
430
+ }
431
+ return null;
432
+ }
433
+
434
+ publishInstanceMessageAll(sourceInstance, messageType, sid, data) {
435
+ const sockets = this.getSocketsList();
436
+ if (sockets) {
437
+ Object.keys(sockets).forEach(i => {
438
+ if (sockets[i].id === sid) {
439
+ this.publishInstanceMessage(sockets[i], sourceInstance, messageType, data);
440
+ }
441
+ });
442
+ }
443
+ }
444
+
426
445
  close() {
427
446
  this._unsubscribeAll();
428
447
 
429
448
  this.commands.destroy();
430
449
 
431
- if (this.server && this.server.sockets) {
450
+ const sockets = this.getSocketsList();
451
+ if (sockets) {
432
452
  // this could be an object or array
433
- const sockets = this.server.sockets.sockets || this.server.sockets.connected;
434
-
435
453
  Object.keys(sockets).forEach(i => {
436
454
  const socket = sockets[i];
437
455
  if (socket._sessionTimer) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iobroker/socket-classes",
3
- "version": "1.4.3",
3
+ "version": "1.4.6",
4
4
  "description": "ioBroker server-side web sockets",
5
5
  "author": {
6
6
  "name": "bluefox",
@@ -21,7 +21,7 @@
21
21
  "url": "https://github.com/ioBroker/ioBroker.socket-classes"
22
22
  },
23
23
  "dependencies": {
24
- "@iobroker/adapter-core": "^3.0.3",
24
+ "@iobroker/adapter-core": "^3.0.4",
25
25
  "axios": "^1.5.1",
26
26
  "cookie-parser": "^1.4.6",
27
27
  "passport": "^0.6.0"