@ali-pay/node-red-contrib-s7 3.2.1 → 3.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/README.md +70 -0
- package/package.json +5 -2
- package/red/locales/de/s7.json +2 -0
- package/red/locales/en-US/s7.json +2 -0
- package/red/locales/zh-CN/s7.json +129 -0
- package/red/s7.html +35 -3
- package/red/s7.js +95 -66
package/README.md
CHANGED
|
@@ -5,6 +5,76 @@ This node was created as part of the [ST-One](https://st-one.io) project.
|
|
|
5
5
|
|
|
6
6
|
## 更新日志
|
|
7
7
|
|
|
8
|
+
### v3.3.1
|
|
9
|
+
|
|
10
|
+
- `s7-out` 节点重构 `失败重写` 的逻辑
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
改前:写入数据->读取最新值->判断是否成功->等待间隔时间->重试写入数据
|
|
14
|
+
改后:写入数据->等待间隔时间->读取最新值->判断是否成功->重试写入数据
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
- `s7-out` 节点重构 `async-await` 的语法
|
|
18
|
+
|
|
19
|
+
### v3.3.0
|
|
20
|
+
|
|
21
|
+
- `s7-endpoint` 节点增加 `失败重写` 的配置
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
注意!
|
|
25
|
+
应与PLC开发人员约定:如果要重置数据,则数据必须至少保留3秒再重置,避免写入数据后立即重置而导致程序认为写入数据失败
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
失败重写次数:该参数大于0时,当写入数据失败后,会自动重试写入数据,直至达到该次数限制
|
|
30
|
+
|
|
31
|
+
失败重写间隔:重试写入数据的时间间隔
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
- `s7-in` 节点重构 `msg` 的输出
|
|
35
|
+
|
|
36
|
+
```json
|
|
37
|
+
{
|
|
38
|
+
// s7 节点参数
|
|
39
|
+
"_s7": {
|
|
40
|
+
"plc": "7#-8#", // plc 名称
|
|
41
|
+
"ip": "172.19.21.70", // plc ip
|
|
42
|
+
"status": "在线", // plc 状态 [在线/离线]
|
|
43
|
+
"time": "2024-07-26T02:15:38.128Z" // msg 消息时间
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
// msg 消息内容
|
|
47
|
+
"payload": { "a":1, "b":2 }
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
- `s7-out` 节点重构 `msg` 的输出
|
|
52
|
+
|
|
53
|
+
```json
|
|
54
|
+
{
|
|
55
|
+
// s7 节点参数
|
|
56
|
+
"_s7": {
|
|
57
|
+
"plc": "7#-8#", // plc 名称
|
|
58
|
+
"ip": "172.19.21.70", // plc ip
|
|
59
|
+
"status": "在线", // plc 状态 [在线/离线]
|
|
60
|
+
"time": "2024-07-26T02:15:38.128Z" // msg 消息时间
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
// msg 消息内容
|
|
64
|
+
"payload": {
|
|
65
|
+
"variable": ["a", "b"], // 写入的键 msg.variable
|
|
66
|
+
"payload": [1, 2], // 写入的值 msg.payload
|
|
67
|
+
"values": { "a":1, "b":2 }, // 写入的键值对
|
|
68
|
+
"newValues": {}, // plc的最新键值对
|
|
69
|
+
"wrongValues": {}, // 跟写入值不一致的键值对
|
|
70
|
+
"bingo": false, // 是否写入成功 [plc的最新值跟写入值是否一致]
|
|
71
|
+
"error": "Error: Not connected" // 错误
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
- `s7` 所有节点增加 `简体中文` 的翻译
|
|
77
|
+
|
|
8
78
|
### v3.2.0
|
|
9
79
|
|
|
10
80
|
- `s7-in` 节点增加 `设备状态` 的输出
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ali-pay/node-red-contrib-s7",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.3.1",
|
|
4
4
|
"description": "A Node-RED node to interact with Siemens S7 PLCs",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"dependencies": {
|
|
@@ -14,7 +14,10 @@
|
|
|
14
14
|
"url": "https://github.com/ali-pay/node-red-contrib-s7/issues"
|
|
15
15
|
},
|
|
16
16
|
"scripts": {
|
|
17
|
-
"
|
|
17
|
+
"publish": "npm publish"
|
|
18
|
+
},
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"registry": "https://registry.npmjs.org"
|
|
18
21
|
},
|
|
19
22
|
"keywords": [
|
|
20
23
|
"hardware",
|
package/red/locales/de/s7.json
CHANGED
|
@@ -48,6 +48,8 @@
|
|
|
48
48
|
"tsap-remote": "Remote TSAP",
|
|
49
49
|
"cycletime": "Zykluszeit",
|
|
50
50
|
"timeout": "Timeout",
|
|
51
|
+
"rewritetimes": "Rewrite Times",
|
|
52
|
+
"rewriteinterval": "Rewrite Interval",
|
|
51
53
|
"verbose": "Debug",
|
|
52
54
|
"adapter": "Adapter",
|
|
53
55
|
"adapterplaceholder": "Bitte 'node-red-contrib-mpi-s7' installieren",
|
|
@@ -48,6 +48,8 @@
|
|
|
48
48
|
"tsap-remote": "Remote TSAP",
|
|
49
49
|
"cycletime": "Cycle time",
|
|
50
50
|
"timeout": "Timeout",
|
|
51
|
+
"rewritetimes": "Rewrite Times",
|
|
52
|
+
"rewriteinterval": "Rewrite Interval",
|
|
51
53
|
"verbose": "Debug",
|
|
52
54
|
"adapter": "Adapter",
|
|
53
55
|
"adapterplaceholder": "Please install 'node-red-contrib-mpi-s7' for MPI/PPI/DP support",
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
{
|
|
2
|
+
"s7": {
|
|
3
|
+
"in": {
|
|
4
|
+
"label": {
|
|
5
|
+
"name": "s7 in",
|
|
6
|
+
"endpoint": "PLC",
|
|
7
|
+
"mode": "输出模式",
|
|
8
|
+
"variable": "变量",
|
|
9
|
+
"diff": "当数据有变化时才输出消息",
|
|
10
|
+
"variable-select": "请选择一个变量",
|
|
11
|
+
"variable-novar": "没有变量,请先定义变量"
|
|
12
|
+
},
|
|
13
|
+
"mode": {
|
|
14
|
+
"single": "单个变量",
|
|
15
|
+
"all-split": "所有变量,每个变量皆输出为一条消息",
|
|
16
|
+
"all": "所有变量"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"out": {
|
|
20
|
+
"label": {
|
|
21
|
+
"name": "s7 out",
|
|
22
|
+
"endpoint": "PLC",
|
|
23
|
+
"variable": "变量",
|
|
24
|
+
"variable-select": "请选择一个变量"
|
|
25
|
+
},
|
|
26
|
+
"disclaimer": "生产环境中,请谨慎使用!"
|
|
27
|
+
},
|
|
28
|
+
"control": {
|
|
29
|
+
"label": {
|
|
30
|
+
"name": "s7 control",
|
|
31
|
+
"function": "Function",
|
|
32
|
+
"endpoint": "PLC"
|
|
33
|
+
},
|
|
34
|
+
"function": {
|
|
35
|
+
"cycletime": "Cycle time",
|
|
36
|
+
"trigger": "Trigger read"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"endpoint": {
|
|
40
|
+
"label": {
|
|
41
|
+
"transport": "Transport",
|
|
42
|
+
"address": "Address",
|
|
43
|
+
"port": "Port",
|
|
44
|
+
"connmode": "Mode",
|
|
45
|
+
"rack": "Rack",
|
|
46
|
+
"slot": "Slot",
|
|
47
|
+
"tsap-local": "Local TSAP",
|
|
48
|
+
"tsap-remote": "Remote TSAP",
|
|
49
|
+
"cycletime": "循环读取周期",
|
|
50
|
+
"timeout": "连接超时时间",
|
|
51
|
+
"rewritetimes": "失败重写次数",
|
|
52
|
+
"rewriteinterval": "失败重写间隔",
|
|
53
|
+
"verbose": "Debug",
|
|
54
|
+
"adapter": "Adapter",
|
|
55
|
+
"adapterplaceholder": "Please install 'node-red-contrib-mpi-s7' for MPI/PPI/DP support",
|
|
56
|
+
"busaddr": "Address",
|
|
57
|
+
"search": "Search",
|
|
58
|
+
"flash-led": "Flash LED",
|
|
59
|
+
"tabs": {
|
|
60
|
+
"connection": "连接设置",
|
|
61
|
+
"variables": "变量设置"
|
|
62
|
+
},
|
|
63
|
+
"variables": {
|
|
64
|
+
"list": "变量列表",
|
|
65
|
+
"add": "添加",
|
|
66
|
+
"clean": "删除所有",
|
|
67
|
+
"del": "删除",
|
|
68
|
+
"name": "变量名",
|
|
69
|
+
"addr": "地址",
|
|
70
|
+
"export": "导出",
|
|
71
|
+
"import": "导入"
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
"placeholder": {
|
|
75
|
+
"address": "IP Address or FQDN (plc01.example.com)"
|
|
76
|
+
},
|
|
77
|
+
"transport": {
|
|
78
|
+
"iso-on-tcp": "Ethernet (ISO-on-TCP)",
|
|
79
|
+
"mpi-s7": "MPI/PPI/DP Adapter"
|
|
80
|
+
},
|
|
81
|
+
"connmode": {
|
|
82
|
+
"rack-slot": "Rack/Slot",
|
|
83
|
+
"tsap": "TSAP"
|
|
84
|
+
},
|
|
85
|
+
"status": {
|
|
86
|
+
"online": "在线",
|
|
87
|
+
"badvalues": "失败",
|
|
88
|
+
"offline": "离线",
|
|
89
|
+
"unknown": "未知",
|
|
90
|
+
"connecting": "连接中"
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
"label": {
|
|
94
|
+
"name": "名称",
|
|
95
|
+
"select": "Select a device"
|
|
96
|
+
},
|
|
97
|
+
"error": {
|
|
98
|
+
"onconnect": "Error connecting to PLC: ",
|
|
99
|
+
"missingconfig": "Missing configuration",
|
|
100
|
+
"varunknown": "Unknown variable '__var__'",
|
|
101
|
+
"valmismatch": "Amount of variables and data mismatch",
|
|
102
|
+
"noresponse": "No response from PLC, restarting communication",
|
|
103
|
+
"invalidconntype": "Invalid configuration type \"__connmode__\"",
|
|
104
|
+
"invalidtransport": "Invalid transport \"__transport__\"",
|
|
105
|
+
"invalidtsap": "Invalid TSAP configuration",
|
|
106
|
+
"invalidtimeinterval": "Invalid time interval for cycle time of \"__interval__\"",
|
|
107
|
+
"invalidcontrolfunction": "Invalid control function \"__function__\""
|
|
108
|
+
},
|
|
109
|
+
"info": {
|
|
110
|
+
"connect": "Initiating connection to PLC",
|
|
111
|
+
"disconnect": "Closing connection to PLC",
|
|
112
|
+
"novars": "No variables configured, skipping cyclic reading",
|
|
113
|
+
"cycletimetooshort": "Cycle time too short, enforcing minimum of __min__ ms"
|
|
114
|
+
},
|
|
115
|
+
"validation": {
|
|
116
|
+
"ERR_PARSE_EMPTY": "Address cannot be empty",
|
|
117
|
+
"ERR_PARSE_UNKNOWN_FORMAT": "Unknown address format, check node's help section",
|
|
118
|
+
"ERR_PARSE_ADDR_OFFSET": "Byte offset is invalid",
|
|
119
|
+
"ERR_PARSE_DB_NUMBER": "Number of DB is unknown or invalid",
|
|
120
|
+
"ERR_PARSE_DB_DATATYPE": "Data type of DB area is unknown or invalid",
|
|
121
|
+
"ERR_PARSE_AREA": "Area addressed is unknown or invalid",
|
|
122
|
+
"ERR_PARSE_DATATYPE": "Data type is unknown or invalid",
|
|
123
|
+
"ERR_PARSE_BIT_OFFSET": "Bit offset is missing or is invalid",
|
|
124
|
+
"ERR_PARSE_STRING_LEN": "String length is missing or is invalid",
|
|
125
|
+
"ERR_PARSE_INVALID_BIT_OFFSET": "Bit offset not supported for this type",
|
|
126
|
+
"ERR_PARSE_INVALID_ARR_LEN": "Array length specified is invalid"
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
package/red/s7.html
CHANGED
|
@@ -66,6 +66,14 @@
|
|
|
66
66
|
<label for="node-config-input-timeout"><i class="fa fa-clock-o"></i> <span data-i18n="s7.endpoint.label.timeout"></span></label>
|
|
67
67
|
<input type="text" id="node-config-input-timeout" data-i18n="[placeholder]s7.endpoint.label.timeout" style="width: 60px;"> <span>ms</span>
|
|
68
68
|
</div>
|
|
69
|
+
<div class="form-row">
|
|
70
|
+
<label for="node-config-input-rewritetimes"><span data-i18n="s7.endpoint.label.rewritetimes"></span></label>
|
|
71
|
+
<input type="text" id="node-config-input-rewritetimes" style="width: 60px;">
|
|
72
|
+
</div>
|
|
73
|
+
<div class="form-row">
|
|
74
|
+
<label for="node-config-input-rewriteinterval"><span data-i18n="s7.endpoint.label.rewriteinterval"></span></label>
|
|
75
|
+
<input type="text" id="node-config-input-rewriteinterval" style="width: 60px;"> <span>ms</span>
|
|
76
|
+
</div>
|
|
69
77
|
<div class="form-row">
|
|
70
78
|
<label for="node-config-input-name"><i class="fa fa-tag"></i> <span data-i18n="s7.label.name"></span></label>
|
|
71
79
|
<input type="text" id="node-config-input-name" data-i18n="[placeholder]s7.label.name">
|
|
@@ -559,7 +567,7 @@
|
|
|
559
567
|
validate: RED.validators.number()
|
|
560
568
|
},
|
|
561
569
|
slot: {
|
|
562
|
-
value: "
|
|
570
|
+
value: "1",
|
|
563
571
|
validate: RED.validators.number()
|
|
564
572
|
},
|
|
565
573
|
localtsaphi: {
|
|
@@ -598,6 +606,12 @@
|
|
|
598
606
|
name: {
|
|
599
607
|
value: ""
|
|
600
608
|
},
|
|
609
|
+
rewritetimes: {
|
|
610
|
+
value: 0
|
|
611
|
+
},
|
|
612
|
+
rewriteinterval: {
|
|
613
|
+
value: 500
|
|
614
|
+
},
|
|
601
615
|
vartable: {
|
|
602
616
|
value: [{
|
|
603
617
|
name: "",
|
|
@@ -780,6 +794,14 @@
|
|
|
780
794
|
});
|
|
781
795
|
});
|
|
782
796
|
|
|
797
|
+
$("#node-config-input-rewritetimes").spinner({
|
|
798
|
+
min: 0
|
|
799
|
+
});
|
|
800
|
+
|
|
801
|
+
$("#node-config-input-rewriteinterval").spinner({
|
|
802
|
+
min: 0
|
|
803
|
+
});
|
|
804
|
+
|
|
783
805
|
$("#node-config-input-cycletime").spinner({
|
|
784
806
|
min: 0
|
|
785
807
|
});
|
|
@@ -877,8 +899,18 @@
|
|
|
877
899
|
connTypeList.val(self.connmode);
|
|
878
900
|
}
|
|
879
901
|
|
|
902
|
+
if (self.rewritetimes === undefined) {
|
|
903
|
+
self.rewritetimes = 0;
|
|
904
|
+
$("#node-config-input-rewritetimes").val(self.rewritetimes);
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
if (self.rewriteinterval === undefined) {
|
|
908
|
+
self.rewriteinterval = 500;
|
|
909
|
+
$("#node-config-input-rewriteinterval").val(self.rewriteinterval);
|
|
910
|
+
}
|
|
911
|
+
|
|
880
912
|
if (self.timeout === undefined) {
|
|
881
|
-
self.timeout =
|
|
913
|
+
self.timeout = 2000;
|
|
882
914
|
$("#node-config-input-timeout").val(self.timeout);
|
|
883
915
|
}
|
|
884
916
|
|
|
@@ -1127,7 +1159,7 @@
|
|
|
1127
1159
|
},
|
|
1128
1160
|
color: "#3FADB5",
|
|
1129
1161
|
inputs: 0,
|
|
1130
|
-
outputs:
|
|
1162
|
+
outputs: 1,
|
|
1131
1163
|
icon: "serial.png",
|
|
1132
1164
|
label: function () {
|
|
1133
1165
|
var self = this;
|
package/red/s7.js
CHANGED
|
@@ -349,7 +349,6 @@ module.exports = function (RED) {
|
|
|
349
349
|
|
|
350
350
|
itemGroup = new nodes7.S7ItemGroup(node.endpoint);
|
|
351
351
|
itemGroup.setTranslationCB(k => node._vars[k]);
|
|
352
|
-
node.itemGroup = itemGroup;
|
|
353
352
|
|
|
354
353
|
let varKeys = Object.keys(node._vars)
|
|
355
354
|
if (!varKeys || !varKeys.length) {
|
|
@@ -359,6 +358,10 @@ module.exports = function (RED) {
|
|
|
359
358
|
itemGroup.addItems(varKeys);
|
|
360
359
|
}
|
|
361
360
|
|
|
361
|
+
// 将字段加入s7 endpoint节点对象中
|
|
362
|
+
node.itemGroup = itemGroup;
|
|
363
|
+
node.rewritetimes = parseInt(config.rewritetimes);
|
|
364
|
+
node.rewriteinterval = parseInt(config.rewriteinterval);
|
|
362
365
|
}
|
|
363
366
|
RED.nodes.registerType("s7 endpoint", S7Endpoint);
|
|
364
367
|
|
|
@@ -377,20 +380,16 @@ module.exports = function (RED) {
|
|
|
377
380
|
function sendMsg(data, key, status) {
|
|
378
381
|
if (key === undefined) key = '';
|
|
379
382
|
if (data instanceof Date) data = data.getTime();
|
|
380
|
-
var msg =
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
},
|
|
391
|
-
topic: ''
|
|
392
|
-
},
|
|
393
|
-
];
|
|
383
|
+
var msg = {
|
|
384
|
+
topic: key,
|
|
385
|
+
payload: data,
|
|
386
|
+
_s7: {
|
|
387
|
+
plc: node.endpoint.name,
|
|
388
|
+
ip: node.endpoint.endpoint._connOptsTcp.host,
|
|
389
|
+
status: node.endpoint.getStatus() === 'online' ? '在线' : '离线',
|
|
390
|
+
time: new Date(),
|
|
391
|
+
}
|
|
392
|
+
};
|
|
394
393
|
statusVal = status !== undefined ? status : data;
|
|
395
394
|
node.send(msg);
|
|
396
395
|
node.status(generateStatus(node.endpoint.getStatus(), statusVal));
|
|
@@ -418,21 +417,17 @@ module.exports = function (RED) {
|
|
|
418
417
|
node.status(generateStatus(s.status, statusVal));
|
|
419
418
|
|
|
420
419
|
// 只触发 ['online', 'offline'] 的事件
|
|
421
|
-
if (!['online', 'offline'].includes(node.endpoint.getStatus())) return;
|
|
422
|
-
var msg =
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
},
|
|
433
|
-
topic: ''
|
|
434
|
-
},
|
|
435
|
-
];
|
|
420
|
+
// if (!['online', 'offline'].includes(node.endpoint.getStatus())) return;
|
|
421
|
+
var msg = {
|
|
422
|
+
topic: '',
|
|
423
|
+
payload: {},
|
|
424
|
+
_s7: {
|
|
425
|
+
plc: node.endpoint.name,
|
|
426
|
+
ip: node.endpoint.endpoint._connOptsTcp.host,
|
|
427
|
+
status: node.endpoint.getStatus() === 'online' ? '在线' : '离线',
|
|
428
|
+
time: new Date(),
|
|
429
|
+
}
|
|
430
|
+
};
|
|
436
431
|
node.send(msg);
|
|
437
432
|
}
|
|
438
433
|
|
|
@@ -500,63 +495,97 @@ module.exports = function (RED) {
|
|
|
500
495
|
val: msg.payload,
|
|
501
496
|
done: (error) => {
|
|
502
497
|
|
|
498
|
+
/**
|
|
499
|
+
* 第一次写入数据后,会进入本函数
|
|
500
|
+
*/
|
|
501
|
+
|
|
502
|
+
// 写入的键
|
|
503
503
|
const variable = config.variable || msg.variable
|
|
504
|
+
// 写入的值
|
|
504
505
|
const payload = msg.payload
|
|
505
|
-
|
|
506
|
+
// 写入的键(数组)
|
|
506
507
|
const variables = Array.isArray(variable) ? variable : [variable]
|
|
508
|
+
// 写入的值(数组)
|
|
507
509
|
const payloads = Array.isArray(payload) ? payload : [payload]
|
|
508
510
|
|
|
511
|
+
// 写入的键值对
|
|
509
512
|
const values = {}
|
|
510
513
|
variables.forEach((key, index) => {
|
|
511
514
|
values[key] = payloads[index]
|
|
512
515
|
})
|
|
513
516
|
|
|
514
|
-
// 调用 s7-out
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
517
|
+
// 调用 s7-out 后的输出消息
|
|
518
|
+
msg._s7 = {
|
|
519
|
+
plc: node.endpoint.name,
|
|
520
|
+
ip: node.endpoint.endpoint._connOptsTcp.host,
|
|
521
|
+
status: node.endpoint.getStatus() === 'online' ? '在线' : '离线',
|
|
522
|
+
time: new Date(),
|
|
523
|
+
}
|
|
524
|
+
msg.payload = {
|
|
525
|
+
variable: variable, // 写入的键
|
|
526
|
+
payload: payload, // 写入的值
|
|
527
|
+
values: values, // 写入的键值对
|
|
528
|
+
newValues: {}, // plc的最新键值对
|
|
529
|
+
wrongValues: {}, // 跟写入值不一致的键值对
|
|
530
|
+
bingo: false, // plc的最新值跟写入值是否一致
|
|
531
|
+
error: error, // 错误
|
|
525
532
|
}
|
|
526
533
|
|
|
527
|
-
// 处理错误 done(
|
|
534
|
+
// 处理错误 done(e) 不生效;需要使用 node.error(e)
|
|
528
535
|
// https://nodered.org/docs/creating-nodes/node-js#handling-errors
|
|
529
536
|
if (error) {
|
|
530
537
|
node.error(error)
|
|
531
|
-
node.send(
|
|
538
|
+
node.send(msg)
|
|
532
539
|
return
|
|
533
540
|
}
|
|
534
541
|
|
|
535
|
-
//
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
542
|
+
// 已重写次数
|
|
543
|
+
let rewriteCount = 0;
|
|
544
|
+
// 读取最新的值判断是否需要重写数据
|
|
545
|
+
async function rewrite() {
|
|
546
|
+
// 延时读取最新的值
|
|
547
|
+
if (node.endpoint.rewritetimes && node.endpoint.rewriteinterval) await new Promise(resolve => setTimeout(resolve, node.endpoint.rewriteinterval))
|
|
548
|
+
try {
|
|
549
|
+
// 读取最新的值
|
|
550
|
+
const newValues = await node.endpoint.itemGroup.readAllItems()
|
|
551
|
+
for (const key in newValues) {
|
|
552
|
+
// 只匹配此次写入的变量
|
|
553
|
+
if (variables.includes(key)) {
|
|
554
|
+
// plc的最新键值对
|
|
555
|
+
msg.payload.newValues[key] = newValues[key]
|
|
556
|
+
// 跟写入值不一致的键值对
|
|
557
|
+
if (newValues[key] !== values[key]) msg.payload.wrongValues[key] = newValues[key]
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
// 判断数据是否完全写入成功
|
|
561
|
+
const v1 = Object.keys(msg.payload.values).length
|
|
562
|
+
const v2 = Object.keys(msg.payload.newValues).length
|
|
563
|
+
const v3 = Object.keys(msg.payload.wrongValues).length
|
|
564
|
+
msg.payload.bingo = v1 === v2 && v3 === 0
|
|
565
|
+
} catch (e) {
|
|
566
|
+
// node.error(e)
|
|
567
|
+
}
|
|
568
|
+
// 判断是否需要重写数据
|
|
569
|
+
if (!msg.payload.bingo && node.endpoint.rewritetimes > rewriteCount) {
|
|
570
|
+
// 递增已重写次数
|
|
571
|
+
rewriteCount++
|
|
572
|
+
try {
|
|
573
|
+
await node.endpoint.itemGroup.writeItems(writeObj.name, writeObj.val)
|
|
574
|
+
}
|
|
575
|
+
catch (e) {
|
|
576
|
+
// node.error(e)
|
|
545
577
|
}
|
|
578
|
+
// 读取最新的值判断是否需要重写数据
|
|
579
|
+
rewrite()
|
|
580
|
+
return
|
|
546
581
|
}
|
|
547
582
|
|
|
548
|
-
//
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
.catch(e => {
|
|
555
|
-
node.error(e)
|
|
556
|
-
})
|
|
557
|
-
.finally(() => {
|
|
558
|
-
node.send(message)
|
|
559
|
-
})
|
|
583
|
+
// 输出消息
|
|
584
|
+
node.send(msg)
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
// 读取最新的值判断是否需要重写数据
|
|
588
|
+
rewrite()
|
|
560
589
|
}
|
|
561
590
|
};
|
|
562
591
|
|