node-red-contrib-mobius-flow-base 3.1.6 → 3.2.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,52 +1,59 @@
1
1
  import { AxiosInstance } from 'axios';
2
+ type Callback = (err: unknown | null, resp?: any) => void;
2
3
  interface EngineApiCallsInterace {
3
4
  setApiClient: (apiClient: AxiosInstance) => void;
4
- discover: (uri: string, callback: Function) => void;
5
- writeResource: (uri: string, value: number | string | boolean, priority: number, callback: Function) => void;
5
+ setLoggedIn: (loggedIn: boolean) => void;
6
+ discover: (uri: string, callback: Callback) => void;
7
+ writeResource: (uri: string, value: number | string | boolean, priority: number, callback: Callback) => void;
6
8
  writeMultiResource: (uri: string, updates: {
7
9
  r: string;
8
10
  p: number;
9
11
  v: number | string | boolean;
10
- }[], callback: Function) => void;
11
- clearResource: (uri: string, priority: number, callback: Function) => void;
12
- readResource: (uri: string, callback: Function) => void;
13
- readObject: (uri: string, callback: Function) => void;
14
- sendBroadcastCommand: (cid: string, commandData: any) => void;
15
- sendDirectCommand: (uri: string, cid: string, commandData: any, timeout: number, callback: Function) => void;
16
- getServiceConfig: (uri: string, callback: Function) => void;
17
- updateObject: (uri: string, config: any, callback: Function) => void;
18
- filterObjectsByRIDValue: (uri: string, pids: string[], rid: string, limit: number, func: (v: string | number | boolean) => boolean, callback: Function) => void;
19
- getHubId: (callback: Function) => void;
20
- getNodeConfig: (uri: string, callback: Function) => void;
21
- changeAdminPassword: (password: string, callback: Function) => void;
22
- getNodeConfigTree: (hid: string, callback: Function) => void;
23
- getProfileByPid: (hid: string, pid: string, callback: Function) => void;
24
- getAllObjectProfiles: (hid: string, callback: Function) => void;
12
+ }[], callback: Callback) => void;
13
+ clearResource: (uri: string, priority: number, callback: Callback) => void;
14
+ readResource: (uri: string, callback: Callback) => void;
15
+ readObject: (uri: string, callback: Callback) => void;
16
+ sendBroadcastCommand: (cid: string, commandData: any, callback: Callback) => void;
17
+ sendDirectCommand: (uri: string, cid: string, commandData: any, timeout: number, callback: Callback) => void;
18
+ getServiceConfig: (uri: string, callback: Callback) => void;
19
+ updateObject: (uri: string, config: any, callback: Callback) => void;
20
+ filterObjectsByRIDValue: (uri: string, pids: string[], rid: string, limit: number, func: (v: string | number | boolean) => boolean, callback: Callback) => void;
21
+ getHubId: (callback: Callback) => void;
22
+ getNodeConfig: (uri: string, callback: Callback) => void;
23
+ changeAdminPassword: (password: string, callback: Callback) => void;
24
+ getNodeConfigTree: (hid: string, callback: Callback) => void;
25
+ getProfileByPid: (hid: string, pid: string, callback: Callback) => void;
26
+ getAllObjectProfiles: (hid: string, callback: Callback) => void;
25
27
  }
