node-red-contrib-mobius-flow-base 3.1.7 → 3.3.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.
@@ -1,306 +1,408 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.EngineApiCalls = void 0;
4
+ const maxQueueLength = 25;
4
5
  class EngineApiCalls {
5
6
  apiClient = null;
6
- constructor() { }
7
+ loggedIn = false;
8
+ // queue of callback functions
9
+ messageQueue = { maxLength: 100, timestep: 500, interval: null, queue: [] };
10
+ constructor() {
11
+ this.startQueueInterval();
12
+ }
13
+ startQueueInterval = () => {
14
+ this.messageQueue.interval = setInterval(() => {
15
+ if (this.messageQueue.queue.length > this.messageQueue.maxLength) {
16
+ this.messageQueue.queue = this.messageQueue.queue.slice(-this.messageQueue.maxLength);
17
+ console.log(`Node-RED Engine API Client: Maximum queue length ${this.messageQueue.maxLength} reached. Dropping oldest message.`);
18
+ }
19
+ if (this.loggedIn) {
20
+ const func = this.messageQueue.queue.shift();
21
+ if (func) {
22
+ func();
23
+ }
24
+ }
25
+ }, this.messageQueue.timestep);
26
+ };
27
+ stopQueueInterval = () => {
28
+ if (this.messageQueue.interval) {
29
+ clearInterval(this.messageQueue.interval);
30
+ this.messageQueue.interval = null;
31
+ }
32
+ };
7
33
  setApiClient = (apiClient) => {
8
34
  this.apiClient = apiClient;
9
35
  };
36
+ setLoggedIn = (loggedIn) => {
37
+ this.loggedIn = loggedIn;
38
+ };
10
39
  discover = async (uri, callback) => {
11
- try {
12
- if (!this.apiClient)
13
- throw new Error('No API client set');
14
- const response = await this.apiClient({
15
- method: 'get',
16
- url: `/mobius/discover/${uri}`,
17
- });
18
- callback(null, response.data);
19
- }
20
- catch (err) {
21
- callback(err);
22
- }
40
+ const func = async () => {
41
+ try {
42
+ if (!this.apiClient)
43
+ throw new Error('No API client set');
44
+ const response = await this.apiClient({
45
+ method: 'get',
46
+ url: `/mobius/discover/${uri}`,
47
+ });
48
+ callback(null, response.data);
49
+ }
50
+ catch (err) {
51
+ if (err.response?.status === 404) {
52
+ callback(null, { ts: new Date().toISOString(), uri, data: [] });
53
+ }
54
+ else {
55
+ callback(err);
56
+ }
57
+ }
58
+ };
59
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
23
60
  };
24
61
  writeResource = async (uri, value, priority, callback) => {
25
- try {
26
- if (!this.apiClient)
27
- throw new Error('No API client set');
28
- const response = await this.apiClient({
29
- method: 'patch',
30
- url: `/mobius/object/resource/${uri}`,
31
- data: {
32
- value,
33
- priority,
34
- },
35
- });
36
- callback(null, response.data);
37
- }
38
- catch (err) {
39
- callback(err);
40
- }
62
+ const func = async () => {
63
+ try {
64
+ if (!this.apiClient)
65
+ throw new Error('No API client set');
66
+ const response = await this.apiClient({
67
+ method: 'patch',
68
+ url: `/mobius/object/resource/${uri}`,
69
+ data: {
70
+ value,
71
+ priority,
72
+ },
73
+ });
74
+ callback(null, response.data);
75
+ }
76
+ catch (err) {
77
+ callback(err);
78
+ }
79
+ };
80
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
41
81
  };
42
82
  writeMultiResource = async (uri, updates, callback) => {
43
- try {
44
- if (!this.apiClient)
45
- throw new Error('No API client set');
46
- const response = await this.apiClient({
47
- method: 'patch',
48
- url: `/mobius/object/resource/${uri}`,
49
- data: updates,
50
- });
51
- callback(null, response.data);
52
- }
53
- catch (err) {
54
- callback(err);
55
- }
83
+ const func = async () => {
84
+ try {
85
+ if (!this.apiClient)
86
+ throw new Error('No API client set');
87
+ const response = await this.apiClient({
88
+ method: 'patch',
89
+ url: `/mobius/object/resource/${uri}`,
90
+ data: updates,
91
+ });
92
+ callback(null, response.data);
93
+ }
94
+ catch (err) {
95
+ callback(err);
96
+ }
97
+ };
98
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
56
99
  };
57
100
  clearResource = async (uri, priority, callback) => {
58
- try {
59
- if (!this.apiClient)
60
- throw new Error('No API client set');
61
- const response = await this.apiClient({
62
- method: 'patch',
63
- url: `/mobius/object/resource/clear/${uri}`,
64
- data: {
65
- priority,
66
- },
67
- });
68
- callback(null, response.data);
69
- }
70
- catch (err) {
71
- callback(err);
72
- }
101
+ const func = async () => {
102
+ try {
103
+ if (!this.apiClient)
104
+ throw new Error('No API client set');
105
+ const response = await this.apiClient({
106
+ method: 'patch',
107
+ url: `/mobius/object/resource/clear/${uri}`,
108
+ data: {
109
+ priority,
110
+ },
111
+ });
112
+ callback(null, response.data);
113
+ }
114
+ catch (err) {
115
+ callback(err);
116
+ }
117
+ };
118
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
73
119
  };
74
120
  readResource = async (uri, callback) => {
75
- try {
76
- if (!this.apiClient)
77
- throw new Error('No API client set');
78
- const response = await this.apiClient({
79
- method: 'get',
80
- url: `/mobius/object/resource/${uri}`,
81
- });
82
- callback(null, response.data);
83
- }
84
- catch (err) {
85
- callback(err);
86
- }
121
+ const func = async () => {
122
+ try {
123
+ if (!this.apiClient)
124
+ throw new Error('No API client set');
125
+ const response = await this.apiClient({
126
+ method: 'get',
127
+ url: `/mobius/object/resource/${uri}`,
128
+ });
129
+ callback(null, response.data);
130
+ }
131
+ catch (err) {
132
+ callback(err);
133
+ }
134
+ };
135
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
87
136
  };
88
137
  readObject = async (uri, callback) => {
89
- try {
90
- if (!this.apiClient)
91
- throw new Error('No API client set');
92
- const response = await this.apiClient({
93
- method: 'get',
94
- url: `/mobius/object/${uri}`,
95
- });
96
- callback(null, response.data);
97
- }
98
- catch (err) {
99
- callback(err);
100
- }
138
+ const func = async () => {
139
+ try {
140
+ if (!this.apiClient)
141
+ throw new Error('No API client set');
142
+ const response = await this.apiClient({
143
+ method: 'get',
144
+ url: `/mobius/object/${uri}`,
145
+ });
146
+ callback(null, response.data);
147
+ }
148
+ catch (err) {
149
+ callback(err);
150
+ }
151
+ };
152
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
101
153
  };
102
- sendBroadcastCommand = async (cid, commandData) => {
103
- try {
104
- if (!this.apiClient)
105
- throw new Error('No API client set');
106
- const response = await this.apiClient({
107
- method: 'post',
108
- url: `/mobius/command/${cid}`,
109
- data: commandData,
110
- });
111
- }
112
- catch (err) {
113
- throw `Failed to send broadcast command: ${err.code ? err.code : 'Unknown'}, ${err.message ? err.message : ''}`;
114
- }
154
+ sendBroadcastCommand = async (cid, commandData, callback) => {
155
+ const func = async () => {
156
+ try {
157
+ if (!this.apiClient)
158
+ throw new Error('No API client set');
159
+ const response = await this.apiClient({
160
+ method: 'post',
161
+ url: `/mobius/command/${cid}`,
162
+ data: commandData,
163
+ });
164
+ callback(null, 'Ok');
165
+ }
166
+ catch (err) {
167
+ callback(err);
168
+ }
169
+ };
170
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
115
171
  };
116
172
  sendDirectCommand = async (uri, cid, commandData, timeout, callback) => {
117
- try {
118
- if (!this.apiClient)
119
- throw new Error('No API client set');
120
- const response = await this.apiClient({
121
- method: 'post',
122
- url: `/mobius/directCommand/${cid}`,
123
- data: {
124
- uri,
125
- commandData,
126
- timeout,
127
- },
128
- });
129
- callback(null, response.data);
130
- }
131
- catch (err) {
132
- callback(err);
133
- }
173
+ const func = async () => {
174
+ try {
175
+ if (!this.apiClient)
176
+ throw new Error('No API client set');
177
+ const response = await this.apiClient({
178
+ method: 'post',
179
+ url: `/mobius/directCommand/${cid}`,
180
+ data: {
181
+ uri,
182
+ commandData,
183
+ timeout,
184
+ },
185
+ });
186
+ callback(null, response.data);
187
+ }
188
+ catch (err) {
189
+ callback(err);
190
+ }
191
+ };
192
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
134
193
  };
135
194
  getServiceConfig = async (uri, callback) => {
136
- try {
137
- if (!this.apiClient)
138
- throw new Error('No API client set');
139
- const response = await this.apiClient({
140
- method: 'get',
141
- url: `/mobius/service/config/${uri}`,
142
- });
143
- callback(null, response.data);
144
- }
145
- catch (err) {
146
- callback(err);
147
- }
148
- };
149
- updateObject = async (uri, config, callback) => {
150
- try {
151
- if (!this.apiClient)
152
- throw new Error('No API client set');
153
- const response = await this.apiClient({
154
- method: 'post',
155
- url: `/mobius/object/config/${uri}`,
156
- data: config,
157
- });
158
- callback(null, response.data);
159
- }
160
- catch (err) {
161
- callback(err);
162
- }
163
- };
164
- filterObjectsByRIDValue = async (uri, pids, rid, limit, func, callback) => {
165
- try {
166
- if (!this.apiClient)
167
- throw new Error('No API client set');
168
- const discoveries = [];
169
- for (let pid of pids) {
170
- discoveries.push(this.apiClient({
195
+ const func = async () => {
196
+ try {
197
+ if (!this.apiClient)
198
+ throw new Error('No API client set');
199
+ const response = await this.apiClient({
171
200
  method: 'get',
172
- url: `/mobius/discover/${uri}/${pid}`,
173
- }));
201
+ url: `/mobius/service/config/${uri}`,
202
+ });
203
+ callback(null, response.data);
174
204
  }
175
- const pidGroups = (await Promise.all(discoveries)).map((response) => ({
176
- uri: response.data.uri,
177
- inss: response.data.data,
178
- }));
179
- const objectUris = [];
180
- for (let pidGroup of pidGroups) {
181
- for (let ins of pidGroup.inss) {
182
- objectUris.push(`${pidGroup.uri}/${ins}`);
183
- }
205
+ catch (err) {
206
+ callback(err);
184
207
  }
185
- const objects = [];
186
- for (let objectUri of objectUris) {
187
- const object = await this.apiClient({
188
- method: 'get',
189
- url: `/mobius/object/${objectUri}`,
208
+ };
209
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
210
+ };
211
+ updateObject = async (uri, config, callback) => {
212
+ const func = async () => {
213
+ try {
214
+ if (!this.apiClient)
215
+ throw new Error('No API client set');
216
+ const response = await this.apiClient({
217
+ method: 'post',
218
+ url: `/mobius/object/config/${uri}`,
219
+ data: config,
190
220
  });
191
- const resource = object.data.data.resources.find((r) => r.rid === rid);
192
- if (func(resource?.pv)) {
193
- objects.push(object.data.data);
221
+ callback(null, response.data);
222
+ }
223
+ catch (err) {
224
+ callback(err);
225
+ }
226
+ };
227
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
228
+ };
229
+ filterObjectsByRIDValue = async (uri, pids, rid, limit, processFunc, callback) => {
230
+ const func = async () => {
231
+ try {
232
+ if (!this.apiClient)
233
+ throw new Error('No API client set');
234
+ const discoveries = [];
235
+ const discoverOnPid = (pid) => {
236
+ return (async () => {
237
+ try {
238
+ const response = await this.apiClient({
239
+ method: 'get',
240
+ url: `/mobius/discover/${uri}/${pid}`,
241
+ });
242
+ return response;
243
+ }
244
+ catch (err) {
245
+ if (err.response?.status === 404) {
246
+ return { data: { data: [], uri: uri } };
247
+ }
248
+ else {
249
+ throw err;
250
+ }
251
+ }
252
+ })();
253
+ };
254
+ for (let pid of pids) {
255
+ discoveries.push(discoverOnPid(pid));
194
256
  }
195
- if (objects.length >= limit) {
196
- return callback(null, {
197
- uri,
198
- ts: new Date().toISOString(),
199
- value: objects,
257
+ const pidGroups = (await Promise.all(discoveries)).map((response) => ({
258
+ uri: response.data.uri,
259
+ inss: response.data.data,
260
+ }));
261
+ const objectUris = [];
262
+ for (let pidGroup of pidGroups) {
263
+ for (let ins of pidGroup.inss) {
264
+ objectUris.push(`${pidGroup.uri}/${ins}`);
265
+ }
266
+ }
267
+ const objects = [];
268
+ for (let objectUri of objectUris) {
269
+ const object = await this.apiClient({
270
+ method: 'get',
271
+ url: `/mobius/object/${objectUri}`,
200
272
  });
273
+ const resource = object.data.data.resources.find((r) => r.rid === rid);
274
+ if (processFunc(resource?.pv)) {
275
+ objects.push(object.data.data);
276
+ }
277
+ if (objects.length >= limit) {
278
+ return callback(null, {
279
+ uri,
280
+ ts: new Date().toISOString(),
281
+ value: objects,
282
+ });
283
+ }
201
284
  }
285
+ return callback(null, {
286
+ uri,
287
+ ts: new Date().toISOString(),
288
+ value: objects,
289
+ });
202
290
  }
203
- return callback(null, {
204
- uri,
205
- ts: new Date().toISOString(),
206
- value: objects,
207
- });
208
- }
209
- catch (err) {
210
- callback(err);
211
- }
291
+ catch (err) {
292
+ callback(err);
293
+ }
294
+ };
295
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
212
296
  };
213
297
  getHubId = async (callback) => {
214
- try {
215
- if (!this.apiClient)
216
- throw new Error('No API client set');
217
- const response = await this.apiClient({
218
- method: 'get',
219
- url: `/mobius/admin/settings`,
220
- });
221
- callback(null, response.data.data.mobiusFlow.hubID.runtime);
222
- }
223
- catch (err) {
224
- callback(err);
225
- }
298
+ const func = async () => {
299
+ try {
300
+ if (!this.apiClient)
301
+ throw new Error('No API client set');
302
+ const response = await this.apiClient({
303
+ method: 'get',
304
+ url: `/mobius/admin/settings`,
305
+ });
306
+ callback(null, response.data.data.mobiusFlow.hubID.runtime);
307
+ }
308
+ catch (err) {
309
+ callback(err);
310
+ }
311
+ };
312
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
226
313
  };
227
314
  getNodeConfig = async (uri, callback) => {
228
- try {
229
- if (!this.apiClient)
230
- throw new Error('No API client set');
231
- const response = await this.apiClient({
232
- method: 'get',
233
- url: `/mobius/node/config/${uri}`,
234
- });
235
- callback(null, response.data);
236
- }
237
- catch (err) {
238
- callback(err);
239
- }
315
+ const func = async () => {
316
+ try {
317
+ if (!this.apiClient)
318
+ throw new Error('No API client set');
319
+ const response = await this.apiClient({
320
+ method: 'get',
321
+ url: `/mobius/node/config/${uri}`,
322
+ });
323
+ callback(null, response.data);
324
+ }
325
+ catch (err) {
326
+ callback(err);
327
+ }
328
+ };
329
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
240
330
  };
241
331
  changeAdminPassword = async (password, callback) => {
242
- try {
243
- if (!this.apiClient)
244
- throw new Error('No API client set');
245
- const response = await this.apiClient({
246
- method: 'get',
247
- url: `/auth/user/admin`,
248
- });
249
- const adminUserId = response.data;
250
- await this.apiClient({
251
- method: 'patch',
252
- url: `/auth/user/password/${adminUserId}`,
253
- data: {
254
- password,
255
- },
256
- });
257
- callback(null, 'Ok');
258
- }
259
- catch (err) {
260
- callback(err);
261
- }
332
+ const func = async () => {
333
+ try {
334
+ if (!this.apiClient)
335
+ throw new Error('No API client set');
336
+ const response = await this.apiClient({
337
+ method: 'get',
338
+ url: `/auth/user/admin`,
339
+ });
340
+ const adminUserId = response.data;
341
+ await this.apiClient({
342
+ method: 'patch',
343
+ url: `/auth/user/password/${adminUserId}`,
344
+ data: {
345
+ password,
346
+ },
347
+ });
348
+ callback(null, 'Ok');
349
+ }
350
+ catch (err) {
351
+ callback(err);
352
+ }
353
+ };
354
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
262
355
  };
263
356
  getNodeConfigTree = async (hid, callback) => {
264
- try {
265
- if (!this.apiClient)
266
- throw new Error('No API client set');
267
- const response = await this.apiClient({
268
- method: 'get',
269
- url: `/mobius/node/configtree/${hid}`,
270
- });
271
- callback(null, response.data);
272
- }
273
- catch (err) {
274
- callback(err);
275
- }
357
+ const func = async () => {
358
+ try {
359
+ if (!this.apiClient)
360
+ throw new Error('No API client set');
361
+ const response = await this.apiClient({
362
+ method: 'get',
363
+ url: `/mobius/node/configtree/${hid}`,
364
+ });
365
+ callback(null, response.data);
366
+ }
367
+ catch (err) {
368
+ callback(err);
369
+ }
370
+ };
371
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
276
372
  };
277
373
  getProfileByPid = async (hid, pid, callback) => {
278
- try {
279
- if (!this.apiClient)
280
- throw new Error('No API client set');
281
- const response = await this.apiClient({
282
- method: 'get',
283
- url: `/mobius/profile/object/${hid}/${pid}`,
284
- });
285
- callback(null, response.data);
286
- }
287
- catch (err) {
288
- callback(err);
289
- }
374
+ const func = async () => {
375
+ try {
376
+ if (!this.apiClient)
377
+ throw new Error('No API client set');
378
+ const response = await this.apiClient({
379
+ method: 'get',
380
+ url: `/mobius/profile/object/${hid}/${pid}`,
381
+ });
382
+ callback(null, response.data);
383
+ }
384
+ catch (err) {
385
+ callback(err);
386
+ }
387
+ };
388
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
290
389
  };
291
390
  getAllObjectProfiles = async (hid, callback) => {
292
- try {
293
- if (!this.apiClient)
294
- throw new Error('No API client set');
295
- const response = await this.apiClient({
296
- method: 'get',
297
- url: `/mobius/profile/object/${hid}`,
298
- });
299
- callback(null, response.data);
300
- }
301
- catch (err) {
302
- callback(err);
303
- }
391
+ const func = async () => {
392
+ try {
393
+ if (!this.apiClient)
394
+ throw new Error('No API client set');
395
+ const response = await this.apiClient({
396
+ method: 'get',
397
+ url: `/mobius/profile/object/${hid}`,
398
+ });
399
+ callback(null, response.data);
400
+ }
401
+ catch (err) {
402
+ callback(err);
403
+ }
404
+ };
405
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
304
406
  };
305
407
  }
306
408
  exports.EngineApiCalls = EngineApiCalls;