node-red教程 3.4 switch控件介绍

node-red 专栏收录该内容
15 篇文章 39 订阅

  Switch本意是开关、转换,用在node-red里有点像一个岔路口,node-red里边有数据流,数据流可能有不同的分支,在“岔路口”管理数据流向的,就可以是switch控件。控件根据数据的一些属性来判断它的流向。就比如你到了公共卫生间,你是男生,就去左边的男卫生间;女生去右边的女卫生间。Switch控件可以根据数据的topic或是payload,或者其它属性来判断数据该发给哪一个出口。
  Switch控件属于“function”类型的控件,function一般翻译为功能或者函数。说到函数可能就会让人想到数学上那些抽象的公式,让人望而却步了。好在编程中用到的函数跟数学上的不太一样,一般是有一定功能的程序。对,就是写程序。但是不要怕,程序里的逻辑一般都是很简单的,简单到可以用朴素的语言描述。比如你在回家的路上,你妈妈告诉你:“要是看到卖包子的,买6个包子。没有卖包子的话,买4个馒头”。在程序里,可能这么表示这个逻辑关系

if (卖包子 == true)
buy(6,包子);
else
buy(4,馒头);

这里写图片描述

3.4.1 switch的说明信息

  在附带这些说明的时候,我其实是忐忑不安的。因为我发现,这个说明信息可能把简单的问题复杂化了。我在没有看这些说明的时候就摸索出了这个节点的基本用法,看了说明以后反而有点不知所云。这是因为,switch节点的高级用法我们可能暂时没有用到,因此如果读一下信息让你感到疑惑,你大可以跳过这段,毕竟我们以应用为主。
以后别的控件的说明信息也可以先跳过,掌握用法以后再来看,可能会好一些。

根据其属性值或序列位置路由消息。(路由在这里可以理解为数据转发)
细节
当消息到达时,节点将评估每一个已定义的规则,并将消息转发到任何匹配规则的相应输出。
可以选择配置为,一旦找到匹配的规则,节点就可以停止评估规则。
可以根据单个消息属性、流或全局上下文属性或JSONata表达式的结果来评估规则。
规则
有四种类型的规则:
数值:规则是根据配置的属性进行评估的
序列:规则可用于消息序列,如分割节点生成的序列规则
表达式:可以提供一个JSONata表达式,它将对整个消息进行评估,并且如果表达式返回true值,则会匹配。
其它规则:如果没有匹配的规则,则可以使用另一种规则来匹配。

处理消息序列
默认情况下,节点不会修改消息的msg.parts属性,这是序列的一部分
重新创建的消息序列选项可以为匹配的每条规则生成新的消息序列。在这种模式下,节点将在发送新序列之前缓冲整个传入序列。运行时设置nodeMessageBufferMaxLength 可以用来限制有多少消息节点将被缓冲。

3.4.2 switch判断数值

  在程序设计中,最重要的不是写程序,而是设计。就像建筑、机械等行业的要画设计图、施工图,程序设计的思路也有必要用图的形式画出来。画图的过程就是思考的过程,由于其直观性,画图的过程本身又促进了思考。在软件工程中,已经发展出了很多种实用的图,为软件产品设计的质量提供保证。接下来我们就来学习程序设计中最简单的一种图——“程序流程图”。
  在程序设计之前,我们先来梳理一下任务:可能会有一些数据需要我们判断大小,然后把数据发到不同的输出分支。有点像你提着三个篮子去摘苹果,大苹果放在篮子1,中苹果放在篮子2,小苹果放在篮子3。我们就以“10”作为判断的标准,把1,10,100作为测试数据。分析任务,可以得到以下流程图。
这里写图片描述
  测试数据的输入使用inject节点,三个输出使用debug节点,相信大家早已轻车熟路。然后拖入一个switch节点。按图示修改
这里写图片描述
这里写图片描述
  改完以后发现switch节点的输出变为3个了,从上到下分别是输出1,输出2与输出3。连接并部署。
这里写图片描述
  分别点击3个inject节点的输入按钮。可以在调试窗口观察到以下现象。可以看出,100是OUT1节点收到的,也就是第一个判断条件(>10)的输出,100>10,没有问题。其它的数也根据判断条件正确输出,说明我们的任务完成了。
这里写图片描述

