node-red-contrib-s7-rust 0.0.0-stage → 1.0.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.
@@ -0,0 +1,51 @@
1
+ {
2
+ "s7rs-endpoint": {
3
+ "label": {
4
+ "name": "Name",
5
+ "protocol": "Protocol",
6
+ "host": "Host",
7
+ "port": "Port",
8
+ "cpu": "CPU",
9
+ "rackSlot": "Rack / Slot",
10
+ "link": "Link type",
11
+ "tsaps": "TSAP local / remote",
12
+ "pdu": "PDU",
13
+ "timeout": "Timeout (ms)",
14
+ "attempts": "Attempts",
15
+ "reconnect": "Reconnect (ms)",
16
+ "gap": "Gap (bytes)",
17
+ "byteOrder": "Byte order",
18
+ "remoteTsap": "Remote TSAP",
19
+ "tls": "TLS 1.3 (S7-1500 ≥ V2.9, S7-1200 ≥ V4.5)",
20
+ "username": "User",
21
+ "password": "Password",
22
+ "logLevel": "Log"
23
+ },
24
+ "protocol": {
25
+ "s7": "S7comm (classic, PUT/GET)",
26
+ "s7plus": "S7CommPlus (S7-1200/1500, symbols)"
27
+ },
28
+ "link": {
29
+ "default": "CPU default",
30
+ "basic": "S7 Basic / other"
31
+ },
32
+ "byteOrder": {
33
+ "big": "Big endian (S7 default)",
34
+ "little": "Little endian"
35
+ },
36
+ "logLevel": {
37
+ "trace": "trace (frames)"
38
+ },
39
+ "placeholder": {
40
+ "default": "default",
41
+ "username": "empty: legacy password only",
42
+ "password": "empty: no legitimation"
43
+ },
44
+ "hint": {
45
+ "gap": "merge reads closer than N bytes; -1 = off"
46
+ },
47
+ "errors": {
48
+ "open": "S7 __host__: __error__"
49
+ }
50
+ }
51
+ }
@@ -0,0 +1,38 @@
1
+ <script type="text/html" data-help-name="s7rs-info">
2
+ <p>Diagnostics and browsing of the PLC when a message arrives.</p>
3
+ <h3>Inputs</h3>
4
+ <dl class="message-properties">
5
+ <dt class="optional">action <span class="property-type">string</span></dt>
6
+ <dd>overrides the action of the node.</dd>
7
+ <dt class="optional">payload <span class="property-type">object</span></dt>
8
+ <dd>arguments of the action. If it is not an object, the node uses its Args.</dd>
9
+ </dl>
10
+ <h3>Outputs</h3>
11
+ <dl class="message-properties">
12
+ <dt>payload <span class="property-type">object | array</span></dt>
13
+ <dd>the result of the action.</dd>
14
+ <dt>s7 <span class="property-type">object</span></dt>
15
+ <dd><code>action</code> and <code>ms</code>.</dd>
16
+ </dl>
17
+ <h3>Details</h3>
18
+ <h4>Both protocols</h4>
19
+ <ul>
20
+ <li><code>status</code>: connection state, requests, errors, latency percentiles</li>
21
+ <li><code>cpuState</code>: RUN or STOP</li>
22
+ </ul>
23
+ <h4>S7comm</h4>
24
+ <ul>
25
+ <li><code>identify</code>: order number, firmware, module names</li>
26
+ <li><code>szlList</code>, <code>szl {id, index}</code>: system status lists (raw data in hex)</li>
27
+ <li><code>clock</code>, <code>setClock {time?}</code>, <code>syncClock {thresholdMs}</code>: PLC clock</li>
28
+ <li><code>blocks</code>, <code>listBlocks {type}</code>, <code>blockInfo {type, number}</code></li>
29
+ </ul>
30
+ <h4>S7CommPlus</h4>
31
+ <ul>
32
+ <li><code>info</code>: session, PAOM string, limits</li>
33
+ <li><code>dbs</code>: data blocks; <code>browse</code>: all symbols with their types</li>
34
+ <li><code>alarms {all}</code>: active alarms; <code>limits</code>: system limits</li>
35
+ </ul>
36
+ <p><code>setClock</code> and <code>syncClock</code> change the PLC clock. STOP/START of the CPU and
37
+ block upload are not available.</p>
38
+ </script>
@@ -0,0 +1,18 @@
1
+ {
2
+ "s7rs-info": {
3
+ "label": {
4
+ "name": "Name",
5
+ "endpoint": "PLC",
6
+ "action": "Action",
7
+ "args": "Args (JSON)"
8
+ },
9
+ "group": {
10
+ "both": "both protocols"
11
+ },
12
+ "errors": {
13
+ "noEndpoint": "no S7 endpoint configured",
14
+ "noAction": "no action: set it in the node or in msg.action",
15
+ "args": "args: __error__"
16
+ }
17
+ }
18
+ }
@@ -0,0 +1,33 @@
1
+ <script type="text/html" data-help-name="s7rs-read">
2
+ <p>Reads S7 tags when a message arrives.</p>
3
+ <h3>Inputs</h3>
4
+ <dl class="message-properties">
5
+ <dt class="optional">tags <span class="property-type">array</span></dt>
6
+ <dd>addresses to read, if the node has no tags. Also <code>msg.payload</code> as an array,
7
+ or one address in <code>msg.topic</code>.</dd>
8
+ </dl>
9
+ <h3>Outputs</h3>
10
+ <dl class="message-properties">
11
+ <dt>payload <span class="property-type">any | object | array</span></dt>
12
+ <dd>the value (one tag), <code>{tag: value}</code> or an array, as set in Output.
13
+ A bad tag gives <code>null</code>.</dd>
14
+ <dt>s7 <span class="property-type">object</span></dt>
15
+ <dd><code>values</code> (tag, value, quality, error), <code>errors</code> ({tag: text},
16
+ only when a tag is bad) and <code>ms</code> (duration).</dd>
17
+ </dl>
18
+ <h3>Details</h3>
19
+ <p>All tags go in as few S7 requests as the PDU allows. Times are ISO strings. 64-bit
20
+ integers outside ±2<sup>53</sup> are strings.</p>
21
+ <h4>Addresses</h4>
22
+ <ul>
23
+ <li>Siemens: <code>DB1.DBW2:int</code>, <code>MD12:real</code>, <code>M0.1</code>,
24
+ <code>MW120[4]</code>, <code>DB1.DBB10:string[20]</code></li>
25
+ <li>nodes7: <code>DB1,REAL4</code>, <code>MI14</code></li>
26
+ <li>S7CommPlus only, symbols: <code>"DB_Opt".st.x</code>, <code>Test_Int</code>, or any
27
+ name with the <code>sym:</code> prefix</li>
28
+ </ul>
29
+ <h3>References</h3>
30
+ <ul>
31
+ <li><a href="https://github.com/oriolrius/node-red-contrib-s7-rust#addresses">Address syntax and types</a></li>
32
+ </ul>
33
+ </script>
@@ -0,0 +1,27 @@
1
+ {
2
+ "s7rs-read": {
3
+ "label": {
4
+ "name": "Name",
5
+ "endpoint": "PLC",
6
+ "tags": "Tags",
7
+ "output": "Output"
8
+ },
9
+ "placeholder": {
10
+ "tags": "one address per line, e.g.\nDB1.DBW2:int\nMD12:real"
11
+ },
12
+ "output": {
13
+ "auto": "value (1 tag) or object",
14
+ "object": "object {tag: value}",
15
+ "array": "array of values"
16
+ },
17
+ "portLabel": {
18
+ "in": "trigger",
19
+ "out": "values"
20
+ },
21
+ "errors": {
22
+ "noEndpoint": "no S7 endpoint configured",
23
+ "noTags": "no tags: set them in the node, in msg.tags or in msg.topic",
24
+ "badTags": "bad tags: __tags__"
25
+ }
26
+ }
27
+ }
@@ -0,0 +1,24 @@
1
+ <script type="text/html" data-help-name="s7rs-watch">
2
+ <p>Reads tags at a fixed interval and sends a message when a value changes.</p>
3
+ <h3>Outputs</h3>
4
+ <dl class="message-properties">
5
+ <dt>payload <span class="property-type">any | object</span></dt>
6
+ <dd>one message per tag: the value, and <code>msg.topic</code> is the tag.
7
+ One message with all tags: <code>{tag: value}</code> with the last value of each tag.</dd>
8
+ <dt>quality <span class="property-type">string | object</span></dt>
9
+ <dd><code>good</code> or <code>bad</code>. A bad tag also has <code>msg.error</code>.</dd>
10
+ <dt class="optional">changed <span class="property-type">array</span></dt>
11
+ <dd>all tags mode only: the tags that changed in this cycle.</dd>
12
+ <dt>ts <span class="property-type">string</span></dt>
13
+ <dd>start time of the read cycle (ISO, UTC).</dd>
14
+ </dl>
15
+ <h3>Details</h3>
16
+ <p>A Rust thread reads the tags and compares the raw PLC bytes. Node-RED gets no message when
17
+ nothing changed. A change of quality (for example, the PLC goes away) is also a change.</p>
18
+ <p>The first cycle sends all tags. Clear "send only values that changed" to send every cycle.</p>
19
+ <h4>Fast while</h4>
20
+ <p>Conditional rate: while this tag is true (or not zero), the node reads every <code>N</code> ms
21
+ instead of the interval. The node does not send the condition tag, unless it is also in the list.</p>
22
+ <h4>S7CommPlus</h4>
23
+ <p>The watch uses polling, not PLC subscriptions.</p>
24
+ </script>
@@ -0,0 +1,32 @@
1
+ {
2
+ "s7rs-watch": {
3
+ "label": {
4
+ "name": "Name",
5
+ "endpoint": "PLC",
6
+ "tags": "Tags",
7
+ "interval": "Interval (ms)",
8
+ "condTag": "Fast while",
9
+ "onChange": "send only values that changed",
10
+ "output": "Output",
11
+ "topic": "Topic"
12
+ },
13
+ "placeholder": {
14
+ "tags": "one address per line",
15
+ "condTag": "optional condition tag, e.g. M0.0",
16
+ "topic": "object mode only"
17
+ },
18
+ "output": {
19
+ "each": "one message per tag (topic = tag)",
20
+ "object": "one message with all tags"
21
+ },
22
+ "portLabel": {
23
+ "out": "changes"
24
+ },
25
+ "status": {
26
+ "noTags": "no tags"
27
+ },
28
+ "errors": {
29
+ "subscribe": "subscribe: __error__"
30
+ }
31
+ }
32
+ }
@@ -0,0 +1,21 @@
1
+ <script type="text/html" data-help-name="s7rs-write">
2
+ <p>Writes S7 tags. Several tags go in one request.</p>
3
+ <h3>Inputs</h3>
4
+ <dl class="message-properties">
5
+ <dt>payload <span class="property-type">any | object</span></dt>
6
+ <dd>the value for the node's tag (or for <code>msg.topic</code>), or <code>{tag: value, …}</code>
7
+ to write several tags. Arrays for <code>[n]</code> tags. Dates as Date or ISO string.</dd>
8
+ <dt class="optional">topic <span class="property-type">string</span></dt>
9
+ <dd>the tag, when the node has no tag and the payload is not an object.</dd>
10
+ </dl>
11
+ <h3>Outputs</h3>
12
+ <dl class="message-properties">
13
+ <dt>s7 <span class="property-type">object</span></dt>
14
+ <dd><code>results</code> (tag, error) and <code>ms</code>.</dd>
15
+ </dl>
16
+ <h3>Details</h3>
17
+ <p>The message goes out only when all writes are good. Otherwise the node raises an error:
18
+ use a Catch node.</p>
19
+ <p>S7CommPlus: a single bit (for example <code>M20.1</code>) cannot be written through absolute
20
+ access. Use a symbol for it.</p>
21
+ </script>
@@ -0,0 +1,17 @@
1
+ {
2
+ "s7rs-write": {
3
+ "label": {
4
+ "name": "Name",
5
+ "endpoint": "PLC",
6
+ "tag": "Tag"
7
+ },
8
+ "placeholder": {
9
+ "tag": "empty: msg.topic or msg.payload object"
10
+ },
11
+ "errors": {
12
+ "noEndpoint": "no S7 endpoint configured",
13
+ "nothing": "nothing to write: set a tag, msg.topic, or send msg.payload = {tag: value}",
14
+ "failed": "write failed: __tags__"
15
+ }
16
+ }
17
+ }
@@ -0,0 +1,150 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('s7rs-endpoint', {
3
+ category: 'config',
4
+ defaults: {
5
+ name: { value: '' },
6
+ protocol: { value: 's7' },
7
+ host: { value: '', required: true },
8
+ port: { value: 102, required: true, validate: RED.validators.number() },
9
+ cpu: { value: '1200' },
10
+ rack: { value: '' },
11
+ slot: { value: '' },
12
+ link: { value: '' },
13
+ localTsap: { value: '' },
14
+ remoteTsap: { value: '' },
15
+ pdu: { value: '' },
16
+ timeout: { value: 5000, validate: RED.validators.number() },
17
+ attempts: { value: 3, validate: RED.validators.number() },
18
+ reconnect: { value: 5000, validate: RED.validators.number() },
19
+ gap: { value: 5 },
20
+ byteOrder: { value: 'big' },
21
+ logLevel: { value: 'warn' },
22
+ tsap: { value: 'SIMATIC-ROOT-HMI' },
23
+ tls: { value: false },
24
+ username: { value: '' },
25
+ },
26
+ credentials: {
27
+ password: { type: 'password' },
28
+ },
29
+ label: function () {
30
+ const p = this.protocol === 's7plus' ? 'S7+' : `S7-${this.cpu}`;
31
+ return this.name || (this.host ? `${p} ${this.host}:${this.port || 102}` : 's7 endpoint');
32
+ },
33
+ oneditprepare: function () {
34
+ const sync = () => {
35
+ const plus = $('#node-config-input-protocol').val() === 's7plus';
36
+ $('.s7rs-classic').toggle(!plus);
37
+ $('.s7rs-plus').toggle(plus);
38
+ };
39
+ $('#node-config-input-protocol').on('change', sync);
40
+ sync();
41
+ },
42
+ });
43
+ </script>
44
+
45
+ <script type="text/html" data-template-name="s7rs-endpoint">
46
+ <div class="form-row">
47
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> <span data-i18n="s7rs-endpoint.label.name"></span></label>
48
+ <input type="text" id="node-config-input-name" />
49
+ </div>
50
+ <div class="form-row">
51
+ <label for="node-config-input-protocol"><i class="fa fa-exchange"></i> <span data-i18n="s7rs-endpoint.label.protocol"></span></label>
52
+ <select id="node-config-input-protocol">
53
+ <option value="s7" data-i18n="s7rs-endpoint.protocol.s7"></option>
54
+ <option value="s7plus" data-i18n="s7rs-endpoint.protocol.s7plus"></option>
55
+ </select>
56
+ </div>
57
+ <div class="form-row">
58
+ <label for="node-config-input-host"><i class="fa fa-server"></i> <span data-i18n="s7rs-endpoint.label.host"></span></label>
59
+ <input type="text" id="node-config-input-host" placeholder="192.168.0.1" style="width: 45%" />
60
+ <label for="node-config-input-port" style="width: auto; margin-left: 10px" data-i18n="s7rs-endpoint.label.port"></label>
61
+ <input type="text" id="node-config-input-port" style="width: 70px" />
62
+ </div>
63
+ <div class="form-row s7rs-classic">
64
+ <label for="node-config-input-cpu"><i class="fa fa-microchip"></i> <span data-i18n="s7rs-endpoint.label.cpu"></span></label>
65
+ <select id="node-config-input-cpu">
66
+ <option value="1200">S7-1200</option>
67
+ <option value="1500">S7-1500</option>
68
+ <option value="300">S7-300</option>
69
+ <option value="400">S7-400</option>
70
+ <option value="200">S7-200</option>
71
+ <option value="logo">LOGO! 8</option>
72
+ <option value="logo-tsap">LOGO! 8 (TSAP 10.00 / 20.00)</option>
73
+ </select>
74
+ </div>
75
+ <div class="form-row s7rs-classic">
76
+ <label for="node-config-input-rack" data-i18n="s7rs-endpoint.label.rackSlot"></label>
77
+ <input type="text" id="node-config-input-rack" data-i18n="[placeholder]s7rs-endpoint.placeholder.default" style="width: 80px" />
78
+ <input type="text" id="node-config-input-slot" data-i18n="[placeholder]s7rs-endpoint.placeholder.default" style="width: 80px" />
79
+ </div>
80
+ <div class="form-row s7rs-classic">
81
+ <label for="node-config-input-link" data-i18n="s7rs-endpoint.label.link"></label>
82
+ <select id="node-config-input-link">
83
+ <option value="" data-i18n="s7rs-endpoint.link.default"></option>
84
+ <option value="1">PG</option>
85
+ <option value="2">OP</option>
86
+ <option value="3" data-i18n="s7rs-endpoint.link.basic"></option>
87
+ </select>
88
+ </div>
89
+ <div class="form-row s7rs-classic">
90
+ <label for="node-config-input-localTsap" data-i18n="s7rs-endpoint.label.tsaps"></label>
91
+ <input type="text" id="node-config-input-localTsap" placeholder="10.00" style="width: 100px" />
92
+ <input type="text" id="node-config-input-remoteTsap" placeholder="20.00" style="width: 100px" />
93
+ </div>
94
+ <div class="form-row s7rs-classic">
95
+ <label for="node-config-input-pdu" data-i18n="s7rs-endpoint.label.pdu"></label>
96
+ <input type="text" id="node-config-input-pdu" placeholder="960" style="width: 100px" />
97
+ </div>
98
+ <div class="form-row">
99
+ <label for="node-config-input-timeout" data-i18n="s7rs-endpoint.label.timeout"></label>
100
+ <input type="text" id="node-config-input-timeout" style="width: 80px" />
101
+ <label for="node-config-input-attempts" style="width: auto; margin-left: 10px" data-i18n="s7rs-endpoint.label.attempts"></label>
102
+ <input type="text" id="node-config-input-attempts" style="width: 50px" />
103
+ </div>
104
+ <div class="form-row">
105
+ <label for="node-config-input-reconnect" data-i18n="s7rs-endpoint.label.reconnect"></label>
106
+ <input type="text" id="node-config-input-reconnect" style="width: 80px" />
107
+ </div>
108
+ <div class="form-row s7rs-classic">
109
+ <label for="node-config-input-gap" data-i18n="s7rs-endpoint.label.gap"></label>
110
+ <input type="text" id="node-config-input-gap" style="width: 80px" />
111
+ <span style="opacity: 0.7" data-i18n="s7rs-endpoint.hint.gap"></span>
112
+ </div>
113
+ <div class="form-row s7rs-classic">
114
+ <label for="node-config-input-byteOrder" data-i18n="s7rs-endpoint.label.byteOrder"></label>
115
+ <select id="node-config-input-byteOrder">
116
+ <option value="big" data-i18n="s7rs-endpoint.byteOrder.big"></option>
117
+ <option value="little" data-i18n="s7rs-endpoint.byteOrder.little"></option>
118
+ </select>
119
+ </div>
120
+ <div class="form-row s7rs-plus">
121
+ <label for="node-config-input-tsap" data-i18n="s7rs-endpoint.label.remoteTsap"></label>
122
+ <select id="node-config-input-tsap">
123
+ <option value="SIMATIC-ROOT-HMI">SIMATIC-ROOT-HMI</option>
124
+ <option value="SIMATIC-ROOT-ES">SIMATIC-ROOT-ES</option>
125
+ </select>
126
+ </div>
127
+ <div class="form-row s7rs-plus">
128
+ <label for="node-config-input-tls">&nbsp;</label>
129
+ <input type="checkbox" id="node-config-input-tls" style="width: auto; display: inline-block" />
130
+ <span data-i18n="s7rs-endpoint.label.tls"></span>
131
+ </div>
132
+ <div class="form-row s7rs-plus">
133
+ <label for="node-config-input-username"><i class="fa fa-user"></i> <span data-i18n="s7rs-endpoint.label.username"></span></label>
134
+ <input type="text" id="node-config-input-username" data-i18n="[placeholder]s7rs-endpoint.placeholder.username" />
135
+ </div>
136
+ <div class="form-row s7rs-plus">
137
+ <label for="node-config-input-password"><i class="fa fa-lock"></i> <span data-i18n="s7rs-endpoint.label.password"></span></label>
138
+ <input type="password" id="node-config-input-password" data-i18n="[placeholder]s7rs-endpoint.placeholder.password" />
139
+ </div>
140
+ <div class="form-row">
141
+ <label for="node-config-input-logLevel" data-i18n="s7rs-endpoint.label.logLevel"></label>
142
+ <select id="node-config-input-logLevel">
143
+ <option value="error">error</option>
144
+ <option value="warn">warn</option>
145
+ <option value="info">info</option>
146
+ <option value="debug">debug</option>
147
+ <option value="trace" data-i18n="s7rs-endpoint.logLevel.trace"></option>
148
+ </select>
149
+ </div>
150
+ </script>
@@ -0,0 +1,122 @@
1
+ 'use strict';
2
+ // s7rs-endpoint: a connection to one Siemens S7 CPU (config node). The protocol
3
+ // runs in Rust inside this process (lib/napi-bridge.js); the flow nodes only
4
+ // route messages.
5
+
6
+ const { acquire, release } = require('../lib/napi-bridge');
7
+ const { text, intOrUndef } = require('../lib/common');
8
+
9
+ // Diagnostics the editor may call through the admin API (read-only actions).
10
+ const ADMIN_ACTIONS = new Set([
11
+ 'identify', 'cpuState', 'szl', 'szlList', 'clock', 'blocks', 'listBlocks', 'blockInfo',
12
+ 'info', 'limits', 'dbs', 'browse', 'alarms',
13
+ ]);
14
+
15
+ module.exports = function (RED) {
16
+ const hooked = new WeakSet();
17
+
18
+ function hookLogs(bridge) {
19
+ if (hooked.has(bridge)) return;
20
+ hooked.add(bridge);
21
+ bridge.on('log', (level, text) => {
22
+ const f = { error: 'error', warn: 'warn', debug: 'debug', trace: 'trace' }[level] || 'info';
23
+ RED.log[f](`[s7rs] ${text}`);
24
+ });
25
+ }
26
+
27
+ function S7rsEndpoint(config) {
28
+ RED.nodes.createNode(this, config);
29
+ const node = this;
30
+ node.bridge = acquire();
31
+ hookLogs(node.bridge);
32
+ node.state = { state: 'connecting', detail: '' };
33
+ node.setMaxListeners(0);
34
+
35
+ const onStatus = (ev) => {
36
+ if (ev.conn !== node.id) return;
37
+ node.state = ev;
38
+ node.emit('s7status', ev);
39
+ };
40
+ node.bridge.on('status', onStatus);
41
+
42
+ const creds = node.credentials || {};
43
+ const params = {
44
+ conn: node.id,
45
+ protocol: config.protocol || 's7',
46
+ logLevel: config.logLevel || 'warn',
47
+ tsap: config.tsap || '',
48
+ tls: config.tls === true || config.tls === 'true',
49
+ username: config.username || '',
50
+ password: creds.password || '',
51
+ host: String(config.host || '').trim(),
52
+ port: intOrUndef(config.port) || 102,
53
+ cpu: config.cpu || '',
54
+ rack: intOrUndef(config.rack),
55
+ slot: intOrUndef(config.slot),
56
+ link: intOrUndef(config.link),
57
+ localTsap: config.localTsap || '',
58
+ remoteTsap: config.remoteTsap || '',
59
+ pdu: intOrUndef(config.pdu) || 0,
60
+ timeoutMs: intOrUndef(config.timeout) || 5000,
61
+ attempts: intOrUndef(config.attempts) || 0,
62
+ reconnectMs: intOrUndef(config.reconnect) || 5000,
63
+ gap: intOrUndef(config.gap),
64
+ littleEndian: config.byteOrder === 'little',
65
+ };
66
+ node.ready = node.bridge.open(params).catch((err) => {
67
+ node.error(text(RED, 's7rs-endpoint.errors.open', { host: params.host, error: err.message }));
68
+ node.state = { state: 'faulted', detail: err.message };
69
+ node.emit('s7status', node.state);
70
+ throw err;
71
+ });
72
+ node.ready.catch(() => {}); // flow nodes await it and report the error themselves
73
+
74
+ node.on('close', (removed, done) => {
75
+ node.bridge.off('status', onStatus);
76
+ node.bridge
77
+ .close(node.id)
78
+ .catch(() => {})
79
+ .finally(() => {
80
+ release();
81
+ done();
82
+ });
83
+ });
84
+ }
85
+ RED.nodes.registerType('s7rs-endpoint', S7rsEndpoint, { credentials: { password: { type: 'password' } } });
86
+
87
+ // Admin API for the editor:
88
+ // GET /s7rs/status/:id connection diagnostics
89
+ // GET /s7rs/call/:id/:action a read-only info action (e.g. browse = S7CommPlus symbols)
90
+ const deployed = (req, res) => {
91
+ const ep = RED.nodes.getNode(req.params.id);
92
+ if (!ep || !ep.bridge) {
93
+ res.status(404).json({ error: 'endpoint not deployed' });
94
+ return null;
95
+ }
96
+ return ep;
97
+ };
98
+
99
+ RED.httpAdmin.get('/s7rs/call/:id/:action', RED.auth.needsPermission('s7rs-endpoint.read'), async (req, res) => {
100
+ const ep = deployed(req, res);
101
+ if (!ep) return;
102
+ const { action } = req.params;
103
+ if (!ADMIN_ACTIONS.has(action)) return res.status(400).json({ error: `action ${action} not allowed here` });
104
+ try {
105
+ await ep.ready;
106
+ const args = req.query && Object.keys(req.query).length ? req.query : undefined;
107
+ res.json(await ep.bridge.call(ep.id, action, args));
108
+ } catch (err) {
109
+ res.status(500).json({ error: err.message });
110
+ }
111
+ });
112
+
113
+ RED.httpAdmin.get('/s7rs/status/:id', RED.auth.needsPermission('s7rs-endpoint.read'), async (req, res) => {
114
+ const ep = deployed(req, res);
115
+ if (!ep) return;
116
+ try {
117
+ res.json(await ep.bridge.status(ep.id));
118
+ } catch (err) {
119
+ res.status(500).json({ error: err.message });
120
+ }
121
+ });
122
+ };
@@ -0,0 +1,72 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('s7rs-info', {
3
+ category: 'network',
4
+ color: '#3FADB5',
5
+ defaults: {
6
+ name: { value: '' },
7
+ endpoint: { value: '', type: 's7rs-endpoint', required: true },
8
+ action: { value: 'cpuState' },
9
+ args: {
10
+ value: '',
11
+ validate: function (v) {
12
+ if (!v) return true;
13
+ try {
14
+ JSON.parse(v);
15
+ return true;
16
+ } catch (e) {
17
+ return false;
18
+ }
19
+ },
20
+ },
21
+ },
22
+ inputs: 1,
23
+ outputs: 1,
24
+ icon: 's7rs-plc.svg',
25
+ paletteLabel: 's7 info',
26
+ label: function () {
27
+ return this.name || `s7 ${this.action || 'info'}`;
28
+ },
29
+ });
30
+ </script>
31
+
32
+ <script type="text/html" data-template-name="s7rs-info">
33
+ <div class="form-row">
34
+ <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="s7rs-info.label.name"></span></label>
35
+ <input type="text" id="node-input-name" />
36
+ </div>
37
+ <div class="form-row">
38
+ <label for="node-input-endpoint"><i class="fa fa-plug"></i> <span data-i18n="s7rs-info.label.endpoint"></span></label>
39
+ <input type="text" id="node-input-endpoint" />
40
+ </div>
41
+ <div class="form-row">
42
+ <label for="node-input-action"><i class="fa fa-cog"></i> <span data-i18n="s7rs-info.label.action"></span></label>
43
+ <select id="node-input-action">
44
+ <optgroup data-i18n="[label]s7rs-info.group.both">
45
+ <option value="status">status</option>
46
+ <option value="cpuState">cpuState (RUN / STOP)</option>
47
+ </optgroup>
48
+ <optgroup label="S7comm">
49
+ <option value="identify">identify</option>
50
+ <option value="szlList">szlList</option>
51
+ <option value="szl">szl {id, index}</option>
52
+ <option value="clock">clock</option>
53
+ <option value="setClock">setClock {time?}</option>
54
+ <option value="syncClock">syncClock {thresholdMs}</option>
55
+ <option value="blocks">blocks</option>
56
+ <option value="listBlocks">listBlocks {type}</option>
57
+ <option value="blockInfo">blockInfo {type, number}</option>
58
+ </optgroup>
59
+ <optgroup label="S7CommPlus">
60
+ <option value="info">info</option>
61
+ <option value="dbs">dbs</option>
62
+ <option value="browse">browse</option>
63
+ <option value="alarms">alarms {all}</option>
64
+ <option value="limits">limits</option>
65
+ </optgroup>
66
+ </select>
67
+ </div>
68
+ <div class="form-row">
69
+ <label for="node-input-args" data-i18n="s7rs-info.label.args"></label>
70
+ <input type="text" id="node-input-args" placeholder='{"id": "0x0011"}' style="font-family: monospace" />
71
+ </div>
72
+ </script>
@@ -0,0 +1,38 @@
1
+ 'use strict';
2
+ // s7rs-info: diagnostics and browsing. msg.action (or the node's action), args in
3
+ // msg.payload when it is an object (or the node's JSON args). Result in msg.payload.
4
+
5
+ const { text, isPlainObject, elapsedMs, getEndpoint } = require('../lib/common');
6
+
7
+ module.exports = function (RED) {
8
+ function S7rsInfo(config) {
9
+ RED.nodes.createNode(this, config);
10
+ const node = this;
11
+ const ep = getEndpoint(RED, node, config);
12
+ let cfgArgs;
13
+ try {
14
+ cfgArgs = config.args ? JSON.parse(config.args) : undefined;
15
+ } catch (err) {
16
+ node.error(text(RED, 's7rs-info.errors.args', { error: err.message }));
17
+ }
18
+
19
+ node.on('input', async (msg, send, done) => {
20
+ if (!ep) return done(new Error(text(RED, 's7rs-info.errors.noEndpoint')));
21
+ const action = String(msg.action || config.action || '').trim();
22
+ if (!action) return done(new Error(text(RED, 's7rs-info.errors.noAction')));
23
+ const args = isPlainObject(msg.payload) ? msg.payload : cfgArgs;
24
+ try {
25
+ await ep.ready;
26
+ const t0 = process.hrtime.bigint();
27
+ msg.payload = action === 'status' ? await ep.bridge.status(ep.id) : await ep.bridge.call(ep.id, action, args);
28
+ msg.topic = msg.topic || action;
29
+ msg.s7 = { action, ms: elapsedMs(t0) };
30
+ send(msg);
31
+ done();
32
+ } catch (err) {
33
+ done(err);
34
+ }
35
+ });
36
+ }
37
+ RED.nodes.registerType('s7rs-info', S7rsInfo);
38
+ };