@bitpoolos/edge-bacnet 1.2.8 → 1.3.1

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/treeBuilder.js ADDED
@@ -0,0 +1,545 @@
1
+ const { Store_Config } = require("./common");
2
+
3
+ /**
4
+ * The `treeBuilder` class is responsible for building and processing the network tree structure.
5
+ * It takes in a list of devices, a network tree object, a render list, a render list count, and an initial tree build flag.
6
+ * The class provides methods for caching data, processing a device and its points, adding the root device folder to the render list,
7
+ * processing points for a device, processing an individual point, extracting point properties, formatting display name for a point,
8
+ * updating the render list, creating the folder JSON structure for a device, finalizing the network tree data, checking the interrupt flag,
9
+ * getting the device IP address, computing the device name, sorting points, sorting devices, getting the point icon, and getting the device icon.
10
+ *
11
+ * @class
12
+ * @name treeBuilder
13
+ * @param {Array} deviceList - The list of devices.
14
+ * @param {Object} networkTree - The network tree object.
15
+ * @param {Array} renderList - The render list.
16
+ * @param {number} renderListCount - The render list count.
17
+ * @param {boolean} initialTreeBuild - The initial tree build flag.
18
+ */
19
+ class treeBuilder {
20
+ constructor(deviceList, networkTree, renderList, renderListCount, initialTreeBuild) {
21
+ this.deviceList = deviceList;
22
+ this.networkTree = networkTree;
23
+ this.renderList = renderList;
24
+ this.renderListCount = renderListCount;
25
+ this.initialTreeBuild = initialTreeBuild;
26
+ }
27
+
28
+ /**
29
+ * Cache the current state of the network tree and other data.
30
+ *
31
+ * @returns {void}
32
+ */
33
+ cacheData() {
34
+ // Cache the current state of the network tree and other data
35
+ Store_Config(JSON.stringify({
36
+ renderList: this.renderList,
37
+ deviceList: this.deviceList,
38
+ pointList: this.networkTree,
39
+ renderListCount: this.renderListCount,
40
+ }));
41
+ }
42
+
43
+ /**
44
+ * Process a device and its points.
45
+ *
46
+ * @param {Device} device - The device to process.
47
+ * @param {number} index - The index of the device.
48
+ * @returns {Promise<void>} - A promise that resolves when the device and its points have been processed.
49
+ */
50
+ async processDevice(device, index) {
51
+ const ipAddress = this.getDeviceIpAddress(device);
52
+ const deviceId = device.getDeviceId();
53
+ const deviceName = this.computeDeviceName(device);
54
+ const deviceKey = `${ipAddress}-${deviceId}`;
55
+ const deviceObject = this.networkTree[deviceKey];
56
+
57
+ // Add the root device folder to the render list
58
+ if (this.initialTreeBuild) {
59
+ this.addRootDeviceFolder(device, deviceName, index, ipAddress, deviceId);
60
+ }
61
+
62
+ // Check if the device object exists and the device name is valid
63
+ //if (deviceObject && typeof deviceName !== 'object') {
64
+ if (deviceObject) {
65
+
66
+
67
+ //console.log("processDevice found deviceObject ");
68
+
69
+ //await this.processDevicePoints(device, deviceObject, "testingDeviceName", ipAddress, deviceId, index);
70
+
71
+ await this.processDevicePoints(device, deviceObject, deviceName, ipAddress, deviceId, index);
72
+
73
+ } else {
74
+ //console.log("Unable to find device object");
75
+ }
76
+ }
77
+
78
+ /**
79
+ * Add the root device folder to the render list.
80
+ *
81
+ * @param {Object} device - The device object.
82
+ * @param {string} deviceName - The name of the device.
83
+ * @param {number} index - The index of the device.
84
+ * @param {string} ipAddress - The IP address of the device.
85
+ * @param {string} deviceId - The ID of the device.
86
+ * @returns {void}
87
+ */
88
+ addRootDeviceFolder(device, deviceName, index, ipAddress, deviceId) {
89
+ if (!this.renderList) {
90
+ this.renderList = [];
91
+ }
92
+
93
+ if (!device.getIsMstpDevice()) {
94
+ let displayName = deviceName ? deviceName : `${ipAddress} - ${deviceId}`;
95
+
96
+ // Check if the device already exists in the renderList
97
+ const existingDeviceIndex = this.renderList.findIndex(item => item.deviceId === deviceId && item.ipAddr === ipAddress);
98
+
99
+ if (existingDeviceIndex === -1) { // Device not found, add new entry
100
+ const rootFolder = {
101
+ key: index,
102
+ label: displayName,
103
+ data: displayName,
104
+ icon: this.getDeviceIcon(device),
105
+ children: [{ key: `${deviceId}-0`, label: 'Points', data: 'Points Folder', icon: 'pi pi-circle-fill', type: 'pointFolder', children: [] }],
106
+ type: 'device',
107
+ lastSeen: device.getLastSeen(),
108
+ showAdded: false,
109
+ ipAddr: ipAddress,
110
+ deviceId,
111
+ isMstpDevice: device.getIsMstpDevice(),
112
+ initialName: device.getDeviceName(),
113
+ };
114
+
115
+ // Add the root folder to the render list
116
+ this.renderList.push(rootFolder);
117
+ }
118
+ }
119
+ }
120
+
121
+ /**
122
+ * Process the points of a device and add them to the render list.
123
+ *
124
+ * @param {Device} device - The device object.
125
+ * @param {Object} deviceObject - The object containing the points of the device.
126
+ * @param {string} deviceName - The name of the device.
127
+ * @param {string} ipAddress - The IP address of the device.
128
+ * @param {string} deviceId - The ID of the device.
129
+ * @param {number} index - The index of the device.
130
+ * @returns {Promise<void>} - A promise that resolves when the points have been processed and added to the render list.
131
+ */
132
+ async processDevicePoints(device, deviceObject, deviceName, ipAddress, deviceId, index) {
133
+ // Initialize the list of children points
134
+ const children = [];
135
+
136
+ // Process each point in the device object
137
+ for (const pointName in deviceObject) {
138
+ // Ensure processing should continue
139
+ this.checkInterruptFlag();
140
+
141
+ // Process the point and add it to the list of children
142
+ const point = deviceObject[pointName];
143
+ const childPoint = await this.processPoint(point, pointName, index, deviceName, device);
144
+ children.push(childPoint);
145
+ }
146
+
147
+ // Add the device and its children to the render list
148
+ this.updateRenderList(children, device, deviceName, index, ipAddress, deviceId);
149
+ }
150
+
151
+ /**
152
+ * Process a point and create a child point object.
153
+ *
154
+ * @param {Object} point - The point to process.
155
+ * @param {string} pointName - The name of the point.
156
+ * @param {number} index - The index of the point.
157
+ * @param {string} deviceName - The name of the parent device.
158
+ * @param {Object} device - The parent device object.
159
+ * @returns {Object} - The child point object.
160
+ */
161
+ async processPoint(point, pointName, index, deviceName, device) {
162
+ // Get the properties and data of the point
163
+ const pointProperties = this.extractPointProperties(point);
164
+
165
+ // Determine the point's display name and apply formatting if necessary
166
+ const displayName = this.formatDisplayName(point, pointName);
167
+
168
+ // Create the child point object
169
+ return {
170
+ key: `${index}-0-${pointName}`,
171
+ label: displayName,
172
+ data: displayName,
173
+ pointName,
174
+ icon: this.getPointIcon(point),
175
+ children: pointProperties,
176
+ type: 'point',
177
+ parentDevice: deviceName,
178
+ parentDeviceId: device.getDeviceId(),
179
+ showAdded: false,
180
+ bacnetType: point.meta.objectId.type,
181
+ };
182
+ }
183
+
184
+ /**
185
+ * Extracts the properties of a point object.
186
+ *
187
+ * @param {Object} point - The point object to extract properties from.
188
+ * @returns {Array} - An array of point properties.
189
+ */
190
+ extractPointProperties(point) {
191
+ const pointProperties = [];
192
+
193
+ // Add properties such as name, type, instance, and others
194
+ this.addPointProperty(pointProperties, 'Name', point.objectName);
195
+ this.addPointProperty(pointProperties, 'Object Type', point.meta.objectId.type);
196
+ this.addPointProperty(pointProperties, 'Object Instance', point.meta.objectId.instance);
197
+ this.addPointProperty(pointProperties, 'Description', point.description);
198
+ this.addPointProperty(pointProperties, 'Units', point.units);
199
+ this.addPointProperty(pointProperties, 'Present Value', point.presentValue);
200
+ this.addPointProperty(pointProperties, 'System Status', point.systemStatus);
201
+ this.addPointProperty(pointProperties, 'Modification Date', point.modificationDate);
202
+ this.addPointProperty(pointProperties, 'Program State', point.programState);
203
+ this.addPointProperty(pointProperties, 'Record Count', point.recordCount);
204
+
205
+ // Return the array of point properties
206
+ return pointProperties;
207
+ }
208
+
209
+ /**
210
+ * Adds a property to the list of point properties.
211
+ *
212
+ * @param {Array} properties - The list of point properties.
213
+ * @param {string} label - The label of the property.
214
+ * @param {any} value - The value of the property.
215
+ * @returns {void}
216
+ */
217
+ addPointProperty(properties, label, value) {
218
+ if (value !== null && value !== undefined && value !== '') {
219
+ properties.push({
220
+ label: `${label}: ${value}`,
221
+ data: value,
222
+ icon: 'pi pi-cog',
223
+ children: null,
224
+ });
225
+ }
226
+ }
227
+
228
+ /**
229
+ * Formats the display name for a point.
230
+ *
231
+ * If the point has a display name, it returns the display name.
232
+ * Otherwise, it removes any special characters from the point name and returns it.
233
+ *
234
+ * @param {Object} point - The point object.
235
+ * @param {string} pointName - The name of the point.
236
+ * @returns {string} - The formatted display name.
237
+ */
238
+ formatDisplayName(point, pointName) {
239
+ const reg = /[$#\/\\+]/gi;
240
+ if (point.displayName) {
241
+ return point.displayName;
242
+ }
243
+ return pointName.replace(reg, '');
244
+ }
245
+
246
+ /**
247
+ * Updates the render list with the folder structure for a device.
248
+ *
249
+ * @param {Array} children - The children of the device.
250
+ * @param {Object} device - The device object.
251
+ * @param {string} deviceName - The name of the device.
252
+ * @param {number} index - The index of the device.
253
+ * @param {string} ipAddress - The IP address of the device.
254
+ * @param {string} deviceId - The ID of the device.
255
+ * @returns {void}
256
+ */
257
+ updateRenderList(children, device, deviceName, index, ipAddress, deviceId) {
258
+ // Create the folder structure for the device
259
+ const folderJson = this.createFolderJson(children, device.hasChildDevices(), deviceId);
260
+
261
+ if (!this.renderList) {
262
+ this.renderList = [];
263
+ }
264
+
265
+ // Find the device's entry in the render list
266
+ let foundIndex = this.renderList.findIndex(ele => ele.deviceId == deviceId && ele.ipAddr == ipAddress);
267
+
268
+ // If the device is not in the render list, add it as a new entry
269
+ const newDeviceEntry = {
270
+ key: index,
271
+ label: deviceName,
272
+ data: deviceName,
273
+ icon: this.getDeviceIcon(device),
274
+ children: folderJson,
275
+ type: 'device',
276
+ lastSeen: device.getLastSeen(),
277
+ showAdded: false,
278
+ ipAddr: ipAddress,
279
+ deviceId,
280
+ isMstpDevice: device.getIsMstpDevice(),
281
+ initialName: device.getDeviceName(),
282
+ };
283
+
284
+ if (device.getIsMstpDevice()) {
285
+ // For child MSTP devices, find the parent device and the MSTP network folder
286
+ const parentDeviceId = device.getParentDeviceId();
287
+ const parentDeviceIndex = this.renderList.findIndex(ele => ele.deviceId == parentDeviceId && ele.ipAddr == ipAddress);
288
+ const mstpNetworkNumber = device.getMstpNetworkNumber();
289
+
290
+ if (parentDeviceIndex !== -1) {
291
+ let parentDeviceEntry = this.renderList[parentDeviceIndex];
292
+ let mstpNetworkFolder = parentDeviceEntry.children.find(child => child.label === `MSTP NET${mstpNetworkNumber}`);
293
+
294
+ // Create the MSTP network folder if it doesn't exist
295
+ if (!mstpNetworkFolder) {
296
+ mstpNetworkFolder = {
297
+ key: `${deviceId}-mstp-${mstpNetworkNumber}`,
298
+ label: `MSTP NET${mstpNetworkNumber}`,
299
+ data: `Devices Folder (${mstpNetworkNumber})`,
300
+ icon: 'pi pi-database',
301
+ type: 'deviceFolder',
302
+ children: [],
303
+ };
304
+
305
+ parentDeviceEntry.children.push(mstpNetworkFolder);
306
+ }
307
+
308
+ // Add or update the child MSTP device in the MSTP folder
309
+ const mstpDeviceIndex = mstpNetworkFolder.children.findIndex(ele => ele.deviceId == deviceId && ele.ipAddr == ipAddress);
310
+ if (mstpDeviceIndex === -1) {
311
+ mstpNetworkFolder.children.push(newDeviceEntry);
312
+ } else {
313
+ mstpNetworkFolder.children[mstpDeviceIndex] = newDeviceEntry;
314
+ }
315
+ }
316
+ } else {
317
+ // Add the new device entry to the root of the render list
318
+ if (foundIndex === -1) {
319
+ this.renderList.push(newDeviceEntry);
320
+ } else {
321
+ // If the device is already in the render list, update its entry
322
+ this.renderList[foundIndex] = newDeviceEntry;
323
+ }
324
+ }
325
+ }
326
+
327
+ /**
328
+ * Creates a folder JSON object for the network tree.
329
+ *
330
+ * @param {Array} children - The children nodes of the folder.
331
+ * @param {boolean} hasChildDevices - Indicates if the device has child devices.
332
+ * @param {string} deviceId - The ID of the device.
333
+ * @returns {Array} - The folder JSON object.
334
+ */
335
+ createFolderJson(children, hasChildDevices, deviceId) {
336
+ const folders = [
337
+ {
338
+ key: `${deviceId}-0`,
339
+ label: 'Points',
340
+ data: 'Points Folder',
341
+ icon: 'pi pi-circle-fill',
342
+ type: 'pointFolder',
343
+ children: children.sort(this.sortPoints),
344
+ }
345
+ ];
346
+
347
+ return folders;
348
+ }
349
+
350
+ /**
351
+ * Finalize the network tree data
352
+ *
353
+ * @returns {Object} The finalized network tree data
354
+ * - renderList: The list of devices and their points
355
+ * - deviceList: The list of devices
356
+ * - pointList: The list of points in the network tree
357
+ * - pollFrequency: The polling schedule for discovery
358
+ */
359
+ finalizeNetworkTreeData() {
360
+ this.renderList.sort(this.sortDevices);
361
+
362
+ return {
363
+ renderList: this.renderList,
364
+ deviceList: this.deviceList,
365
+ pointList: this.networkTree,
366
+ pollFrequency: this.discover_polling_schedule,
367
+ };
368
+ }
369
+
370
+ /**
371
+ * Checks if the buildTreeException flag is set and throws an error if it is.
372
+ *
373
+ * @throws {Error} - Throws an error with the message 'Build tree interrupted' if the buildTreeException flag is set.
374
+ */
375
+ checkInterruptFlag() {
376
+ if (this.buildTreeException) {
377
+ throw new Error('Build tree interrupted');
378
+ }
379
+ }
380
+
381
+ /**
382
+ * Returns the IP address of the given device.
383
+ *
384
+ * @param {object} device - The device object.
385
+ * @returns {string} The IP address of the device.
386
+ */
387
+ getDeviceIpAddress(device) {
388
+ switch (typeof device.getAddress()) {
389
+ case "object":
390
+ return device.getAddress().address;
391
+ case "string":
392
+ return device.getAddress();
393
+ default:
394
+ return device.getAddress();
395
+ }
396
+ }
397
+
398
+ /**
399
+ * Computes the device name based on the provided device object.
400
+ * If the device has a display name, it will be returned.
401
+ * Otherwise, the device name will be returned.
402
+ *
403
+ * @param {Object} device - The device object.
404
+ * @returns {string} - The computed device name.
405
+ */
406
+ computeDeviceName(device) {
407
+ if (device.getDeviceName() == null && device.getDisplayName() == null) {
408
+ return `${this.getDeviceIpAddress(device)}-${device.getDeviceId()}`;
409
+ } else if (device.getDisplayName() !== null && device.getDisplayName() !== "" && device.getDisplayName() !== undefined) {
410
+ return device.getDisplayName();
411
+ }
412
+ return device.getDeviceName();
413
+ }
414
+
415
+ /**
416
+ * Sorts the points based on their BACnet type and label.
417
+ *
418
+ * @param {Object} a - The first point object to compare.
419
+ * @param {Object} b - The second point object to compare.
420
+ * @returns {number} - A negative number if a should be sorted before b, a positive number if b should be sorted before a, or 0 if they are equal.
421
+ */
422
+ sortPoints(a, b) {
423
+ if (a.bacnetType > b.bacnetType) {
424
+ return 1;
425
+ } else if (a.bacnetType < b.bacnetType) {
426
+ return -1;
427
+ } else if (a.bacnetType == b.bacnetType) {
428
+ return 0;
429
+ }
430
+
431
+ return a.label.localeCompare(b.label);
432
+ }
433
+
434
+ /**
435
+ * Sorts devices based on their deviceId.
436
+ *
437
+ * @param {Object} a - The first device object to compare.
438
+ * @param {Object} b - The second device object to compare.
439
+ * @returns {number} - Returns -1 if a.deviceId is less than b.deviceId, 1 if a.deviceId is greater than b.deviceId, or 0 if they are equal.
440
+ */
441
+ sortDevices(a, b) {
442
+ if (a.deviceId < b.deviceId) {
443
+ return -1;
444
+ } else if (a.deviceId > b.deviceId) {
445
+ return 1;
446
+ }
447
+ return 0; // deviceIds are equal
448
+ }
449
+
450
+ /**
451
+ * Returns the icon class name for a given point based on its object type.
452
+ *
453
+ * @param {Object} values - The values object containing the point's metadata.
454
+ * @returns {string} - The icon class name for the point.
455
+ */
456
+ getPointIcon(values) {
457
+ const objectId = values.meta.objectId.type;
458
+ const hasPriorityArray =
459
+ values.hasPriorityArray && values.hasOwnProperty("hasPriorityArray") ? values.hasPriorityArray : false;
460
+
461
+ if (hasPriorityArray) {
462
+ return "pi writePointIcon";
463
+ } else {
464
+ switch (objectId) {
465
+ case 0:
466
+ //AI
467
+ return "pi readPointIcon";
468
+ case 1:
469
+ //AO
470
+ return "pi readPointIcon";
471
+ case 2:
472
+ //AV
473
+ return "pi readPointIcon";
474
+ case 3:
475
+ //BI
476
+ return "pi readPointIcon";
477
+ case 4:
478
+ //BO
479
+ return "pi readPointIcon";
480
+ case 5:
481
+ //BV
482
+ return "pi readPointIcon";
483
+ case 8:
484
+ //Device
485
+ return "pi pi-box";
486
+ case 13:
487
+ //MI
488
+ return "pi readPointIcon";
489
+ case 14:
490
+ //MO
491
+ return "pi readPointIcon";
492
+ case 19:
493
+ //MV
494
+ return "pi readPointIcon";
495
+ case 10:
496
+ //File
497
+ return "pi pi-file";
498
+ case 16:
499
+ //Program
500
+ return "pi pi-database";
501
+ case 20:
502
+ //Trendlog
503
+ return "pi pi-chart-line";
504
+ case 15:
505
+ //Notification Class
506
+ return "pi pi-bell";
507
+ case 56:
508
+ return "pi pi-sitemap";
509
+ case 178:
510
+ return "pi pi-lock";
511
+ case 17:
512
+ return "pi pi-calendar";
513
+ case 6:
514
+ return "pi pi-calendar";
515
+ default:
516
+ //Return circle for all other types
517
+ return "pi readPointIcon";
518
+ }
519
+ }
520
+ }
521
+
522
+ /**
523
+ * Returns the icon for a given device based on its properties.
524
+ *
525
+ * @param {Object} device - The device object.
526
+ * @returns {string} - The icon class name.
527
+ */
528
+ getDeviceIcon(device) {
529
+ const isMstp = device.getIsMstpDevice();
530
+ const manualDiscoveryMode = device.getManualDiscoveryMode();
531
+
532
+ if (manualDiscoveryMode == true) {
533
+ return "pi pi-question-circle";
534
+ } else if (manualDiscoveryMode == false) {
535
+ if (isMstp == true) {
536
+ return "pi pi-box";
537
+ } else if (isMstp == false) {
538
+ return "pi pi-server";
539
+ }
540
+ }
541
+ return "pi pi-server";
542
+ }
543
+ }
544
+
545
+ module.exports = { treeBuilder };