跳到主要内容

流程控制-消息属性路由

根据消息的某个属性来路由消息到不同的分支

问题背景

根据 msg.topic 属性的值将消息路由到不同的流。例如,您有一个订阅了多个传感器的 MQTT 节点,并且希望将消息传递到不同的下游节点,处理不同的业务。

解决方案

使用 分支处理 节点检查属性的值是否与节点不同输出对应的不同值相符。

示例

switch-property.png

示例JSON

[{"id":"3bc8e1d2.744abe","type":"switch","z":"e028a121bc4ae061","name":"Route ","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"temperature","vt":"str"},{"t":"eq","v":"humidity","vt":"str"},{"t":"eq","v":"pressure","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":450,"y":240,"wires":[["907bf3b8.def45"],["fe425938.926838"],["ec261304.52f73"]]},{"id":"be3da36c.1c142","type":"inject","z":"e028a121bc4ae061","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"temperature","payload":"27","payloadType":"num","x":260,"y":200,"wires":[["3bc8e1d2.744abe"]]},{"id":"f271ceef.172b3","type":"inject","z":"e028a121bc4ae061","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"humidity","payload":"45","payloadType":"num","x":250,"y":240,"wires":[["3bc8e1d2.744abe"]]},{"id":"907bf3b8.def45","type":"debug","z":"e028a121bc4ae061","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":630,"y":200,"wires":[]},{"id":"fe425938.926838","type":"debug","z":"e028a121bc4ae061","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":620,"y":240,"wires":[]},{"id":"ec261304.52f73","type":"debug","z":"e028a121bc4ae061","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":620,"y":280,"wires":[]},{"id":"fca957dd.9d8078","type":"inject","z":"e028a121bc4ae061","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"pressure","payload":"1001","payloadType":"num","x":250,"y":280,"wires":[["3bc8e1d2.744abe"]]}]

运行结果

我们的示例中,有三个 注入 节点,分别配置了不同的 topicpayload 值,通过 分支处理 节点,当接收到消息时,根据携带的 topic 信息转发到不同的下游节点

switch-property-result.png