node-red-contrib-teamogy-api 0.0.5 → 0.0.7

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.
package/teamogy-client.js CHANGED
@@ -93,10 +93,10 @@ module.exports = function(RED) {
93
93
  }
94
94
 
95
95
  } catch (e) {
96
- node.error(e.message);
96
+ this.error(e.message);
97
97
  }
98
98
  }
99
-
99
+
100
100
  RED.nodes.registerType('teamogy-config', ConnectionNode, {
101
101
  credentials: {
102
102
  token: {type: 'password'}
@@ -104,9 +104,8 @@ module.exports = function(RED) {
104
104
  });
105
105
 
106
106
  function teamogyClient(data) {
107
+ var node = this;
107
108
  try {
108
- var node = this;
109
-
110
109
  node.closingId = '';
111
110
 
112
111
  RED.nodes.createNode(node,data);
@@ -133,16 +132,19 @@ module.exports = function(RED) {
133
132
  let mlimit = 0
134
133
  let mpaging = 1000
135
134
  let moffset = 0
136
- let entity = ''
135
+ let entity = ''
137
136
  let method = 'GET'
138
137
  let delay = 0;
139
138
  let repeat = 5;
140
139
  let rdelay = 30;
140
+ let mtoken = token
141
+ let mhost = host
142
+ let munit = unit
141
143
 
142
144
  if(data.source=='dynamic') {
143
145
  if(typeof msg.params == 'string') { mparams = msg.params }
144
146
  if(typeof msg.params == 'object') {
145
-
147
+
146
148
  let pa=Object.entries(msg.params)
147
149
  if(pa.length > 0) {
148
150
  mparams = pa[0][0] + '=' + pa[0][1]
@@ -156,37 +158,37 @@ module.exports = function(RED) {
156
158
  if(typeof msg[data.bodysource] == 'string') { body = msg[data.bodysource] }
157
159
  if(typeof msg[data.bodysource] == 'object') { body = JSON.stringify(msg[data.bodysource]) }
158
160
 
159
- if(typeof msg.merge == 'boolean') { mmerge = msg.merge }
160
- if(typeof msg.limit == 'number') { mlimit = msg.limit }
161
- if(typeof msg.paging == 'number') { mpaging = msg.paging }
162
- if(typeof msg.offset == 'number') { moffset = msg.offset }
163
- if(typeof msg.unit == 'number') { unit = msg.unit }
161
+ if(typeof msg.merge == 'boolean') { mmerge = msg.merge }
162
+ if(typeof msg.limit == 'number') { mlimit = msg.limit }
163
+ if(typeof msg.paging == 'number') { mpaging = msg.paging }
164
+ if(typeof msg.offset == 'number') { moffset = msg.offset }
165
+ if(typeof msg.unit == 'number') { munit = msg.unit }
164
166
  if(typeof msg.entity == 'string') { entity = msg.entity } else { entity = data.entity }
165
167
  if(typeof msg.method == 'string') { method = msg.method } else { method = data.method }
166
168
  if(typeof msg.delay == 'number') { delay = msg.delay * 1000 } else { delay = data.delay * 1000 }
167
169
  if(typeof msg.repeat == 'number') { repeat = msg.repeat ?? 5 } else { repeat = data.repeat ?? 5 }
168
170
  if(typeof msg.rdelay == 'number') { rdelay = msg.rdelay * 1000 } else { rdelay = data.rdelay * 1000 }
169
-
171
+
170
172
  if(typeof msg.connection == 'string') {
171
173
  if(msg.connection) {
172
174
  const customConfig = RED.nodes.getNode(getConfigId(msg.connection));
173
175
  if(customConfig) {
174
- token = customConfig.credentials.token;
175
- host = customConfig.host.replace(/^https?:\/\//, '').split('/')[0];
176
- unit = customConfig.unit;
176
+ mtoken = customConfig.credentials.token;
177
+ mhost = customConfig.host.replace(/^https?:\/\//, '').split('/')[0];
178
+ munit = customConfig.unit;
177
179
  } else {
178
180
  node.warn("Specified connection not found: " + msg.connection + ", using default: " + connName);
179
181
  }
180
182
  }
181
183
  }
182
-
184
+
183
185
  }
184
-
186
+
185
187
  if(data.source=='static') {
186
188
  const pa = data.cparams.split(/\r?\n/)
187
189
  if(pa.length > 0) {
188
190
  mparams = pa[0]
189
-
191
+
190
192
  for (let i = 1; i < pa.length; i++) {
191
193
  if(pa[i].length > 0) { mparams = mparams + '&' + pa[i] }
192
194
  }
@@ -204,16 +206,16 @@ module.exports = function(RED) {
204
206
  }
205
207
 
206
208
  const headers = {
207
- 'Authorization': 'Bearer ' + token,
209
+ 'Authorization': 'Bearer ' + mtoken,
208
210
  'Accept': 'application/json',
209
211
  'Content-type': 'application/json'
210
212
  };
211
213
 
212
- let url = `https://${host}/rest/v1/${unit}/`
214
+ let url = `https://${mhost}/rest/v1/${munit}/`
213
215
 
214
216
  if(entity.split('_')[0] == 'v') { url = url + 'views/'}
215
217
 
216
- url = url + entity.split('_')[1].replaceAll('-','.')
218
+ url = url + entity.substring(entity.indexOf('_') + 1).replaceAll('-','.')
217
219
 
218
220
  if(!isEmpty(mparams)) { url = url + '?' + mparams }
219
221
 
@@ -239,7 +241,7 @@ module.exports = function(RED) {
239
241
  if(parseInt(mlimit) == 0) { mlimit = 1000000000}
240
242
  if(parseInt(mlimit) < parseInt(mpaging)) { mpaging = mlimit }
241
243
 
242
- eurl = encodeURI(url + 'limit=' + mpaging +'&offset=' + offset)
244
+ let eurl = encodeURI(url + 'limit=' + mpaging +'&offset=' + offset)
243
245
 
244
246
  const response = await fetchWithRetry(eurl, { headers, method, body }, repeat, rdelay, node, apiLimit);
245
247
 
@@ -274,7 +276,7 @@ module.exports = function(RED) {
274
276
  msg.error = 'Request failed after all attempts.'
275
277
  msg.payload = null;
276
278
  if(msg.res){
277
- msg.payload = JSON.stringify('Request failed after all attempts.');
279
+ msg.payload = 'Request failed after all attempts.';
278
280
  msg.statusCode = 500;
279
281
  }
280
282
  node.send(msg);
@@ -288,7 +290,7 @@ module.exports = function(RED) {
288
290
 
289
291
  if(mmerge == true) {
290
292
  let body = {}
291
- metadata.limit = parseInt(data.paging)
293
+ metadata.limit = parseInt(mpaging)
292
294
  body.metadata = metadata
293
295
  body.data = rdata
294
296
  msg.payload = body
@@ -310,7 +312,7 @@ module.exports = function(RED) {
310
312
  msg.error = 'Request failed after all attempts.'
311
313
  msg.payload = null;
312
314
  if(msg.res){
313
- msg.payload = JSON.stringify('Request failed after all attempts.');
315
+ msg.payload = 'Request failed after all attempts.';
314
316
  msg.statusCode = 500;
315
317
  }
316
318
  node.send(msg);
@@ -382,7 +384,16 @@ module.exports = function(RED) {
382
384
 
383
385
  node.on('input', async function(msg, send, done) {
384
386
  try {
385
- if(st == null){ st = setInterval(function () { setTimer(host) }, 1000)}
387
+ if(st == null){
388
+ const timerInterval = Math.floor(60000 / apiLimit);
389
+ let timerBusy = false;
390
+ st = setInterval(function () {
391
+ if (!timerBusy) {
392
+ timerBusy = true;
393
+ setTimer(host).finally(() => { timerBusy = false; });
394
+ }
395
+ }, timerInterval);
396
+ }
386
397
 
387
398
  if(msg.skip != true) {
388
399
  let stime = 0