[{"id":"d7d8d28a.66a29","type":"switch","z":"a1f259d6.8791a8","name":"判断数字","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"10","vt":"num"},{"t":"eq","v":"10","vt":"str"},{"t":"lt","v":"10","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":380,"y":140,"wires":[["a9c17a9c.6080d8"],["460c1daf.59a374"],["e05c0491.0a53d8"]]},{"id":"5bb99142.a869a","type":"inject","z":"a1f259d6.8791a8","name":"","topic":"","payload":"100","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":80,"wires":[["d7d8d28a.66a29"]]},{"id":"fa6bef86.f1046","type":"inject","z":"a1f259d6.8791a8","name":"","topic":"","payload":"10","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":140,"wires":[["d7d8d28a.66a29"]]},{"id":"883913c2.9c3ab","type":"inject","z":"a1f259d6.8791a8","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":200,"wires":[["d7d8d28a.66a29"]]},{"id":"a9c17a9c.6080d8","type":"debug","z":"a1f259d6.8791a8","name":"OUT1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":590,"y":80,"wires":[]},{"id":"460c1daf.59a374","type":"debug","z":"a1f259d6.8791a8","name":"OUT2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":590,"y":140,"wires":[]},{"id":"e05c0491.0a53d8","type":"debug","z":"a1f259d6.8791a8","name":"OUT3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":590,"y":200,"wires":[]}]

3.4.3 switch判断数组

  刚刚的例子只能判断单个数字,实际使用中,我们常常需要判断数组的某一位。接下来,在刚刚的例子上进行一点点的修改,来实现可以根据数组的第二位的情况来分发数据。
  注入内容改为数组。
这里写图片描述
  Switch节点需要注意属性,是msg.payload[1]!因为数组下标从0开始,所以数组的第一位是msg.payload[0],第二位才是msg.payload[1]。
  除了刚刚使用的大于,等于和小于,还有很多判断条件,感兴趣的可以尝试下。我这里随便用了几个。
  顺带提一下,在switch的说明信息里,有很多关于sequence rules和JSONata exp的描述,这些可能是很有用的判断条件,不过我们暂时没有用到,因此看不懂说明信息。感兴趣的可以查一下。
这里写图片描述
这里写图片描述

[{"id":"fc2f904.f999f7","type":"switch","z":"35ef3f16.088cc","name":"判断数字","property":"payload[1]","propertyType":"msg","rules":[{"t":"eq","v":"2","vt":"num"},{"t":"btwn","v":"5","vt":"num","v2":"6","v2t":"num"},{"t":"cont","v":"7","vt":"num"}],"checkall":"true","repair":false,"outputs":3,"x":440,"y":220,"wires":[["b425dfbd.2d50c"],["ee083586.6939a8"],["96d980b9.81bc7"]]},{"id":"48f7a616.f295d8","type":"inject","z":"35ef3f16.088cc","name":"","topic":"","payload":"[1,2,3]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":160,"wires":[["fc2f904.f999f7"]]},{"id":"ab2bdee9.29b41","type":"inject","z":"35ef3f16.088cc","name":"","topic":"","payload":"[4,5,6]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":220,"wires":[["fc2f904.f999f7"]]},{"id":"518c940.856a46c","type":"inject","z":"35ef3f16.088cc","name":"","topic":"","payload":"[7,78,9]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":280,"wires":[["fc2f904.f999f7"]]},{"id":"b425dfbd.2d50c","type":"debug","z":"35ef3f16.088cc","name":"OUT1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":650,"y":160,"wires":[]},{"id":"ee083586.6939a8","type":"debug","z":"35ef3f16.088cc","name":"OUT2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":650,"y":220,"wires":[]},{"id":"96d980b9.81bc7","type":"debug","z":"35ef3f16.088cc","name":"OUT3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":650,"y":280,"wires":[]}]

3.4.4 switch判断topic

  前边两个例子都是根据msg.payload属性来判断的,msg里边的其它属性也可以作为判断条件,在3.1.5小节中提到过,接下来尝试操作一下。
  拖入3个inject节点,topic要填写,我填写的topic是“apple”,“banana”和“orange”。Switch节点进行如下修改。
这里写图片描述
  连线并部署。点击inject按钮观察现象。
这里写图片描述
  我想,除了点击一下apple的输入按钮,OUT1与OUT3都收到数据以外,其它的现象都和你预料的一致。Apple为什么会输出两次?
  这是因为条件3是包含“e”,apple也是包含“e”的,所以apple是满足条件3的。
这里写图片描述
  如果想让apple满足条件1以后就不再判断条件3,可以选择“接受第一条匹配消息后停止”。
这里写图片描述

代码如下:

[{"id":"6b822d6c.b64d34","type":"switch","z":"2cabb877.dd50b8","name":"判断主题","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"apple","vt":"str"},{"t":"eq","v":"banana","vt":"str"},{"t":"cont","v":"e","vt":"str"}],"checkall":"false","repair":false,"outputs":3,"x":360,"y":160,"wires":[["5723c235.995c1c"],["7a3d0ed7.1fd12"],["24f93dff.b007a2"]]},{"id":"3eeb3b18.884db4","type":"inject","z":"2cabb877.dd50b8","name":"","topic":"apple","payload":"apple","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":100,"wires":[["6b822d6c.b64d34"]]},{"id":"9ee97777.be6de8","type":"inject","z":"2cabb877.dd50b8","name":"","topic":"banana","payload":"banana","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":160,"wires":[["6b822d6c.b64d34"]]},{"id":"1215b92e.3a45f7","type":"inject","z":"2cabb877.dd50b8","name":"","topic":"orange","payload":"orange","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":220,"wires":[["6b822d6c.b64d34"]]},{"id":"5723c235.995c1c","type":"debug","z":"2cabb877.dd50b8","name":"OUT1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":570,"y":100,"wires":[]},{"id":"7a3d0ed7.1fd12","type":"debug","z":"2cabb877.dd50b8","name":"OUT2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":570,"y":160,"wires":[]},{"id":"24f93dff.b007a2","type":"debug","z":"2cabb877.dd50b8","name":"OUT3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":570,"y":220,"wires":[]}]
  • 8
    点赞
  • 2
    评论
  • 20
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

打赏
文章很值,打赏犒劳作者一下
相关推荐
<p> <span style="color:#313d54;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif;font-size:16px;background-color:#ffffff;">Node-RED是工业网物联网的重要组成部分,我最开始接触Node-RED,也算是一个偶然的机会吧,上班后领导安排我的第一个任务就是调研一下Node-RED,我之后上网查了一下,那个时候网上相对于Node-RED的资料也比较少,只知道它是IBM公司的一个开源项目。直到最近,发现许多大公司的产品都支持Node-RED,比如西门子公司的IoT2000,研华公司的WISE PaaS 网关,美国OPTO 22等设备中都安装了Node-RED,表明它在工业物联网和控制中已经广泛应用了。</span> </p> <p> <img src="https://img-bss.csdnimg.cn/202010200935441926.png" alt="" width="1316" height="524" /> </p> <p style="font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif;color:#313d54;font-size:16px;background-color:#ffffff;"> 那么工业物联网为什么要用它?它又处于工业物联网那个层次?它具有哪些特性?它帮助物联网解决了什么问题?为什么说它是柔性动态可重构的解决方案呢? </p> <p style="font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif;color:#313d54;font-size:16px;background-color:#ffffff;">   </p> <p style="font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif;color:#313d54;font-size:16px;background-color:#ffffff;"> ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ </p> <p style="font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif;color:#313d54;font-size:16px;background-color:#ffffff;">   </p> <p style="font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif;color:#313d54;font-size:16px;background-color:#ffffff;"> 本门课程,老师将带领你从Node-RED的发展,工业物联网定位开始讲解,并带领着大家进行手把手安装Node-RED,实际操作演练Node-RED,并搭建一个物联网小平台,给大家带来更好的学习效果。 </p> <p style="font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif;color:#313d54;font-size:16px;background-color:#ffffff;"> <img src="https://img-bss.csdnimg.cn/202010200936289051.png" alt="" width="1841" height="948" /> </p> <p style="font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif;color:#313d54;font-size:16px;background-color:#ffffff;"> <img src="https://img-bss.csdnimg.cn/202010200936561564.png" alt="" width="1920" height="977" /> </p> <p style="font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif;color:#313d54;font-size:16px;background-color:#ffffff;">   </p> <p style="font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif;color:#313d54;font-size:16px;background-color:#ffffff;"> ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ </p> <p style="font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif;color:#313d54;font-size:16px;background-color:#ffffff;">   </p> <p style="font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif;color:#313d54;font-size:16px;background-color:#ffffff;"> 为了能够让小伙伴们快速了解本门课程的结构,本门课程从以下几个方面展开: </p> <p style="font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif;color:#313d54;font-size:16px;background-color:#ffffff;"> Node-RED入门 </p> <p style="font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif;color:#313d54;font-size:16px;background-color:#ffffff;"> Node-RED安装与配置 </p> <p style="font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif;color:#313d54;font-size:16px;background-color:#ffffff;"> Node-RED教学实战 </p> <p style="font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif;color:#313d54;font-size:16px;background-color:#ffffff;"> Node-RED的优势与不足 </p> <p style="font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif;color:#313d54;font-size:16px;background-color:#ffffff;"> Node-RED能为我们带来什么 </p> <p style="font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', Arial, sans-serif;color:#313d54;font-size:16px;background-color:#ffffff;"> Node-RED总结与展望 </p>
©️2020 CSDN 皮肤主题: 护眼 设计师:闪电赇 返回首页

打赏

yummy说电子

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值