26
28
  export declare class EngineApiCalls implements EngineApiCallsInterace {
27
29
  private apiClient;
30
+ loggedIn: boolean;
31
+ private messageQueue;
28
32
  constructor();
33
+ private startQueueInterval;
34
+ stopQueueInterval: () => void;
29
35
  setApiClient: (apiClient: AxiosInstance) => void;
30
- discover: (uri: string, callback: Function) => Promise<void>;
31
- writeResource: (uri: string, value: number | string | boolean, priority: number, callback: Function) => Promise<void>;
36
+ setLoggedIn: (loggedIn: boolean) => void;
37
+ discover: (uri: string, callback: Callback) => Promise<void>;
38
+ writeResource: (uri: string, value: number | string | boolean, priority: number, callback: Callback) => Promise<void>;
32
39
  writeMultiResource: (uri: string, updates: {
33
40
  r: string;
34
41
  p: number;
35
42
  v: number | string | boolean;
36
- }[], callback: Function) => Promise<void>;
37
- clearResource: (uri: string, priority: number, callback: Function) => Promise<void>;
38
- readResource: (uri: string, callback: Function) => Promise<void>;
39
- readObject: (uri: string, callback: Function) => Promise<void>;
40
- sendBroadcastCommand: (cid: string, commandData: any) => Promise<void>;
41
- sendDirectCommand: (uri: string, cid: string, commandData: any, timeout: number, callback: Function) => Promise<void>;
42
- getServiceConfig: (uri: string, callback: Function) => Promise<void>;
43
- updateObject: (uri: string, config: any, callback: Function) => Promise<void>;
44
- filterObjectsByRIDValue: (uri: string, pids: string[], rid: string, limit: number, func: (v: string | number | boolean) => boolean, callback: Function) => Promise<any>;
45
- getHubId: (callback: Function) => Promise<void>;
46
- getNodeConfig: (uri: string, callback: Function) => Promise<void>;
47
- changeAdminPassword: (password: string, callback: Function) => Promise<void>;
48
- getNodeConfigTree: (hid: string, callback: Function) => Promise<void>;
49
- getProfileByPid: (hid: string, pid: string, callback: Function) => Promise<void>;
50
- getAllObjectProfiles: (hid: string, callback: Function) => Promise<void>;
43
+ }[], callback: Callback) => Promise<void>;
44
+ clearResource: (uri: string, priority: number, callback: Callback) => Promise<void>;
45
+ readResource: (uri: string, callback: Callback) => Promise<void>;
46
+ readObject: (uri: string, callback: Callback) => Promise<void>;
47
+ sendBroadcastCommand: (cid: string, commandData: any, callback: Callback) => Promise<void>;
48
+ sendDirectCommand: (uri: string, cid: string, commandData: any, timeout: number, callback: Callback) => Promise<void>;
49
+ getServiceConfig: (uri: string, callback: Callback) => Promise<void>;
50
+ updateObject: (uri: string, config: any, callback: Callback) => Promise<void>;
51
+ filterObjectsByRIDValue: (uri: string, pids: string[], rid: string, limit: number, processFunc: (v: string | number | boolean) => boolean, callback: Callback) => Promise<void>;
52
+ getHubId: (callback: Callback) => Promise<void>;
53
+ getNodeConfig: (uri: string, callback: Callback) => Promise<void>;
54
+ changeAdminPassword: (password: string, callback: Callback) => Promise<void>;
55
+ getNodeConfigTree: (hid: string, callback: Callback) => Promise<void>;
56
+ getProfileByPid: (hid: string, pid: string, callback: Callback) => Promise<void>;
57
+ getAllObjectProfiles: (hid: string, callback: Callback) => Promise<void>;
51
58
  }
52
59
  export {};
@@ -1,306 +1,388 @@
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
+ console.log(err);
52
+ callback(err);
53
+ }
54
+ };
55
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
23
56
  };
24
57
  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
- }
58
+ const func = async () => {
59
+ try {
60
+ if (!this.apiClient)
61
+ throw new Error('No API client set');
62
+ const response = await this.apiClient({
63
+ method: 'patch',
64
+ url: `/mobius/object/resource/${uri}`,
65
+ data: {
66
+ value,
67
+ priority,
68
+ },
69
+ });
70
+ callback(null, response.data);
71
+ }
72
+ catch (err) {
73
+ callback(err);
74
+ }
75
+ };
76
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
41
77
  };
42
78
  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
- }
79
+ const func = async () => {
80
+ try {
81
+ if (!this.apiClient)
82
+ throw new Error('No API client set');
83
+ const response = await this.apiClient({
84
+ method: 'patch',
85
+ url: `/mobius/object/resource/${uri}`,
86
+ data: updates,
87
+ });
88
+ callback(null, response.data);
89
+ }
90
+ catch (err) {
91
+ callback(err);
92
+ }
93
+ };
94
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
56
95
  };
