iobroker.rest-api 2.0.2 → 3.0.0

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.
Files changed (51) hide show
  1. package/README.md +49 -8
  2. package/admin/i18n/{de/translations.json → de.json} +22 -22
  3. package/admin/i18n/{en/translations.json → en.json} +22 -23
  4. package/admin/i18n/{es/translations.json → es.json} +22 -23
  5. package/admin/i18n/{fr/translations.json → fr.json} +22 -23
  6. package/admin/i18n/{it/translations.json → it.json} +22 -23
  7. package/admin/i18n/{nl/translations.json → nl.json} +22 -23
  8. package/admin/i18n/{pl/translations.json → pl.json} +22 -23
  9. package/admin/i18n/{pt/translations.json → pt.json} +22 -23
  10. package/admin/i18n/{ru/translations.json → ru.json} +22 -23
  11. package/admin/i18n/uk.json +32 -0
  12. package/admin/i18n/{zh-cn/translations.json → zh-cn.json} +22 -23
  13. package/admin/jsonConfig.json +178 -180
  14. package/admin/rest-api.svg +8 -0
  15. package/dist/lib/api/controllers/common.js +129 -0
  16. package/dist/lib/api/controllers/common.js.map +1 -0
  17. package/dist/lib/api/controllers/enum.js +58 -0
  18. package/dist/lib/api/controllers/enum.js.map +1 -0
  19. package/dist/lib/api/controllers/file.js +104 -0
  20. package/dist/lib/api/controllers/file.js.map +1 -0
  21. package/dist/lib/api/controllers/history.js +262 -0
  22. package/dist/lib/api/controllers/history.js.map +1 -0
  23. package/dist/lib/api/controllers/object.js +346 -0
  24. package/dist/lib/api/controllers/object.js.map +1 -0
  25. package/dist/lib/api/controllers/sendTo.js +118 -0
  26. package/dist/lib/api/controllers/sendTo.js.map +1 -0
  27. package/dist/lib/api/controllers/state.js +545 -0
  28. package/dist/lib/api/controllers/state.js.map +1 -0
  29. package/dist/lib/api/swagger/swagger.yaml +2551 -0
  30. package/{lib → dist/lib}/common.js +8 -10
  31. package/dist/lib/common.js.map +1 -0
  32. package/dist/lib/rest-api.js +1216 -0
  33. package/dist/lib/rest-api.js.map +1 -0
  34. package/dist/main.js +159 -0
  35. package/dist/main.js.map +1 -0
  36. package/examples/demoBrowserClient.html +95 -82
  37. package/examples/demoNodeClient.js +8 -7
  38. package/examples/longPolling.js +46 -45
  39. package/io-package.json +43 -34
  40. package/package.json +36 -24
  41. package/lib/api/controllers/common.js +0 -150
  42. package/lib/api/controllers/enum.js +0 -44
  43. package/lib/api/controllers/file.js +0 -74
  44. package/lib/api/controllers/history.js +0 -239
  45. package/lib/api/controllers/object.js +0 -273
  46. package/lib/api/controllers/sendTo.js +0 -123
  47. package/lib/api/controllers/state.js +0 -565
  48. package/lib/api/swagger/swagger.yaml +0 -2624
  49. package/lib/rest-api.js +0 -1123
  50. package/main.js +0 -173
  51. /package/{lib → dist/lib}/config/default.yaml +0 -0
