@halsystems/red-bacnet 2.0.2 → 2.1.0-alpha.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.
- package/CHANGELOG.md +15 -0
- package/common/func.js +13 -1
- package/common/job/discover_device.js +5 -0
- package/ext/node-bacstack/dist/lib/client.js +7 -0
- package/ext/node-bacstack/dist/lib/transport.js +84 -32
- package/package.json +1 -1
- package/red-bacnet/discover_device.js +117 -114
- package/red-bacnet/discover_point.js +97 -93
- package/red-bacnet/read_point.js +101 -97
- package/red-bacnet/write_point.js +99 -95
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [2.1.0-alpha.0]
|
|
4
|
+
### Added
|
|
5
|
+
- BACnet client now listening to both `interface` and `broadcastAddress`
|
|
6
|
+
|
|
7
|
+
### Changed
|
|
8
|
+
- `discover_devices` to drop devices that are not within the low and high limit
|
|
9
|
+
|
|
10
|
+
### Fixed
|
|
11
|
+
- Fixed unable to discover devices from broadcast when `interface` is not set to `0.0.0.0`
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
## [2.0.3]
|
|
15
|
+
### Fixed
|
|
16
|
+
- Added unique job id to same node type to avoid same time triggered nodes job being coalesced
|
|
17
|
+
|
|
3
18
|
## [2.0.2]
|
|
4
19
|
### Fixed
|
|
5
20
|
- CI pipeline adjustments
|
package/common/func.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const { randomUUID } = require('crypto');
|
|
4
|
+
|
|
3
5
|
// ---------------------------------- export ----------------------------------
|
|
4
6
|
module.exports = {
|
|
5
7
|
/**
|
|
@@ -63,4 +65,14 @@ module.exports = {
|
|
|
63
65
|
else
|
|
64
66
|
return err
|
|
65
67
|
},
|
|
66
|
-
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Generate a unique job ID using node ID and uuid
|
|
71
|
+
* @param {string} nodeId - The Node-RED node instance ID.
|
|
72
|
+
* @param {string} prefix - The prefix for the job ID (e.g., 'readPoints', 'writePoints').
|
|
73
|
+
* @returns {string} - A unique job ID.
|
|
74
|
+
*/
|
|
75
|
+
generateUniqueJobId: function (nodeId, prefix) {
|
|
76
|
+
return `${prefix}-${nodeId}-${randomUUID()}`;
|
|
77
|
+
},
|
|
78
|
+
};
|
|
@@ -165,7 +165,12 @@ module.exports = {
|
|
|
165
165
|
const adr = device.adr
|
|
166
166
|
const net = device.net
|
|
167
167
|
|
|
168
|
+
// Check for duplicate
|
|
168
169
|
if (this.knownDevices.has(deviceId)) return;
|
|
170
|
+
|
|
171
|
+
// Drop device if deviceId is not within the low and high limit
|
|
172
|
+
if (deviceId < this.lowLimit || deviceId > this.highLimit) return;
|
|
173
|
+
|
|
169
174
|
this.discoverList.push({
|
|
170
175
|
deviceId: deviceId,
|
|
171
176
|
address: address,
|
|
@@ -59,6 +59,13 @@ class Client extends events_1.EventEmitter {
|
|
|
59
59
|
// Setup code
|
|
60
60
|
this._transport.on('message', this._receiveData.bind(this));
|
|
61
61
|
this._transport.on('error', this._receiveError.bind(this));
|
|
62
|
+
|
|
63
|
+
// Add a default error handler to prevent unhandled error events
|
|
64
|
+
// This will be overridden if the user adds their own error listener
|
|
65
|
+
this.on('error', (err) => {
|
|
66
|
+
debug('Unhandled error event:', err);
|
|
67
|
+
});
|
|
68
|
+
|
|
62
69
|
this._transport.open();
|
|
63
70
|
}
|
|
64
71
|
// Helper utils
|
|
@@ -1,62 +1,114 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Transport = void 0;
|
|
4
4
|
const dgram_1 = require("dgram");
|
|
5
5
|
const events_1 = require("events");
|
|
6
6
|
const DEFAULT_BACNET_PORT = 47808;
|
|
7
|
+
const GLOBAL_BROADCAST_ADDRESS = "255.255.255.255";
|
|
8
|
+
const UDP_TYPE = "udp4";
|
|
9
|
+
|
|
7
10
|
class Transport extends events_1.EventEmitter {
|
|
8
11
|
constructor(settings) {
|
|
9
12
|
super();
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
this._server = (0, dgram_1.createSocket)({ type: 'udp4', reuseAddr: true });
|
|
14
|
-
this._server.on('message', (msg, rinfo) => {
|
|
15
|
-
// NOTE HAL modified
|
|
16
|
-
const port = rinfo.port || DEFAULT_BACNET_PORT;
|
|
17
|
-
// this.emit('message', msg, `${rinfo.address}:${port}`);
|
|
18
|
-
if (port === DEFAULT_BACNET_PORT)
|
|
19
|
-
this.emit('message', msg, rinfo.address);
|
|
20
|
-
else
|
|
21
|
-
this.emit('message', msg, `${rinfo.address}:${rinfo.port || DEFAULT_BACNET_PORT}`);
|
|
22
|
-
});
|
|
23
|
-
this._server.on('error', (err) => this.emit('message', err));
|
|
13
|
+
this._settings = settings;
|
|
14
|
+
this._server = null;
|
|
15
|
+
this._broadcastServer = null;
|
|
24
16
|
|
|
25
|
-
|
|
26
|
-
|
|
17
|
+
this._server = (0, dgram_1.createSocket)({
|
|
18
|
+
type: UDP_TYPE,
|
|
19
|
+
reuseAddr: true,
|
|
20
|
+
});
|
|
21
|
+
this._setupSocket(this._server);
|
|
22
|
+
|
|
23
|
+
if (
|
|
24
|
+
this._settings.broadcastAddress &&
|
|
25
|
+
this._settings.broadcastAddress !== GLOBAL_BROADCAST_ADDRESS &&
|
|
26
|
+
this._settings.broadcastAddress !== this._settings.interface
|
|
27
|
+
) {
|
|
28
|
+
this._broadcastServer = (0, dgram_1.createSocket)({
|
|
29
|
+
type: UDP_TYPE,
|
|
30
|
+
reuseAddr: true,
|
|
31
|
+
});
|
|
32
|
+
this._setupSocket(this._broadcastServer);
|
|
27
33
|
}
|
|
28
34
|
}
|
|
35
|
+
|
|
36
|
+
_setupSocket(socket) {
|
|
37
|
+
socket.on("message", (msg, rinfo) => {
|
|
38
|
+
const port = rinfo.port || DEFAULT_BACNET_PORT;
|
|
39
|
+
if (port === DEFAULT_BACNET_PORT) {
|
|
40
|
+
this.emit("message", msg, rinfo.address);
|
|
41
|
+
} else {
|
|
42
|
+
this.emit("message", msg, `${rinfo.address}:${port}`);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
socket.on("error", (err) => this.emit("error", err));
|
|
46
|
+
}
|
|
47
|
+
|
|
29
48
|
getBroadcastAddress() {
|
|
30
49
|
return this._settings.broadcastAddress;
|
|
31
50
|
}
|
|
51
|
+
|
|
32
52
|
getMaxPayload() {
|
|
33
53
|
return 1482;
|
|
34
54
|
}
|
|
55
|
+
|
|
35
56
|
send(buffer, offset, receiver) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
buffer.copy(dataToSend, 0, 0, offset);
|
|
44
|
-
this._lastSendMessages[messageKey] = dataToSend;
|
|
45
|
-
setTimeout(() => {
|
|
46
|
-
delete this._lastSendMessages[messageKey];
|
|
47
|
-
}, 10000); // delete after 10s, hopefully all cases are handled by that
|
|
57
|
+
const target = receiver || this.getBroadcastAddress();
|
|
58
|
+
if (!target) {
|
|
59
|
+
this.emit(
|
|
60
|
+
"error",
|
|
61
|
+
new Error("No receiver specified and no broadcast address configured"),
|
|
62
|
+
);
|
|
63
|
+
return;
|
|
48
64
|
}
|
|
49
65
|
|
|
50
|
-
const [address, port] =
|
|
51
|
-
this._server.send(
|
|
66
|
+
const [address, port] = target.split(":");
|
|
67
|
+
this._server.send(
|
|
68
|
+
buffer,
|
|
69
|
+
0,
|
|
70
|
+
offset,
|
|
71
|
+
port || this._settings.port || DEFAULT_BACNET_PORT,
|
|
72
|
+
address,
|
|
73
|
+
(err) => {
|
|
74
|
+
if (err) {
|
|
75
|
+
this.emit("error", err);
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
);
|
|
52
79
|
}
|
|
80
|
+
|
|
53
81
|
open() {
|
|
82
|
+
this._server.on("error", (err) => this.emit("error", err));
|
|
54
83
|
this._server.bind(this._settings.port, this._settings.interface, () => {
|
|
55
84
|
this._server.setBroadcast(true);
|
|
56
85
|
});
|
|
86
|
+
|
|
87
|
+
if (this._broadcastServer) {
|
|
88
|
+
this._broadcastServer.on("error", (err) => {
|
|
89
|
+
this.emit("error", err);
|
|
90
|
+
this._broadcastServer.close();
|
|
91
|
+
this._broadcastServer = null;
|
|
92
|
+
});
|
|
93
|
+
this._broadcastServer.bind(
|
|
94
|
+
this._settings.port,
|
|
95
|
+
this._settings.broadcastAddress,
|
|
96
|
+
() => {
|
|
97
|
+
this._broadcastServer.setBroadcast(true);
|
|
98
|
+
}
|
|
99
|
+
);
|
|
100
|
+
}
|
|
57
101
|
}
|
|
102
|
+
|
|
58
103
|
close() {
|
|
59
|
-
this._server
|
|
104
|
+
if (this._server) {
|
|
105
|
+
this._server.removeAllListeners();
|
|
106
|
+
this._server.close();
|
|
107
|
+
}
|
|
108
|
+
if (this._broadcastServer) {
|
|
109
|
+
this._broadcastServer.removeAllListeners();
|
|
110
|
+
this._broadcastServer.close();
|
|
111
|
+
}
|
|
60
112
|
}
|
|
61
113
|
}
|
|
62
114
|
exports.Transport = Transport;
|
package/package.json
CHANGED
|
@@ -1,114 +1,117 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
require('./_alias.js');
|
|
3
|
-
|
|
4
|
-
const EventEmitter = require('events');
|
|
5
|
-
|
|
6
|
-
const { print } = require('@root/common/core/util.js')
|
|
7
|
-
const { nowFormatted } = require('@root/common/core/util.js')
|
|
8
|
-
const { DiscoverDeviceJob } = require('@root/common/job/discover_device.js')
|
|
9
|
-
const { getQueue } = require('@root/common/job/global_queue_manager.js')
|
|
10
|
-
const {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
//
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
this.
|
|
30
|
-
this.
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
this
|
|
34
|
-
|
|
35
|
-
//
|
|
36
|
-
this
|
|
37
|
-
|
|
38
|
-
//
|
|
39
|
-
this.
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
this.
|
|
44
|
-
this
|
|
45
|
-
this.
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
this.
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
// @ts-ignore
|
|
101
|
-
this.status({ fill: '
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
require('./_alias.js');
|
|
3
|
+
|
|
4
|
+
const EventEmitter = require('events');
|
|
5
|
+
|
|
6
|
+
const { print } = require('@root/common/core/util.js')
|
|
7
|
+
const { nowFormatted } = require('@root/common/core/util.js')
|
|
8
|
+
const { DiscoverDeviceJob } = require('@root/common/job/discover_device.js')
|
|
9
|
+
const { getQueue } = require('@root/common/job/global_queue_manager.js')
|
|
10
|
+
const { generateUniqueJobId } = require('@root/common/func.js')
|
|
11
|
+
const {
|
|
12
|
+
EVENT_UPDATE_STATUS, EVENT_ERROR, EVENT_INPUT, EVENT_OUTPUT, EVENT_CLOSE
|
|
13
|
+
} = require('@root/common/core/constant.js')
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
// -------------------------------- functions --------------------------------
|
|
17
|
+
// coalesced job remove on start and on stop, move it to here
|
|
18
|
+
module.exports = function (RED) {
|
|
19
|
+
class DiscoverDevice {
|
|
20
|
+
#eventEmitter = new EventEmitter();
|
|
21
|
+
|
|
22
|
+
constructor(config) {
|
|
23
|
+
RED.nodes.createNode(this, config);
|
|
24
|
+
|
|
25
|
+
// get config
|
|
26
|
+
this.client = RED.nodes.getNode(config.client).instance;
|
|
27
|
+
|
|
28
|
+
// @ts-ignore
|
|
29
|
+
this.jobId = generateUniqueJobId(this.id, 'discoverDevices');
|
|
30
|
+
this.network = +config.network
|
|
31
|
+
this.lowLimit = +config.lowLimit
|
|
32
|
+
this.highLimit = +config.highLimit
|
|
33
|
+
this.whoIsTimeout = +config.whoIsTimeout
|
|
34
|
+
|
|
35
|
+
// events
|
|
36
|
+
this.#subscribeListeners();
|
|
37
|
+
|
|
38
|
+
// configure job queue - use global queue
|
|
39
|
+
this.job = getQueue();
|
|
40
|
+
|
|
41
|
+
// init task
|
|
42
|
+
this.task = new DiscoverDeviceJob(
|
|
43
|
+
this.client,
|
|
44
|
+
this.#eventEmitter,
|
|
45
|
+
this.network,
|
|
46
|
+
this.lowLimit,
|
|
47
|
+
this.highLimit,
|
|
48
|
+
this.whoIsTimeout,
|
|
49
|
+
);
|
|
50
|
+
this.task.onStart();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
#subscribeListeners() {
|
|
54
|
+
/**
|
|
55
|
+
* @param {Object} msg - The message object.
|
|
56
|
+
*/
|
|
57
|
+
// @ts-ignore
|
|
58
|
+
this.on(EVENT_INPUT, async function (msg) {
|
|
59
|
+
/*
|
|
60
|
+
BACnet iAm handler is causing memory leak due to unable to detach after use
|
|
61
|
+
as a result, workaround is introduce before strategy to reuse discoverDeviceJob instead
|
|
62
|
+
of creating new one every time onInput is triggered
|
|
63
|
+
*/
|
|
64
|
+
const jobId = (typeof msg.id === 'string' || typeof msg.id === 'number') ? msg.id : this.jobId;
|
|
65
|
+
|
|
66
|
+
if (this.job.queue.map(item => item.id).includes(jobId)) {
|
|
67
|
+
// @ts-ignore
|
|
68
|
+
print(`Coalesced job: ${jobId}`, true)
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
this.task.network = (msg.network === undefined) ? this.network : msg.network;
|
|
73
|
+
this.task.lowLimit = (msg.lowLimit === undefined) ? this.lowLimit : msg.lowLimit;
|
|
74
|
+
this.task.highLimit = (msg.highLimit === undefined) ? this.highLimit : msg.highLimit;
|
|
75
|
+
|
|
76
|
+
this.job.addJob({
|
|
77
|
+
id: jobId,
|
|
78
|
+
task: this.task,
|
|
79
|
+
priority: Number.isFinite(msg.priority) ? msg.priority : 5,
|
|
80
|
+
});
|
|
81
|
+
// @ts-ignore
|
|
82
|
+
this.status({ fill: 'yellow', shape: 'dot', text: `in queue` });
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// @ts-ignore
|
|
86
|
+
this.on(EVENT_CLOSE, () => {
|
|
87
|
+
this.task.onStop();
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
this.#eventEmitter.on(EVENT_OUTPUT, (data) => {
|
|
91
|
+
const msg = {
|
|
92
|
+
payload: data
|
|
93
|
+
};
|
|
94
|
+
// @ts-ignore
|
|
95
|
+
this.send(msg);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
this.#eventEmitter.on(EVENT_UPDATE_STATUS, (msg) => {
|
|
99
|
+
if (msg === 100)
|
|
100
|
+
// @ts-ignore
|
|
101
|
+
this.status({ fill: 'green', shape: 'dot', text: `completed: ${nowFormatted()}` });
|
|
102
|
+
else
|
|
103
|
+
// @ts-ignore
|
|
104
|
+
this.status({ fill: 'yellow', shape: 'dot', text: `progress: ${msg} %` });
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
this.#eventEmitter.on(EVENT_ERROR, (err) => {
|
|
108
|
+
// @ts-ignore
|
|
109
|
+
this.error(err);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// ----- register node -----
|
|
115
|
+
RED.nodes.registerType('discover device', DiscoverDevice);
|
|
116
|
+
}
|
|
117
|
+
|
|
@@ -1,93 +1,97 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
require('./_alias.js');
|
|
3
|
-
|
|
4
|
-
const EventEmitter = require('events');
|
|
5
|
-
|
|
6
|
-
const { print } = require('@root/common/core/util.js')
|
|
7
|
-
const { nowFormatted } = require('@root/common/core/util.js')
|
|
8
|
-
const { DiscoverPointJob } = require('@root/common/job/discover_point.js')
|
|
9
|
-
const { getQueue } = require('@root/common/job/global_queue_manager.js')
|
|
10
|
-
const {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
this.
|
|
25
|
-
this.
|
|
26
|
-
this.
|
|
27
|
-
this.
|
|
28
|
-
this.
|
|
29
|
-
this.
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
1
|
+
'use strict';
|
|
2
|
+
require('./_alias.js');
|
|
3
|
+
|
|
4
|
+
const EventEmitter = require('events');
|
|
5
|
+
|
|
6
|
+
const { print } = require('@root/common/core/util.js')
|
|
7
|
+
const { nowFormatted } = require('@root/common/core/util.js')
|
|
8
|
+
const { DiscoverPointJob } = require('@root/common/job/discover_point.js')
|
|
9
|
+
const { getQueue } = require('@root/common/job/global_queue_manager.js')
|
|
10
|
+
const { generateUniqueJobId } = require('@root/common/func.js')
|
|
11
|
+
const {
|
|
12
|
+
EVENT_UPDATE_STATUS, EVENT_ERROR, EVENT_INPUT, EVENT_OUTPUT
|
|
13
|
+
} = require('@root/common/core/constant.js')
|
|
14
|
+
|
|
15
|
+
// -------------------------------- functions --------------------------------
|
|
16
|
+
module.exports = function (RED) {
|
|
17
|
+
class DiscoverPoint {
|
|
18
|
+
#eventEmitter = new EventEmitter();
|
|
19
|
+
|
|
20
|
+
constructor(config) {
|
|
21
|
+
RED.nodes.createNode(this, config);
|
|
22
|
+
|
|
23
|
+
// get config
|
|
24
|
+
this.client = RED.nodes.getNode(config.client).instance;
|
|
25
|
+
this.discoverMode = +config.discoverMode
|
|
26
|
+
this.readMethod = +config.readMethod
|
|
27
|
+
this.groupExportDeviceCount = +config.groupExportDeviceCount
|
|
28
|
+
this.maxConcurrentDeviceRead = +config.maxConcurrentDeviceRead
|
|
29
|
+
this.maxConcurrentSinglePointRead = +config.maxConcurrentSinglePointRead
|
|
30
|
+
this.concurrentTaskDelay = +config.concurrentTaskDelay
|
|
31
|
+
|
|
32
|
+
// @ts-ignore
|
|
33
|
+
this.jobId = generateUniqueJobId(this.id, 'discoverPoints');
|
|
34
|
+
|
|
35
|
+
// events
|
|
36
|
+
this.#subscribeListeners();
|
|
37
|
+
|
|
38
|
+
// configure job queue - use global queue
|
|
39
|
+
this.job = getQueue();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
#subscribeListeners() {
|
|
43
|
+
/**
|
|
44
|
+
* @param {Object} msg - The message object.
|
|
45
|
+
*/
|
|
46
|
+
// @ts-ignore
|
|
47
|
+
this.on(EVENT_INPUT, async function (msg) {
|
|
48
|
+
const jobId = (typeof msg.id === 'string' || typeof msg.id === 'number') ? msg.id : this.jobId;
|
|
49
|
+
|
|
50
|
+
if (this.job.queue.map(item => item.id).includes(jobId)) {
|
|
51
|
+
// @ts-ignore
|
|
52
|
+
print(`Coalesced job: ${jobId}`, true)
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const task = new DiscoverPointJob(
|
|
57
|
+
this.client, this.#eventEmitter, msg?.devices, this.discoverMode, this.readMethod,
|
|
58
|
+
this.groupExportDeviceCount, this.maxConcurrentDeviceRead,
|
|
59
|
+
this.maxConcurrentSinglePointRead, this.concurrentTaskDelay
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
this.job.addJob({
|
|
63
|
+
id: jobId,
|
|
64
|
+
task: task,
|
|
65
|
+
priority: Number.isFinite(msg.priority) ? msg.priority : 5,
|
|
66
|
+
});
|
|
67
|
+
// @ts-ignore
|
|
68
|
+
this.status({ fill: 'yellow', shape: 'dot', text: `in queue` });
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
this.#eventEmitter.on(EVENT_OUTPUT, (data) => {
|
|
72
|
+
const msg = {
|
|
73
|
+
payload: data
|
|
74
|
+
};
|
|
75
|
+
// @ts-ignore
|
|
76
|
+
this.send(msg);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
this.#eventEmitter.on(EVENT_UPDATE_STATUS, (msg) => {
|
|
80
|
+
if (msg === 100)
|
|
81
|
+
// @ts-ignore
|
|
82
|
+
this.status({ fill: 'green', shape: 'dot', text: `completed: ${nowFormatted()}` });
|
|
83
|
+
else
|
|
84
|
+
// @ts-ignore
|
|
85
|
+
this.status({ fill: 'yellow', shape: 'dot', text: `progress: ${msg} %` });
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
this.#eventEmitter.on(EVENT_ERROR, (err) => {
|
|
89
|
+
// @ts-ignore
|
|
90
|
+
this.error(err);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// ----- register node -----
|
|
96
|
+
RED.nodes.registerType('discover point', DiscoverPoint);
|
|
97
|
+
}
|
package/red-bacnet/read_point.js
CHANGED
|
@@ -1,97 +1,101 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
require('./_alias.js');
|
|
3
|
-
|
|
4
|
-
const EventEmitter = require('events');
|
|
5
|
-
|
|
6
|
-
const { print } = require('@root/common/core/util.js')
|
|
7
|
-
const { nowFormatted } = require('@root/common/core/util.js')
|
|
8
|
-
const { ReadPointJob } = require('@root/common/job/read_point.js')
|
|
9
|
-
const { getQueue } = require('@root/common/job/global_queue_manager.js')
|
|
10
|
-
const {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
this.
|
|
26
|
-
this.
|
|
27
|
-
this.
|
|
28
|
-
this.
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
this.
|
|
49
|
-
this
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
1
|
+
'use strict';
|
|
2
|
+
require('./_alias.js');
|
|
3
|
+
|
|
4
|
+
const EventEmitter = require('events');
|
|
5
|
+
|
|
6
|
+
const { print } = require('@root/common/core/util.js')
|
|
7
|
+
const { nowFormatted } = require('@root/common/core/util.js')
|
|
8
|
+
const { ReadPointJob } = require('@root/common/job/read_point.js')
|
|
9
|
+
const { getQueue } = require('@root/common/job/global_queue_manager.js')
|
|
10
|
+
const { generateUniqueJobId } = require('@root/common/func.js')
|
|
11
|
+
const {
|
|
12
|
+
EVENT_UPDATE_STATUS, EVENT_ERROR, EVENT_INPUT, EVENT_OUTPUT
|
|
13
|
+
} = require('@root/common/core/constant.js')
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
// -------------------------------- functions --------------------------------
|
|
17
|
+
module.exports = function (RED) {
|
|
18
|
+
class ReadPoint {
|
|
19
|
+
#eventEmitter = new EventEmitter();
|
|
20
|
+
|
|
21
|
+
constructor(config) {
|
|
22
|
+
RED.nodes.createNode(this, config);
|
|
23
|
+
|
|
24
|
+
// get config
|
|
25
|
+
this.client = RED.nodes.getNode(config.client).instance;
|
|
26
|
+
this.readMethod = +config.readMethod
|
|
27
|
+
this.maxConcurrentDeviceRead = +config.maxConcurrentDeviceRead
|
|
28
|
+
this.maxConcurrentSinglePointRead = +config.maxConcurrentSinglePointRead
|
|
29
|
+
this.concurrentTaskDelay = +config.concurrentTaskDelay
|
|
30
|
+
|
|
31
|
+
// @ts-ignore
|
|
32
|
+
this.jobId = generateUniqueJobId(this.id, 'readPoints');
|
|
33
|
+
|
|
34
|
+
// events
|
|
35
|
+
this.#subscribeListeners();
|
|
36
|
+
|
|
37
|
+
// configure job queue - use global queue
|
|
38
|
+
this.job = getQueue();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
#subscribeListeners() {
|
|
42
|
+
/**
|
|
43
|
+
* @param {Object} msg - The message object.
|
|
44
|
+
*/
|
|
45
|
+
// @ts-ignore
|
|
46
|
+
this.on(EVENT_INPUT, async function (msg) {
|
|
47
|
+
const task = new ReadPointJob(
|
|
48
|
+
this.client,
|
|
49
|
+
this.#eventEmitter,
|
|
50
|
+
msg.devices,
|
|
51
|
+
msg.points,
|
|
52
|
+
this.readMethod,
|
|
53
|
+
this.maxConcurrentDeviceRead,
|
|
54
|
+
this.maxConcurrentSinglePointRead,
|
|
55
|
+
this.concurrentTaskDelay
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
const jobId = (typeof msg.id === 'string' || typeof msg.id === 'number') ? msg.id : this.jobId;
|
|
59
|
+
|
|
60
|
+
if (this.job.queue.map(item => item.id).includes(jobId)) {
|
|
61
|
+
// @ts-ignore
|
|
62
|
+
print(`Coalesced job: ${jobId}`, true)
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
this.job.addJob({
|
|
67
|
+
id: jobId,
|
|
68
|
+
task: task,
|
|
69
|
+
priority: Number.isFinite(msg.priority) ? msg.priority : 2,
|
|
70
|
+
});
|
|
71
|
+
// @ts-ignore
|
|
72
|
+
this.status({ fill: 'yellow', shape: 'dot', text: `in queue` });
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
this.#eventEmitter.on(EVENT_OUTPUT, (data) => {
|
|
76
|
+
const msg = {
|
|
77
|
+
payload: data
|
|
78
|
+
};
|
|
79
|
+
// @ts-ignore
|
|
80
|
+
this.send(msg);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
this.#eventEmitter.on(EVENT_UPDATE_STATUS, (msg) => {
|
|
84
|
+
if (msg === 100)
|
|
85
|
+
// @ts-ignore
|
|
86
|
+
this.status({ fill: 'green', shape: 'dot', text: `completed: ${nowFormatted()}` });
|
|
87
|
+
else
|
|
88
|
+
// @ts-ignore
|
|
89
|
+
this.status({ fill: 'yellow', shape: 'dot', text: `progress: ${msg} %` });
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
this.#eventEmitter.on(EVENT_ERROR, (err) => {
|
|
93
|
+
// @ts-ignore
|
|
94
|
+
this.error(err);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// ----- register node -----
|
|
100
|
+
RED.nodes.registerType('read point', ReadPoint);
|
|
101
|
+
}
|
|
@@ -1,95 +1,99 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
require('./_alias.js');
|
|
3
|
-
|
|
4
|
-
const EventEmitter = require('events');
|
|
5
|
-
|
|
6
|
-
const { print } = require('@root/common/core/util.js')
|
|
7
|
-
const { nowFormatted } = require('@root/common/core/util.js')
|
|
8
|
-
const { WritePointJob } = require('@root/common/job/write_point.js')
|
|
9
|
-
const { getQueue } = require('@root/common/job/global_queue_manager.js')
|
|
10
|
-
const {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
this.
|
|
26
|
-
this.
|
|
27
|
-
this.
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
this
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
1
|
+
'use strict';
|
|
2
|
+
require('./_alias.js');
|
|
3
|
+
|
|
4
|
+
const EventEmitter = require('events');
|
|
5
|
+
|
|
6
|
+
const { print } = require('@root/common/core/util.js')
|
|
7
|
+
const { nowFormatted } = require('@root/common/core/util.js')
|
|
8
|
+
const { WritePointJob } = require('@root/common/job/write_point.js')
|
|
9
|
+
const { getQueue } = require('@root/common/job/global_queue_manager.js')
|
|
10
|
+
const { generateUniqueJobId } = require('@root/common/func.js')
|
|
11
|
+
const {
|
|
12
|
+
EVENT_UPDATE_STATUS, EVENT_ERROR, EVENT_INPUT, EVENT_OUTPUT
|
|
13
|
+
} = require('@root/common/core/constant.js')
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
// -------------------------------- functions --------------------------------
|
|
17
|
+
module.exports = function (RED) {
|
|
18
|
+
class WritePoint {
|
|
19
|
+
#eventEmitter = new EventEmitter();
|
|
20
|
+
|
|
21
|
+
constructor(config) {
|
|
22
|
+
RED.nodes.createNode(this, config);
|
|
23
|
+
|
|
24
|
+
// get config
|
|
25
|
+
this.client = RED.nodes.getNode(config.client).instance;
|
|
26
|
+
this.maxConcurrentDeviceWrite = +config.maxConcurrentDeviceWrite
|
|
27
|
+
this.maxConcurrentPointWrite = +config.maxConcurrentPointWrite
|
|
28
|
+
this.concurrentTaskDelay = +config.concurrentTaskDelay
|
|
29
|
+
|
|
30
|
+
// @ts-ignore
|
|
31
|
+
this.jobId = generateUniqueJobId(this.id, 'writePoints');
|
|
32
|
+
|
|
33
|
+
// events
|
|
34
|
+
this.#subscribeListeners();
|
|
35
|
+
|
|
36
|
+
// configure job queue - use global queue
|
|
37
|
+
this.job = getQueue();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
#subscribeListeners() {
|
|
41
|
+
/**
|
|
42
|
+
* @param {Object} msg - The message object.
|
|
43
|
+
*/
|
|
44
|
+
// @ts-ignore
|
|
45
|
+
this.on(EVENT_INPUT, async function (msg) {
|
|
46
|
+
const task = new WritePointJob(
|
|
47
|
+
this.client,
|
|
48
|
+
this.#eventEmitter,
|
|
49
|
+
msg.devices,
|
|
50
|
+
msg.points,
|
|
51
|
+
msg.writePoints,
|
|
52
|
+
this.maxConcurrentDeviceWrite,
|
|
53
|
+
this.maxConcurrentPointWrite,
|
|
54
|
+
this.concurrentTaskDelay
|
|
55
|
+
);
|
|
56
|
+
const jobId = (typeof msg.id === 'string' || typeof msg.id === 'number') ? msg.id : this.jobId;
|
|
57
|
+
|
|
58
|
+
if (this.job.queue.map(item => item.id).includes(jobId)) {
|
|
59
|
+
// @ts-ignore
|
|
60
|
+
print(`Coalesced job: ${jobId}`, true)
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
this.job.addJob({
|
|
65
|
+
id: jobId,
|
|
66
|
+
task: task,
|
|
67
|
+
priority: Number.isFinite(msg.priority) ? msg.priority : 2,
|
|
68
|
+
});
|
|
69
|
+
// @ts-ignore
|
|
70
|
+
this.status({ fill: 'yellow', shape: 'dot', text: `in queue` });
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
this.#eventEmitter.on(EVENT_OUTPUT, (data) => {
|
|
74
|
+
const msg = {
|
|
75
|
+
payload: data
|
|
76
|
+
};
|
|
77
|
+
// @ts-ignore
|
|
78
|
+
this.send(msg);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
this.#eventEmitter.on(EVENT_UPDATE_STATUS, (msg) => {
|
|
82
|
+
if (msg === 100)
|
|
83
|
+
// @ts-ignore
|
|
84
|
+
this.status({ fill: 'green', shape: 'dot', text: `completed: ${nowFormatted()}` });
|
|
85
|
+
else
|
|
86
|
+
// @ts-ignore
|
|
87
|
+
this.status({ fill: 'yellow', shape: 'dot', text: `progress: ${msg} %` });
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
this.#eventEmitter.on(EVENT_ERROR, (err) => {
|
|
91
|
+
// @ts-ignore
|
|
92
|
+
this.error(err);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// ----- register node -----
|
|
98
|
+
RED.nodes.registerType('write point', WritePoint);
|
|
99
|
+
}
|