节点 - 红色:使用配置节点 - 每个“客户端”节点发送相同的消息

时间:2016-08-01 20:12:51

标签: node-red

this问题之后,我设法让config-node工作并将服务器共享到各自的客户端节点(类似于mqtt节点)

基本思路如下:

  • 建立UDP侦听套接字作为config-node
  • 解析客户端节点中的传入消息,根据节点的信道返回有效负载
  • 套接字始终接收以14个字节编码的所有通道,但每个客户端节点只应对其负责的通道做出反应

这是我到目前为止所做的事情,请耐心等待。因为我正在学习编码,所以我花了很多试验和错误(更多的错误)。

    function TACMIServer(n) {
    RED.nodes.createNode(this, n);
    this.host = n.host;
    this.port = n.port;
    this.cmiIP = n.cmiIP;
    this.CoEConnection = {};

    var node = this;

    node.CoEConnectionInitialize = function (handler) {
        if (node.CoEConnection.isConnected) {
            logInfo(node, 'Socket already listening on ' + this.host + ':' + this.port);
            if (handler && (typeof handler === 'function')) {
                handler(node.CoEConnection);
            }
            return node.CoEConnection;
        }
        node.CoEConnection.socket = dgram.createSocket('udp4');

        node.CoEConnection.socket.on('listening', function () {
            var address = node.CoEConnection.socket.address();
            logInfo(node, 'UDP Server listening on ' + address.address + ":" + address.port);
            //logInfo(node, 'CoEConnection1: ' + util.inspect(node.CoEConnection, { showHidden: true, depth: null }));
        });

        node.CoEConnection.socket.bind({
            address: this.host,
            port: this.port,
            exclusive: true
        });

        node.setMaxListeners(0);

        node.CoEConnection.isConnected = true;

        if (handler && (typeof handler === 'function')) {
            handler(node.CoEConnection);
        }
        return node.CoEConnection;

    };

    node.on('close', function () {
        node.CoEConnection.socket.close();
        node.CoEConnection.isConnected = false;
    });
    }
    RED.nodes.registerType("cmiCoE-server", TACMIServer);




    function TACoEInAnalogue(n) {
    RED.nodes.createNode(this, n);

    this.port = n.port;
    this.host = n.host;
    this.channel = n.channel;
    this.connection = null;
    var serverConfig = RED.nodes.getNode(n.server);

    var node = this;


    serverConfig.CoEConnectionInitialize(function (connection) {
        node.connection = connection;
        if (node.connection.isConnected) {
            node.connection.socket.on('message', function (message, remote) {
                node.connection.bb = new ByteBuffer.fromBase64(message, 1, 0);
                node.connection.bb.toBinary();
                node.connection.CoEdata = decodeCoE(node.connection.bb);
                ReadCoEdata(node.connection.CoEdata);
                //logInfo(node, 'CoEData: ' + util.inspect(node.connection, { showHidden: true, depth: null }));
            });
        } else {
            //todo: handle not connected
        }

        function ReadCoEdata(data) {
            var msg = {};
            switch (data.type) {
            case "digital":
                msg.channel = parseInt(node.channel);
                logInfo(node, 'digitalValues: ' + data.digitalValues.printDebug());
                logInfo(node, 'Channel: ' + node.channel);
                msg.payload = getDigitalChannel(msg.channel, data.digitalValues);

                msg.pdo = data.PDO;
                msg.node = data.CANnode;
                node.status({
                    fill: "green",
                    shape: "dot",
                    text: "Ch: " + node.channel + "D"
                });
                logInfo(node, 'Node: ' + node + " msg: " + msg.payload);
                node.send(msg);
                break;
            case "analog":
                var result = null;
                msg.channel = parseInt(node.channel);
                result = getAnalogueChannel(msg.channel, data.analogValues);
                msg.uname = result.name;
                msg.unit = result.unit;
                msg.payload = result.value;
                logInfo(node, 'Node: ' + node + " msg: " + msg.payload);
                node.send(msg);
                break;
            }
        }

        node.on('close', function () {
            node.connection.close();
        });

    });
    }        
    RED.nodes.registerType("cmiCoE-analogue-in", TACoEInAnalogue);

现在我的问题是每个客户端节点都发送与msg相同的数据。

如何让客户端节点A只对套接字消息的某个部分负责,而另一部分负责客户端节点B?

0 个答案:

没有答案