@iobroker/socket-classes 0.5.3 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,21 +1,7 @@
1
+ const utils = require('@iobroker/adapter-core'); // Get common adapter utils
2
+ const pattern2RegEx = utils.commonTools.pattern2RegEx;
1
3
  let axios = null;
2
- let zipFiles;
3
-
4
- // taken from here: https://github.com/ioBroker/ioBroker.js-controller/blob/master/packages/common/src/lib/common/tools.ts#L2386
5
- /**
6
- * Converts ioB pattern into regex.
7
- * @param pattern - Regex string to use it in new RegExp(pattern)
8
- */
9
- function pattern2RegEx(pattern) {
10
- pattern = (pattern || '').toString();
11
-
12
- const startsWithWildcard = pattern[0] === '*';
13
- const endsWithWildcard = pattern[pattern.length - 1] === '*';
14
-
15
- pattern = pattern.replace(/[-/\\^$+?.()|[\]{}]/g, '\\$&').replace(/\*/g, '.*');
16
-
17
- return (startsWithWildcard ? '' : '^') + pattern + (endsWithWildcard ? '' : '$');
18
- }
4
+ let zipFiles = null;
19
5
 
20
6
  class SocketCommands {
21
7
  static ERROR_PERMISSION = 'permissionError';
@@ -483,7 +469,7 @@ class SocketCommands {
483
469
 
484
470
  _subscribeStates(socket, pattern, callback) {
485
471
  if (this._checkPermissions(socket, 'subscribe', callback, pattern)) {
486
- if (pattern && typeof pattern === 'object' && pattern instanceof Array) {
472
+ if (Array.isArray(pattern)) {
487
473
  for (let p = 0; p < pattern.length; p++) {
488
474
  this.subscribe(socket, 'stateChange', pattern[p]);
489
475
  }
@@ -515,7 +501,7 @@ class SocketCommands {
515
501
 
516
502
  _subscribeFiles(socket, id, pattern, callback) {
517
503
  if (this._checkPermissions(socket, 'subscribeFiles', callback, pattern)) {
518
- if (pattern && typeof pattern === 'object' && pattern instanceof Array) {
504
+ if (Array.isArray(pattern)) {
519
505
  for (let p = 0; p < pattern.length; p++) {
520
506
  this.subscribe(socket, 'fileChange', id, pattern[p]);
521
507
  }
@@ -531,12 +517,12 @@ class SocketCommands {
531
517
 
532
518
  _unsubscribeFiles(socket, id, pattern, callback) {
533
519
  if (this._checkPermissions(socket, 'unsubscribeFiles', callback, pattern)) {
534
- if (pattern && typeof pattern === 'object' && pattern instanceof Array) {
520
+ if (Array.isArray(pattern)) {
535
521
  for (let p = 0; p < pattern.length; p++) {
536
522
  this.unsubscribe(socket, 'fileChange', id, pattern[p]);
537
523
  }
538
524
  } else {
539
- this.unsubscribe(socket, 'fileChange', pattern);
525
+ this.unsubscribe(socket, 'fileChange', id, pattern);
540
526
  }
541
527
 
542
528
  this.adapter.log.level === 'debug' && this._showSubscribes(socket, 'fileChange');
@@ -595,6 +581,10 @@ class SocketCommands {
595
581
 
596
582
  __initCommandsCommon() {
597
583
  this.commands['authenticate'] = (socket, user, pass, callback) => {
584
+ // Authenticate user by login and password
585
+ // @param {string} user - user name
586
+ // @param {string} pass - password
587
+ // @param {function} callback - `function (isUserAuthenticated, isAuthenticationUsed)`
598
588
  if (socket && socket.___socket) {
599
589
  socket = socket.___socket;
600
590
  }
@@ -615,10 +605,15 @@ class SocketCommands {
615
605
  };
616
606
 
617
607
  this.commands['error'] = (socket, error) => {
608
+ // Write error into ioBroker log
609
+ // @param {string} error - error text
618
610
  this.adapter.log.error('Socket error: ' + error);
619
611
  };
620
612
 
621
613
  this.commands['log'] = (socket, text, level) => {
614
+ // Write log entry into ioBroker log
615
+ // @param {string} text - log text
616
+ // @param {string} level - one of `['silly', 'debug', 'info', 'warn', 'error']`. Default is 'debug'.
622
617
  if (level === 'error') {
623
618
  this.adapter.log.error(text);
624
619
  } else if (level === 'warn') {
@@ -632,6 +627,10 @@ class SocketCommands {
632
627
 
633
628
  // new History
634
629
  this.commands['getHistory'] = (socket, id, options, callback) => {
630
+ // Get history data from specific instance
631
+ // @param {string} id - object ID
632
+ // @param {object} options - See object description here: https://github.com/ioBroker/ioBroker.history/blob/master/docs/en/README.md#access-values-from-javascript-adapter
633
+ // @param {function} callback - `function (error, result)`
635
634
  if (this._checkPermissions(socket, 'getStateHistory', callback, id)) {
636
635
  if (typeof options === 'string') {
637
636
  options = {
@@ -653,6 +652,9 @@ class SocketCommands {
653
652
 
654
653
  // HTTP
655
654
  this.commands['httpGet'] = (socket, url, callback) => {
655
+ // Read content of HTTP(S) page server-side (without CORS and stuff)
656
+ // @param {string} url - Page URL
657
+ // @param {function} callback - `function (error, {status, statusText}, body)`
656
658
  if (this._checkPermissions(socket, 'httpGet', callback, url)) {
657
659
  axios = axios || require('axios');
658
660
  this.adapter.log.debug('httpGet: ' + url);
@@ -672,6 +674,11 @@ class SocketCommands {
672
674
 
673
675
  // commands
674
676
  this.commands['sendTo'] = (socket, adapterInstance, command, message, callback) => {
677
+ // Send message to specific instance
678
+ // @param {string} adapterInstance - instance name, e.g. `history.0`
679
+ // @param {string} command - command name
680
+ // @param {object} message - message is instance dependent
681
+ // @param {function} callback - `function (result)`
675
682
  if (this._checkPermissions(socket, 'sendTo', callback, command)) {
676
683
  try {
677
684
  this.adapter.sendTo(adapterInstance, command, message, res =>
@@ -687,15 +694,15 @@ class SocketCommands {
687
694
  const protectedCommands = ['cmdExec', 'getLocationOnDisk', 'getDiagData', 'getDevList', 'delLogs', 'writeDirAsZip', 'writeObjectsAsZip', 'readObjectsAsZip', 'checkLogging', 'updateMultihost', 'rebuildAdapter'];
688
695
 
689
696
  this.commands['sendToHost'] = (socket, host, command, message, callback) => {
690
- // host can answer following commands: cmdExec, getRepository, getInstalled, getInstalledAdapter, getVersion, getDiagData, getLocationOnDisk, getDevList, getLogs, getHostInfo,
691
- // delLogs, readDirAsZip, writeDirAsZip, readObjectsAsZip, writeObjectsAsZip, checkLogging, updateMultihost
697
+ // Send message to specific host.
698
+ // Host can answer following commands: `cmdExec, getRepository, getInstalled, getInstalledAdapter, getVersion, getDiagData, getLocationOnDisk, getDevList, getLogs, getHostInfo, delLogs, readDirAsZip, writeDirAsZip, readObjectsAsZip, writeObjectsAsZip, checkLogging, updateMultihost`.
699
+ // @param {string} host - instance name, e.g. `history.0`
700
+ // @param {string} command - command name
701
+ // @param {object} message - message is command specific
702
+ // @param {function} callback - `function (result)`
692
703
  if (this._checkPermissions(socket, protectedCommands.includes(command) ? 'cmdExec' : 'sendToHost', callback, command)) {
693
704
  // Try to decode this file locally as redis has a limitation for files bigger than 20MB
694
705
  if (command === 'writeDirAsZip' && message && message.data.length > 1024 * 1024) {
695
- if (!zipFiles) {
696
- const utils = require('@iobroker/adapter-core'); // Get common adapter utils
697
- zipFiles = require(utils.controllerDir + '/lib/zipFiles.js');
698
- }
699
706
  let buffer;
700
707
  try {
701
708
  buffer = Buffer.from(message.data, 'base64');
@@ -704,6 +711,8 @@ class SocketCommands {
704
711
  return callback && callback({error: `Cannot convert data: ${error.toString()}`});
705
712
  }
706
713
 
714
+ zipFiles = zipFiles || utils.commonTools.zipFiles;
715
+
707
716
  zipFiles
708
717
  .writeDirAsZip(
709
718
  this.adapter, // normally we have to pass here the internal "objects" object, but as
@@ -733,6 +742,8 @@ class SocketCommands {
733
742
  };
734
743
 
735
744
  this.commands['authEnabled'] = (socket, callback) => {
745
+ // Ask server is authentication enabled and if the user authenticated
746
+ // @param {function} callback - `function (isAuthenticationUsed, userName)`
736
747
  if (this._checkPermissions(socket, 'authEnabled', callback)) {
737
748
  if (typeof callback === 'function') {
738
749
  callback(this.adapter.config.auth, (socket._acl.user || '').replace(/^system\.user\./, ''));
@@ -743,10 +754,14 @@ class SocketCommands {
743
754
  };
744
755
 
745
756
  this.commands['logout'] = (socket, callback) => {
757
+ // Logout user
758
+ // @param {function} callback - function (error)
746
759
  this.adapter.destroySession(socket._sessionID, callback);
747
760
  };
748
761
 
749
762
  this.commands['listPermissions'] = (socket, callback) => {
763
+ // List commands and permissions
764
+ // @param {function} callback - `function (permissions)`
750
765
  if (typeof callback === 'function') {
751
766
  callback(SocketCommands.COMMANDS_PERMISSIONS);
752
767
  } else {
@@ -755,6 +770,8 @@ class SocketCommands {
755
770
  };
756
771
 
757
772
  this.commands['getUserPermissions'] = (socket, callback) => {
773
+ // Get user permissions
774
+ // @param {function} callback - `function (error, permissions)`
758
775
  if (this._checkPermissions(socket, 'getUserPermissions', callback)) {
759
776
  if (typeof callback === 'function') {
760
777
  callback(null, socket._acl);
@@ -765,6 +782,8 @@ class SocketCommands {
765
782
  };
766
783
 
767
784
  this.commands['getVersion'] = (socket, callback) => {
785
+ // Get adapter version. Not the socket-classes version!
786
+ // @param {function} callback - `function (error, adapterVersion, adapterName)`
768
787
  if (this._checkPermissions(socket, 'getVersion', callback)) {
769
788
  if (typeof callback === 'function') {
770
789
  callback(null, this.adapter.version, this.adapter.name);
@@ -775,6 +794,8 @@ class SocketCommands {
775
794
  };
776
795
 
777
796
  this.commands['getAdapterName'] = (socket, callback) => {
797
+ // Get adapter name. Not the socket-classes version!
798
+ // @param {function} callback - `function (error, adapterVersion)`
778
799
  if (this._checkPermissions(socket, 'getAdapterName', callback)) {
779
800
  if (typeof callback === 'function') {
780
801
  callback(null, this.adapter.name || 'unknown');
@@ -783,12 +804,15 @@ class SocketCommands {
783
804
  }
784
805
  }
785
806
  };
786
-
787
807
  }
788
808
 
789
809
  __initCommandsFiles() {
790
810
  // file operations
791
811
  this.commands['readFile'] = (socket, _adapter, fileName, callback) => {
812
+ // Read file from ioBroker DB
813
+ // @param {string} _adapter - instance name, e.g. `vis.0`
814
+ // @param {string} fileName - file name, e.g `main/vis-views.json`
815
+ // @param {function} callback - `function (error, data, mimeType)`
792
816
  if (this._checkPermissions(socket, 'readFile', callback, fileName)) {
793
817
  try {
794
818
  this.adapter.readFile(_adapter, fileName, {user: socket._acl.user}, (error, ...args) =>
@@ -801,6 +825,10 @@ class SocketCommands {
801
825
  };
802
826
 
803
827
  this.commands['readFile64'] = (socket, _adapter, fileName, callback) => {
828
+ // Read file from ioBroker DB as base64 string
829
+ // @param {string} _adapter - instance name, e.g. `vis.0`
830
+ // @param {string} fileName - file name, e.g `main/vis-views.json`
831
+ // @param {function} callback - `function (error, base64, mimeType)`
804
832
  if (this._checkPermissions(socket, 'readFile64', callback, fileName)) {
805
833
  try {
806
834
  this.adapter.readFile(_adapter, fileName, {user: socket._acl.user}, (error, buffer, type) => {
@@ -836,6 +864,12 @@ class SocketCommands {
836
864
  };
837
865
 
838
866
  this.commands['writeFile64'] = (socket, _adapter, fileName, data64, options, callback) => {
867
+ // Write file into ioBroker DB as base64 string
868
+ // @param {string} _adapter - instance name, e.g. `vis.0`
869
+ // @param {string} fileName - file name, e.g `main/vis-views.json`
870
+ // @param {string} data64 - file content as base64 string
871
+ // @param {object} options - optional `{mode: 0x0644}`
872
+ // @param {function} callback - `function (error)`
839
873
  if (typeof options === 'function') {
840
874
  callback = options;
841
875
  options = {user: socket._acl.user};
@@ -863,6 +897,12 @@ class SocketCommands {
863
897
 
864
898
  // this function is overloaded in admin (because admin accepts only base64)
865
899
  this.commands['writeFile'] = (socket, _adapter, fileName, data, options, callback) => {
900
+ // Write file into ioBroker DB as text **DEPRECATED**
901
+ // @param {string} _adapter - instance name, e.g. `vis.0`
902
+ // @param {string} fileName - file name, e.g `main/vis-views.json`
903
+ // @param {string} data64 - file content as base64 string
904
+ // @param {object} options - optional `{mode: 0x644}`
905
+ // @param {function} callback - `function (error)`
866
906
  if (this._checkPermissions(socket, 'writeFile', callback, fileName)) {
867
907
  if (typeof options === 'function') {
868
908
  callback = options;
@@ -883,6 +923,10 @@ class SocketCommands {
883
923
  };
884
924
 
885
925
  this.commands['unlink'] = (socket, _adapter, name, callback) => {
926
+ // Delete file in ioBroker DB
927
+ // @param {string} _adapter - instance name, e.g. `vis.0`
928
+ // @param {string} name - file name, e.g `main/vis-views.json`
929
+ // @param {function} callback - `function (error)`
886
930
  if (this._checkPermissions(socket, 'unlink', callback, name)) {
887
931
  try {
888
932
  this._unlink(_adapter, name, {user: socket._acl.user})
@@ -896,6 +940,10 @@ class SocketCommands {
896
940
  };
897
941
 
898
942
  this.commands['deleteFile'] = (socket, _adapter, name, callback) => {
943
+ // Delete file in ioBroker DB (same as unlink, but only for files)
944
+ // @param {string} _adapter - instance name, e.g. `vis.0`
945
+ // @param {string} name - file name, e.g `main/vis-views.json`
946
+ // @param {function} callback - `function (error)`
899
947
  if (this._checkPermissions(socket, 'unlink', callback, name)) {
900
948
  try {
901
949
  this.adapter.unlink(_adapter, name, {user: socket._acl.user}, (error, ...args) =>
@@ -908,6 +956,10 @@ class SocketCommands {
908
956
  };
909
957
 
910
958
  this.commands['deleteFolder'] = (socket, _adapter, name, callback) => {
959
+ // Delete file in ioBroker DB (same as unlink, but only for folders)
960
+ // @param {string} _adapter - instance name, e.g. `vis.0`
961
+ // @param {string} name - folder name, e.g `main`
962
+ // @param {function} callback - `function (error)`
911
963
  if (this._checkPermissions(socket, 'unlink', callback, name)) {
912
964
  try {
913
965
  this._unlink(_adapter, name, {user: socket._acl.user})
@@ -921,6 +973,11 @@ class SocketCommands {
921
973
  };
922
974
 
923
975
  this.commands['renameFile'] = (socket, _adapter, oldName, newName, callback) => {
976
+ // Rename file in ioBroker DB
977
+ // @param {string} _adapter - instance name, e.g. `vis.0`
978
+ // @param {string} oldName - current file name, e.g `main/vis-views.json`
979
+ // @param {string} newName - new file name, e.g `main/vis-views-new.json`
980
+ // @param {function} callback - `function (error)`
924
981
  if (this._checkPermissions(socket, 'rename', callback, oldName)) {
925
982
  try {
926
983
  this.adapter.rename(_adapter, oldName, newName, {user: socket._acl.user}, (error, ...args) =>
@@ -933,6 +990,11 @@ class SocketCommands {
933
990
  };
934
991
 
935
992
  this.commands['rename'] = (socket, _adapter, oldName, newName, callback) => {
993
+ // Rename file or folder in ioBroker DB
994
+ // @param {string} _adapter - instance name, e.g. `vis.0`
995
+ // @param {string} oldName - current file name, e.g `main/vis-views.json`
996
+ // @param {string} newName - new file name, e.g `main/vis-views-new.json`
997
+ // @param {function} callback - `function (error)`
936
998
  if (this._checkPermissions(socket, 'rename', callback, oldName)) {
937
999
  try {
938
1000
  this._rename(_adapter, oldName, newName, {user: socket._acl.user})
@@ -946,6 +1008,10 @@ class SocketCommands {
946
1008
  };
947
1009
 
948
1010
  this.commands['mkdir'] = (socket, _adapter, dirName, callback) => {
1011
+ // Create folder in ioBroker DB
1012
+ // @param {string} _adapter - instance name, e.g. `vis.0`
1013
+ // @param {string} dirName - desired folder name, e.g `main`
1014
+ // @param {function} callback - `function (error)`
949
1015
  if (this._checkPermissions(socket, 'mkdir', callback, dirName)) {
950
1016
  try {
951
1017
  this.adapter.mkdir(_adapter, dirName, {user: socket._acl.user}, (error, ...args) =>
@@ -958,6 +1024,11 @@ class SocketCommands {
958
1024
  };
959
1025
 
960
1026
  this.commands['readDir'] = (socket, _adapter, dirName, options, callback) => {
1027
+ // Read content of folder in ioBroker DB
1028
+ // @param {string} _adapter - instance name, e.g. `vis.0`
1029
+ // @param {string} dirName - folder name, e.g `main`
1030
+ // @param {object} options - optional `{filter: '*'}` or `{filter: '*.json'}`
1031
+ // @param {function} callback - `function (error, files)` where `files` is an array of objects, like `{file: 'vis-views.json', isDir: false, stats: {size: 123}, modifiedAt: 1661336290090, acl: {owner: 'system.user.admin', ownerGroup: 'system.group.administrator', permissions: 1632, read: true, write: true}`
961
1032
  if (typeof options === 'function') {
962
1033
  callback = options;
963
1034
  options = {};
@@ -981,6 +1052,11 @@ class SocketCommands {
981
1052
  };
982
1053
 
983
1054
  this.commands['chmodFile'] = (socket, _adapter, fileName, options, callback) => {
1055
+ // Change file mode in ioBroker DB
1056
+ // @param {string} _adapter - instance name, e.g. `vis.0`
1057
+ // @param {string} fileName - file name, e.g `main/vis-views.json`
1058
+ // @param {object} options - `{mode: 0x644}` or 0x644. First digit is user, second group, third others. Bit 1 is `execute`, bit 2 is `write`, bit 3 is `read`
1059
+ // @param {function} callback - `function (error)`
984
1060
  if (typeof options === 'function') {
985
1061
  callback = options;
986
1062
  options = {};
@@ -1004,6 +1080,11 @@ class SocketCommands {
1004
1080
  };
1005
1081
 
1006
1082
  this.commands['chownFile'] = (socket, _adapter, fileName, options, callback) => {
1083
+ // Change file owner in ioBroker DB
1084
+ // @param {string} _adapter - instance name, e.g. `vis.0`
1085
+ // @param {string} fileName - file name, e.g `main/vis-views.json`
1086
+ // @param {object} options - `{owner: 'system.user.user', ownerGroup: ''system.group.administrator'}` or 'system.user.user'. If ownerGroup is not defined, it will be taken from owner.
1087
+ // @param {function} callback - `function (error)`
1007
1088
  if (this._checkPermissions(socket, 'chownFile', callback, fileName)) {
1008
1089
  options = options || {};
1009
1090
  options.user = socket._acl.user;
@@ -1018,6 +1099,10 @@ class SocketCommands {
1018
1099
  };
1019
1100
 
1020
1101
  this.commands['fileExists'] = (socket, _adapter, fileName, callback) => {
1102
+ // Checks if the file or folder exists in ioBroker DB
1103
+ // @param {string} _adapter - instance name, e.g. `vis.0`
1104
+ // @param {string} fileName - file name, e.g `main/vis-views.json`
1105
+ // @param {function} callback - `function (error, isExist)`
1021
1106
  if (this._checkPermissions(socket, 'fileExists', callback, fileName)) {
1022
1107
  try {
1023
1108
  this.adapter.fileExists(_adapter, fileName, {user: socket._acl.user}, (error, ...args) =>
@@ -1029,11 +1114,27 @@ class SocketCommands {
1029
1114
  }
1030
1115
  };
1031
1116
 
1032
- this.commands['subscribeFiles'] = (socket, id, pattern, callback) => this._subscribeFiles(socket, id, pattern, callback);
1117
+ this.commands['subscribeFiles'] = (socket, id, pattern, callback) => {
1118
+ // Subscribe on file changes in ioBroker DB
1119
+ // @param {string} id - instance name, e.g. `vis.0` or any object ID of type `meta`. `id` could have wildcards `*` too.
1120
+ // @param {string} pattern - file name pattern, e.g `main/*.json`
1121
+ // @param {function} callback - `function (error)`
1122
+ return this._subscribeFiles(socket, id, pattern, callback);
1123
+ }
1124
+
1125
+ this.commands['unsubscribeFiles'] = (socket, id, pattern, callback) => {
1126
+ // Unsubscribe on file changes in ioBroker DB
1127
+ // @param {string} id - instance name, e.g. `vis.0` or any object ID of type `meta`. `id` could have wildcards `*` too.
1128
+ // @param {string} pattern - file name pattern, e.g `main/*.json`
1129
+ // @param {function} callback - `function (error)`
1033
1130
 
1034
- this.commands['unsubscribeFiles'] = (socket, id, pattern, callback) => this._unsubscribeFiles(socket, id, pattern, callback);
1131
+ return this._unsubscribeFiles(socket, id, pattern, callback);
1132
+ }
1035
1133
 
1036
1134
  this.commands['getAdapterInstances'] = (socket, adapterName, callback) => {
1135
+ // Read all instances of the given adapter, or all instances of all adapters if adapterName is not defined
1136
+ // @param {string} adapterName - optional adapter name, e.g `history`.
1137
+ // @param {function} callback - `function (error, instanceList)`, where instanceList is an array of instance objects, e.g. `{_id: 'system.adapter.history.0', common: {name: 'history', ...}, native: {...}}`
1037
1138
  if (typeof callback === 'function') {
1038
1139
  if (this._checkPermissions(socket, 'getObject', callback)) {
1039
1140
  let _adapterName = adapterName !== undefined && adapterName !== null ? adapterName : this.adapterName || '';
@@ -1071,6 +1172,9 @@ class SocketCommands {
1071
1172
 
1072
1173
  __initCommandsStates() {
1073
1174
  this.commands['getStates'] = (socket, pattern, callback) => {
1175
+ // Read states by pattern
1176
+ // @param {string} pattern - optional pattern, like `system.adapter.*` or array of state IDs
1177
+ // @param {function} callback - `function (error, states)`, where `states` is an object like `{'system.adapter.history.0': {_id: 'system.adapter.history.0', common: {name: 'history', ...}, native: {...}, 'system.adapter.history.1': {...}}}`
1074
1178
  if (this._checkPermissions(socket, 'getStates', callback, pattern)) {
1075
1179
  if (typeof pattern === 'function') {
1076
1180
  callback = pattern;
@@ -1090,25 +1194,10 @@ class SocketCommands {
1090
1194
  }
1091
1195
  };
1092
1196
 
1093
- // this function is overloaded in admin
1094
- this.commands['delObject'] = (socket, id, options, callback) => {
1095
- // only flot allowed
1096
- if (id.startsWith('flot.')) {
1097
- if (this._checkPermissions(socket, 'delObject', callback, id)) {
1098
- try {
1099
- this.adapter.delForeignObject(id, {user: socket._acl.user}, (error, ...args) =>
1100
- SocketCommands._fixCallback(callback, error, ...args));
1101
- } catch (error) {
1102
- this.adapter.log.error('[delObject] ERROR: ' + error.toString());
1103
- SocketCommands._fixCallback(callback, error);
1104
- }
1105
- }
1106
- } else {
1107
- SocketCommands._fixCallback(callback, SocketCommands.ERROR_PERMISSION);
1108
- }
1109
- };
1110
-
1111
1197
  this.commands['getState'] = (socket, id, callback) => {
1198
+ // Read one state.
1199
+ // @param {string} id - State ID like, 'system.adapter.admin.0.memRss'
1200
+ // @param {function} callback - `function (error, state)`, where `state` is an object like `{val: 123, ts: 1663915537418, ack: true, from: 'system.adapter.admin.0', q: 0, lc: 1663915537418, c: 'javascript.0'}`
1112
1201
  if (this._checkPermissions(socket, 'getState', callback, id)) {
1113
1202
  if (typeof callback === 'function') {
1114
1203
  if (this.states && this.states[id]) {
@@ -1129,6 +1218,10 @@ class SocketCommands {
1129
1218
  };
1130
1219
 
1131
1220
  this.commands['setState'] = (socket, id, state, callback) => {
1221
+ // Write one state.
1222
+ // @param {string} id - State ID like, 'system.adapter.admin.0.memRss'
1223
+ // @param {any} state - value or object like `{val: 123, ack: true}`
1224
+ // @param {function} callback - `function (error, state)`, where `state` is an object like `{val: 123, ts: 1663915537418, ack: true, from: 'system.adapter.admin.0', q: 0, lc: 1663915537418, c: 'javascript.0'}`
1132
1225
  if (this._checkPermissions(socket, 'setState', callback, id)) {
1133
1226
  if (typeof state !== 'object') {
1134
1227
  state = {val: state};
@@ -1150,6 +1243,9 @@ class SocketCommands {
1150
1243
  };
1151
1244
 
1152
1245
  this.commands['getBinaryState'] = (socket, id, callback) => {
1246
+ // Read one binary state.
1247
+ // @param {string} id - State ID like, 'javascript.0.binary'
1248
+ // @param {function} callback - `function (error, base64)`
1153
1249
  if (this._checkPermissions(socket, 'getState', callback, id)) {
1154
1250
  if (typeof callback === 'function') {
1155
1251
  try {
@@ -1187,6 +1283,10 @@ class SocketCommands {
1187
1283
  };
1188
1284
 
1189
1285
  this.commands['setBinaryState'] = (socket, id, base64, callback) => {
1286
+ // Write one binary state.
1287
+ // @param {string} id - State ID like, 'javascript.0.binary'
1288
+ // @param {string} base64 - State value as base64 string. Binary states has no acknowledge flag.
1289
+ // @param {function} callback - `function (error)`
1190
1290
  if (this._checkPermissions(socket, 'setState', callback, id)) {
1191
1291
  if (typeof callback === 'function') {
1192
1292
  let data = null;
@@ -1214,15 +1314,40 @@ class SocketCommands {
1214
1314
  }
1215
1315
  };
1216
1316
 
1217
- this.commands['subscribe'] = (socket, pattern, callback) => this._subscribeStates(socket, pattern, callback);
1218
- this.commands['subscribeStates'] = (socket, pattern, callback) => this._subscribeStates(socket, pattern, callback);
1317
+ this.commands['subscribe'] = (socket, pattern, callback) => {
1318
+ // Subscribe on state changes by pattern. The events will come as 'stateChange' events to the socket.
1319
+ // @param {string} pattern - pattern like 'system.adapter.*' or array of states like ['system.adapter.admin.0.memRss', 'system.adapter.admin.0.memHeapTotal']
1320
+ // @param {function} callback - `function (error)`
1321
+ return this._subscribeStates(socket, pattern, callback);
1322
+ };
1323
+
1324
+ this.commands['subscribeStates'] = (socket, pattern, callback) => {
1325
+ // Subscribe on state changes by pattern. Same as `subscribe`. The events will come as 'stateChange' events to the socket.
1326
+ // @param {string} pattern - pattern like 'system.adapter.*' or array of states like ['system.adapter.admin.0.memRss', 'system.adapter.admin.0.memHeapTotal']
1327
+ // @param {function} callback - `function (error)`
1328
+ return this._subscribeStates(socket, pattern, callback);
1329
+ };
1330
+
1331
+ this.commands['unsubscribe'] = (socket, pattern, callback) => {
1332
+ // Unsubscribe from state changes by pattern.
1333
+ // @param {string} pattern - pattern like 'system.adapter.*' or array of states like ['system.adapter.admin.0.memRss', 'system.adapter.admin.0.memHeapTotal']
1334
+ // @param {function} callback - `function (error)`
1335
+ return this._unsubscribeStates(socket, pattern, callback);
1336
+ };
1219
1337
 
1220
- this.commands['unsubscribe'] = (socket, pattern, callback) => this._unsubscribeStates(socket, pattern, callback);
1221
- this.commands['unsubscribeStates'] = (socket, pattern, callback) => this._unsubscribeStates(socket, pattern, callback);
1338
+ this.commands['unsubscribeStates'] = (socket, pattern, callback) => {
1339
+ // Unsubscribe from state changes by pattern. Same as `unsubscribe`.
1340
+ // @param {string} pattern - pattern like 'system.adapter.*' or array of states like ['system.adapter.admin.0.memRss', 'system.adapter.admin.0.memHeapTotal']
1341
+ // @param {function} callback - `function (error)`
1342
+ return this._unsubscribeStates(socket, pattern, callback);
1343
+ };
1222
1344
  }
1223
1345
 
1224
1346
  __initCommandsObjects() {
1225
1347
  this.commands['getObject'] = (socket, id, callback) => {
1348
+ // Get one object
1349
+ // @param {string} id - object ID.
1350
+ // @param {function} callback - `function (error, obj)`
1226
1351
  if (this._checkPermissions(socket, 'getObject', callback, id)) {
1227
1352
  try {
1228
1353
  this.adapter.getForeignObject(id, {user: socket._acl.user}, (error, obj) => {
@@ -1240,7 +1365,11 @@ class SocketCommands {
1240
1365
  };
1241
1366
 
1242
1367
  // not admin version of "all objects"
1368
+ // this function is overloaded in admin
1243
1369
  this.commands['getObjects'] = (socket, callback) => {
1370
+ // Get all objects that are relevant for web: all states and enums with rooms
1371
+ // @param {string} id - object ID.
1372
+ // @param {function} callback - `function (error, obj)`
1244
1373
  if (this._checkPermissions(socket, 'getObjects', callback)) {
1245
1374
  try {
1246
1375
  this.adapter.getForeignObjects('*', 'state', 'rooms', {user: socket._acl.user}, (error, objs) => {
@@ -1263,6 +1392,9 @@ class SocketCommands {
1263
1392
  };
1264
1393
 
1265
1394
  this.commands['subscribeObjects'] = (socket, pattern, callback) => {
1395
+ // Subscribe on object changes by pattern. The events will come as 'objectChange' events to the socket.
1396
+ // @param {string} pattern - pattern like 'system.adapter.*' or array of IDs like ['system.adapter.admin.0.memRss', 'system.adapter.admin.0.memHeapTotal']
1397
+ // @param {function} callback - `function (error)`
1266
1398
  if (this._checkPermissions(socket, 'subscribeObjects', callback, pattern)) {
1267
1399
  try {
1268
1400
  if (pattern && typeof pattern === 'object' && pattern instanceof Array) {
@@ -1284,6 +1416,9 @@ class SocketCommands {
1284
1416
  };
1285
1417
 
1286
1418
  this.commands['unsubscribeObjects'] = (socket, pattern, callback) => {
1419
+ // Unsubscribe on object changes by pattern.
1420
+ // @param {string} pattern - pattern like 'system.adapter.*' or array of IDs like ['system.adapter.admin.0.memRss', 'system.adapter.admin.0.memHeapTotal']
1421
+ // @param {function} callback - `function (error)`
1287
1422
  if (this._checkPermissions(socket, 'unsubscribeObjects', callback, pattern)) {
1288
1423
  try {
1289
1424
  if (pattern && typeof pattern === 'object' && pattern instanceof Array) {
@@ -1305,6 +1440,11 @@ class SocketCommands {
1305
1440
  };
1306
1441
 
1307
1442
  this.commands['getObjectView'] = (socket, design, search, params, callback) => {
1443
+ // Make a query to the object database.
1444
+ // @param {string} design - 'system' or other designs like `custom`, but it must exist object `_design/custom`. Too 99,9% use `system`.
1445
+ // @param {string} search - object type, like `state`, `instance`, `adapter`, `host`, ...
1446
+ // @param {string} params - parameters for the query in form `{startkey: 'system.adapter.', endkey: 'system.adapter.\u9999'}`
1447
+ // @param {function} callback - `function (error)`
1308
1448
  if (typeof callback === 'function') {
1309
1449
  if (this._checkPermissions(socket, 'getObjectView', callback, search)) {
1310
1450
  try {
@@ -1320,6 +1460,10 @@ class SocketCommands {
1320
1460
  };
1321
1461
 
1322
1462
  this.commands['setObject'] = (socket, id, obj, callback) => {
1463
+ // Set object.
1464
+ // @param {string} id - object ID
1465
+ // @param {object} obj - object itself
1466
+ // @param {function} callback - `function (error)`
1323
1467
  if (this._checkPermissions(socket, 'setObject', callback, id)) {
1324
1468
  try {
1325
1469
  this.adapter.setForeignObject(id, obj, {user: socket._acl.user}, (error, ...args) =>
@@ -1330,6 +1474,27 @@ class SocketCommands {
1330
1474
  }
1331
1475
  }
1332
1476
  };
1477
+
1478
+ // this function is overloaded in admin
1479
+ this.commands['delObject'] = (socket, id, options, callback) => {
1480
+ // Delete object. Only deletion of flot objects is allowed
1481
+ // @param {string} id - Object ID like, 'flot.0.myChart'
1482
+ // @param {string} options - ignored
1483
+ // @param {function} callback - `function (error)`
1484
+ if (id.startsWith('flot.')) {
1485
+ if (this._checkPermissions(socket, 'delObject', callback, id)) {
1486
+ try {
1487
+ this.adapter.delForeignObject(id, {user: socket._acl.user}, (error, ...args) =>
1488
+ SocketCommands._fixCallback(callback, error, ...args));
1489
+ } catch (error) {
1490
+ this.adapter.log.error('[delObject] ERROR: ' + error.toString());
1491
+ SocketCommands._fixCallback(callback, error);
1492
+ }
1493
+ }
1494
+ } else {
1495
+ SocketCommands._fixCallback(callback, SocketCommands.ERROR_PERMISSION);
1496
+ }
1497
+ };
1333
1498
  }
1334
1499
 
1335
1500
  _initCommands() {