@iobroker/socket-classes 0.1.2 → 0.1.5

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
@@ -12,7 +12,7 @@ This library is used for following adapters:
12
12
  ```
13
13
  const TTL_SEC = 3600;
14
14
 
15
- const SocketAdmin = require('./lib/socketAdmin');
15
+ const SocketAdmin = require('@iobroker/socket-classes').SocketAdmin;
16
16
  const ws = require('@iobroker/ws-server');
17
17
  const session = require('express-session');
18
18
  const utils = require('@iobroker/adapter-core'); // Get common adapter utils
@@ -44,7 +44,7 @@ io.close();
44
44
  const TTL_SEC = 3600;
45
45
 
46
46
  const ws = require('@iobroker/ws-server');
47
- const SocketWS = require('./lib/socketWS.js');
47
+ const SocketWS = require('@iobroker/socket-classes').SocketCommon;
48
48
  const session = require('express-session');
49
49
  const utils = require('@iobroker/adapter-core'); // Get common adapter utils
50
50
  const AdapterStore = require(utils.controllerDir + '/lib/session.js')(session, TTL_SEC);
@@ -69,6 +69,12 @@ io.close();
69
69
  -->
70
70
 
71
71
  ## Changelog
72
+ ### 0.1.5 (2022-04-25)
73
+ * (bluefox) added updateRatings
74
+
75
+ ### 0.1.4 (2022-04-24)
76
+ * (bluefox) added passportSocket
77
+
72
78
  ### 0.1.2 (2022-04-24)
73
79
  * (bluefox) initial commit
74
80
 
package/index.js CHANGED
@@ -2,10 +2,18 @@ const SocketAdmin = require('./lib/socketAdmin');
2
2
  const SocketCommon = require('./lib/socketCommon');
3
3
  const SocketCommands = require('./lib/socketCommands');
4
4
  const SocketCommandsAdmin = require('./lib/socketCommandsAdmin');
5
+ const passportSocket = require('./lib/passportSocket');
5
6
 
6
7
  module.exports = {
7
8
  SocketAdmin,
8
9
  SocketCommon,
9
10
  SocketCommands,
10
- SocketCommandsAdmin
11
+ SocketCommandsAdmin,
12
+ passportSocket,
13
+ get passport() {
14
+ return require('passport');
15
+ },
16
+ get cookieParser() {
17
+ return require('cookie-parser');
18
+ }
11
19
  };
@@ -173,6 +173,10 @@ class SocketAdmin extends SocketCommon {
173
173
  this.server.sockets.emit(obj.command, obj.message.id, obj.message.data);
174
174
  }
175
175
  }
176
+
177
+ updateRatings(uuid, isAutoUpdate) {
178
+ return this.commands.updateRatings(uuid, isAutoUpdate);
179
+ }
176
180
  }
177
181
 
178
182
  module.exports = SocketAdmin;
@@ -750,18 +750,18 @@ class SocketCommands {
750
750
  }
751
751
  };
752
752
 
753
- this.commands['chownFile'] = (socket, _adapter, filename, options, callback) => {
754
- if (this._checkPermissions(socket, 'chownFile', callback, filename)) {
753
+ this.commands['chownFile'] = (socket, _adapter, fileName, options, callback) => {
754
+ if (this._checkPermissions(socket, 'chownFile', callback, fileName)) {
755
755
  options = options || {};
756
756
  options.user = socket._acl.user;
757
- this.adapter.chownFile(_adapter, filename, options, (err, ...args) =>
757
+ this.adapter.chownFile(_adapter, fileName, options, (err, ...args) =>
758
758
  SocketCommands._fixCallback(callback, err, ...args));
759
759
  }
760
760
  };
761
761
 
762
- this.commands['fileExists'] = (socket, _adapter, filename, callback) => {
763
- if (this._checkPermissions(socket, 'fileExists', callback, filename)) {
764
- this.adapter.fileExists(_adapter, filename, {user: socket._acl.user}, (err, ...args) =>
762
+ this.commands['fileExists'] = (socket, _adapter, fileName, callback) => {
763
+ if (this._checkPermissions(socket, 'fileExists', callback, fileName)) {
764
+ this.adapter.fileExists(_adapter, fileName, {user: socket._acl.user}, (err, ...args) =>
765
765
  SocketCommands._fixCallback(callback, err, ...args));
766
766
  }
767
767
  };
@@ -97,6 +97,36 @@ class SocketCommandsAdmin extends SocketCommands {
97
97
  }
98
98
  }
99
99
 
100
+ async updateRatings(uuid) {
101
+ if (!uuid) {
102
+ const obj = await this.adapter.getForeignObjectAsync('system.meta.uuid');
103
+ uuid = obj.native.uuid;
104
+ }
105
+
106
+ try {
107
+ const response = await axios.get('https://rating.iobroker.net/rating?uuid=' + uuid, {timeout: 15000, validateStatus: status => status < 400})
108
+ this.adapter._ratings = response.data;
109
+ if (!this.adapter._ratings || typeof this.adapter._ratings !== 'object' || Array.isArray(this.adapter._ratings)) {
110
+ this.adapter._ratings = {};
111
+ }
112
+ this.adapter._ratings.uuid = uuid;
113
+
114
+ // auto update only in admin
115
+ if (this.adapter.name === 'admin') {
116
+ this.adapter.ratingTimeout && clearTimeout(this.adapter.ratingTimeout);
117
+ this.adapter.ratingTimeout = setTimeout(() => {
118
+ this.adapter.ratingTimeout = null;
119
+ this.updateRatings(uuid, true)
120
+ .then(() => this.adapter.log.info('Adapter rating updated'));
121
+ }, 24 * 3600000);
122
+ }
123
+
124
+ return this.adapter._ratings;
125
+ } catch (error) {
126
+ this.adapter.log.warn('Cannot update rating: ' + (error.response ? error.response.data : (error.message || error.code)));
127
+ }
128
+ }
129
+
100
130
  _readInstanceConfig(id, user, isTab, configs) {
101
131
  return new Promise(resolve =>
102
132
  this.adapter.getForeignObject('system.adapter.' + id, {user}, (err, obj) => {
@@ -607,25 +637,25 @@ class SocketCommandsAdmin extends SocketCommands {
607
637
  if (config && config.log && config.log.transport) {
608
638
  for (const transport in config.log.transport) {
609
639
  if (Object.prototype.hasOwnProperty.call(config.log.transport, transport) && config.log.transport[transport].type === 'file') {
610
- let filename = config.log.transport[transport].filename || 'log/';
611
- const parts = filename.replace(/\\/g, '/').split('/');
640
+ let fileName = config.log.transport[transport].filename || 'log/';
641
+ const parts = fileName.replace(/\\/g, '/').split('/');
612
642
  parts.pop();
613
- filename = parts.join('/');
614
- if (filename[0] !== '/' && !filename.match(/^\W:/)) {
615
- const _filename = path.normalize(__dirname + '/../../../') + filename;
643
+ fileName = parts.join('/');
644
+ if (fileName[0] !== '/' && !fileName.match(/^\W:/)) {
645
+ const _filename = path.normalize(__dirname + '/../../../') + fileName;
616
646
  if (!fs.existsSync(_filename)) {
617
- filename = path.normalize(__dirname + '/../../') + filename;
647
+ fileName = path.normalize(__dirname + '/../../') + fileName;
618
648
  } else {
619
- filename = _filename;
649
+ fileName = _filename;
620
650
  }
621
651
  }
622
- if (fs.existsSync(filename)) {
623
- const files = fs.readdirSync(filename);
652
+ if (fs.existsSync(fileName)) {
653
+ const files = fs.readdirSync(fileName);
624
654
 
625
655
  for (let f = 0; f < files.length; f++) {
626
656
  try {
627
657
  if (!files[f].endsWith('-audit.json')) {
628
- const stat = fs.lstatSync(filename + '/' + files[f]);
658
+ const stat = fs.lstatSync(fileName + '/' + files[f]);
629
659
  if (!stat.isDirectory()) {
630
660
  result.list.push({
631
661
  fileName: 'log/' + transport + '/' + files[f],
@@ -636,7 +666,7 @@ class SocketCommandsAdmin extends SocketCommands {
636
666
  } catch (e) {
637
667
  // push unchecked
638
668
  // result.list.push('log/' + transport + '/' + files[f]);
639
- this.adapter.log.error(`Cannot check file: ${filename}/${files[f]}`);
669
+ this.adapter.log.error(`Cannot check file: ${fileName}/${files[f]}`);
640
670
  }
641
671
  }
642
672
  }
@@ -692,8 +722,8 @@ class SocketCommandsAdmin extends SocketCommands {
692
722
  };
693
723
 
694
724
  this.commands['getRatings'] = (socket, update, callback) => {
695
- if (update) {
696
- this.adapter._updateRatings()
725
+ if (update || !this.adapter._ratings) {
726
+ this.updateRatings()
697
727
  .then(() => typeof callback === 'function' && callback(null, this.adapter._ratings));
698
728
  } else {
699
729
  typeof callback === 'function' && callback(null, this.adapter._ratings);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iobroker/socket-classes",
3
- "version": "0.1.2",
3
+ "version": "0.1.5",
4
4
  "description": "ioBroker server-side web sockets",
5
5
  "author": {
6
6
  "name": "bluefox",