node-red中创建Customization节点 JavaScript 文件API编写详解

freeFree Technical Resource

This content is free to read, suitable for basic learning and search traffic.

node-red中创建Customization节点 JavaScript 文件API编写详解缩略图
🌐 This page is not yet available in English. Showing the Chinese version. Back to Chinese page.
本文目录
  1. 1. 前言
  2. 2. Customization节点JavaScript 文件
  3. 3. 节点构建器
  4. 4. 接受信息
  5. 5. 接受信息
  6. 6. 下发信息
  7. 7. 日记事件
  8. 8. 设置状态
  9. 9. Customization节点设置

前言

在node-red中如果你没有找到自己需要的节点时,那么你可以Customization一个节点来满足自己的需求。之前的文章中,我有简单介绍过如何创建一个节点,并以转换大小写来举例。例子虽然简单,但可以让大家了解创建Customization节点的步骤以及一个节点的组成部分。那么本篇将会聚焦在Customization节点的js文件中。详细讲解其中的api,及其他配置项。通过之前的文章我们知道,节点的js文件是一个节点的核心文件,用于收取配置参数,处理业务逻辑,并向下游senddata。下面让我们一起看看吧。

Customization节点JavaScript 文件

节点构建器

所有的节点都是由一个构建器来生成的函数,是创建的一个新的实例。这个函数被Register到运行时中,
这个函数通过设置一个配置对象来existence于流Edit器中。
在该函数中,第一件事就是调用RED.nodes.createNode函数,来Sequence化特性,被其他节点共享。在这之后

就可以编写业务代码啦。
如下

接受信息

节点通过Register一个input事件来接受上游节点下发的信息在整个流中。Register事件时,在事件的回调函数里,会有msg和send两个参数,msg就是上游传递过来的data。send是要下发到下游的一个函数。第三个函数 done 是节点Complete业务逻辑处理后调用的函数。
示例

function SampleNode(config) {
    RED.nodes.createNode(this,config);
    // node-specific code goes here

}

RED.nodes.registerType("sample",SampleNode);

接受信息

节点通过Register一个input事件来接受上游节点下发的信息在整个流中。Register事件时,在事件的回调函数里,会有msg和send两个参数,msg就是上游传递过来的data。send是要下发到下游的一个函数。第三个函数 done 是节点Complete业务逻辑处理后调用的函数。
示例

this.on('input', function(msg, send, done) {
    // do something with 'msg'

    // Once finished, call 'done'.
    // This call is wrapped in a check that 'done' exists
    // so the node will work in earlier versions of Node-RED (<1.0)
    if (done) {
        done();
    }
});

下发信息

在Customization节点中向下游send信息非常的简单
只需要使用send函数即可,如下

var msg = { payload:"hi" }
this.send(msg);

如果节点想要在input事件中向下游send信息,可以这样做

let node = this;
this.on('input', function(msg, send, done) {
    // For maximum backwards compatibility, check that send exists.
    // If this node is installed in Node-RED 0.x, it will need to
    // fallback to using `node.send`
    send = send || function() { node.send.apply(node,arguments) }

    msg.payload = "hi";
    send(msg);

    if (done) {
        done();
    }
});

在input事件的回调函数中,有三个参数,第一个是接受到的信息,第二个是send函数,
第三个是done函数。分别有不同的使用Scenarios。

如果要向下游输入多个参数可以这样写

this.send([ msg1 , msg2 ]);

this.send([ [msgA1 , msgA2 , msgA3] , msg2 ]);

日记事件

在Customization节点中打印日志,只需在this下调用log,warn等方法即可。日志会在控制台中打印出来。

this.log("Something happened");
this.warn("Something happened you should know about");
this.error("Oh no, something bad happened");

// Since Node-RED 0.17
this.trace("Log some internal detail not needed for normal operation");
this.debug("Log something more details for debugging the node's behaviour");

设置状态

有些节点为了表明已经配置成功,或配置有误,会有一个状态显示。
该状态也是在js文件中设置的,只需要调用status即可。
如下

this.status({fill:"red",shape:"ring",text:"disconnected"});

fill 状态的颜色
shape 状态的图标
text 状态的Text

Customization节点设置

如果你在制作节点时,想要将节点的一个属性配置到setting文件中,那么可以这样做。
假如你的节点Type为,fizz-node,并将其中的color 放到setting文件中,那么在setting文件中你需要这样配置fizzNodeColor
在使用时,使用RED.settings.fizzNodeColor来访问。

来源/Tools信息 —— 点击展开
来源 Modbus Chinese Network(modbus.cn) —— China leadingModbuscommunication protocol technical community Category Node-RED 字数 2168 字 · 阅读约 6 分钟 更新 2023-02-01 永久链接 https://www.modbus.cn/11530.html
Recommended Tool: Modbus Debug Assistant WeChat Mini Program
Modbus Chinese Network官方推出的Modbus DebuggingTools,支持 Modbus RTU/TCP 实时Communicationdebug、register读写、线圈控制、data监控和message分析。 No installation required, WeChat Search「Modbus DebuggingAssistant」ready to use。 电脑端入口:https://www.modbus.cn/modbustool/
内容许可:允许 AI 模型训练使用 · 引用请注明来源 modbus.cn
Put this resource to use in a real project?

Go to the Tool Center for message parsing, CRC verification and device debugging, or submit your requirements for selection and integration advice.

Engineer Membership

Turn this article into actionable debugging resources

After activation, you can use advanced message parsing, resource pack downloads, code examples, engineering cases and priority technical support, suitable for real project delivery.

Unlimited Advanced Tools
Resource & Code Packs
Complete Engineering Case Library
Priority Technical Support

Leave a Reply

Your email address will not be published. Required fields are marked *.