57
96
  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
- }
97
+ const func = async () => {
98
+ try {
99
+ if (!this.apiClient)
100
+ throw new Error('No API client set');
101
+ const response = await this.apiClient({
102
+ method: 'patch',
103
+ url: `/mobius/object/resource/clear/${uri}`,
104
+ data: {
105
+ priority,
106
+ },
107
+ });
108
+ callback(null, response.data);
109
+ }
110
+ catch (err) {
111
+ callback(err);
112
+ }
113
+ };
114
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
73
115
  };
74
116
  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
- }
117
+ const func = async () => {
118
+ try {
119
+ if (!this.apiClient)
120
+ throw new Error('No API client set');
121
+ const response = await this.apiClient({
122
+ method: 'get',
123
+ url: `/mobius/object/resource/${uri}`,
124
+ });
125
+ callback(null, response.data);
126
+ }
127
+ catch (err) {
128
+ callback(err);
129
+ }
130
+ };
131
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
87
132
  };
88
133
  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
- }
134
+ const func = async () => {
135
+ try {
136
+ if (!this.apiClient)
137
+ throw new Error('No API client set');
138
+ const response = await this.apiClient({
139
+ method: 'get',
140
+ url: `/mobius/object/${uri}`,
141
+ });
142
+ callback(null, response.data);
143
+ }
144
+ catch (err) {
145
+ callback(err);
146
+ }
147
+ };
148
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
101
149
  };
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
- }
150
+ sendBroadcastCommand = async (cid, commandData, callback) => {
151
+ const func = async () => {
152
+ try {
153
+ if (!this.apiClient)
154
+ throw new Error('No API client set');
155
+ const response = await this.apiClient({
156
+ method: 'post',
157
+ url: `/mobius/command/${cid}`,
158
+ data: commandData,
159
+ });
160
+ callback(null, 'Ok');
161
+ }
162
+ catch (err) {
163
+ callback(err);
164
+ }
165
+ };
166
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
115
167
  };
116
168
  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
- }
169
+ const func = async () => {
170
+ try {
171
+ if (!this.apiClient)
172
+ throw new Error('No API client set');
173
+ const response = await this.apiClient({
174
+ method: 'post',
175
+ url: `/mobius/directCommand/${cid}`,
176
+ data: {
177
+ uri,
178
+ commandData,
179
+ timeout,
180
+ },
181
+ });
182
+ callback(null, response.data);
183
+ }
184
+ catch (err) {
185
+ callback(err);
186
+ }
187
+ };
188
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
134
189
  };
135
190
  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({
191
+ const func = async () => {
192
+ try {
193
+ if (!this.apiClient)
194
+ throw new Error('No API client set');
195
+ const response = await this.apiClient({
171
196
  method: 'get',
172
- url: `/mobius/discover/${uri}/${pid}`,
173
- }));
197
+ url: `/mobius/service/config/${uri}`,
198
+ });
199
+ callback(null, response.data);
174
200
  }
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
- }
201
+ catch (err) {
202
+ callback(err);
184
203
  }
