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,123 +0,0 @@
1
- 'use strict';
2
- const commonLib = require('./common.js');
3
-
4
- function sendTo(req, res) {
5
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'other', operation: 'sendto'}], async error => {
6
- if (error) {
7
- commonLib.errorResponse(req, res, error);
8
- } else {
9
- const params = commonLib.parseUrl(req.url, req.swagger, req._adapter.WEB_EXTENSION_PREFIX);
10
- let message = req.query.message;
11
- let noResponse = req.query.noResponse;
12
- let timeout = req.query.timeout;
13
- let data = req.query.data;
14
- if (req.body && req.body.message) {
15
- message = req.query.message;
16
- }
17
- if (req.body && req.body.timeout) {
18
- timeout = req.query.timeout;
19
- }
20
- timeout = parseInt(timeout, 10) || 10000;
21
- if (req.body && req.body.noResponse !== undefined) {
22
- noResponse = req.query.noResponse;
23
- }
24
- noResponse = noResponse === 'true';
25
-
26
- if (req.body && req.body.data !== undefined) {
27
- data = req.query.data;
28
- } else {
29
- if (data !== undefined && data !== null) {
30
- if (data === 'null') {
31
- data = null;
32
- } else if (data === 'undefined') {
33
- data = undefined;
34
- } else if (data === 'true') {
35
- data = true;
36
- } else if (data === 'false') {
37
- data = false;
38
- } else if (isFinite(data)) {
39
- data = parseFloat(data);
40
- } else if (data.startsWith('{') && data.endsWith('}')) {
41
- try {
42
- data = JSON.parse(data);
43
- } catch (error) {
44
- // ignore
45
- }
46
- } else if (data.startsWith('[') && data.endsWith(']')) {
47
- try {
48
- data = JSON.parse(data);
49
- } catch (error) {
50
- // ignore
51
- }
52
- }
53
- }
54
- }
55
- const instance = params.instance;
56
-
57
- if (!instance) {
58
- res
59
- .status(422)
60
- .json({error: 'No instance provided'});
61
- return;
62
- }
63
- if (!message) {
64
- res
65
- .status(422)
66
- .json({error: 'No message provided'});
67
- return;
68
- }
69
-
70
- // check if instance is alive
71
- let state;
72
- try {
73
- state = await req._adapter.getForeignStateAsync(`system.adapter.${instance}.alive`);
74
- if (!state || !state.val) {
75
- res
76
- .status(500)
77
- .json({error: 'instance is not online', instance});
78
- return;
79
- }
80
- } catch (error) {
81
- res
82
- .status(500)
83
- .json({error: 'invalid instance', instance});
84
- return;
85
- }
86
- if (noResponse) {
87
- req._adapter.sendTo(instance, message, data);
88
- res.json({result: 'sent'});
89
- } else {
90
- let timer;
91
- let answerDone = false;
92
- if (timeout) {
93
- timer = setTimeout(() => {
94
- timer = null;
95
- if (!answerDone) {
96
- answerDone = true;
97
- res.status(408).json({error: 'timeout'});
98
- }
99
- }, timeout);
100
- }
101
-
102
- req._adapter.sendTo(instance, message, data, (result, result1) => {
103
- timer && clearTimeout(timer);
104
- if (!answerDone) {
105
- answerDone = true;
106
- if (!result && result1) {
107
- res.json(result1);
108
- } else if (result && !result1) {
109
- res.json(result);
110
- } else {
111
- res.json({error: result, result: result1});
112
- }
113
- }
114
- });
115
- }
116
- }
117
- });
118
- }
119
-
120
- module.exports = {
121
- sendToPost: sendTo,
122
- sendTo: sendTo,
123
- };
@@ -1,565 +0,0 @@
1
- 'use strict';
2
- const commonLib = require('./common.js');
3
-
4
- function getIDs(oids) {
5
- return (oids || '').toString().split(',').map(t => t.trim()).filter(t => t);
6
- }
7
-
8
- async function updateState(adapter, user, id, timeout, val, res) {
9
- if (val && typeof val !== 'object') {
10
- if (val === 'true' || val === 'false') {
11
- const obj = await adapter.getForeignObjectAsync(id, {user, limitToOwnerRights: adapter.config.onlyAllowWhenUserIsOwner});
12
- if (obj && obj.common && obj.common.type === 'boolean') {
13
- val = val === 'true';
14
- }
15
- } else if (typeof val === 'string' && isFinite(val)) {
16
- try {
17
- const obj = await adapter.getForeignObjectAsync(id, {user, limitToOwnerRights: adapter.config.onlyAllowWhenUserIsOwner});
18
- if (obj && obj.common && obj.common.type === 'number') {
19
- val = parseFloat(val);
20
- }
21
- } catch (error) {
22
- adapter.log.warn(`Cannot read object ${id}: ${error.toString()}`);
23
- val = parseFloat(val);
24
- }
25
- }
26
- }
27
-
28
- try {
29
- if (!timeout) {
30
- if (typeof val !== 'object') {
31
- await adapter.setForeignStateAsync(id, val, false, {user, limitToOwnerRights: adapter.config.onlyAllowWhenUserIsOwner});
32
- res.json({id, val});
33
- } else {
34
- await adapter.setForeignStateAsync(id, val, {user, limitToOwnerRights: adapter.config.onlyAllowWhenUserIsOwner});
35
- val.id = id;
36
- res.json(val);
37
- }
38
- } else {
39
- await adapter._addTimeout({id, val, res, timeout});
40
- if (typeof val !== 'object') {
41
- await adapter.setForeignStateAsync(id, val, false, {user, limitToOwnerRights: adapter.config.onlyAllowWhenUserIsOwner});
42
- } else {
43
- await adapter.setForeignStateAsync(id, val, {user, limitToOwnerRights: adapter.config.onlyAllowWhenUserIsOwner});
44
- }
45
- }
46
- } catch (error) {
47
- commonLib.errorResponse({_adapter: adapter, url: 'updateState'}, res, error, {id});
48
- }
49
- }
50
-
51
- function subscribeState(req, res) {
52
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'state', operation: 'read'}], async error => {
53
- if (error) {
54
- res.status(403).json({error: error});
55
- } else {
56
- const params = commonLib.parseUrl(req.url, req.swagger, req._adapter.WEB_EXTENSION_PREFIX);
57
- let url = req.body.url;
58
- if ((req.query && req.query.method === 'polling') || (req.body && req.body.method === 'polling')) {
59
- url = req.query.sid || req.headers['x-forwarded-for'] || req.socket.remoteAddress;
60
- }
61
-
62
- if (!url) {
63
- res.status(422).json({error: 'url not provided', expectedBody: {url: 'http://ipaddress:9000/hook/'}});
64
- return;
65
- }
66
-
67
- try {
68
- const obj = await req._adapter.getForeignObjectAsync(params.stateId, {user: req._user});
69
- if (!obj) {
70
- res.status(404).json({error: 'object not found', url: req.body.url});
71
- } else if (obj.type !== 'state') {
72
- res.status(500).json({error: 'Cannot subscribe on non-state', stateId: params.stateId, type: obj.type, url: req.body.url});
73
- } else {
74
- const error = await req._swaggerObject.registerSubscribe(url, params.stateId, 'state', req._user, {
75
- method: (req.query && req.query.method) || (req.body && req.body.method),
76
- delta: (req.query && req.query.delta) || (req.body && req.body.delta),
77
- onchange: (req.query && req.query.onchange) || (req.body && req.body.onchange),
78
- });
79
- if (error) {
80
- commonLib.errorResponse(req, res, error, {stateId: params.stateId, url: req.body.url});
81
- return;
82
- }
83
- const state = await req._adapter.getForeignStateAsync(params.stateId, {user: req._user});
84
- res.status(200).json(state);
85
- }
86
- } catch (error) {
87
- commonLib.errorResponse(req, res, error, {stateId: params.stateId});
88
- }
89
- }
90
- });
91
- }
92
-
93
- function toggleState(req, res, oId) {
94
- let timeout = 0;
95
- if (req.query.timeout) {
96
- timeout = parseInt(req.query.timeout, 10);
97
-
98
- if (timeout > 60000) {
99
- timeout = 60000;
100
- } // maximum 1 minute
101
- }
102
-
103
- commonLib.findState(req._adapter, oId, req._user, async (error, id, originId) => {
104
- if (error && error.message && error.message.includes('permissionError')) {
105
- // assume it is ID
106
- id = oId;
107
- error = null;
108
- }
109
- if (error) {
110
- commonLib.errorResponse(req, res, error, {id: oId});
111
- } else if (!id) {
112
- res.status(404).json({error: 'ID not found', id: originId});
113
- } else {
114
- try {
115
- const state = await req._adapter.getForeignStateAsync(id, {user: req._user, limitToOwnerRights: req._adapter.config.onlyAllowWhenUserIsOwner});
116
- if (!state || typeof state !== 'object') {
117
- res.status(500).json({error: 'State not initiated', id: originId});
118
- } else {
119
- let obj;
120
- try {
121
- obj = await req._adapter.getForeignObjectAsync(id, {user: req._user, limitToOwnerRights: req._adapter.config.onlyAllowWhenUserIsOwner});
122
- } catch (error) {
123
- req._adapter.log.warn(`Cannot read object ${id}: ${error}`);
124
- }
125
- let val;
126
- if (state.val === 'true') {
127
- val = 'false';
128
- } else if (state.val === 'false') {
129
- val = 'true';
130
- } else if (state.val === 'on') {
131
- val = 'off';
132
- } else if (state.val === 'off') {
133
- val = 'on';
134
- } else if (state.val === 'OFF') {
135
- val = 'ON';
136
- } else if (state.val === 'ON') {
137
- val = 'OFF';
138
- } else if (state.val === '0') {
139
- val = '1';
140
- } else if (state.val === '1') {
141
- val = '0';
142
- } else if (typeof state.val === 'number') {
143
- val = state.val ? 0 : 1;
144
- } else {
145
- val = !state.val;
146
- }
147
-
148
- if (obj && obj.common) {
149
- if (obj.common.type === 'boolean') {
150
- state.val = state.val === 'true' || state.val === true;
151
- } else if (obj.common.type === 'number') {
152
- if (obj.common.min !== undefined && obj.common.max !== undefined) {
153
- val = parseFloat(state.val);
154
- if (val > obj.common.max) {
155
- val = obj.common.max;
156
- } else
157
- if (val < obj.common.min) {
158
- val = obj.common.min;
159
- }
160
- val = obj.common.max + obj.common.min - val;
161
- } else {
162
- val = parseFloat(val);
163
- }
164
- }
165
- }
166
-
167
- await updateState(req._adapter, req._user, id, timeout, val, res);
168
- }
169
- } catch (error) {
170
- commonLib.errorResponse(req, res, error, {id: oId});
171
- }
172
- }
173
- });
174
- }
175
-
176
- module.exports = {
177
- updateState: function (req, res) {
178
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'state', operation: 'write'}], error => {
179
- if (error) {
180
- commonLib.errorResponse(req, res, error);
181
- } else {
182
- const params = commonLib.parseUrl(req.url, req.swagger, req._adapter.WEB_EXTENSION_PREFIX);
183
- const oId = getIDs(params.stateId);
184
- let timeout = 0;
185
- if (req.query.timeout) {
186
- timeout = parseInt(req.query.timeout, 10);
187
-
188
- if (timeout > 60000) {
189
- timeout = 60000;
190
- } // maximum 1 minute
191
- }
192
-
193
- commonLib.findState(req._adapter, oId[0], req._user, async (error, id, originId) => {
194
- if (error && error.message && error.message.includes('permissionError')) {
195
- // assume it is ID
196
- id = oId[0];
197
- error = null;
198
- }
199
-
200
- if (error) {
201
- commonLib.errorResponse(req, res, error, {id: oId[0]});
202
- } else if (!id) {
203
- res.status(404).json({error: 'ID not found', id: originId});
204
- } else {
205
- await updateState(req._adapter, req._user, id, timeout, req.body, res);
206
- }
207
- });
208
- }
209
- });
210
- },
211
-
212
- toggleState: function (req, res) {
213
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'state', operation: 'write'}], error => {
214
- if (error) {
215
- commonLib.errorResponse(req, res, error);
216
- } else {
217
- const params = commonLib.parseUrl(req.url, req.swagger, req._adapter.WEB_EXTENSION_PREFIX);
218
- const oId = getIDs(params.stateId);
219
- toggleState(req, res, oId[0]);
220
- }
221
- });
222
- },
223
-
224
- readState: function (req, res) {
225
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'state', operation: 'read'}], async error => {
226
- if (error) {
227
- commonLib.errorResponse(req, res, error);
228
- } else {
229
- const params = commonLib.parseUrl(req.url, req.swagger, req._adapter.WEB_EXTENSION_PREFIX);
230
- const oId = getIDs(params.stateId);
231
- let timeout = 0;
232
- if (req.query.timeout) {
233
- timeout = parseInt(req.query.timeout, 10);
234
-
235
- if (timeout > 60000) {
236
- timeout = 60000;
237
- } // maximum 1 minute
238
- }
239
-
240
- let result;
241
- for (let k = 0; k < oId.length; k++) {
242
- try {
243
- const {state, id, originId} = await new Promise((resolve, reject) => commonLib.getState(req._adapter, oId[k], req._user, (error, state, id, originId) =>
244
- error ? reject(error) : resolve({state, id, originId})));
245
-
246
- if (!id) {
247
- res.status(404).json({error: 'ID not found', id: originId});
248
- return;
249
- } else {
250
- if (req.query.value !== undefined) {
251
- await updateState(req._adapter, req._user, id, timeout, req.query.value, res);
252
- return;
253
- } else if (req.query.toggle !== undefined) {
254
- await toggleState(req, res, id);
255
- return;
256
- }
257
-
258
- const vObj = state || {};
259
- if (req.query.withInfo === 'true') {
260
- try {
261
- const obj = await req._adapter.getForeignObjectAsync(id);
262
- // copy all attributes of the object into state
263
- if (obj) {
264
- Object.keys(obj).forEach(attr => {
265
- if (attr === '_id') {
266
- vObj.id = obj._id;
267
- } else {
268
- vObj[attr] = obj[attr];
269
- }
270
- });
271
- }
272
- } catch (error) {
273
- req._adapter.log.warn(`Error by reading of object "${id}": ${error}`);
274
- }
275
- }
276
-
277
- if (!result) {
278
- result = vObj;
279
- } else {
280
- if (!Array.isArray(result)) {
281
- result = [result];
282
- }
283
- result.push(vObj);
284
- }
285
- }
286
- } catch (error) {
287
- req._adapter.log.warn(`Cannot read ${oId}: ${error}`);
288
- commonLib.errorResponse(req, res, error, {id: oId});
289
- return;
290
- }
291
- }
292
-
293
- res.json(result);
294
- }
295
- });
296
- },
297
-
298
- readBinaryState: function (req, res) {
299
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'state', operation: 'read'}], async error => {
300
- if (error) {
301
- commonLib.errorResponse(req, res, error);
302
- } else {
303
- const params = commonLib.parseUrl(req.url, req.swagger, req._adapter.WEB_EXTENSION_PREFIX);
304
- const oId = getIDs(params.stateId)[0];
305
-
306
- let result;
307
- let foundID;
308
- try {
309
- const {binary, id, originId} = await new Promise((resolve, reject) => commonLib.getBinaryState(req._adapter, oId, req._user, (error, binary, id) =>
310
- error ? reject(error) : resolve({binary, id})));
311
-
312
- if (!id) {
313
- res.status(404).json({error: 'ID not found', id: originId});
314
- return;
315
- }
316
- result = binary;
317
- foundID = id;
318
- } catch (error) {
319
- req._adapter.log.warn(`Cannot read ${oId}: ${error}`);
320
- commonLib.errorResponse(req, res, error, {id: oId});
321
- return;
322
- }
323
-
324
- // try to detect popular content types
325
- foundID = foundID.toLowerCase()
326
- if (foundID.endsWith('.png')) {
327
- res.setHeader('Content-Type', 'image/png');
328
- } else if (foundID.endsWith('.jpg')) {
329
- res.setHeader('Content-Type', 'image/jpeg');
330
- } else if (foundID.endsWith('.svg')) {
331
- res.setHeader('Content-Type', 'image/svg+xml');
332
- } else if (foundID.endsWith('.txt')) {
333
- res.setHeader('Content-Type', 'text/plain');
334
- } else if (foundID.endsWith('.json')) {
335
- res.setHeader('Content-Type', 'application/json');
336
- } else if (foundID.endsWith('.mp3')) {
337
- res.setHeader('Content-Type', 'audio/mpeg');
338
- } else if (foundID.endsWith('.wav')) {
339
- res.setHeader('Content-Type', 'audio/wav');
340
- } else if (foundID.endsWith('.ogg')) {
341
- res.setHeader('Content-Type', 'audio/ogg');
342
- } else {
343
- res.setHeader('Content-Type', 'application/octet-stream');
344
- }
345
-
346
- res.send(result);
347
- }
348
- });
349
- },
350
-
351
- writeBinaryState: function (req, res) {
352
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'state', operation: 'write'}], error => {
353
- if (error) {
354
- commonLib.errorResponse(req, res, error);
355
- } else {
356
- const params = commonLib.parseUrl(req.url, req.swagger, req._adapter.WEB_EXTENSION_PREFIX);
357
- const oId = getIDs(params.stateId)[0];
358
-
359
- commonLib.findState(req._adapter, oId, req._user, async (error, id, originId) => {
360
- if (error && error.message && error.message.includes('permissionError')) {
361
- // assume it is ID
362
- id = oId;
363
- error = null;
364
- }
365
-
366
- if (error) {
367
- commonLib.errorResponse(req, res, error, {id: oId});
368
- } else if (!id) {
369
- res.status(404).json({error: 'ID not found', id: oId});
370
- } else {
371
- try {
372
- const obj = await req._adapter.getForeignObjectAsync(id, {user: req._user, limitToOwnerRights: req._adapter.config.onlyAllowWhenUserIsOwner});
373
- if (obj && obj.common && obj.common.type === 'file') {
374
- if (req._adapter.setForeignBinaryStateAsync) {
375
- await req._adapter.setForeignBinaryStateAsync(id, req.body, {user: req._user, limitToOwnerRights: req._adapter.config.onlyAllowWhenUserIsOwner});
376
- } else {
377
- await req._adapter.setBinaryStateAsync(id, req.body, {user: req._user, limitToOwnerRights: req._adapter.config.onlyAllowWhenUserIsOwner});
378
- }
379
- res.status(200).json({result: 'ok', id});
380
- } else {
381
- res.status(422).json({error: 'State is not binary', id});
382
- }
383
- } catch (error) {
384
- req._adapter.log.warn(`Cannot read object ${id}: ${error.toString()}`);
385
- res.status(401).json({error: `Cannot read object ${id}: ${error.toString()}`, id});
386
- }
387
- }
388
- });
389
- }
390
- });
391
- },
392
-
393
- plainState: function (req, res) {
394
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'state', operation: 'read'}], async error => {
395
- if (error) {
396
- commonLib.errorResponse(req, res, error);
397
- } else {
398
- const params = commonLib.parseUrl(req.url, req.swagger, req._adapter.WEB_EXTENSION_PREFIX);
399
- const oId = getIDs(params.stateId);
400
- try {
401
- const {state, id, originId} = await new Promise((resolve, reject) => commonLib.getState(req._adapter, oId[0], req._user, (error, state, id, originId) =>
402
- error ? reject(error) : resolve({state, id, originId})));
403
-
404
- if (!id) {
405
- res.status(404).json({error: 'ID not found', id: originId});
406
- } else if (!state || typeof state !== 'object') {
407
- res.status(404).json({error: 'State not found', id: originId});
408
- } else {
409
- if (req.query.extraPlain === 'true') {
410
- if (state.val === null) {
411
- res.send('null');
412
- } else if (state.val === undefined) {
413
- res.send('undefined');
414
- } else {
415
- res.send(state.val.toString());
416
- }
417
- } else {
418
- res.send(JSON.stringify(state.val));
419
- }
420
- }
421
- } catch (error) {
422
- commonLib.errorResponse(req, res, error, {id: oId});
423
- }
424
- }
425
- });
426
- },
427
-
428
- listStates: function (req, res) {
429
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'state', operation: 'list'}], error => {
430
- if (error) {
431
- commonLib.errorResponse(req, res, error);
432
- } else {
433
- req._adapter.getForeignStates(req.query.filter || '*', {
434
- user: req._user,
435
- limitToOwnerRights: req._adapter.config.onlyAllowWhenUserIsOwner
436
- }, (error, list) => {
437
- if (error) {
438
- commonLib.errorResponse(req, res, error, {filter: req.query.filter});
439
- } else {
440
- res.json(list || []);
441
- }
442
- });
443
- }
444
- });
445
- },
446
-
447
- subscribeStateGet: subscribeState,
448
-
449
- subscribeState: subscribeState,
450
-
451
- unsubscribeState: function (req, res) {
452
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'state', operation: 'read'}], async error => {
453
- if (error) {
454
- commonLib.errorResponse(req, res, error);
455
- } else {
456
- const params = commonLib.parseUrl(req.url, req.swagger, req._adapter.WEB_EXTENSION_PREFIX);
457
-
458
- let url = req.body.url;
459
- if ((req.query && req.query.method === 'polling') || (req.body && req.body.method === 'polling')) {
460
- url = req.query.sid || req.headers['x-forwarded-for'] || req.socket.remoteAddress;
461
- }
462
-
463
- if (!url) {
464
- res.status(422).json({error: 'url not provided', expectedBody: {url: 'http://ipaddress:9000/hook/'}});
465
- return;
466
- }
467
-
468
- try {
469
- await req._swaggerObject.unregisterSubscribe(url, params.stateId, 'state', req._user, (req.query && req.query.method) || (req.body && req.body.method));
470
- res.status(200).json({result: 'OK'});
471
- } catch (error) {
472
- commonLib.errorResponse(req, res, error, {stateId: params.stateId});
473
- }
474
- }
475
- });
476
- },
477
-
478
- subscribeStates: function (req, res) {
479
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'state', operation: 'read'}], async error => {
480
- if (error) {
481
- commonLib.errorResponse(req, res, error);
482
- } else {
483
- let url = req.body.url;
484
- if ((req.query && req.query.method === 'polling') || (req.body && req.body.method === 'polling')) {
485
- url = req.query.sid || req.headers['x-forwarded-for'] || req.socket.remoteAddress;
486
- }
487
-
488
- if (!url) {
489
- res.status(422).json({error: 'url not provided', expectedBody: {url: 'http://ipaddress:9000/hook/'}});
490
- return;
491
- }
492
-
493
- if (!req.body.pattern) {
494
- res.status(422).json({
495
- error: 'pattern not provided',
496
- expectedBody: {url: 'http://ipaddress:9000/hook/', pattern: 'system.adapter.admin.0.*'}
497
- });
498
- return;
499
- }
500
- try {
501
- await req._swaggerObject.registerSubscribe(url, req.body.pattern, 'state', req._user, {
502
- method: req.body.method,
503
- onchange: req.body.onchange === 'true',
504
- delta: req.body.delta !== undefined ? parseFloat(req.body.delta) : undefined,
505
- });
506
- } catch (error) {
507
- commonLib.errorResponse(req, res, error, {pattern: req.body.pattern, url: req.body.url});
508
- }
509
- }
510
- });
511
- },
512
-
513
- unsubscribeStates: function (req, res) {
514
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'state', operation: 'read'}], async error => {
515
- if (error) {
516
- commonLib.errorResponse(req, res, error);
517
- } else {
518
- let url = req.body.url;
519
- if (req.body.method === 'polling') {
520
- url = req.query.sid || req.headers['x-forwarded-for'] || req.socket.remoteAddress;
521
- }
522
-
523
- if (!url) {
524
- res.status(422).json({error: 'url not provided', expectedBody: {url: 'http://ipaddress:9000/hook/'}});
525
- return;
526
- }
527
- try {
528
- await req._swaggerObject.unregisterSubscribe(url, req.body.pattern, 'state', req._user, req.body.method);
529
- res.status(200).json({result: 'OK'});
530
- } catch (error) {
531
- commonLib.errorResponse(req, res, error, {pattern: req.body.pattern, url: req.body.url});
532
- }
533
- }
534
- });
535
- },
536
-
537
- getStatesSubscribes: function (req, res) {
538
- commonLib.checkPermissions(req._adapter, req._user, [{type: 'state', operation: 'read'}], async error => {
539
- if (error) {
540
- commonLib.errorResponse(req, res, error);
541
- } else {
542
- let url = req.body.url;
543
- if ((req.query && req.query.method === 'polling') || (req.body && req.body.method === 'polling')) {
544
- url = req.query.sid || req.headers['x-forwarded-for'] || req.socket.remoteAddress;
545
- }
546
-
547
- if (!url) {
548
- res.status(422).json({error: 'url not provided', expectedBody: {url: 'http://ipaddress:9000/hook/'}});
549
- return;
550
- }
551
-
552
- try {
553
- const result = await req._swaggerObject.getSubscribes(url, req.body.pattern, 'state');
554
- if (result === null) {
555
- res.status(404).json({error: 'URL or session not found'});
556
- return;
557
- }
558
- res.json({states: result});
559
- } catch (error) {
560
- commonLib.errorResponse(req, res, error, {pattern: req.body.pattern, url: req.body.url});
561
- }
562
- }
563
- });
564
- },
565
- };