创建第二个 Node-RED 流程

概览

本教程在第一个流程的基础上,引入外部数据,并在本地进行有用的处理。流程会定时从网站取得信息,将信息转换为方便使用的形式,最后在 Debug 侧栏显示结果。

1. 添加 Inject 节点

上一教程通过单击 Inject 节点上的按钮触发流程。这次将它配置为定时触发。从节点面板拖一个 Inject 节点到工作区,双击打开编辑对话框,将重复间隔设置为 every 5 minutes,然后单击 Done 关闭对话框。

2. 添加 HTTP Request 节点

HTTP Request 节点受到触发后,可以获取网页。将一个这样的节点添加到工作区,编辑其 URL 属性:

https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_month.csv

单击 Done。这个地址是美国地质调查局网站提供的过去一个月重大地震的 CSV 数据源。完成教程后,也可以尝试该网站提供的其他数据源。

3. 添加 CSV 节点

添加 CSV 节点,编辑属性,启用 First row contains column names(第一行包含列名),然后单击 Done。

4. 添加 Debug 节点

添加一个 Debug 节点,用于接收输出。

5. 连接节点

  • 将 Inject 节点的输出连接到 HTTP Request 节点的输入。
  • 将 HTTP Request 节点的输出连接到 CSV 节点的输入。
  • 将 CSV 节点的输出连接到 Debug 节点的输入。

6. 添加 Switch 节点

将 Switch 节点添加到工作区。编辑其属性,使它检查 msg.payload.mag,运算符设为 >=,比较值类型设为 number,值设为 7。单击 Done,然后从 CSV 节点再连接一条线到 Switch 节点。

7. 添加 Change 节点

添加 Change 节点,并将它连接到 Switch 节点的输出。将其配置为把 msg.payload 设置为字符串 PANIC!。

8. 再添加一个 Debug 节点

把新的 Debug 节点连接到 Change 节点的输出。

9. 部署

单击 Deploy,把流程部署到运行时。打开 Debug 侧栏后,单击 Inject 按钮。应当能看到一组消息,内容类似下面这个原文示例:

msg.payload : Object
{"time":"2017-11-19T15:09:03.120Z","latitude":-21.5167,"longitude":168.5426,"depth":14.19,"mag":6.6,"magType":"mww","gap":21,"dmin":0.478,"rms":0.86,"net":"us","id":"us2000brgk","updated":"2017-11-19T17:10:58.449Z","place":"68km E of Tadine, New Caledonia","type":"earthquake","horizontalError":6.2,"depthError":2.8,"magError":0.037,"magNst":72,"status":"reviewed","locationSource":"us","magSource":"us"}

单击每个属性左侧的小箭头,可以展开查看其内容。如果数据中存在震级达到或超过 7 的地震,还会看到如下调试消息:

msg.payload : string(6)
"PANIC!"

为了测试,可以把 Switch 的阈值从 7 调小。修改后记得重新单击 Deploy。

小结

该流程每 5 分钟自动触发一次,从 URL 获取数据,解析后显示在 Debug 侧栏。它还检查数据中的震级,对震级大于或等于 7 的消息建立分支,修改这些消息的 payload,再将其显示在 Debug 侧栏。

流程源码

下面的 JSON 表示本教程的完整流程。复制它,在编辑器的 Import 对话框中粘贴即可导入。

[{"id":"e36406f2.8ef798","type":"inject","z":"f03b57d5.e525f8","name":"","topic":"","payload":"","payloadType":"str","repeat":"300","crontab":"","once":false,"x":130,"y":900,"wires":[["c3c50023.3bbed"]]},{"id":"c3c50023.3bbed","type":"http request","z":"f03b57d5.e525f8","name":"Recent Quakes","method":"GET","url":"https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_month.csv","tls":"","x":300,"y":900,"wires":[["8afc6cac.e0812"]]},{"id":"8afc6cac.e0812","type":"csv","z":"f03b57d5.e525f8","name":"","sep":",","hdrin":true,"hdrout":"","multi":"one","ret":"\\n","temp":"","x":470,"y":900,"wires":[["44779781.4190f8","6f0eb546.9e208c"]]},{"id":"44779781.4190f8","type":"debug","z":"f03b57d5.e525f8","name":"","active":true,"complete":false,"x":630,"y":900,"wires":[]},{"id":"6f0eb546.9e208c","type":"switch","z":"f03b57d5.e525f8","name":"","property":"payload.mag","propertyType":"msg","rules":[{"t":"gte","v":"7","vt":"num"}],"checkall":"true","outputs":1,"x":510,"y":960,"wires":[["d78d4aa8.8c8208"]]},{"id":"d78d4aa8.8c8208","type":"change","z":"f03b57d5.e525f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"PANIC!","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":650,"y":1020,"wires":[["72fddece.fac0d"]]},{"id":"72fddece.fac0d","type":"debug","z":"f03b57d5.e525f8","name":"","active":true,"complete":false,"x":750,"y":960,"wires":[]}]

相关阅读

来源与许可

原文:Node-RED:Creating your second flow。Copyright OpenJS Foundation and Node-RED contributors。本文为中文翻译,示例保留原样;阈值说明统一为节点实际配置的“大于或等于 7”。文档仓库采用 Apache License 2.0,保留如下原始声明:

Copyright 2016 JS Foundation and other contributors, https://js.foundation/
Copyright 2013, 2016 IBM Corp.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容