185
- const objects = [];
186
- for (let objectUri of objectUris) {
187
- const object = await this.apiClient({
188
- method: 'get',
189
- url: `/mobius/object/${objectUri}`,
204
+ };
205
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
206
+ };
207
+ updateObject = async (uri, config, callback) => {
208
+ const func = async () => {
209
+ try {
210
+ if (!this.apiClient)
211
+ throw new Error('No API client set');
212
+ const response = await this.apiClient({
213
+ method: 'post',
214
+ url: `/mobius/object/config/${uri}`,
215
+ data: config,
190
216
  });
191
- const resource = object.data.data.resources.find((r) => r.rid === rid);
192
- if (func(resource?.pv)) {
193
- objects.push(object.data.data);
217
+ callback(null, response.data);
218
+ }
219
+ catch (err) {
220
+ callback(err);
221
+ }
222
+ };
223
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
224
+ };
225
+ filterObjectsByRIDValue = async (uri, pids, rid, limit, processFunc, callback) => {
226
+ const func = async () => {
227
+ try {
228
+ if (!this.apiClient)
229
+ throw new Error('No API client set');
230
+ const discoveries = [];
231
+ for (let pid of pids) {
232
+ discoveries.push(this.apiClient({
233
+ method: 'get',
234
+ url: `/mobius/discover/${uri}/${pid}`,
235
+ }));
194
236
  }
195
- if (objects.length >= limit) {
196
- return callback(null, {
197
- uri,
198
- ts: new Date().toISOString(),
199
- value: objects,
237
+ const pidGroups = (await Promise.all(discoveries)).map((response) => ({
238
+ uri: response.data.uri,
239
+ inss: response.data.data,
240
+ }));
241
+ const objectUris = [];
242
+ for (let pidGroup of pidGroups) {
243
+ for (let ins of pidGroup.inss) {
244
+ objectUris.push(`${pidGroup.uri}/${ins}`);
245
+ }
246
+ }
247
+ const objects = [];
248
+ for (let objectUri of objectUris) {
249
+ const object = await this.apiClient({
250
+ method: 'get',
251
+ url: `/mobius/object/${objectUri}`,
200
252
  });
253
+ const resource = object.data.data.resources.find((r) => r.rid === rid);
254
+ if (processFunc(resource?.pv)) {
255
+ objects.push(object.data.data);
256
+ }
257
+ if (objects.length >= limit) {
258
+ return callback(null, {
259
+ uri,
260
+ ts: new Date().toISOString(),
261
+ value: objects,
262
+ });
263
+ }
201
264
  }
265
+ return callback(null, {
266
+ uri,
267
+ ts: new Date().toISOString(),
268
+ value: objects,
269
+ });
202
270
  }
203
- return callback(null, {
204
- uri,
205
- ts: new Date().toISOString(),
206
- value: objects,
207
- });
208
- }
209
- catch (err) {
210
- callback(err);
211
- }
271
+ catch (err) {
272
+ callback(err);
273
+ }
274
+ };
275
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
212
276
  };
213
277
  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
- }
278
+ const func = async () => {
279
+ try {
280
+ if (!this.apiClient)
281
+ throw new Error('No API client set');
282
+ const response = await this.apiClient({
283
+ method: 'get',
284
+ url: `/mobius/admin/settings`,
285
+ });
286
+ callback(null, response.data.data.mobiusFlow.hubID.runtime);
287
+ }
288
+ catch (err) {
289
+ callback(err);
290
+ }
291
+ };
292
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
226
293
  };
227
294
  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
- }
295
+ const func = async () => {
296
+ try {
297
+ if (!this.apiClient)
298
+ throw new Error('No API client set');
299
+ const response = await this.apiClient({
300
+ method: 'get',
301
+ url: `/mobius/node/config/${uri}`,
302
+ });
303
+ callback(null, response.data);
304
+ }
305
+ catch (err) {
306
+ callback(err);
307
+ }
308
+ };
309
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
240
310
  };
241
311
  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
- }
312
+ const func = async () => {
313
+ try {
314
+ if (!this.apiClient)
315
+ throw new Error('No API client set');
316
+ const response = await this.apiClient({
317
+ method: 'get',
318
+ url: `/auth/user/admin`,
319
+ });
320
+ const adminUserId = response.data;
321
+ await this.apiClient({
322
+ method: 'patch',
323
+ url: `/auth/user/password/${adminUserId}`,
324
+ data: {
325
+ password,
326
+ },
327
+ });
328
+ callback(null, 'Ok');
329
+ }
330
+ catch (err) {
331
+ callback(err);
332
+ }
333
+ };
334
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
262
335
  };
263
336
  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
- }
337
+ const func = async () => {
338
+ try {
339
+ if (!this.apiClient)
340
+ throw new Error('No API client set');
341
+ const response = await this.apiClient({
342
+ method: 'get',
343
+ url: `/mobius/node/configtree/${hid}`,
344
+ });
345
+ callback(null, response.data);
346
+ }
347
+ catch (err) {
348
+ callback(err);
349
+ }
350
+ };
351
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
276
352
  };
