node-red-contrib-s7-rust 0.0.0-stage → 1.0.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/CHANGELOG.md +38 -0
- package/COPYING +674 -0
- package/LICENSE +165 -0
- package/NOTICE +22 -0
- package/README.md +309 -2
- package/bin/linux-x64-gnu/s7napi.node +0 -0
- package/bin/linux-x64-musl/s7napi.node +0 -0
- package/examples/01 - Read and write (S7comm).json +180 -0
- package/examples/02 - Watch with conditional rate.json +71 -0
- package/examples/03 - S7CommPlus symbolic access.json +153 -0
- package/examples/04 - PLC diagnostics.json +180 -0
- package/lib/common.js +78 -0
- package/lib/napi-bridge.js +182 -0
- package/nodes/icons/s7rs-plc.svg +14 -0
- package/nodes/locales/en-US/s7rs-endpoint.html +24 -0
- package/nodes/locales/en-US/s7rs-endpoint.json +51 -0
- package/nodes/locales/en-US/s7rs-info.html +38 -0
- package/nodes/locales/en-US/s7rs-info.json +18 -0
- package/nodes/locales/en-US/s7rs-read.html +33 -0
- package/nodes/locales/en-US/s7rs-read.json +27 -0
- package/nodes/locales/en-US/s7rs-watch.html +24 -0
- package/nodes/locales/en-US/s7rs-watch.json +32 -0
- package/nodes/locales/en-US/s7rs-write.html +21 -0
- package/nodes/locales/en-US/s7rs-write.json +17 -0
- package/nodes/s7rs-endpoint.html +150 -0
- package/nodes/s7rs-endpoint.js +122 -0
- package/nodes/s7rs-info.html +72 -0
- package/nodes/s7rs-info.js +38 -0
- package/nodes/s7rs-read.html +50 -0
- package/nodes/s7rs-read.js +46 -0
- package/nodes/s7rs-watch.html +69 -0
- package/nodes/s7rs-watch.js +73 -0
- package/nodes/s7rs-write.html +34 -0
- package/nodes/s7rs-write.js +38 -0
- package/package.json +77 -4
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"id": "c2",
|
|
4
|
+
"type": "comment",
|
|
5
|
+
"name": "Watch: only changes, faster while a tag is true",
|
|
6
|
+
"info": "The watch node reads the tags in a Rust thread every 1000 ms and sends a message only when a value changes.\nWhile M0.0 is true it reads every 100 ms (Fast while).\nOutput 'one message per tag': msg.topic = tag, msg.payload = value, msg.quality.",
|
|
7
|
+
"x": 260,
|
|
8
|
+
"y": 40,
|
|
9
|
+
"wires": []
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
"id": "wa1",
|
|
13
|
+
"type": "s7rs-watch",
|
|
14
|
+
"name": "",
|
|
15
|
+
"endpoint": "ep2",
|
|
16
|
+
"tags": "MW10:int\nMD12:real\nDB1.DBX0.0",
|
|
17
|
+
"interval": 1000,
|
|
18
|
+
"onChange": true,
|
|
19
|
+
"output": "each",
|
|
20
|
+
"topic": "",
|
|
21
|
+
"condTag": "M0.0",
|
|
22
|
+
"condInterval": 100,
|
|
23
|
+
"x": 170,
|
|
24
|
+
"y": 100,
|
|
25
|
+
"wires": [
|
|
26
|
+
[
|
|
27
|
+
"d3"
|
|
28
|
+
]
|
|
29
|
+
]
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
"id": "d3",
|
|
33
|
+
"type": "debug",
|
|
34
|
+
"name": "changes",
|
|
35
|
+
"active": true,
|
|
36
|
+
"tosidebar": true,
|
|
37
|
+
"console": false,
|
|
38
|
+
"tostatus": false,
|
|
39
|
+
"complete": "true",
|
|
40
|
+
"targetType": "full",
|
|
41
|
+
"statusVal": "",
|
|
42
|
+
"statusType": "auto",
|
|
43
|
+
"x": 400,
|
|
44
|
+
"y": 100,
|
|
45
|
+
"wires": []
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
"id": "ep2",
|
|
49
|
+
"type": "s7rs-endpoint",
|
|
50
|
+
"name": "PLC (S7comm)",
|
|
51
|
+
"protocol": "s7",
|
|
52
|
+
"host": "192.168.0.1",
|
|
53
|
+
"port": 102,
|
|
54
|
+
"cpu": "1500",
|
|
55
|
+
"rack": "",
|
|
56
|
+
"slot": "",
|
|
57
|
+
"link": "",
|
|
58
|
+
"localTsap": "",
|
|
59
|
+
"remoteTsap": "",
|
|
60
|
+
"pdu": "",
|
|
61
|
+
"timeout": 5000,
|
|
62
|
+
"attempts": 3,
|
|
63
|
+
"reconnect": 5000,
|
|
64
|
+
"gap": 5,
|
|
65
|
+
"byteOrder": "big",
|
|
66
|
+
"logLevel": "warn",
|
|
67
|
+
"tsap": "SIMATIC-ROOT-HMI",
|
|
68
|
+
"tls": false,
|
|
69
|
+
"username": ""
|
|
70
|
+
}
|
|
71
|
+
]
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"id": "c3",
|
|
4
|
+
"type": "comment",
|
|
5
|
+
"name": "S7CommPlus: symbols of optimized DBs and PLC tags",
|
|
6
|
+
"info": "S7-1200/1500 only. No PUT/GET needed. Symbols in double quotes for DBs (\"DB_Data\".speed), PLC tag names as they are (Motor_On).\nAbsolute addresses (MW10:int) work too, in the same request.\nThe browse button lists every symbol of the PLC.",
|
|
7
|
+
"x": 270,
|
|
8
|
+
"y": 40,
|
|
9
|
+
"wires": []
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
"id": "i4",
|
|
13
|
+
"type": "inject",
|
|
14
|
+
"name": "read",
|
|
15
|
+
"props": [
|
|
16
|
+
{
|
|
17
|
+
"p": "payload"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"p": "topic",
|
|
21
|
+
"vt": "str"
|
|
22
|
+
}
|
|
23
|
+
],
|
|
24
|
+
"repeat": "",
|
|
25
|
+
"crontab": "",
|
|
26
|
+
"once": false,
|
|
27
|
+
"onceDelay": 0.5,
|
|
28
|
+
"topic": "",
|
|
29
|
+
"payload": "",
|
|
30
|
+
"payloadType": "date",
|
|
31
|
+
"x": 130,
|
|
32
|
+
"y": 100,
|
|
33
|
+
"wires": [
|
|
34
|
+
[
|
|
35
|
+
"r2"
|
|
36
|
+
]
|
|
37
|
+
]
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
"id": "r2",
|
|
41
|
+
"type": "s7rs-read",
|
|
42
|
+
"name": "",
|
|
43
|
+
"endpoint": "ep3",
|
|
44
|
+
"tags": "\"DB_Data\".speed\n\"DB_Data\".values[2]\nMotor_On\nMW10:int",
|
|
45
|
+
"output": "object",
|
|
46
|
+
"x": 310,
|
|
47
|
+
"y": 100,
|
|
48
|
+
"wires": [
|
|
49
|
+
[
|
|
50
|
+
"d4"
|
|
51
|
+
]
|
|
52
|
+
]
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
"id": "d4",
|
|
56
|
+
"type": "debug",
|
|
57
|
+
"name": "values",
|
|
58
|
+
"active": true,
|
|
59
|
+
"tosidebar": true,
|
|
60
|
+
"console": false,
|
|
61
|
+
"tostatus": false,
|
|
62
|
+
"complete": "payload",
|
|
63
|
+
"targetType": "msg",
|
|
64
|
+
"statusVal": "",
|
|
65
|
+
"statusType": "auto",
|
|
66
|
+
"x": 520,
|
|
67
|
+
"y": 100,
|
|
68
|
+
"wires": []
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
"id": "i5",
|
|
72
|
+
"type": "inject",
|
|
73
|
+
"name": "browse",
|
|
74
|
+
"props": [
|
|
75
|
+
{
|
|
76
|
+
"p": "payload"
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
"p": "topic",
|
|
80
|
+
"vt": "str"
|
|
81
|
+
}
|
|
82
|
+
],
|
|
83
|
+
"repeat": "",
|
|
84
|
+
"crontab": "",
|
|
85
|
+
"once": false,
|
|
86
|
+
"onceDelay": 0.5,
|
|
87
|
+
"topic": "",
|
|
88
|
+
"payload": "",
|
|
89
|
+
"payloadType": "date",
|
|
90
|
+
"x": 130,
|
|
91
|
+
"y": 160,
|
|
92
|
+
"wires": [
|
|
93
|
+
[
|
|
94
|
+
"n1"
|
|
95
|
+
]
|
|
96
|
+
]
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
"id": "n1",
|
|
100
|
+
"type": "s7rs-info",
|
|
101
|
+
"name": "",
|
|
102
|
+
"endpoint": "ep3",
|
|
103
|
+
"action": "browse",
|
|
104
|
+
"args": "",
|
|
105
|
+
"x": 310,
|
|
106
|
+
"y": 160,
|
|
107
|
+
"wires": [
|
|
108
|
+
[
|
|
109
|
+
"d5"
|
|
110
|
+
]
|
|
111
|
+
]
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
"id": "d5",
|
|
115
|
+
"type": "debug",
|
|
116
|
+
"name": "symbols",
|
|
117
|
+
"active": true,
|
|
118
|
+
"tosidebar": true,
|
|
119
|
+
"console": false,
|
|
120
|
+
"tostatus": false,
|
|
121
|
+
"complete": "payload",
|
|
122
|
+
"targetType": "msg",
|
|
123
|
+
"statusVal": "",
|
|
124
|
+
"statusType": "auto",
|
|
125
|
+
"x": 520,
|
|
126
|
+
"y": 160,
|
|
127
|
+
"wires": []
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
"id": "ep3",
|
|
131
|
+
"type": "s7rs-endpoint",
|
|
132
|
+
"name": "PLC (S7CommPlus)",
|
|
133
|
+
"protocol": "s7plus",
|
|
134
|
+
"host": "192.168.0.1",
|
|
135
|
+
"port": 102,
|
|
136
|
+
"cpu": "1200",
|
|
137
|
+
"rack": "",
|
|
138
|
+
"slot": "",
|
|
139
|
+
"link": "",
|
|
140
|
+
"localTsap": "",
|
|
141
|
+
"remoteTsap": "",
|
|
142
|
+
"pdu": "",
|
|
143
|
+
"timeout": 5000,
|
|
144
|
+
"attempts": 3,
|
|
145
|
+
"reconnect": 5000,
|
|
146
|
+
"gap": 5,
|
|
147
|
+
"byteOrder": "big",
|
|
148
|
+
"logLevel": "warn",
|
|
149
|
+
"tsap": "SIMATIC-ROOT-HMI",
|
|
150
|
+
"tls": false,
|
|
151
|
+
"username": ""
|
|
152
|
+
}
|
|
153
|
+
]
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"id": "c4",
|
|
4
|
+
"type": "comment",
|
|
5
|
+
"name": "Diagnostics with s7 info",
|
|
6
|
+
"info": "status: connection state and latency (no PLC request).\ncpuState: RUN or STOP.\nidentify: order number and firmware (S7comm).\nmsg.action overrides the action of the node.",
|
|
7
|
+
"x": 200,
|
|
8
|
+
"y": 40,
|
|
9
|
+
"wires": []
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
"id": "i6",
|
|
13
|
+
"type": "inject",
|
|
14
|
+
"name": "status",
|
|
15
|
+
"props": [
|
|
16
|
+
{
|
|
17
|
+
"p": "payload"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"p": "topic",
|
|
21
|
+
"vt": "str"
|
|
22
|
+
}
|
|
23
|
+
],
|
|
24
|
+
"repeat": "",
|
|
25
|
+
"crontab": "",
|
|
26
|
+
"once": false,
|
|
27
|
+
"onceDelay": 0.5,
|
|
28
|
+
"topic": "",
|
|
29
|
+
"payload": "",
|
|
30
|
+
"payloadType": "date",
|
|
31
|
+
"x": 130,
|
|
32
|
+
"y": 100,
|
|
33
|
+
"wires": [
|
|
34
|
+
[
|
|
35
|
+
"n2"
|
|
36
|
+
]
|
|
37
|
+
]
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
"id": "n2",
|
|
41
|
+
"type": "s7rs-info",
|
|
42
|
+
"name": "",
|
|
43
|
+
"endpoint": "ep4",
|
|
44
|
+
"action": "status",
|
|
45
|
+
"args": "",
|
|
46
|
+
"x": 310,
|
|
47
|
+
"y": 100,
|
|
48
|
+
"wires": [
|
|
49
|
+
[
|
|
50
|
+
"d6"
|
|
51
|
+
]
|
|
52
|
+
]
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
"id": "i7",
|
|
56
|
+
"type": "inject",
|
|
57
|
+
"name": "cpuState",
|
|
58
|
+
"props": [
|
|
59
|
+
{
|
|
60
|
+
"p": "payload"
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
"p": "topic",
|
|
64
|
+
"vt": "str"
|
|
65
|
+
}
|
|
66
|
+
],
|
|
67
|
+
"repeat": "",
|
|
68
|
+
"crontab": "",
|
|
69
|
+
"once": false,
|
|
70
|
+
"onceDelay": 0.5,
|
|
71
|
+
"topic": "",
|
|
72
|
+
"payload": "",
|
|
73
|
+
"payloadType": "date",
|
|
74
|
+
"x": 130,
|
|
75
|
+
"y": 140,
|
|
76
|
+
"wires": [
|
|
77
|
+
[
|
|
78
|
+
"n3"
|
|
79
|
+
]
|
|
80
|
+
]
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
"id": "n3",
|
|
84
|
+
"type": "s7rs-info",
|
|
85
|
+
"name": "",
|
|
86
|
+
"endpoint": "ep4",
|
|
87
|
+
"action": "cpuState",
|
|
88
|
+
"args": "",
|
|
89
|
+
"x": 320,
|
|
90
|
+
"y": 140,
|
|
91
|
+
"wires": [
|
|
92
|
+
[
|
|
93
|
+
"d6"
|
|
94
|
+
]
|
|
95
|
+
]
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
"id": "i8",
|
|
99
|
+
"type": "inject",
|
|
100
|
+
"name": "identify",
|
|
101
|
+
"props": [
|
|
102
|
+
{
|
|
103
|
+
"p": "payload"
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
"p": "topic",
|
|
107
|
+
"vt": "str"
|
|
108
|
+
}
|
|
109
|
+
],
|
|
110
|
+
"repeat": "",
|
|
111
|
+
"crontab": "",
|
|
112
|
+
"once": false,
|
|
113
|
+
"onceDelay": 0.5,
|
|
114
|
+
"topic": "",
|
|
115
|
+
"payload": "",
|
|
116
|
+
"payloadType": "date",
|
|
117
|
+
"x": 130,
|
|
118
|
+
"y": 180,
|
|
119
|
+
"wires": [
|
|
120
|
+
[
|
|
121
|
+
"n4"
|
|
122
|
+
]
|
|
123
|
+
]
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
"id": "n4",
|
|
127
|
+
"type": "s7rs-info",
|
|
128
|
+
"name": "",
|
|
129
|
+
"endpoint": "ep4",
|
|
130
|
+
"action": "identify",
|
|
131
|
+
"args": "",
|
|
132
|
+
"x": 320,
|
|
133
|
+
"y": 180,
|
|
134
|
+
"wires": [
|
|
135
|
+
[
|
|
136
|
+
"d6"
|
|
137
|
+
]
|
|
138
|
+
]
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
"id": "d6",
|
|
142
|
+
"type": "debug",
|
|
143
|
+
"name": "result",
|
|
144
|
+
"active": true,
|
|
145
|
+
"tosidebar": true,
|
|
146
|
+
"console": false,
|
|
147
|
+
"tostatus": false,
|
|
148
|
+
"complete": "payload",
|
|
149
|
+
"targetType": "msg",
|
|
150
|
+
"statusVal": "",
|
|
151
|
+
"statusType": "auto",
|
|
152
|
+
"x": 520,
|
|
153
|
+
"y": 140,
|
|
154
|
+
"wires": []
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
"id": "ep4",
|
|
158
|
+
"type": "s7rs-endpoint",
|
|
159
|
+
"name": "PLC (S7comm)",
|
|
160
|
+
"protocol": "s7",
|
|
161
|
+
"host": "192.168.0.1",
|
|
162
|
+
"port": 102,
|
|
163
|
+
"cpu": "1200",
|
|
164
|
+
"rack": "",
|
|
165
|
+
"slot": "",
|
|
166
|
+
"link": "",
|
|
167
|
+
"localTsap": "",
|
|
168
|
+
"remoteTsap": "",
|
|
169
|
+
"pdu": "",
|
|
170
|
+
"timeout": 5000,
|
|
171
|
+
"attempts": 3,
|
|
172
|
+
"reconnect": 5000,
|
|
173
|
+
"gap": 5,
|
|
174
|
+
"byteOrder": "big",
|
|
175
|
+
"logLevel": "warn",
|
|
176
|
+
"tsap": "SIMATIC-ROOT-HMI",
|
|
177
|
+
"tls": false,
|
|
178
|
+
"username": ""
|
|
179
|
+
}
|
|
180
|
+
]
|
package/lib/common.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// Helpers shared by the node files.
|
|
3
|
+
|
|
4
|
+
const fs = require('node:fs');
|
|
5
|
+
const path = require('node:path');
|
|
6
|
+
|
|
7
|
+
// English catalogs, for runtimes whose RED._ has no catalogs (the test helper
|
|
8
|
+
// returns the key).
|
|
9
|
+
const fallback = {};
|
|
10
|
+
for (const f of fs.readdirSync(path.join(__dirname, '..', 'nodes', 'locales', 'en-US'))) {
|
|
11
|
+
if (f.endsWith('.json')) Object.assign(fallback, require(path.join(__dirname, '..', 'nodes', 'locales', 'en-US', f)));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// text: RED._(key, vars), or the English text when the runtime gives the key back.
|
|
15
|
+
function text(RED, key, vars) {
|
|
16
|
+
const s = RED._(key, vars);
|
|
17
|
+
if (s !== key) return s;
|
|
18
|
+
const v = key.split('.').reduce((o, k) => (o && typeof o === 'object' ? o[k] : undefined), fallback);
|
|
19
|
+
if (typeof v !== 'string') return key;
|
|
20
|
+
return v.replace(/__(\w+)__/g, (m, k) => (vars && k in vars ? String(vars[k]) : m));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function intOrUndef(v) {
|
|
24
|
+
if (v === undefined || v === null || String(v).trim() === '') return undefined;
|
|
25
|
+
const n = Number(v);
|
|
26
|
+
return Number.isFinite(n) ? Math.trunc(n) : undefined;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// parseTags: one address per line; empty lines and # or // comments are skipped.
|
|
30
|
+
function parseTags(text) {
|
|
31
|
+
return String(text || '')
|
|
32
|
+
.split(/\r?\n/)
|
|
33
|
+
.map((s) => s.trim())
|
|
34
|
+
.filter((s) => s && !s.startsWith('#') && !s.startsWith('//'));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// toWireValue: the forms the addon accepts for writes.
|
|
38
|
+
function toWireValue(v) {
|
|
39
|
+
if (v instanceof Date) return v.toISOString();
|
|
40
|
+
if (typeof v === 'bigint') return v.toString();
|
|
41
|
+
if (Buffer.isBuffer(v)) return Array.from(v);
|
|
42
|
+
return v;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function isPlainObject(p) {
|
|
46
|
+
return p !== null && typeof p === 'object' && !Array.isArray(p) && !(p instanceof Date) && !Buffer.isBuffer(p);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function elapsedMs(t0) {
|
|
50
|
+
return Number(process.hrtime.bigint() - t0) / 1e6;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// bindStatus shows the connection state of the endpoint on a flow node.
|
|
54
|
+
function bindStatus(node, ep) {
|
|
55
|
+
const show = (s) => {
|
|
56
|
+
const st = s.state;
|
|
57
|
+
if (st === 'connected') node.status({ fill: 'green', shape: 'dot', text: s.detail || 'connected' });
|
|
58
|
+
else if (st === 'connecting') node.status({ fill: 'yellow', shape: 'ring', text: 'connecting' });
|
|
59
|
+
else if (st === 'faulted') node.status({ fill: 'red', shape: 'ring', text: (s.detail || 'faulted').slice(0, 60) });
|
|
60
|
+
else node.status({ fill: 'grey', shape: 'ring', text: st || 'disconnected' });
|
|
61
|
+
};
|
|
62
|
+
show(ep.state);
|
|
63
|
+
ep.on('s7status', show);
|
|
64
|
+
node.on('close', () => ep.off('s7status', show));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// getEndpoint returns the deployed s7rs-endpoint config node, or null.
|
|
68
|
+
function getEndpoint(RED, node, config) {
|
|
69
|
+
const ep = RED.nodes.getNode(config.endpoint);
|
|
70
|
+
if (!ep || !ep.bridge) {
|
|
71
|
+
node.status({ fill: 'red', shape: 'ring', text: 'no endpoint' });
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
bindStatus(node, ep);
|
|
75
|
+
return ep;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
module.exports = { text, intOrUndef, parseTags, toWireValue, isPlainObject, elapsedMs, bindStatus, getEndpoint };
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// The S7 clients in Rust, inside the Node process (Node-API addon s7napi, source in
|
|
3
|
+
// crates/): classic S7comm (crate s7) and S7CommPlus (crate s7plus). No child
|
|
4
|
+
// process, no IPC, no JSON on the hot path. Linux x64 only (glibc or musl).
|
|
5
|
+
//
|
|
6
|
+
// API (Promise based, events 'status', 'data', 'log'):
|
|
7
|
+
// open(params) close(conn) read(conn, tags) write(conn, [{tag, value}])
|
|
8
|
+
// subscribe(params) unsubscribe(sub) status(conn) call(conn, action, args) stop()
|
|
9
|
+
// acquire()/release(): one shared instance per Node-RED runtime.
|
|
10
|
+
|
|
11
|
+
const { EventEmitter } = require('node:events');
|
|
12
|
+
const fs = require('node:fs');
|
|
13
|
+
const path = require('node:path');
|
|
14
|
+
|
|
15
|
+
function isMusl() {
|
|
16
|
+
const r = process.report && process.report.getReport && process.report.getReport();
|
|
17
|
+
return !(r && r.header && r.header.glibcVersionRuntime);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// addonPath: S7RS_NAPI (tests, custom builds) or the prebuilt binary of this host.
|
|
21
|
+
function addonPath() {
|
|
22
|
+
if (process.env.S7RS_NAPI) return process.env.S7RS_NAPI;
|
|
23
|
+
if (process.platform !== 'linux' || process.arch !== 'x64') {
|
|
24
|
+
throw new Error(`node-red-contrib-s7-rust supports Linux x64 only (this host: ${process.platform}-${process.arch})`);
|
|
25
|
+
}
|
|
26
|
+
const dir = `linux-x64-${isMusl() ? 'musl' : 'gnu'}`;
|
|
27
|
+
const p = path.join(__dirname, '..', 'bin', dir, 's7napi.node');
|
|
28
|
+
if (!fs.existsSync(p)) throw new Error(`s7napi addon not found (${p}); run "npm run build" or set S7RS_NAPI`);
|
|
29
|
+
return p;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let addon = null;
|
|
33
|
+
function loadAddon() {
|
|
34
|
+
if (!addon) addon = require(addonPath());
|
|
35
|
+
return addon;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// rows builds the {tag, value, quality, error?} objects from the addon's columns.
|
|
39
|
+
// Creating them here (V8, one shape) is much cheaper than one Node-API call per
|
|
40
|
+
// property in Rust.
|
|
41
|
+
function rows(tags, vals, errs, idx) {
|
|
42
|
+
const n = vals.length;
|
|
43
|
+
const out = new Array(n);
|
|
44
|
+
for (let k = 0; k < n; k++) out[k] = { tag: idx ? tags[idx[k]] : tags[k], value: vals[k], quality: 'good' };
|
|
45
|
+
for (let j = 0; j < errs.length; j++) {
|
|
46
|
+
const o = out[errs[j][0]];
|
|
47
|
+
o.quality = 'bad';
|
|
48
|
+
o.error = errs[j][1];
|
|
49
|
+
}
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
class NapiBridge extends EventEmitter {
|
|
54
|
+
constructor() {
|
|
55
|
+
super();
|
|
56
|
+
this.setMaxListeners(0);
|
|
57
|
+
this.native = null;
|
|
58
|
+
this.subTags = new Map(); // sub -> tags (the addon sends columns, not tag names)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
start() {
|
|
62
|
+
if (this.native) return;
|
|
63
|
+
const { S7Native } = loadAddon();
|
|
64
|
+
this.native = new S7Native(
|
|
65
|
+
(conn, state, detail) => this.emit('status', { event: 'status', conn, state, detail }),
|
|
66
|
+
(ev) => {
|
|
67
|
+
const tags = this.subTags.get(ev.sub);
|
|
68
|
+
if (!tags) return; // unsubscribed meanwhile
|
|
69
|
+
this.emit('data', { event: 'data', sub: ev.sub, conn: ev.conn, values: rows(tags, ev.vals, ev.errs, ev.idx), ts: ev.ts, ms: ev.ms });
|
|
70
|
+
},
|
|
71
|
+
(conn, level, text) => this.emit('log', level, `${conn}: ${text}`),
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
pid() {
|
|
76
|
+
return undefined; // in-process: its CPU is part of the Node process
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async open(p) {
|
|
80
|
+
this.start();
|
|
81
|
+
return this.native.open(p);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async close(conn) {
|
|
85
|
+
if (this.native) this.native.close(conn);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async read(conn, tags) {
|
|
89
|
+
const r = await this.native.read(conn, tags);
|
|
90
|
+
return { values: rows(tags, r.vals, r.errs), nativeMs: r.nativeMs };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async write(conn, items) {
|
|
94
|
+
const n = items.length;
|
|
95
|
+
const tags = new Array(n);
|
|
96
|
+
const vals = new Array(n);
|
|
97
|
+
for (let i = 0; i < n; i++) {
|
|
98
|
+
tags[i] = items[i].tag;
|
|
99
|
+
vals[i] = items[i].value;
|
|
100
|
+
}
|
|
101
|
+
const r = await this.native.write(conn, tags, vals);
|
|
102
|
+
const results = new Array(n);
|
|
103
|
+
for (let i = 0; i < n; i++) results[i] = { tag: tags[i] };
|
|
104
|
+
for (let j = 0; j < r.errs.length; j++) results[r.errs[j][0]].error = r.errs[j][1];
|
|
105
|
+
return { results, nativeMs: r.nativeMs };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// subscribe({conn, sub, tags, intervalMs, onChange, condTag, condIntervalMs}) -> {invalid}
|
|
109
|
+
async subscribe(p) {
|
|
110
|
+
this.start();
|
|
111
|
+
this.subTags.set(p.sub, p.tags);
|
|
112
|
+
return { invalid: await this.native.subscribe(p) };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async unsubscribe(sub) {
|
|
116
|
+
this.subTags.delete(sub);
|
|
117
|
+
if (this.native) this.native.unsubscribe(sub);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async status(conn) {
|
|
121
|
+
return this.native.status(conn);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// call(conn, action, args) -> parsed JSON. Actions:
|
|
125
|
+
// s7: identify, cpuState, szl {id, index}, szlList, clock, setClock {time?},
|
|
126
|
+
// syncClock {thresholdMs}, blocks, listBlocks {type}, blockInfo {type, number}
|
|
127
|
+
// s7plus: info, cpuState, limits, dbs, browse, alarms {all}
|
|
128
|
+
async call(conn, action, args) {
|
|
129
|
+
const text = await this.native.call(conn, action, args === undefined ? undefined : JSON.stringify(args));
|
|
130
|
+
return JSON.parse(text);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
stop() {
|
|
134
|
+
if (this.native) this.native.stop();
|
|
135
|
+
this.subTags.clear();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// ---- one shared instance per runtime -----------------------------------------
|
|
140
|
+
|
|
141
|
+
const GRACE_MS = 3000; // a deploy closes and recreates the nodes: keep the instance
|
|
142
|
+
let shared = null;
|
|
143
|
+
let refs = 0;
|
|
144
|
+
let stopTimer = null;
|
|
145
|
+
|
|
146
|
+
function acquire() {
|
|
147
|
+
clearTimeout(stopTimer);
|
|
148
|
+
stopTimer = null;
|
|
149
|
+
if (!shared) {
|
|
150
|
+
shared = new NapiBridge();
|
|
151
|
+
shared.start();
|
|
152
|
+
}
|
|
153
|
+
refs++;
|
|
154
|
+
return shared;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function release() {
|
|
158
|
+
if (refs > 0) refs--;
|
|
159
|
+
if (refs === 0 && shared && !stopTimer) {
|
|
160
|
+
stopTimer = setTimeout(() => {
|
|
161
|
+
stopTimer = null;
|
|
162
|
+
if (refs === 0 && shared) {
|
|
163
|
+
shared.stop();
|
|
164
|
+
shared = null;
|
|
165
|
+
}
|
|
166
|
+
}, GRACE_MS);
|
|
167
|
+
stopTimer.unref();
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// stopAll stops the shared instance at once (tests; in Node-RED, release does it).
|
|
172
|
+
function stopAll() {
|
|
173
|
+
clearTimeout(stopTimer);
|
|
174
|
+
stopTimer = null;
|
|
175
|
+
refs = 0;
|
|
176
|
+
if (shared) {
|
|
177
|
+
shared.stop();
|
|
178
|
+
shared = null;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
module.exports = { NapiBridge, acquire, release, stopAll, addonPath, loadAddon };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="40" height="60" viewBox="0 0 40 60">
|
|
2
|
+
<g fill="none" stroke="#fff" stroke-width="2.5">
|
|
3
|
+
<rect x="6" y="14" width="28" height="32" rx="2"/>
|
|
4
|
+
<line x1="6" y1="22" x2="34" y2="22"/>
|
|
5
|
+
</g>
|
|
6
|
+
<g fill="#fff">
|
|
7
|
+
<rect x="10" y="27" width="5" height="5"/>
|
|
8
|
+
<rect x="17.5" y="27" width="5" height="5"/>
|
|
9
|
+
<rect x="25" y="27" width="5" height="5"/>
|
|
10
|
+
<rect x="10" y="36" width="20" height="5"/>
|
|
11
|
+
<circle cx="11" cy="18" r="1.5"/>
|
|
12
|
+
<circle cx="16" cy="18" r="1.5"/>
|
|
13
|
+
</g>
|
|
14
|
+
</svg>
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
<script type="text/html" data-help-name="s7rs-endpoint">
|
|
2
|
+
<p>A connection to one Siemens S7 CPU (ISO-on-TCP, port 102). The protocol runs in Rust inside
|
|
3
|
+
the Node-RED process, one thread per connection.</p>
|
|
4
|
+
<h3>Details</h3>
|
|
5
|
+
<h4>Protocol</h4>
|
|
6
|
+
<p><code>S7comm</code> is the classic protocol: all S7 families and LOGO!, absolute addresses.
|
|
7
|
+
S7-1200/1500 need PUT/GET access enabled, and the DBs must be not optimized.</p>
|
|
8
|
+
<p><code>S7CommPlus</code> is the S7-1200/1500 protocol: symbolic access to optimized DBs
|
|
9
|
+
(<code>"DB_Opt".mat[1,2]</code>), PLC tag names (<code>Test_Int</code>), absolute I/Q/M/DB
|
|
10
|
+
addresses too, optional TLS and password (legitimation).</p>
|
|
11
|
+
<h4>Classic connection</h4>
|
|
12
|
+
<p>Rack, slot and link type come from the CPU family when they are empty
|
|
13
|
+
(S7-300: slot 2; S7-1200/1500: slot 1). With TSAPs set, rack and slot are not used.</p>
|
|
14
|
+
<p>Gap: tags in the same area and DB that are closer than N bytes are read as one block.</p>
|
|
15
|
+
<h4>S7CommPlus</h4>
|
|
16
|
+
<p>Symbols are resolved once per session (a reconnect resolves them again). The PLC
|
|
17
|
+
certificate is not verified. The password is stored as a Node-RED credential.</p>
|
|
18
|
+
<h4>Log</h4>
|
|
19
|
+
<p>Messages go to the Node-RED log. <code>trace</code> logs every frame in hex.</p>
|
|
20
|
+
<h3>References</h3>
|
|
21
|
+
<ul>
|
|
22
|
+
<li><a href="https://github.com/oriolrius/node-red-contrib-s7-rust#readme">README</a> - addresses and types</li>
|
|
23
|
+
</ul>
|
|
24
|
+
</script>
|