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

freeFree Technical Resource

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

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

前言

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

自定义节点JavaScript 文件

节点构建器

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

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

接受信息

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

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

}

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

接受信息

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

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();
    }
});

下发信息

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

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

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

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函数。分别有不同的使用场景。

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

this.send([ msg1 , msg2 ]);

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

日记事件

在自定义节点中打印日志,只需在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:"disconne ccted "});

fill 상태 의 색 상
shape 상태에 대한 아이 콘
text 상태 의 텍스트

노 드 설정 사용자 정의

노 드를 만들 때 설정 파 일에 노 드의 속 성을 구성 하고 싶다면 이렇게 할 수 있습니다 .
노 드 유형 이 fiz z - node 이 고 설정 파 일에 그 색 상을 넣 으면 설정 파 일에 fiz z N ode Col or 를 구성 해야합니다 . 
사용 중 인 경우 R ED . sett ings . f izz N ode Col or 를 사용하여 액세스 합니다 .

Put this resource to use in a real project?

Go to the Tool Center for message parsing, CRC 검증 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 주소 will not be published. Required fields are marked *.