控制看门狗
x/control/watchdog 组件:软PLC 看门狗(失联保护)。正常时把每条消息原样透传给下游,并据此"喂狗"重置计时;一旦超过 timeout 没再收到任何消息,判定上位机/链路失联,自动向下游下发预设的故障安全 JSON(如关阀、停机),把设备拉到安全态。协议与数据格式无关,常串在 x/iotWrite 之前。
需要额外引入扩展库:rulego-components-iot (opens new window)
# 配置
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
| timeout | string | 是 | 喂狗超时:距上一条消息超过此时长仍未收到新消息,即判定失联并下发故障安全值;期间每收到一条消息就重置计时(喂狗)。如 10s、500ms,支持 ${metadata.xx} |
| failsafe | string(JSON) | 是 | 失联时下发的安全状态 JSON,如 {"valve":0,"motor":0}(关阀、停机);经 Success 链写入 msg.Data,其键需与下游写点位 ${msg.xx} 对应 |
# 行为
- 收到消息:原始消息透传至下游(
Success),并重置计时。 - 达到
timeout未收到消息:经Success链下发failsafeJSON。 - 消息条数:正常持续喂狗时,每条喂狗消息透传 1 次、闹钟被新消息重置,不会触发故障安全;只有在真正失联(超时)时,才在"最后一条透传"之后额外下发 1 条故障安全值。
- 看门狗不会阻塞规则链:透传与故障安全都正常走
Success,超时闹钟不参与规则链的完成计数。
# Relation Type
- Success: 消息透传,或下发故障安全载荷。
- Failure: 执行失败(如
failsafe未配置),把消息发送到Failure链。
# 示例
{
"ruleChain": {
"id": "watchdog_demo",
"name": "看门狗故障安全示例",
"root": true,
"debugMode": false
},
"metadata": {
"firstNodeIndex": 0,
"nodes": [
{
"type": "x/control/watchdog",
"name": "上位机失联保护",
"debugMode": false,
"configuration": {
"timeout": "10s",
"failsafe": "{\"valve\":0,\"motor\":0}"
}
}
],
"connections": []
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 完整场景:上位机失联保护
工艺: 上位机周期性下发射频/设定值(兼作心跳);一旦上位机或链路故障、超过 10 秒没有新消息,就把阀门关到 0、电机停到 0,进入安全态。
{
"ruleChain": {"id": "watchdog-guard", "name": "上位机失联保护", "root": true},
"metadata": {
"nodes": [
{"id": "e1", "type": "endpoint/modbusServer", "configuration": {
"listen": "tcp://:5020", "unitId": 1
}},
{"id": "wd", "type": "x/control/watchdog", "configuration": {
"timeout": "10s", "failsafe": "{\"valve\":0,\"motor\":0}"
}},
{"id": "w1", "type": "x/iotWrite", "configuration": {
"driver": "modbus", "server": "tcp://192.168.1.100:502",
"points": [
{"name": "阀", "addr": "40001", "type": "UINT16", "value": "${msg.valve}"},
{"name": "电机", "addr": "40002", "type": "UINT16", "value": "${msg.motor}"}
]
}}
],
"connections": [
{"fromId": "e1", "toId": "wd", "type": "ip"},
{"fromId": "wd", "toId": "w1", "type": "Success"}
]
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
上位机经
endpoint/modbusServer写寄存器(心跳+设定值)→ watchdog 透传并喂狗 →x/iotWrite把${msg.valve}/${msg.motor}写到设备。失联超时后 watchdog 下发failsafe,写节点把阀/电机拉到 0。
时序行为:
| 场景 | 消息 | 下一节点收到 |
|---|---|---|
| 正常持续喂狗(间隔 < 10s) | 每条喂狗透传 1 次,闹钟被重置 | 每个喂狗 1 条透传,无故障安全 |
| 失联(> 10s 无消息) | 最后一条透传 + 超时故障安全 | 透传 1 条 + 故障安全 1 条({"valve":0,"motor":0}) |
更多组合示例见:IoT 场景示例 · 软PLC式逻辑控制
在 GitHub 上编辑此页 (opens new window)
上次更新: 2026/07/29, 10:24:31