lab06- Node-RED 其他控件节点

一、目录

1. 实验目的

  • 掌握 node-red 其他特有的 API

2. 实验步骤

2.1 node 相关 api 的应用

  • 函数控件的功能强大,甚至可以用函数控件实现其它所有功能控件的功能。在函数空间中,除了可以调用 JS 自带的各种方法以外,含有一些 node-red 特有的 API 可以使用,说明如下:
node.id : 函 数 节 点 的ID, 在0 .19 版 本 添 加
node.name : 函 数 节 点 的 名 字, 在0 .19 版 本 增 加
node.log ( . . ) : 记 录 一 条 消 息
node.warn ( . . ) : 记 录 一 条 警 告 消 息
node.error ( . . ) : 记 录 一 条 错 误 消 息
node.debug ( . . ) : 记 录 一 条 调 试 消 息
node.trace ( . . ) : 记 录 跟 踪 消 息
node.on ( . . ) : 注 册 一 个 事 件 处 理 函 数
node.status ( . . ) : 更 新 节 点 的 状 态
node.send ( . . ) : 发 送 消 息

2.2 使用 send 发送数据

  • 在实验四中,我们已经可以使用函数简单判断字符串的走向,但如果使用 if-else 的判断当字符串满足其中一个条件就输出对应的口,但是当某个字符串既满足条件 1 又满足条件 3 的情况下,条件 3 就不会执行。


  • 有没有既可以发送数据,又不会结束函数的方法?当然有。我们查询了节点可以调用的 API,发现有send 函数,满足我们的要求。可以将判断节点做如下的修改:



    该修改首先是放弃了 if-else 的写法,而使用了 if 的写法,使用 node.send 往下一个流发送数据。然后部署并观察现象,可以发现,当点击 apple 的输入按钮时,OUT1 与 OUT3 都可以收到消息。


实现
1.按照上图,拖拽节点,效果如下图
2.设置节点参数

设置第一个节点的参数设置,剩余的2个,重复以下操作




设置函数节点配置



设置函数节点代码
if (msg.topic ==="apple") 
{
    node.send([msg, null, null]);
}
if (msg.topic === "banana") 
{
    node.send([null, msg, null]);
}
if (msg.topic.indexOf("e") != -1) 
{
    node.send([null, null, msg]);
}

把所有的节点连接起来


3.部署查看效果

2.3 流程之间传递函数

  • Context 元素除了可以传递变量以外,也可以传递函数。我们在流 1 中新建一个名为 hello 的函数,在流 2 中调用它。总体部署如下:


  • 第一个流中的 function 节点写法:


  • 第二个流中的 function 节点写法,调用第一个流的中的函数:


  • 注意: 第二个节点可以调用第一个节点的函数,是因为第一个节点创建的函数已经保存到了全局变量 global 里。部署后,先点击 IN1 的输入按钮,再点击 IN2 的输入按钮,可以看到 OUT2 中打印出了 helloworld,其中的“hello”是通过调用流 1 的 hello() 函数打印出来的,说明实验成功了。
实现
1.按照上图拖拽节点,并连接,如下图
2.设置节点代码

设置第一个节点代码

context.global.hello = function () { return "hello" };
msg.payload = context.global.hello();
return msg;

设置第二个节点代码

msg.payload = context.global.hello()+"World";
return msg;
3.部署,查看效果

2.4 使用状态标识

delay 节点用于产生一个随机数,在运行的时候显示出了一个表示时间的字符,显示这个随机的时间是多少,这个功能很直观的可以显示出我们感兴趣的一些数据或状态,可以省去到处用 debug 节点打印的烦恼。我们也可以使用 node.status 来更新节点的状态.

  • step1: 搭建 flow 首先要搭建一个可变的输入信息,并且要持续一段时间,避免状态变得太快看不到。用函数节点 +delay 节点可以实现。先拖拽节点,形成数据流。
  • step2: 第一个节点用于发送 5 个数字:
  • step3: 延时节点设置为每秒只接收一个字符信息:
  • step4: 第二个节点显示节点信息:
实现
1.按照上图,拖拽节点并连接
2.设计节点函数

设置第一个函数节点

for (var i = 0; i < 5; i++) 
{
    node.send({ payload: i });
}
return null;

设置延时节点参数



设置第二个函数节点

node.status
(
    {
        fill: "green", 
        shape: "dot",text: "Received " + msg.payload 
    }
);
return msg;
  • step5: 部署,运行,查看效果

3. 全部代码

