跳到主要内容

流程控制-在流停止后发送占位消息

当流停止发送数据后发送占位符消息

问题背景

有来自传感器的固定时间间隔的消息流。如果传感器停止发送消息,需要以相同的频率发送占位符消息。

例如,传感器数据可能正在发送仪表板图表。如果传感器停止发送,图表将停止更新。因此,需要使用占位符消息,让图表更新为 0 值,以突出显示传感器已停止,让界面可以直观看到传感器已停止。

解决方案

使用 触发器 节点检测消息在定义的时间间隔后是否到达,并使用第二个 触发器 节点以固定的时间间隔发送占位符消息。

示例

点击 注入 节点,发送消息,当2s内没有再次发送消息,触发2s 触发器 节点将会发送一条消息给 周期性重发2s 触发器 节点,直到 触发2s 重新接收到消息后 周期性重发2s 这个触发器才会停止发送消息

send-placeholder-msg-after-stream-stop

示例JSON

[{"id":"9ccdf268.c96ff","type":"inject","z":"e028a121bc4ae061","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":190,"y":1200,"wires":[["38950a5.28d15f6","2c532f67.0330e"]]},{"id":"38950a5.28d15f6","type":"debug","z":"e028a121bc4ae061","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":700,"y":1200,"wires":[]},{"id":"2c532f67.0330e","type":"trigger","z":"e028a121bc4ae061","name":"","op1":"reset","op2":"true","op1type":"str","op2type":"bool","duration":"2","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":350,"y":1240,"wires":[["e4e42b96.97a338"]]},{"id":"e4e42b96.97a338","type":"trigger","z":"e028a121bc4ae061","name":"","op1":"0","op2":"0","op1type":"num","op2type":"str","duration":"-2","extend":false,"overrideDelay":false,"units":"s","reset":"reset","bytopic":"all","topic":"topic","outputs":1,"x":510,"y":1240,"wires":[["38950a5.28d15f6"]]}]

运行结果

send-placeholder-msg-after-stream-stop-result