Node-Red:创建服务器并共享输入

Node-Red: Create server and share input

我正在尝试为 Node-Red 创建一个新节点。基本上它是一个 udp 侦听套接字,应通过配置节点建立,并将所有传入消息传递给专用节点进行处理。 这是我的基本情况:

function udpServer(n) {
    RED.nodes.createNode(this, n);
    this.addr = n.host;
    this.port = n.port;

    var node = this;

    var socket = dgram.createSocket('udp4');

    socket.on('listening', function () {
        var address = socket.address();
        logInfo('UDP Server listening on ' + address.address + ":" + address.port);
    });

    socket.on('message', function (message, remote) {
        var bb = new ByteBuffer.fromBinary(message,1,0);
        var CoEdata = decodeCoE(bb);
        if (CoEdata.type == 'digital') { //handle digital output
            // pass to digital handling node
        }
        else if (CoEdata.type == 'analogue'){ //handle analogue output
            // pass to analogue handling node
        }
    });     

    socket.on("error", function (err) {
        logError("Socket error: " + err);
        socket.close();         
    });

    socket.bind({
        address: node.addr,
        port: node.port,
        exclusive: true
    });

    node.on("close", function(done) {
        socket.close();
    });
}
RED.nodes.registerType("myServernode", udpServer);

对于处理节点:

function ProcessAnalog(n) {
    RED.nodes.createNode(this, n);
    var node = this;
    this.serverConfig = RED.nodes.getNode(this.server);

    this.channel = n.channel;

    // how do I get the server's message here?

}
RED.nodes.registerType("process-analogue-in", ProcessAnalog);

我不知道如何将套接字接收到的消息传递给可变数量的处理节点,即多个处理节点应在服务器实例上共享。

==== 编辑更清晰 =====

我想开发一组新的节点:

一个服务器节点:

一对多处理节点

引用关于配置节点的 Node-Red 文档:

A common use of config nodes is to represent a shared connection to a remote system. In that instance, the config node may also be responsible for creating the connection and making it available to the nodes that use the config node. In such cases, the config node should also handle the close event to disconnect when the node is stopped.

据我了解,我通过 this.serverConfig = RED.nodes.getNode(this.server); 使连接可用,但我不知道如何将此连接接收到的数据传递到使用此连接的节点。

节点不知道它连接到下游的节点。

您可以从第一个节点做的最好的事情是有 2 个输出并将数字发送到一个,模拟发送到另一个。

您可以通过将数组传递给 node.send() 函数来完成此操作。

例如

//this sends output to just the first output
node.sent([msg,null]);

//this sends output to just the second output
node.send([null,msg]);

有接收消息的节点需要为input

添加一个监听器

例如

node.on('input', function(msg) {
   ...
});

所有这些都在 Node-RED 上有详细记录page

另一种选择是,如果 udpServer 节点是 config 节点,那么您需要实现自己的侦听器,最好的办法是看起来像 MQTT 节点在核心中获取池连接示例