[
    {
        "id": "fd24dd19e0ab88ca",
        "type": "tab",
        "label": "流程 5",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "bbdf1e33d3b2040c",
        "type": "inject",
        "z": "fd24dd19e0ab88ca",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "apple",
        "payload": "apple",
        "payloadType": "str",
        "x": 90,
        "y": 100,
        "wires": [
            [
                "e5f5bc529125975f"
            ]
        ]
    },
    {
        "id": "db1fec6ee1aa2c71",
        "type": "inject",
        "z": "fd24dd19e0ab88ca",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "banana",
        "payload": "banana",
        "payloadType": "str",
        "x": 100,
        "y": 160,
        "wires": [
            [
                "e5f5bc529125975f"
            ]
        ]
    },
    {
        "id": "98fbcf4584a2f73f",
        "type": "inject",
        "z": "fd24dd19e0ab88ca",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "orange",
        "payload": "orange",
        "payloadType": "str",
        "x": 100,
        "y": 220,
        "wires": [
            [
                "e5f5bc529125975f"
            ]
        ]
    },
    {
        "id": "e5f5bc529125975f",
        "type": "function",
        "z": "fd24dd19e0ab88ca",
        "name": "function 13",
        "func": "if (msg.topic === \"apple\") \n{\n    node.send([msg, null, null]);\n}\nif (msg.topic === \"banana\") \n{\n    node.send([null, msg, null]);\n}\nif (msg.topic.indexOf(\"e\") != -1) \n{\n    node.send([null, null, msg]);\n}\n",
        "outputs": 3,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 290,
        "y": 160,
        "wires": [
            [
                "e0df73d7264cf691"
            ],
            [
                "db705ff2959f7d84"
            ],
            [
                "975fc285f54b1bfa"
            ]
        ]
    },
    {
        "id": "e0df73d7264cf691",
        "type": "debug",
        "z": "fd24dd19e0ab88ca",
        "name": "debug 24",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 460,
        "y": 100,
        "wires": []
    },
    {
        "id": "db705ff2959f7d84",
        "type": "debug",
        "z": "fd24dd19e0ab88ca",
        "name": "debug 25",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 460,
        "y": 160,
        "wires": []
    },
    {
        "id": "975fc285f54b1bfa",
        "type": "debug",
        "z": "fd24dd19e0ab88ca",
        "name": "debug 26",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 480,
        "y": 220,
        "wires": []
    },
    {
        "id": "cfde4bc22548a5da",
        "type": "inject",
        "z": "fd24dd19e0ab88ca",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 130,
        "y": 360,
        "wires": [
            [
                "833d9441148aa8b0"
            ]
        ]
    },
    {
        "id": "833d9441148aa8b0",
        "type": "function",
        "z": "fd24dd19e0ab88ca",
        "name": "function 14",
        "func": "context.global.hello = function () { return \"hello\" };\nmsg.payload = context.global.hello();\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 290,
        "y": 360,
        "wires": [
            [
                "02d01bfca0ec64bb"
            ]
        ]
    },
    {
        "id": "02d01bfca0ec64bb",
        "type": "debug",
        "z": "fd24dd19e0ab88ca",
        "name": "debug 27",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 500,
        "y": 360,
        "wires": []
    },
    {
        "id": "d0896441365092f6",
        "type": "inject",
        "z": "fd24dd19e0ab88ca",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 130,
        "y": 440,
        "wires": [
            [
                "7162a1b78eacc220"
            ]
        ]
    },
    {
        "id": "7162a1b78eacc220",
        "type": "function",
        "z": "fd24dd19e0ab88ca",
        "name": "function 15",
        "func": "msg.payload = context.global.hello()+\"World\";\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 290,
        "y": 440,
        "wires": [
            [
                "7e957b78d99d7970"
            ]
        ]
    },
    {
        "id": "7e957b78d99d7970",
        "type": "debug",
        "z": "fd24dd19e0ab88ca",
        "name": "debug 28",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 500,
        "y": 440,
        "wires": []
    },
    {
        "id": "ca10b10e2b9597b1",
        "type": "inject",
        "z": "fd24dd19e0ab88ca",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 130,
        "y": 620,
        "wires": [
            [
                "84f68edfbbf9836a"
            ]
        ]
    },
    {
        "id": "84f68edfbbf9836a",
        "type": "function",
        "z": "fd24dd19e0ab88ca",
        "name": "function 16",
        "func": "for (var i = 0; i < 5; i++) \n{\n    node.send({ payload: i });\n}\nreturn null;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 130,
        "y": 700,
        "wires": [
            [
                "8e34506111ecdc36"
            ]
        ]
    },
    {
        "id": "8e34506111ecdc36",
        "type": "delay",
        "z": "fd24dd19e0ab88ca",
        "name": "",
        "pauseType": "rate",
        "timeout": "5",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "allowrate": false,
        "outputs": 1,
        "x": 150,
        "y": 780,
        "wires": [
            [
                "ea85e1d28efac455"
            ]
        ]
    },
    {
        "id": "ea85e1d28efac455",
        "type": "function",
        "z": "fd24dd19e0ab88ca",
        "name": "function 17",
        "func": "node.status\n(\n    {\n        fill: \"green\", \n        shape: \"dot\",text: \"Received \" + msg.payload \n    }\n);\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 150,
        "y": 860,
        "wires": [
            [
                "2902fbb971fd1fb8"
            ]
        ]
    },
    {
        "id": "2902fbb971fd1fb8",
        "type": "debug",
        "z": "fd24dd19e0ab88ca",
        "name": "debug 29",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 140,
        "y": 940,
        "wires": []
    }
]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容