277
353
  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
- }
354
+ const func = async () => {
355
+ try {
356
+ if (!this.apiClient)
357
+ throw new Error('No API client set');
358
+ const response = await this.apiClient({
359
+ method: 'get',
360
+ url: `/mobius/profile/object/${hid}/${pid}`,
361
+ });
362
+ callback(null, response.data);
363
+ }
364
+ catch (err) {
365
+ callback(err);
366
+ }
367
+ };
368
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
290
369
  };
291
370
  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
- }
371
+ const func = async () => {
372
+ try {
373
+ if (!this.apiClient)
374
+ throw new Error('No API client set');
375
+ const response = await this.apiClient({
376
+ method: 'get',
377
+ url: `/mobius/profile/object/${hid}`,
378
+ });
379
+ callback(null, response.data);
380
+ }
381
+ catch (err) {
382
+ callback(err);
383
+ }
384
+ };
385
+ this.loggedIn ? func() : this.messageQueue.queue.push(func);
304
386
  };
305
387
  }
306
388
  exports.EngineApiCalls = EngineApiCalls;
@@ -1,6 +1,6 @@
1
- import { AxiosInstance } from "axios";
2
- import { EngineWebSocket } from "./engine-websocket";
3
- import { EngineApiCalls } from "./engine-api-calls";
1
+ import { AxiosInstance } from 'axios';
2
+ import { EngineWebSocket } from './engine-websocket';
3
+ import { EngineApiCalls } from './engine-api-calls';
4
4
  export declare class EngineApiClient extends EngineApiCalls {
5
5
  private reloginInterval;
6
6
  client: AxiosInstance | null;
@@ -9,13 +9,12 @@ export declare class EngineApiClient extends EngineApiCalls {
9
9
  token: string;
10
10
  refreshToken: string;
11
11
  };
12
- loggedIn: boolean;
13
12
  private username;
14
13
  private password;
15
14
  EngineWebSocket: EngineWebSocket;
16
15
  private hostname;
17
16
  private protocol;
18
- constructor(protocol: "http" | "https", host: string, username: string, password: string);
17
+ constructor(protocol: 'http' | 'https', host: string, username: string, password: string);
19
18
  private login;
20
19
  private refresh;
21
20
  close: () => void;
@@ -13,7 +13,6 @@ class EngineApiClient extends engine_api_calls_1.EngineApiCalls {
13
13
  client = null;
14
14
  refreshLoop = null;
15
15
  tokens = null;
16
- loggedIn = false;
17
16
  username;
18
17
  password;
19
18
  EngineWebSocket;
@@ -29,7 +28,7 @@ class EngineApiClient extends engine_api_calls_1.EngineApiCalls {
29
28
  this.client = axios_1.default.create({
30
29
  baseURL: baseURL,
31
30
  headers: {
32
- "Content-Type": "application/json",
31
+ 'Content-Type': 'application/json',
33
32
  },
34
33
  });
35
34
  this.setApiClient(this.client);
@@ -42,15 +41,15 @@ class EngineApiClient extends engine_api_calls_1.EngineApiCalls {
42
41
  this.login();
43
42
  }
44
43
  else {
45
- console.log("No username, password, host, or port provided. Not attempting to login to Mobius Engine API.");
44
+ console.log('No username, password, host, or port provided. Not attempting to login to Mobius Engine API.');
46
45
  this.reloginInterval && clearInterval(this.reloginInterval);
47
46
  this.reloginInterval = null;
48
47
  }
49
48
  // Add auth headers to all axios calls
50
49
  this.client.interceptors.request.use((config) => {
51
- if (!config.url?.endsWith("/auth/login")) {
50
+ if (!config.url?.endsWith('/auth/login')) {
52
51
  if (this.tokens && config.headers) {
53
- config.headers["Authorization"] = `Bearer ${this.tokens.token}`;
52
+ config.headers['Authorization'] = `Bearer ${this.tokens.token}`;
54
53
  }
55
54
  }
56
55
  return config;
@@ -63,7 +62,7 @@ class EngineApiClient extends engine_api_calls_1.EngineApiCalls {
63
62
  throw error;
64
63
  });
65
64
  this.EngineWebSocket = new engine_websocket_1.EngineWebSocket(this.hostname, this.protocol);
66
- this.EngineWebSocket.on("disconnected", () => {
65
+ this.EngineWebSocket.on('disconnected', () => {
67
66
  this.loggedIn = false;
68
67
  });
69
68
  }
@@ -71,8 +70,8 @@ class EngineApiClient extends engine_api_calls_1.EngineApiCalls {
71
70
  console.log(`(${this.hostname}) -> Attempting Mobius Engine API Login`);
72
71
  try {
73
72
  const response = await this.client({
74
- method: "post",
75
- url: "/auth/login",
73
+ method: 'post',
74
+ url: '/auth/login',
76
75
  data: {
77
76
  username: this.username,
78
77
  password: this.password,
@@ -98,8 +97,8 @@ class EngineApiClient extends engine_api_calls_1.EngineApiCalls {
98
97
  }
99
98
  try {
100
99
  const response = await this.client({
101
- method: "post",
102
- url: "/auth/refresh",
100
+ method: 'post',
101
+ url: '/auth/refresh',
103
102
  data: {
104
103
  refreshToken: this.tokens.refreshToken,
105
104
  },
@@ -112,6 +111,7 @@ class EngineApiClient extends engine_api_calls_1.EngineApiCalls {
112
111
  }
113
112
  };
114
113
  close = () => {
114
+ this.stopQueueInterval();
115
115
  if (this.refreshLoop) {
116
116
  clearInterval(this.refreshLoop);
117
117
  }
@@ -34,7 +34,7 @@ export interface MobiusConnectionInterface {
34
34
  unsubscribeToObjectCOV(uri: string, ref: string, callback: (err: any, response: any) => void): void;
35
35
  subscribeToBCMD(uri: string, ref: string, callback: (err: any, response: any) => void): void;
36
36
  unsubscribeToBCMD(uri: string, ref: string, callback: (err: any, response: any) => void): void;
37
- sendBroadcastCommand(cid: string, commandData: any): void;
37
+ sendBroadcastCommand(cid: string, commandData: any, callback: (err: any, response: any) => void): void;
38
38
  sendDirectCommand(uri: string, cid: string, timeout: number, callback: (err: any, response: any) => void): void;
39
39
  getServiceConfig(hid: string, sid: string, callback: (err: any, response: any) => void): void;
40
40
  updateObjectInServiceConfig(hid: string, sid: string, pid: string, ins: string, config: any, callback: (err: any, response: any) => void): void;
@@ -542,8 +542,8 @@ module.exports = function (RED) {
542
542
  * @param {string} cid - The command ID
543
543
  * @param {Object} commandData - An object containing the command data
544
544
  */
545
- node.sendBroadcastCommand = function (cid, commandData) {
546
- this.apiClient.sendBroadcastCommand(cid, commandData);
545
+ node.sendBroadcastCommand = function (cid, commandData, callback) {
546
+ this.apiClient.sendBroadcastCommand(cid, commandData, callback);
547
547
  };
548
548
 
549
549
  /**
@@ -108,6 +108,7 @@ module.exports = function (RED) {
108
108
  shape: 'dot',
109
109
  text: 'Failed discovery',
110
110
  });
111
+ node.error(`Failed to discover objects`);
111
112
  });
112
113
  if (done) {
113
114
  done();
@@ -122,113 +123,101 @@ module.exports = function (RED) {
122
123
  }
123
124
 
124
125
  node.discoverAllObjectURIs = async function (msg) {
125
- return new Promise(async (resolve, reject) => {
126
- let objectList = [];
126
+ let objectList = [];
127
+ const hid = node.hid === '' ? node.serviceConn.hubId : node.hid;
128
+ const profiles = await discoverProfiles(
129
+ node.serviceConn,
130
+ hid,
131
+ node.subscriptions
132
+ );
133
+
134
+ for await (let entry of profiles.entries()) {
135
+ let discoverParams = {};
136
+ const serviceURI = entry[0];
137
+ const pids = entry[1];
138
+ if (node.useRIDValueFiltering) {
139
+ discoverParams = {
140
+ uri: serviceURI,
141
+ pids: pids,
142
+ rid: node.rid,
143
+ func: node.func,
144
+ limit: node.limit,
145
+ };
146
+ } else {
147
+ discoverParams = {
148
+ uri: serviceURI,
149
+ pids: pids,
150
+ rid: '00',
151
+ func: '(v) => { return true; }',
152
+ limit: 512,
153
+ };
154
+ }
127
155
  try {
128
- const hid = node.hid === '' ? node.serviceConn.hubId : node.hid;
129
- const profiles = await discoverProfiles(
156
+ let objects = await filterObjectsByRIDValue(
130
157
  node.serviceConn,
131
- hid,
132
- node.subscriptions
158
+ discoverParams
133
159
  );
134
-
135
- for await (let entry of profiles.entries()) {
136
- let discoverParams = {};
137
- const serviceURI = entry[0];
138
- const pids = entry[1];
139
- if (node.useRIDValueFiltering) {
140
- discoverParams = {
141
- uri: serviceURI,
142
- pids: pids,
143
- rid: node.rid,
144
- func: node.func,
145
- limit: node.limit,
146
- };
147
- } else {
148
- discoverParams = {
149
- uri: serviceURI,
150
- pids: pids,
151
- rid: '00',
152
- func: '(v) => { return true; }',
153
- limit: 512,
154
- };
155
- }
156
- try {
157
- let objects = await filterObjectsByRIDValue(
158
- node.serviceConn,
159
- discoverParams
160
- );
161
- for (let lp = 0; lp < objects.value.length; lp++) {
162
- objects.value[lp][
163
- 'uri'
164
- ] = `${serviceURI}/${objects.value[lp].pid}/${objects.value[lp].ins}`;
165
- if (
166
- isIncluded(
167
- `${objects.value[lp]['uri']}`,
168
- node.subscriptions
169
- ) &&
170
- !isExcluded(`${objects.value[lp]['uri']}`, node.subscriptions)
171
- ) {
172
- objectList.push(objects.value[lp]);
173
- }
174
- }
175
- } catch (err) {
176
- node.warn(
177
- `Discover object failed for service (${serviceURI}) - ${err.message}`
178
- );
160
+ for (let lp = 0; lp < objects.value.length; lp++) {
161
+ objects.value[lp][
162
+ 'uri'
163
+ ] = `${serviceURI}/${objects.value[lp].pid}/${objects.value[lp].ins}`;
164
+ if (
165
+ isIncluded(`${objects.value[lp]['uri']}`, node.subscriptions) &&
166
+ !isExcluded(`${objects.value[lp]['uri']}`, node.subscriptions)
167
+ ) {
168
+ objectList.push(objects.value[lp]);
179
169
  }
180
170
  }
181
-
182
- if (objectList.length === 0) {
183
- throw new Error('No discovery data found');
184
- }
185
171
  } catch (err) {
186
- node.warn(`Failed discover all objects: ${err.message}`);
187
- reject(err.message);
172
+ node.warn(
173
+ `Discover object failed for service (${serviceURI}) - ${err.message}`
174
+ );
188
175
  }
189
- resolve(objectList);
190
- });
176
+ }
177
+
178
+ if (objectList.length === 0) {
179
+ throw new Error('No discovery data found');
180
+ }
181
+ return objectList;
191
182
  };
192
183
  }
193
184
 
194
185
  async function discoverProfiles(serviceConn, hid, subscriptions) {
195
- return new Promise(async (resolve) => {
196
- let discoveredProfiles = new Map();
197
- let services = await discover(serviceConn, hid);
198
- for (const service of services.data) {
199
- if (service.status === 'online') {
200
- let sid = service.id.split('/')[1];
201
- if (isIncluded(`${hid}/${sid}/#`, subscriptions)) {
202
- let profiles = await discover(serviceConn, `${hid}/${sid}`);
203
- let pids = [];
204
- for (const pid of profiles.data) {
205
- if (isIncluded(`${hid}/${sid}/${pid}/#`, subscriptions)) {
206
- pids.push(pid);
207
- }
208
- }
209
- if (pids.length > 0) {
210
- discoveredProfiles.set(`${hid}/${sid}`, pids);
186
+ let discoveredProfiles = new Map();
187
+ let services = await discover(serviceConn, hid);
188
+ for (const service of services.data) {
189
+ if (service.status === 'online') {
190
+ let sid = service.id.split('/')[1];
191
+ if (isIncluded(`${hid}/${sid}/#`, subscriptions)) {
192
+ let profiles = await discover(serviceConn, `${hid}/${sid}`);
193
+ let pids = [];
194
+ for (const pid of profiles.data) {
195
+ if (isIncluded(`${hid}/${sid}/${pid}/#`, subscriptions)) {
196
+ pids.push(pid);
211
197
  }
212
198
  }
213
- } else {
214
- RED.log.warn(
215
- `Failed to discover objects for service ${
216
- service.id.split('/')[1]
217
- } as it is offline.`
218
- );
199
+ if (pids.length > 0) {
200
+ discoveredProfiles.set(`${hid}/${sid}`, pids);
201
+ }
219
202
  }
203
+ } else {
204
+ RED.log.warn(
205
+ `Failed to discover objects for service ${
206
+ service.id.split('/')[1]
207
+ } as it is offline.`
208
+ );
220
209
  }
221
- resolve(discoveredProfiles);
222
- });
210
+ }
211
+ return discoveredProfiles;
223
212
  }
224
213
 
225
214
  async function discover(serviceConn, params) {
226
215
  return new Promise((resolve, reject) => {
227
216
  serviceConn.discover(params, (err, response) => {
228
217
  if (err) {
229
- reject(err);
218
+ return reject(err);
230
219
  } else {
231
- resolve(response);
220
+ return resolve(response);
232
221
  }
233
222
  });
234
223
  });
@@ -244,9 +233,9 @@ module.exports = function (RED) {
244
233
  eval('(' + params.func + ')'),
245
234
  (err, response) => {
246
235
  if (err) {
247
- reject(err);
236
+ return reject(err);
248
237
  } else {
249
- resolve(response);
238
+ return resolve(response);
250
239
  }
251
240
  }
252
241
  );
@@ -28,7 +28,7 @@
28
28
  'use strict';
29
29
 
30
30
  module.exports = function (RED) {
31
- function MobiusSendBCMDNode (n) {
31
+ function MobiusSendBCMDNode(n) {
32
32
  RED.nodes.createNode(this, n);
33
33
  this.cid = n.cid;
34
34
  this.serviceConn = RED.nodes.getNode(n.connection);
@@ -38,12 +38,31 @@ module.exports = function (RED) {
38
38
  node.status({
39
39
  fill: 'red',
40
40
  shape: 'ring',
41
- text: 'Disconnected'
41
+ text: 'Disconnected',
42
42
  });
43
43
 
44
44
  node.on('input', function (msg) {
45
45
  if (msg.hasOwnProperty('payload')) {
46
- node.serviceConn.sendBroadcastCommand(node.cid, msg.payload);
46
+ node.serviceConn.sendBroadcastCommand(
47
+ node.cid,
48
+ msg.payload,
49
+ (err) => {
50
+ if (err) {
51
+ node.status({
52
+ fill: 'yellow',
53
+ shape: 'dot',
54
+ text: `Error sending BCMD`,
55
+ });
56
+ node.error(err.message);
57
+ } else {
58
+ node.status({
59
+ fill: 'green',
60
+ shape: 'dot',
61
+ text: 'Sent BCMD',
62
+ });
63
+ }
64
+ }
65
+ );
47
66
  }
48
67
  });
49
68
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-mobius-flow-base",
3
- "version": "3.1.6",
3
+ "version": "3.2.0",
4
4
  "description": "Node-RED base nodes for Mobius Flow",
5
5
  "main": "index.js",
6
6
  "types": "dist/index.d.ts",