@bitpoolos/edge-bacnet 1.2.2 → 1.2.4

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/bacnet_client.js CHANGED
@@ -80,7 +80,6 @@ class BacnetClient extends EventEmitter {
80
80
  const queryDevices = new Task('simple task', () => {
81
81
  if(!that.pollInProgress) that.queryDevices();
82
82
  that.sanitizeDeviceList();
83
- //that.queryDevicesManually();
84
83
  });
85
84
 
86
85
  const queryJob = new SimpleIntervalJob({ seconds: parseInt(that.device_read_schedule), }, queryDevices)
@@ -199,90 +198,137 @@ class BacnetClient extends EventEmitter {
199
198
  query(index);
200
199
 
201
200
  function query(index) {
202
- let device = that.deviceList[index];
201
+ that.queryPriorityDevices().then(function() {
203
202
 
204
- if(index < that.deviceList.length) {
205
- index++;
206
-
207
- if(typeof device == "object") {
208
- if(!device.getManualDiscoveryMode()) {
209
- try {
210
- that.getDevicePointList(device).then(function() {
211
- that.removeDeviceFromManualQueue(device);
212
- that.buildJsonObject(device).then(function() {
213
- query(index);
203
+ let device = that.deviceList[index];
204
+
205
+ if(index < that.deviceList.length) {
206
+ index++;
207
+
208
+ if(typeof device == "object") {
209
+ if(!device.getManualDiscoveryMode()) {
210
+ try {
211
+ that.getDevicePointList(device).then(function() {
212
+ that.removeDeviceFromManualQueue(device);
213
+ that.buildJsonObject(device, null).then(function() {
214
+ query(index);
215
+ }).catch(function(e) {
216
+ that.logOut(`getDevicePointList error: ${device.getAddress()}`, e);
217
+ query(index);
218
+ });
214
219
  }).catch(function(e) {
215
220
  that.logOut(`getDevicePointList error: ${device.getAddress()}`, e);
221
+ that.addDeviceToManualQueue(device);
216
222
  query(index);
217
223
  });
218
- }).catch(function(e) {
219
- that.logOut(`getDevicePointList error: ${device.getAddress()}`, e);
220
- that.addDeviceToManualQueue(device);
224
+ } catch(e) {
225
+ that.logOut("Error while querying devices: ", e);
221
226
  query(index);
222
- });
223
- } catch(e) {
224
- that.logOut("Error while querying devices: ", e);
227
+ }
228
+ } else {
225
229
  query(index);
226
230
  }
227
231
  } else {
232
+ that.logOut("queryDevices: invalid device found: ", device);
228
233
  query(index);
229
234
  }
230
- } else {
231
- that.logOut("queryDevices: invalid device found: ", device);
232
- query(index);
233
- }
234
- } else if(index == that.deviceList.length) {
235
+ } else if(index == that.deviceList.length) {
235
236
 
236
- if(that.manualDiscoverQueue.length > 0) {
237
- that.queryDevicesManually();
238
- } else {
239
- that.pollInProgress = false;
237
+ if(that.manualDiscoverQueue.length > 0) {
238
+ that.queryDevicesManually();
239
+ } else {
240
+ that.pollInProgress = false;
241
+ }
242
+
240
243
  }
241
-
242
- }
244
+ });
243
245
  }
244
246
  }
245
247
 
246
248
  queryDevicesManually() {
247
249
  let that = this;
248
-
249
250
  let index = 0;
250
-
251
251
  query(index);
252
252
 
253
253
  function query(index) {
254
- let device = that.manualDiscoverQueue[index];
254
+ that.queryPriorityDevices().then(function() {
255
+ let device = that.manualDiscoverQueue[index];
256
+ if(index < that.manualDiscoverQueue.length) {
257
+ index++;
258
+ if(typeof device == "object") {
259
+ try {
260
+ if(device.shouldBeInManualMode()) {
261
+ that.getDevicePointListWithoutObjectList(device).then(function() {
262
+ that.buildJsonObject(device, null).then(function() {
263
+ query(index);
264
+ }).catch(function(e) {
265
+ that.logOut(`getDevicePointList error: ${device.getAddress()}`, e);
266
+ query(index);
267
+ });
268
+ }).catch(function(e){
269
+ query(index);
270
+ });
271
+ } else {
272
+ that.removeDeviceFromManualQueue(device);
273
+ query(index);
274
+ }
275
+ } catch(e) {
276
+ query(index);
277
+ }
278
+ } else {
279
+ query(index);
280
+ }
281
+ } else if(index == that.manualDiscoverQueue.length) {
282
+ that.pollInProgress = false;
283
+ }
284
+ });
285
+ }
286
+ }
255
287
 
256
- if(index < that.manualDiscoverQueue.length) {
257
- index++;
258
-
259
- if(typeof device == "object") {
260
- try {
261
- if(device.shouldBeInManualMode()) {
262
- that.getDevicePointListWithoutObjectList(device).then(function() {
263
- that.buildJsonObject(device).then(function() {
288
+ queryPriorityDevices() {
289
+ let that = this;
290
+ return new Promise((resolve, reject) => {
291
+ let priorityDevices = that.getPriorityDevices();
292
+
293
+ if(priorityDevices.length > 0) {
294
+ let index = 0;
295
+
296
+ query(index);
297
+
298
+ function query(index) {
299
+ let device = priorityDevices[index];
300
+
301
+ if(index < priorityDevices.length) {
302
+ index++;
303
+
304
+ if(typeof device == "object" && ((Date.now() - device.getLastPriorityQueueTS()) / 1000) > parseInt(that.device_read_schedule) ) {
305
+
306
+ try {
307
+ let points = device.getPriorityQueue();
308
+ that.buildJsonObject(device, points).then(function() {
309
+ device.setLastPriorityQueueTS();
264
310
  query(index);
265
311
  }).catch(function(e) {
266
- that.logOut(`getDevicePointList error: ${device.getAddress()}`, e);
312
+ that.logOut(`queryPriorityDevices error: ${device.getAddress()}`, e);
267
313
  query(index);
268
314
  });
269
- }).catch(function(e){
315
+
316
+ } catch(e) {
317
+ that.logOut("Error while querying priority devices: ", e);
270
318
  query(index);
271
- });
319
+ }
320
+
272
321
  } else {
273
- that.removeDeviceFromManualQueue(device);
274
322
  query(index);
275
323
  }
276
- } catch(e) {
277
- query(index);
324
+ } else if(index == priorityDevices.length) {
325
+ resolve()
278
326
  }
279
- } else {
280
- query(index);
281
327
  }
282
- } else if(index == that.manualDiscoverQueue.length) {
283
- that.pollInProgress = false;
328
+ } else if(priorityDevices.length == 0) {
329
+ resolve()
284
330
  }
285
- }
331
+ });
286
332
  }
287
333
 
288
334
  addDeviceToManualQueue(device) {
@@ -312,12 +358,12 @@ class BacnetClient extends EventEmitter {
312
358
  sanitizeDeviceList() {
313
359
  let that = this;
314
360
 
315
- //1 hour in seconds
316
- let timeoutThreshold = parseInt(that.discover_polling_schedule);
361
+ //Discover frequencey x 2
362
+ let timeoutThreshold = parseInt(that.discover_polling_schedule) * 2;
317
363
 
318
364
  that.deviceList.forEach(function(device, index) {
319
- if(((Date.now() - device.lastSeen) / 1000) > timeoutThreshold) {
320
- //render device hasnt responded to whoIs for over an hour
365
+ if(((Date.now() - device.getLastSeen()) / 1000) > timeoutThreshold && device.getPriorityQueueIsActive() == false) {
366
+ //render device hasnt responded to whoIs for disover frequency x 2
321
367
 
322
368
  let renderListIndex = that.renderList.findIndex(ele => ele.deviceId == device.getDeviceId());
323
369
 
@@ -326,9 +372,7 @@ class BacnetClient extends EventEmitter {
326
372
 
327
373
  delete that.networkTree[deviceKey];
328
374
 
329
- if(((Date.now() - device.getLastSeen()) / 1000) > timeoutThreshold) {
330
- that.renderList.splice(renderListIndex, 1);
331
- }
375
+ that.renderList.splice(renderListIndex, 1);
332
376
 
333
377
  that.deviceList.splice(index, 1);
334
378
  }
@@ -943,6 +987,42 @@ class BacnetClient extends EventEmitter {
943
987
  });
944
988
  }
945
989
 
990
+ updatePriorityQueue(priorityDevices) {
991
+ let that = this;
992
+ return new Promise(async function(resolve, reject) {
993
+ try {
994
+ let keys = Object.keys(priorityDevices);
995
+ if(keys.length > 0) {
996
+ keys.forEach(function(key) {
997
+ let device = that.deviceList.find(ele => `${that.getDeviceAddress(ele)}-${ele.getDeviceId()}` == key);
998
+ let points = priorityDevices[key];
999
+ if(device) {
1000
+ device.setPriorityQueue(points);
1001
+ }
1002
+ });
1003
+ } else if(keys.length == 0) {
1004
+ that.clearPriorityQueues();
1005
+ }
1006
+ resolve(true);
1007
+ } catch(e){
1008
+ reject(e);
1009
+ }
1010
+ });
1011
+ }
1012
+
1013
+ clearPriorityQueues() {
1014
+ let that = this;
1015
+ that.deviceList.forEach(function(device) {
1016
+ device.clearPriorityQueue();
1017
+ });
1018
+ }
1019
+
1020
+ getPriorityDevices() {
1021
+ let that = this;
1022
+ let priorityDevices = that.deviceList.filter(device => device.getPriorityQueueIsActive() == true);
1023
+ return priorityDevices;
1024
+ }
1025
+
946
1026
  sortDevices(a, b) {
947
1027
  if (a.deviceId < b.deviceId) {
948
1028
  return -1;
@@ -1065,21 +1145,10 @@ class BacnetClient extends EventEmitter {
1065
1145
  });
1066
1146
  }
1067
1147
 
1068
- buildObjects() {
1069
- let that = this;
1070
-
1071
- that.deviceList.forEach(function(device) {
1072
- that.buildJsonObject(device).then(function() {
1073
- }).catch(function(e) {
1074
- that.logOut(device.getAddress(), e)
1075
- });
1076
- });
1077
- }
1078
-
1079
- buildJsonObject(device) {
1148
+ buildJsonObject(device, priorityQueue) {
1080
1149
  let that = this;
1081
1150
  let address = device.address;
1082
- let pointList = device.getPointsList();
1151
+ let pointList = priorityQueue !== null ? priorityQueue : device.getPointsList();
1083
1152
  let requestMutex = new Mutex();
1084
1153
 
1085
1154
  return new Promise(function(resolve, reject) {
@@ -1241,7 +1310,8 @@ class BacnetClient extends EventEmitter {
1241
1310
  }
1242
1311
  }
1243
1312
  }
1244
- reject("Unexpectedly found end of loop, line 1214");
1313
+ that.networkTree[deviceKey] = values;
1314
+ resolve(that.networkTree);
1245
1315
  });
1246
1316
  }
1247
1317
 
package/bacnet_device.js CHANGED
@@ -16,6 +16,9 @@ class BacnetDevice {
16
16
  that.manualDiscoveryMode = config.manualDiscoveryMode;
17
17
  that.mDiscoverInstanceRange = config.mDiscoverInstanceRange;
18
18
  that.pointListRetryCount = config.pointListRetryCount;
19
+ that.priorityQueueIsActive = config.priorityQueueIsActive;
20
+ that.priorityQueue = config.priorityQueue;
21
+ that.lastPriorityQueueTS = config.lastPriorityQueueTS;
19
22
 
20
23
  } else if(fromImport == false) {
21
24
  if(config.net && config.adr) {
@@ -36,9 +39,67 @@ class BacnetDevice {
36
39
  that.manualDiscoveryMode = false;
37
40
  that.mDiscoverInstanceRange = {start: 0, end: 100};
38
41
  that.pointListRetryCount = 0;
42
+ that.priorityQueueIsActive = false;
43
+ that.priorityQueue = [];
44
+ that.lastPriorityQueueTS = null;
39
45
  }
40
46
  }
41
47
 
48
+ setLastPriorityQueueTS() {
49
+ this.lastPriorityQueueTS = Date.now();
50
+ }
51
+
52
+ getLastPriorityQueueTS() {
53
+ return this.lastPriorityQueueTS;
54
+ }
55
+
56
+ setPriorityQueueIsActive(bool) {
57
+ this.priorityQueueIsActive = bool;
58
+ }
59
+
60
+ getPriorityQueueIsActive() {
61
+ return this.priorityQueueIsActive;
62
+ }
63
+
64
+ updatePriorityQueue(point) {
65
+ let foundIndex = this.priorityQueue.findIndex(ele => ele.value.type == point.value.type && ele.value.instance == point.value.instance);
66
+ if(foundIndex == -1 ) {
67
+ //not found
68
+ this.priorityQueue.push(point);
69
+ }
70
+
71
+ if(this.priorityQueue.length > 0) {
72
+ this.setPriorityQueueIsActive(true);
73
+ } else if(this.priorityQueue.length == 0) {
74
+ this.setPriorityQueueIsActive(false);
75
+ }
76
+ }
77
+
78
+ setPriorityQueue(points) {
79
+ let queue = [];
80
+ let keys = Object.keys(points);
81
+ if(keys.length > 0) {
82
+ keys.forEach(function(key) {
83
+ let point = points[key];
84
+ let pointRequestObject = {type: 12, value: point.meta.objectId}
85
+ queue.push(pointRequestObject);
86
+ });
87
+ this.priorityQueue = queue;
88
+ this.setPriorityQueueIsActive(true);
89
+ } else if(keys.length == 0) {
90
+ this.setPriorityQueueIsActive(false);
91
+ }
92
+ }
93
+
94
+ getPriorityQueue(){
95
+ return this.priorityQueue;
96
+ }
97
+
98
+ clearPriorityQueue() {
99
+ this.priorityQueue = [];
100
+ this.setPriorityQueueIsActive(false);
101
+ }
102
+
42
103
  updateDeviceConfig(config) {
43
104
  if(config.address !== "" && config.address !== null && config.address !== "undefined") {
44
105
  if(config.net && config.adr) {
package/bacnet_gateway.js CHANGED
@@ -177,6 +177,11 @@ module.exports = function (RED) {
177
177
  } else if (msg.payload == "BindEvents") {
178
178
  node.bacnetClient.removeAllListeners();
179
179
  bindEventListeners();
180
+ } else if (msg.doUpdatePriorityDevices == true && msg.priorityDevices !== null) {
181
+ node.bacnetClient.updatePriorityQueue(msg.priorityDevices).then(function (result) {
182
+ }).catch(function (error) {
183
+ logOut("Error updating priorityQueue: ", error);
184
+ });
180
185
  }
181
186
 
182
187
  });
package/bacnet_read.html CHANGED
@@ -159,7 +159,7 @@
159
159
  this.$forceUpdate()
160
160
 
161
161
  //update node-red data structure to forward to gateway
162
- let device = this.deviceList.find(ele => ele.deviceName == slotProps.node.label);
162
+ let device = this.deviceList.find(ele => ele.address == slotProps.node.ipAddr && ele.deviceId == slotProps.node.deviceId);
163
163
  let deviceAddress = app.getDeviceAddress(device.address);
164
164
  let key = `${deviceAddress}-${device.deviceId}`;
165
165
  let points = this.pointList[key];
@@ -208,7 +208,7 @@
208
208
  this.$forceUpdate();
209
209
 
210
210
  //update node-red data structure
211
- let device = this.deviceList.find(ele => ele.deviceName == slotProps.node.label);
211
+ let device = this.deviceList.find(ele => ele.address == slotProps.node.ipAddr && ele.deviceId == slotProps.node.deviceId);
212
212
  let deviceAddress = app.getDeviceAddress(device.address);
213
213
  let key = `${deviceAddress}-${device.deviceId}`;
214
214
  if (this.pointsToRead[key]) {
@@ -254,7 +254,7 @@
254
254
  this.$forceUpdate();
255
255
 
256
256
  //update node-red data structure
257
- let device = this.deviceList.find(ele => ele.deviceName == parentDeviceName);
257
+ let device = this.deviceList.find(ele => ele.address == parentDevice.ipAddr && ele.deviceId == parentDevice.deviceId);
258
258
  let deviceAddress = app.getDeviceAddress(device.address);
259
259
  let key = `${deviceAddress}-${device.deviceId}`;
260
260
 
@@ -292,7 +292,7 @@
292
292
  this.$forceUpdate();
293
293
 
294
294
  //update node-red data stucture
295
- let device = this.deviceList.find(ele => ele.deviceName == parentDeviceName);
295
+ let device = this.deviceList.find(ele => ele.address == parentDevice.ipAddr && ele.deviceId == parentDevice.deviceId);
296
296
  let deviceAddress = app.getDeviceAddress(device.address);
297
297
  let key = `${deviceAddress}-${device.deviceId}`;
298
298
  let point = this.pointList[key][slotProps.node.pointName];
@@ -436,21 +436,12 @@
436
436
  if (node.vm.$data.devices) node.devices = node.vm.$data.devices;
437
437
  if (node.vm.$data.readDevices) node.readDevices = node.vm.$data.readDevices;
438
438
  if (node.vm.$data.pointsToRead) node.pointsToRead = node.vm.$data.pointsToRead;
439
-
440
- // if (node.vm.$data.devices && node.vm.$data.devices.length > 0) node.devices = node.vm.$data.devices;
441
- // if (node.vm.$data.readDevices && node.vm.$data.readDevices.length > 0) node.readDevices = node.vm.$data.readDevices;
442
- // if (node.vm.$data.pointsToRead && node.vm.$data.pointsToRead.length > 0) node.pointsToRead = node.vm.$data.pointsToRead;
443
-
444
439
  },
445
440
  oneditcancel: function () {
446
441
  let node = this;
447
442
  if (node.vm.$data.devices) node.devices = node.vm.$data.devices;
448
443
  if (node.vm.$data.readDevices) node.readDevices = node.vm.$data.readDevices;
449
444
  if (node.vm.$data.pointsToRead) node.pointsToRead = node.vm.$data.pointsToRead;
450
-
451
- // if (node.vm.$data.devices && node.vm.$data.devices.length > 0) node.devices = node.vm.$data.devices;
452
- // if (node.vm.$data.readDevices && node.vm.$data.readDevices.length > 0) node.readDevices = node.vm.$data.readDevices;
453
- // if (node.vm.$data.pointsToRead && node.vm.$data.pointsToRead.length > 0) node.pointsToRead = node.vm.$data.pointsToRead;
454
445
  }
455
446
  });
456
447
 
package/bacnet_read.js CHANGED
@@ -5,6 +5,8 @@
5
5
 
6
6
 
7
7
  module.exports = function (RED) {
8
+ const fetch = require('node-fetch');
9
+ const http = require("http");
8
10
  const { ReadCommandConfig } = require('./common');
9
11
  const baEnum = require('./resources/node-bacstack-ts/dist/index.js').enum;
10
12
 
@@ -49,7 +51,21 @@ module.exports = function (RED) {
49
51
  return propArr;
50
52
  };
51
53
 
52
- var nodeContext = this.context().flow;
54
+ //send point list status for device priority queue
55
+ let headers = {
56
+ 'Content-Type' : "application/json"
57
+ };
58
+
59
+ const agent = new http.Agent({
60
+ rejectUnauthorized: false
61
+ });
62
+
63
+ let priorityDevicesMsg = {
64
+ doUpdatePriorityDevices: true,
65
+ priorityDevices: node.pointsToRead
66
+ };
67
+
68
+ node.send(priorityDevicesMsg);
53
69
 
54
70
  node.on('input', function(msg) {
55
71
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bitpoolos/edge-bacnet",
3
- "version": "1.2.2",
3
+ "version": "1.2.4",
4
4
  "description": "A bacnet gateway for node-red",
5
5
  "dependencies": {
6
6
  "@plus4nodered/ts-node-bacnet": "^1.0.0-beta.2",
@@ -8,6 +8,7 @@
8
8
  "cronosjs": "^1.7.1",
9
9
  "debug": "^4.1.1",
10
10
  "iconv-lite": "^0.5.1",
11
+ "node-fetch": "^2.6.1",
11
12
  "toad-scheduler": "^1.6.0",
12
13
  "underscore": "^1.10.2",
13
14
  "winston": "^3.2.1"