@@ -1,74 +0,0 @@
1
- 'use strict';
2
- const commonLib = require('./common.js');
3
-
4
- module.exports = {
5
- readFile: function (req, res) {
6
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'file', operation: 'read'}], async error => {
7
- if (error) {
8
- commonLib.errorResponse(req, res, error);
9
- } else {
10
- const params = commonLib.parseUrl(req.url, req.swagger, req._adapter.WEB_EXTENSION_PREFIX);
11
- try {
12
- const data = await req._adapter.readFileAsync(params.objectId, params.fileName, {user: req._user, limitToOwnerRights: req._adapter.config.onlyAllowWhenUserIsOwner});
13
- if (data && data.mimeType) {
14
- res.set('Content-Type', data.mimeType);
15
- res.send(data.file);
16
- } else {
17
- res.status(404).send(Buffer.from(''));
18
- }
19
- } catch (error) {
20
- commonLib.errorResponse(req, res, error);
21
- }
22
- }
23
- });
24
- },
25
- deleteFile: function (req, res) {
26
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'file', operation: 'delete'}], async error => {
27
- if (error) {
28
- commonLib.errorResponse(req, res, error);
29
- } else {
30
- const params = commonLib.parseUrl(req.url, req.swagger, req._adapter.WEB_EXTENSION_PREFIX);
31
- try {
32
- await req._adapter.delFileAsync(params.objectId, params.fileName, {user: req._user, limitToOwnerRights: req._adapter.config.onlyAllowWhenUserIsOwner});
33
- res.json({success: true});
34
- } catch (err) {
35
- if (err.toString().includes('Not exists')) {
36
- res.status(404).json({error: err.toString()});
37
- } else {
38
- commonLib.errorResponse(req, res, err);
39
- }
40
- }
41
- }
42
- });
43
- },
44
- writeFile: function (req, res) {
45
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'file', operation: 'write'}], async error => {
46
- if (error) {
47
- commonLib.errorResponse(req, res, error);
48
- } else {
49
- const params = commonLib.parseUrl(req.url, req.swagger, req._adapter.WEB_EXTENSION_PREFIX);
50
- try {
51
- await req._adapter.writeFileAsync(params.objectId, params.fileName, req.files.file[0].buffer, {user: req._user, limitToOwnerRights: req._adapter.config.onlyAllowWhenUserIsOwner});
52
- res.json({success: true});
53
- } catch (err) {
54
- commonLib.errorResponse(req, res, err);
55
- }
56
- }
57
- });
58
- },
59
- readDir: function (req, res) {
60
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'file', operation: 'list'}], async error => {
61
- if (error) {
62
- commonLib.errorResponse(req, res, error);
63
- } else {
64
- const params = commonLib.parseUrl(req.url, req.swagger, req._adapter.WEB_EXTENSION_PREFIX);
65
- try {
66
- const response = await req._adapter.readDirAsync(params.objectId, params.dirName || '', {user: req._user, limitToOwnerRights: req._adapter.config.onlyAllowWhenUserIsOwner});
67
- res.json(response);
68
- } catch (err) {
69
- commonLib.errorResponse(req, res, err);
70
- }
71
- }
72
- });
73
- },
74
- };
@@ -1,239 +0,0 @@
1
- 'use strict';
2
- const commonLib = require('./common.js');
3
-
4
- const PARAMETERS = {
5
- start: 'number',
6
- end:'number',
7
- count: 'number',
8
- from: 'boolean',
9
- ack: 'boolean',
10
- q: 'boolean',
11
- addId: 'boolean',
12
- limit: 'number',
13
- ignoreNull: 'boolean',
14
- removeBorderValues: 'boolean',
15
- returnNewestEntries: 'boolean',
16
- aggregate: ['minmax', 'max', 'min', 'average', 'total', 'count', 'percentile', 'quantile', 'integral', 'none'],
17
- percentile: 'number',
18
- quantile: 'number',
19
- integralUnit: 'string',
20
- integralInterpolation: 'string',
21
- enum: ['linear', 'none'],
22
- }
23
-
24
- const PARAMETERS_ADD = {
25
- val: 'string',
26
- from: 'string',
27
- ack: 'boolean',
28
- q: 'number',
29
- ts: 'number',
30
- }
31
-
32
- module.exports = {
33
- getHistory: function (req, res) {
34
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'other', operation: 'sendto'}], async error => {
35
- if (error) {
36
- commonLib.errorResponse(req, res, error);
37
- } else {
38
- if (req._adapter.config.dataSource) {
39
- // check if instance is alive
40
- const state = await req._adapter.getForeignStateAsync(`system.adapter.${req._adapter.config.dataSource}.alive`);
41
- if (state.val) {
42
- const params = commonLib.parseUrl(req.url, req.swagger, req._adapter.WEB_EXTENSION_PREFIX);
43
- req._adapter.log.debug(`Read data from: ${req._adapter.config.dataSource}`);
44
-
45
- const options = {};
46
- Object.keys(PARAMETERS).forEach(attr => {
47
- if (Object.hasOwnProperty.call(req.query, attr)) {
48
- if (PARAMETERS[attr] === 'boolean') {
49
- options[attr] = req.query[attr] === 'true';
50
- } else if (PARAMETERS[attr] === 'number') {
51
- options[attr] = parseFloat(req.query[attr]);
52
- } else if (Array.isArray(PARAMETERS[attr])) {
53
- if (PARAMETERS[attr].includes(req.query[attr])) {
54
- options[attr] = req.query[attr];
55
- } else {
56
- req._adapter.log.warn(`Unknown value ${req.query[attr]} for attribute ${attr}. Allowed: ${PARAMETERS[attr].join(', ')}`);
57
- }
58
- } else if (PARAMETERS[attr] === 'string') {
59
- options[attr] = req.query[attr];
60
- }
61
- }
62
- });
63
-
64
- req._adapter.sendTo(req._adapter.config.dataSource, 'getHistory', {id: params.stateId, options}, (result, step, error) => {
65
- if (error) {
66
- commonLib.errorResponse(req, res, error);
67
- return;
68
- }
69
- // req._adapter.log.debug(`[QUERY] sendTo result = ${JSON.stringify(result)}`);
70
- res.json(result.result);
71
- });
72
- } else {
73
- res.status(500).json({error: 'Instance is not alive', instance: req._adapter.config.dataSource});
74
- }
75
- } else {
76
- res.status(422).json({error: 'No dataSource defined!'});
77
- }
78
- }
79
- });
80
- },
81
- postHistory: function (req, res) {
82
- commonLib.checkPermissions(req._adapter, req._user, req.body.options, async error => {
83
- if (error) {
84
- commonLib.errorResponse(req, res, error);
85
- } else {
86
- if (req._adapter.config.dataSource) {
87
- const state = await req._adapter.getForeignStateAsync(`system.adapter.${req._adapter.config.dataSource}.alive`);
88
- if (state.val) {
89
- req._adapter.log.debug(`Read data from: ${req._adapter.config.dataSource}`);
90
- const options = {
91
- id: req.body.id,
92
- options: {}
93
- };
94
-
95
- if (req.body.options && typeof req.body.options === 'object') {
96
- Object.keys(PARAMETERS).forEach(attr => {
97
- if (Object.hasOwnProperty.call(req.body.options, attr)) {
98
- if (PARAMETERS[attr] === 'boolean') {
99
- options.options[attr] = req.body.options[attr] === 'true';
100
- } else if (PARAMETERS[attr] === 'number') {
101
- options.options[attr] = parseFloat(req.body.options[attr]);
102
- } else if (Array.isArray(PARAMETERS[attr])) {
103
- if (PARAMETERS[attr].includes(req.body.options[attr])) {
104
- options.options[attr] = req.body.options[attr];
105
- } else {
106
- req._adapter.log.warn(`Unknown value ${req.body.options[attr]} for attribute ${attr}. Allowed: ${PARAMETERS[attr].join(', ')}`);
107
- }
108
- } else if (PARAMETERS[attr] === 'string') {
109
- options.options[attr] = req.body.options[attr];
110
- }
111
- }
112
- });
113
- }
114
-
115
- req._adapter.sendTo(req._adapter.config.dataSource, 'getHistory', options, (result, step, error) => {
116
- if (error) {
117
- commonLib.errorResponse(req, res, error);
118
- return;
119
- }
120
- // req._adapter.log.debug(`[QUERY] sendTo result = ${JSON.stringify(result)}`);
121
- res.json(result.result);
122
- });
123
- } else {
124
- res.status(500).json({error: 'Instance is not alive', instance: req._adapter.config.dataSource});
125
- }
126
- } else {
127
- res.status(422).json({error: 'No dataSource defined!'});
128
- }
129
- }
130
- });
131
- },
132
-
133
- addHistoryByGet: function (req, res) {
134
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'other', operation: 'sendto'}], async error => {
135
- if (error) {
136
- commonLib.errorResponse(req, res, error);
137
- } else {
138
- if (req._adapter.config.dataSource) {
139
- // check if instance is alive
140
- const aliveState = await req._adapter.getForeignStateAsync(`system.adapter.${req._adapter.config.dataSource}.alive`);
141
- if (aliveState.val) {
142
- const params = commonLib.parseUrl(req.url, req.swagger, req._adapter.WEB_EXTENSION_PREFIX);
143
- req._adapter.log.debug(`Read data from: ${req._adapter.config.dataSource}`);
144
- const obj = await req._adapter.getForeignObjectAsync(params.stateId);
145
-
146
- if (obj && obj.type === 'state' && obj.common && obj.common.type) {
147
- const state = {};
148
- Object.keys(PARAMETERS_ADD).forEach(attr => {
149
- if (Object.hasOwnProperty.call(req.query, attr)) {
150
- if (PARAMETERS_ADD[attr] === 'boolean') {
151
- state[attr] = req.query[attr] === 'true';
152
- } else if (PARAMETERS_ADD[attr] === 'number') {
153
- state[attr] = parseFloat(req.query[attr]);
154
- } else if (PARAMETERS_ADD[attr] === 'string') {
155
- state[attr] = req.query[attr];
156
- }
157
- if (attr === 'val') {
158
- if (obj.common.type === 'boolean') {
159
- state.val = state.val === 'true' || state.val === '1';
160
- } else if (obj.common.type === 'number') {
161
- state.val = parseFloat(state.val);
162
- }
163
- }
164
- }
165
- });
166
-
167
- state.ts = state.ts || Date.now();
168
-
169
- req._adapter.sendTo(req._adapter.config.dataSource, 'storeState', {id: params.stateId, state}, (result, step, error) => {
170
- if (error) {
171
- commonLib.errorResponse(req, res, error);
172
- return;
173
- }
174
- // req._adapter.log.debug(`[QUERY] sendTo result = ${JSON.stringify(result)}`);
175
- res.json(result);
176
- });
177
- } else {
178
- res.status(404).json({error: 'State not found', stateId: params.stateId});
179
- }
180
- } else {
181
- res.status(500).json({error: 'Instance is not alive', instance: req._adapter.config.dataSource});
182
- }
183
- } else {
184
- res.status(422).json({error: 'No dataSource defined!'});
185
- }
186
- }
187
- });
188
- },
189
- addHistoryByPost: function (req, res) {
190
- commonLib.checkPermissions(req._adapter, req._user, req.body.options, async error => {
191
- if (error) {
192
- commonLib.errorResponse(req, res, error);
193
- } else {
194
- if (req._adapter.config.dataSource) {
195
- const state = await req._adapter.getForeignStateAsync(`system.adapter.${req._adapter.config.dataSource}.alive`);
196
- if (state.val) {
197
- req._adapter.log.debug(`Read data from: ${req._adapter.config.dataSource}`);
198
- const options = {
199
- id: req.body.id,
200
- state: {}
201
- };
202
-
203
- if (req.body.state && typeof req.body.state === 'object') {
204
- Object.keys(PARAMETERS_ADD).forEach(attr => {
205
- if (Object.hasOwnProperty.call(req.body.state, attr)) {
206
- if (PARAMETERS_ADD[attr] === 'boolean') {
207
- options.state[attr] = req.body.state[attr] === 'true';
208
- } else if (PARAMETERS_ADD[attr] === 'number') {
209
- options.state[attr] = parseFloat(req.body.state[attr]);
210
- } else if (PARAMETERS_ADD[attr] === 'string') {
211
- options.state[attr] = req.body.state[attr];
212
- }
213
- }
214
- });
215
- }
216
-
217
- options.state.ts = options.state.ts || Date.now();
218
-
219
- req._adapter.sendTo(req._adapter.config.dataSource, 'storeState', options, (result, step, error) => {
220
- if (error) {
221
- commonLib.errorResponse(req, res, error);
222
- return;
223
- }
224
- // req._adapter.log.debug(`[QUERY] sendTo result = ${JSON.stringify(result)}`);
225
- res.json(result);
226
- });
227
- } else {
228
- res.status(500).json({
229
- error: 'Instance is not alive',
230
- instance: req._adapter.config.dataSource
231
- });
232
- }
233
- } else {
234
- res.status(422).json({error: 'No dataSource defined!'});
235
- }
236
- }
237
- });
238
- },
239
- };
@@ -1,273 +0,0 @@
1
- 'use strict';
2
- const commonLib = require('./common.js');
3
-
4
- module.exports = {
5
- readObject: function (req, res) {
6
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'object', operation: 'read'}], error => {
7
- if (error) {
8
- commonLib.errorResponse(req, res, error);
9
- } else {
10
- const params = commonLib.parseUrl(req.url, req.swagger, req._adapter.WEB_EXTENSION_PREFIX);
11
- req._adapter.getForeignObject(params.objectId, {user: req._user, limitToOwnerRights: req._adapter.config.onlyAllowWhenUserIsOwner}, (error, obj) => {
12
- if (error) {
13
- commonLib.errorResponse(req, res, error, {objectId: req.query.objectId});
14
- } else {
15
- if (!obj) {
16
- res.status(404).json({error: 'object not found'});
17
- } else {
18
- res.json(obj);
19
- }
20
- }
21
- });
22
- }
23
- });
24
- },
25
-
26
- updateObject: function (req, res) {
27
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'object', operation: 'write'}], async error => {
28
- if (error) {
29
- commonLib.errorResponse(req, res, error);
30
- } else {
31
- const params = commonLib.parseUrl(req.url, req.swagger, req._adapter.WEB_EXTENSION_PREFIX);
32
- const body = req.body;
33
- try {
34
- const obj = await req._adapter.getForeignObjectAsync(params.objectId, {user: req._user, limitToOwnerRights: req._adapter.config.onlyAllowWhenUserIsOwner});
35
- if (!obj) {
36
- await req._adapter.setForeignObjectAsync(params.objectId, body, {user: req._user, limitToOwnerRights: req._adapter.config.onlyAllowWhenUserIsOwner});
37
- res.status(200).json(body);
38
- } else {
39
- // merge objects together
40
- Object.keys(body).forEach(attr => {
41
- if (body[attr] === null || body[attr] === undefined) {
42
- delete obj[attr];
43
- } else
44
- if (typeof body[attr] === 'object') {
45
- obj[attr] = obj[attr] || {};
46
- Object.keys(body[attr]).forEach(attr2 => {
47
- if (body[attr][attr2] === null) {
48
- delete obj[attr][attr2];
49
- } else {
50
- obj[attr][attr2] = body[attr][attr2];
51
- }
52
- })
53
- } else {
54
- obj[attr] = body[attr];
55
- }
56
- });
57
- await req._adapter.setForeignObjectAsync(params.objectId, obj, {user: req._user, limitToOwnerRights: req._adapter.config.onlyAllowWhenUserIsOwner});
58
- res.status(200).json(obj);
59
- }
60
- } catch (error) {
61
- commonLib.errorResponse(req, res, error, {objectId: params.objectId});
62
- }
63
- }
64
- });
65
- },
66
-
67
- createObject: function (req, res) {
68
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'object', operation: 'write'}], async error => {
69
- if (error) {
70
- commonLib.errorResponse(req, res, error);
71
- } else {
72
- const params = commonLib.parseUrl(req.url, req.swagger, req._adapter.WEB_EXTENSION_PREFIX);
73
- const body = req.body;
74
- try {
75
- const obj = await req._adapter.getForeignObjectAsync(params.objectId, {user: req._user, limitToOwnerRights: req._adapter.config.onlyAllowWhenUserIsOwner});
76
- if (!obj) {
77
- await req._adapter.setForeignObjectAsync(params.objectId, body, {user: req._user, limitToOwnerRights: req._adapter.config.onlyAllowWhenUserIsOwner});
78
- res.status(200).json(body);
79
- } else {
80
- res.status(409).json({error: 'Object already exists', id: params.objectId});
81
- }
82
- } catch (error) {
83
- commonLib.errorResponse(req, res, error, {objectId: params.objectId});
84
- }
85
- }
86
- });
87
- },
88
-
89
- deleteObject: function (req, res) {
90
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'object', operation: 'write'}], async error => {
91
- if (error) {
92
- commonLib.errorResponse(req, res, error);
93
- } else {
94
- const params = commonLib.parseUrl(req.url, req.swagger, req._adapter.WEB_EXTENSION_PREFIX);
95
- try {
96
- const obj = await req._adapter.getForeignObjectAsync(params.objectId, {user: req._user, limitToOwnerRights: req._adapter.config.onlyAllowWhenUserIsOwner});
97
- if (!obj) {
98
- res.status(404).json({objectId: params.objectId, error: 'object not found'});
99
- } else {
100
- await req._adapter.delForeignObjectAsync(params.objectId, {user: req._user, limitToOwnerRights: req._adapter.config.onlyAllowWhenUserIsOwner});
101
- res.status(200).json({});
102
- }
103
- } catch (error) {
104
- commonLib.errorResponse(req, res, error, {objectId: params.objectId});
105
- }
106
- }
107
- });
108
- },
109
-
110
- listObjects: function (req, res) {
111
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'object', operation: 'read'}], error => {
112
- if (error) {
113
- commonLib.errorResponse(req, res, error);
114
- } else {
115
- req._adapter.getForeignObjects(req.query.filter || '*', req.query.type || null, {
116
- user: req._user,
117
- limitToOwnerRights: req._adapter.config.onlyAllowWhenUserIsOwner
118
- }, (error, list) => {
119
- if (error) {
120
- commonLib.errorResponse(req, res, error, {filter: req.query.filter});
121
- } else {
122
- res.json(list || []);
123
- }
124
- });
125
- }
126
- });
127
- },
128
-
129
- subscribeObject: function (req, res) {
130
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'object', operation: 'read'}], async error => {
131
- if (error) {
132
- commonLib.errorResponse(req, res, error);
133
- } else {
134
- const params = commonLib.parseUrl(req.url, req.swagger, req._adapter.WEB_EXTENSION_PREFIX);
135
-
136
- let url = req.body.url;
137
- if ((req.query && req.query.method === 'polling') || (req.body && req.body.method === 'polling')) {
138
- url = req.query.sid || req.headers['x-forwarded-for'] || req.socket.remoteAddress;
139
- }
140
-
141
- if (!url) {
142
- res.status(422).json({error: 'url not provided', expectedBody: {url: 'http://ipaddress:9000/hook/'}});
143
- return;
144
- }
145
-
146
- try {
147
- const obj = await req._adapter.getForeignObjectAsync(params.stateId, {user: req._user, limitToOwnerRights: req._adapter.config.onlyAllowWhenUserIsOwner});
148
- if (!obj) {
149
- res.status(404).json({error: 'object not found'});
150
- } else {
151
- await req._swaggerObject.registerSubscribe(url, params.objectId, 'object', req._user, (req.query && req.query.method) || (req.body && req.body.method));
152
- const obj = await req._adapter.getForeignStateAsync(params.objectId, {user: req._user, limitToOwnerRights: req._adapter.config.onlyAllowWhenUserIsOwner});
153
- res.status(200).json(obj);
154
- }
155
- } catch (error) {
156
- req._adapter.log.warn(`Cannot read ${params.objectId}: ${error}`);
157
- commonLib.errorResponse(req, res, error, {objectId: params.objectId});
158
- }
159
- }
160
- });
161
- },
162
-
163
- unsubscribeObject: function (req, res) {
164
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'object', operation: 'read'}], async error => {
165
- if (error) {
166
- commonLib.errorResponse(req, res, error);
167
- } else {
168
- const params = commonLib.parseUrl(req.url, req.swagger, req._adapter.WEB_EXTENSION_PREFIX);
169
-
170
- let url = req.body.url;
171
- if ((req.query && req.query.method === 'polling') || (req.body && req.body.method === 'polling')) {
172
- url = req.query.sid || req.headers['x-forwarded-for'] || req.socket.remoteAddress;
173
- }
174
-
175
- if (!url) {
176
- res.status(422).json({error: 'url not provided', expectedBody: {url: 'http://ipaddress:9000/hook/'}});
177
- return;
178
- }
179
- try {
180
- await req._swaggerObject.unregisterSubscribe(url, params.objectId, 'object', req._user, (req.query && req.query.method) || (req.body && req.body.method));
181
- res.status(200).json({result: 'OK'});
182
- } catch (error) {
183
- commonLib.errorResponse(req, res, error, {objectId: params.objectId});
184
- }
185
- }
186
- });
187
- },
188
-
189
- subscribeObjects: function (req, res) {
190
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'object', operation: 'read'}], async error => {
191
- if (error) {
192
- commonLib.errorResponse(req, res, error);
193
- } else {
194
- let url = req.body.url;
195
- if (req.body.method === 'polling') {
196
- url = req.query.sid || req.headers['x-forwarded-for'] || req.socket.remoteAddress;
197
- }
198
-
199
- if (!url) {
200
- res.status(422).json({error: 'url not provided', expectedBody: {url: 'http://ipaddress:9000/hook/'}});
201
- return;
202
- }
203
- if (!req.body.pattern) {
204
- res.status(422).json({
205
- error: 'pattern not provided',
206
- expectedBody: {url: 'http://ipaddress:9000/hook/', pattern: 'system.adapter.admin.0.*'}
207
- });
208
- return;
209
- }
210
- try {
211
- await req._swaggerObject.registerSubscribe(url, req.body.pattern, 'object', req._user, req.body.method);
212
- } catch (error) {
213
- commonLib.errorResponse(req, res, error, {pattern: req.body.pattern, url: req.body.url});
214
- }
215
- }
216
- });
217
- },
218
-
219
- unsubscribeObjects: function (req, res) {
220
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'object', operation: 'read'}], async error => {
221
- if (error) {
222
- commonLib.errorResponse(req, res, error);
223
- } else {
224
- let url = req.body.url;
225
- if (req.body.method === 'polling') {
226
- url = req.query.sid || req.headers['x-forwarded-for'] || req.socket.remoteAddress;
227
- }
228
-
229
- if (!url) {
230
- res.status(422).json({error: 'url not provided', expectedBody: {url: 'http://ipaddress:9000/hook/'}});
231
- return;
232
- }
233
-
234
- try {
235
- await req._swaggerObject.unregisterSubscribe(url, req.body.pattern, 'object', req._user, req.body.method);
236
- res.status(200).json({result: 'OK'});
237
- } catch (error) {
238
- commonLib.errorResponse(req, res, error, {pattern: req.body.pattern, url: req.body.url});
239
- }
240
- }
241
- });
242
- },
243
-
244
- getObjectsSubscribes: function (req, res) {
245
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'state', operation: 'read'}], async error => {
246
- if (error) {
247
- commonLib.errorResponse(req, res, error);
248
- } else {
249
- let url = req.body.url;
250
- if ((req.query && req.query.method === 'polling') || (req.body && req.body.method === 'polling')) {
251
- url = req.query.sid || req.headers['x-forwarded-for'] || req.socket.remoteAddress;
252
- }
253
-
254
- if (!url) {
255
- res.status(422).json({error: 'url not provided', expectedBody: {url: 'http://ipaddress:9000/hook/'}});
256
- return;
257
- }
258
-
259
- try {
260
- const result = await req._swaggerObject.getSubscribes(url, req.body.pattern, 'object');
261
- if (result === null) {
262
- res.status(404).json({error: 'URL or session not found'});
263
- return;
264
- }
265
- res.json({states: result});
266
-
267
- } catch (error) {
268
- commonLib.errorResponse(req, res, error, {pattern: req.body.pattern, url: req.body.url});
269
- }
270
- }
271
- });
272
- },
273
- };