前言
目标:掌握功能节点中函数节点的基本使用方法。
懂的朋友欢迎订阅。
一、演示环境
win10系统,Node-RED最新版本2.1.3(2021年11月21号)。
二、功能节点
由于该部分节点较多,且具有一定复杂性,因此对其中特定节点进行单独的演示。
1.function节点
函数节点,定义对接收到的消息进行处理的JavaScript代码(函数的主体)。
1.1向调试节点发送消息
函数节点配置:
js代码如下:
// returning message send it to output port
msg.payload = "Hello, "+msg.payload +"!";
return msg;
运行效果:
1.2 发送多条消息
函数节点配置:
// returning array of message send elements to output ports
var msg1 = { payload:"first out of output 1" };
var msg2 = { payload:"second out of output 1" };
var msg3 = { payload:"third out of output 1" };
var msg4 = { payload:"only message from output 2" };
return [ [ msg1, msg2, msg3 ], msg4 ];
运行效果:
1.3异步发送消息
函数节点配置:
// setTimeout calls calls specified callback function asynchronously after a specified time
setTimeout(function () {
node.send(msg);
}, 2*1000);
运行效果:
1.4记录事件
函数节点配置:
// In function node, node.log, node.warn, and node.error functions can be used for logging
// See debug sidebar and console output
node.log("Something happened");
node.warn("Something happened you should know about");
node.error("Oh no, something bad happened");
return msg;
运行效果:
1.5错误信息可在msg.error中找到
函数节点配置:
// In function node, calling node.error functions with the original input message as its second argument triggers catch node
// See debug sidebar and console output
node.error("Oh no, something bad happened", msg);
// execution should stops here
运行效果:
1.6在上下文中存储数据,只对当前流有效
函数节点配置:
// initialise the counter to 0 if it doesn't exist already
var count = context.get('count')||0;
count += 1;
// store the value back
context.set('count',count);
// make it part of the outgoing msg object
msg.payload = count;
return msg;
运行效果:
1.7显示状态信息
函数节点配置:
// calling node.status show status information below the function node
switch (msg.payload) {
case "red":
node.status({fill:"red",shape:"ring",text:"disconnected"});
break;
case "green":
node.status({fill:"green",shape:"dot",text:"connected"});
break;
case "text":
node.status({text:"Just text status"});
break;
case "clear":
node.status({}); // to clear the status
break;
}
运行效果:
1.8导入外部模块属于高级功能
涉及到修改settings.js文件,这里不做演示。
1.9设置和关闭
函数节点配置:
// initialize global counter
global.set('count', 0);
// get value of global counter
var count = global.get('count');
count += 1;
// store the value back
global.set('count',count);
// make it part of the outgoing msg object
msg.payload = count;
return msg;
// report current counter to console
var count = global.get('count');
console.log("count:", count);
运行效果:
1.10异步设置
属于高级功能,了解即可:
函数节点配置:
// set initial value of message
global.set("message", "Initializing, World!");
// create promise for async work
var promise = new Promise((resolve, reject) => {
// async work: wait 1s, then set message
setTimeout(() => {
global.set("message", "Hello, World!");
// report this work successfuly ended
resolve();
}, 1000);
});
// return the promise that should be executed before function code
return promise;
// retrieve message value
msg.payload = global.get('message');
return msg;
运行效果:
总结
函数节点较为灵活,需掌握js基本编程。