mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00:00 
			
		
		
		
	Let trigger node support per topic mode
This commit is contained in:
		@@ -56,6 +56,13 @@
 | 
				
			|||||||
        </ul>
 | 
					        </ul>
 | 
				
			||||||
    </div>
 | 
					    </div>
 | 
				
			||||||
    <br/>
 | 
					    <br/>
 | 
				
			||||||
 | 
					    <div class="form-row">
 | 
				
			||||||
 | 
					        <label data-i18n="trigger.for" for="node-input-bytopic"></label>
 | 
				
			||||||
 | 
					        <select id="node-input-bytopic">
 | 
				
			||||||
 | 
					            <option value="all" data-i18n="trigger.alltopics"></option>
 | 
				
			||||||
 | 
					            <option value="topic" data-i18n="trigger.bytopics"></option>
 | 
				
			||||||
 | 
					        </select>
 | 
				
			||||||
 | 
					    </div>
 | 
				
			||||||
    <div class="form-row">
 | 
					    <div class="form-row">
 | 
				
			||||||
        <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
 | 
					        <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
 | 
				
			||||||
        <input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name"></input>
 | 
					        <input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name"></input>
 | 
				
			||||||
@@ -103,6 +110,7 @@
 | 
				
			|||||||
            extend: {value:"false"},
 | 
					            extend: {value:"false"},
 | 
				
			||||||
            units: {value:"ms"},
 | 
					            units: {value:"ms"},
 | 
				
			||||||
            reset: {value:""},
 | 
					            reset: {value:""},
 | 
				
			||||||
 | 
					            bytopic: {value: "all"},
 | 
				
			||||||
            name: {value:""}
 | 
					            name: {value:""}
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        inputs:1,
 | 
					        inputs:1,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -19,6 +19,7 @@ module.exports = function(RED) {
 | 
				
			|||||||
    var mustache = require("mustache");
 | 
					    var mustache = require("mustache");
 | 
				
			||||||
    function TriggerNode(n) {
 | 
					    function TriggerNode(n) {
 | 
				
			||||||
        RED.nodes.createNode(this,n);
 | 
					        RED.nodes.createNode(this,n);
 | 
				
			||||||
 | 
					        this.bytopic = n.bytopic || "all";
 | 
				
			||||||
        this.op1 = n.op1 || "1";
 | 
					        this.op1 = n.op1 || "1";
 | 
				
			||||||
        this.op2 = n.op2 || "0";
 | 
					        this.op2 = n.op2 || "0";
 | 
				
			||||||
        this.op1type = n.op1type || "str";
 | 
					        this.op1type = n.op1type || "str";
 | 
				
			||||||
@@ -47,7 +48,7 @@ module.exports = function(RED) {
 | 
				
			|||||||
        this.extend = n.extend || "false";
 | 
					        this.extend = n.extend || "false";
 | 
				
			||||||
        this.units = n.units || "ms";
 | 
					        this.units = n.units || "ms";
 | 
				
			||||||
        this.reset = n.reset || '';
 | 
					        this.reset = n.reset || '';
 | 
				
			||||||
        this.duration = parseInt(n.duration);
 | 
					        this.duration = parseFloat(n.duration);
 | 
				
			||||||
        if (isNaN(this.duration)) {
 | 
					        if (isNaN(this.duration)) {
 | 
				
			||||||
            this.duration = 250;
 | 
					            this.duration = 250;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@@ -73,21 +74,24 @@ module.exports = function(RED) {
 | 
				
			|||||||
        //catch(e) { this.op2 = this.op2; }
 | 
					        //catch(e) { this.op2 = this.op2; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        var node = this;
 | 
					        var node = this;
 | 
				
			||||||
        var tout = null;
 | 
					        node.topics = {};
 | 
				
			||||||
        var m2;
 | 
					
 | 
				
			||||||
        this.on("input", function(msg) {
 | 
					        this.on("input", function(msg) {
 | 
				
			||||||
 | 
					            var topic = msg.topic || "_none";
 | 
				
			||||||
 | 
					            if (node.bytopic === "all") { topic = "_none"; }
 | 
				
			||||||
 | 
					            node.topics[topic] = node.topics[topic] || {};
 | 
				
			||||||
            if (msg.hasOwnProperty("reset") || ((node.reset !== '') && (msg.payload == node.reset)) ) {
 | 
					            if (msg.hasOwnProperty("reset") || ((node.reset !== '') && (msg.payload == node.reset)) ) {
 | 
				
			||||||
                if (node.loop === true) { clearInterval(tout); }
 | 
					                if (node.loop === true) { clearInterval(node.topics[topic].tout); }
 | 
				
			||||||
                else { clearTimeout(tout); }
 | 
					                else { clearTimeout(node.topics[topic].tout); }
 | 
				
			||||||
                tout = null;
 | 
					                delete node.topics[topic];
 | 
				
			||||||
                node.status({});
 | 
					                node.status({});
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            else {
 | 
					            else {
 | 
				
			||||||
                if (((!tout) && (tout !== 0)) || (node.loop === true)) {
 | 
					                if (((!node.topics[topic].tout) && (node.topics[topic].tout !== 0)) || (node.loop === true)) {
 | 
				
			||||||
                    if (node.op2type === "pay" || node.op2type === "payl") { m2 = msg.payload; }
 | 
					                    if (node.op2type === "pay" || node.op2type === "payl") { node.topics[topic].m2 = RED.util.cloneMessage(msg.payload); }
 | 
				
			||||||
                    else if (node.op2Templated) { m2 = mustache.render(node.op2,msg); }
 | 
					                    else if (node.op2Templated) { node.topics[topic].m2 = mustache.render(node.op2,msg); }
 | 
				
			||||||
                    else if (node.op2type !== "nul") {
 | 
					                    else if (node.op2type !== "nul") {
 | 
				
			||||||
                        m2 = RED.util.evaluateNodeProperty(node.op2,node.op2type,node,msg);
 | 
					                        node.topics[topic].m2 = RED.util.evaluateNodeProperty(node.op2,node.op2type,node,msg);
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    if (node.op1type === "pay") { }
 | 
					                    if (node.op1type === "pay") { }
 | 
				
			||||||
@@ -98,56 +102,58 @@ module.exports = function(RED) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
                    if (node.op1type !== "nul") { node.send(msg); }
 | 
					                    if (node.op1type !== "nul") { node.send(msg); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    if (node.duration === 0) { tout = 0; }
 | 
					                    if (node.duration === 0) { node.topics[topic].tout = 0; }
 | 
				
			||||||
                    else if (node.loop === true) {
 | 
					                    else if (node.loop === true) {
 | 
				
			||||||
                        if (tout) { clearInterval(tout); }
 | 
					                        if (node.topics[topic].tout) { clearInterval(node.topics[topic].tout); }
 | 
				
			||||||
                        if (node.op1type !== "nul") {
 | 
					                        if (node.op1type !== "nul") {
 | 
				
			||||||
                            var msg2 = RED.util.cloneMessage(msg);
 | 
					                            var msg2 = RED.util.cloneMessage(msg);
 | 
				
			||||||
                            tout = setInterval(function() { node.send(msg2); },node.duration);
 | 
					                            node.topics[topic].tout = setInterval(function() { node.send(msg2); }, node.duration);
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                    else {
 | 
					                    else {
 | 
				
			||||||
                        tout = setTimeout(function() {
 | 
					                        node.topics[topic].tout = setTimeout(function() {
 | 
				
			||||||
                            if (node.op2type !== "nul") {
 | 
					                            if (node.op2type !== "nul") {
 | 
				
			||||||
                                var msg2 = RED.util.cloneMessage(msg);
 | 
					                                var msg2 = RED.util.cloneMessage(msg);
 | 
				
			||||||
                                if (node.op2type === "flow" || node.op2type === "global") {
 | 
					                                if (node.op2type === "flow" || node.op2type === "global") {
 | 
				
			||||||
                                    m2 = RED.util.evaluateNodeProperty(node.op2,node.op2type,node,msg);
 | 
					                                    node.topics[topic].m2 = RED.util.evaluateNodeProperty(node.op2,node.op2type,node,msg);
 | 
				
			||||||
                                }
 | 
					                                }
 | 
				
			||||||
                                msg2.payload = m2;
 | 
					                                msg2.payload = node.topics[topic].m2;
 | 
				
			||||||
                                node.send(msg2);
 | 
					                                node.send(msg2);
 | 
				
			||||||
                            }
 | 
					                            }
 | 
				
			||||||
                            tout = null;
 | 
					                            delete node.topics[topic];
 | 
				
			||||||
                            node.status({});
 | 
					                            node.status({});
 | 
				
			||||||
                        },node.duration);
 | 
					                        }, node.duration);
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                    node.status({fill:"blue",shape:"dot",text:" "});
 | 
					                    node.status({fill:"blue",shape:"dot",text:" "});
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                else if ((node.extend === "true" || node.extend === true) && (node.duration > 0)) {
 | 
					                else if ((node.extend === "true" || node.extend === true) && (node.duration > 0)) {
 | 
				
			||||||
                    if (tout) { clearTimeout(tout); }
 | 
					                    if (node.topics[topic].tout) { clearTimeout(node.topics[topic].tout); }
 | 
				
			||||||
                    if (node.op2type === "payl") { m2 = msg.payload; }
 | 
					                    if (node.op2type === "payl") { node.topics[topic].m2 = RED.util.cloneMessage(msg.payload); }
 | 
				
			||||||
                    tout = setTimeout(function() {
 | 
					                    node.topics[topic].tout = setTimeout(function() {
 | 
				
			||||||
                        if (node.op2type !== "nul") {
 | 
					                        if (node.op2type !== "nul") {
 | 
				
			||||||
                            var msg2 = RED.util.cloneMessage(msg);
 | 
					                            var msg2 = RED.util.cloneMessage(msg);
 | 
				
			||||||
                            if (node.op2type === "flow" || node.op2type === "global") {
 | 
					                            if (node.op2type === "flow" || node.op2type === "global") {
 | 
				
			||||||
                                m2 = RED.util.evaluateNodeProperty(node.op2,node.op2type,node,msg);
 | 
					                                node.topics[topic].m2 = RED.util.evaluateNodeProperty(node.op2,node.op2type,node,msg);
 | 
				
			||||||
                            }
 | 
					                            }
 | 
				
			||||||
                            msg2.payload = m2;
 | 
					                            msg2.payload = node.topics[topic].m2;
 | 
				
			||||||
                            node.send(msg2);
 | 
					                            node.send(msg2);
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
                        tout = null;
 | 
					                        delete node.topics[topic];
 | 
				
			||||||
                        node.status({});
 | 
					                        node.status({});
 | 
				
			||||||
                    },node.duration);
 | 
					                    }, node.duration);
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                else {
 | 
					                else {
 | 
				
			||||||
                    if (node.op2type === "payl") { m2 = msg.payload; }
 | 
					                    if (node.op2type === "payl") { node.topics[topic].m2 = msg.payload; }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
        this.on("close", function() {
 | 
					        this.on("close", function() {
 | 
				
			||||||
            if (tout) {
 | 
					            for (var t in node.topics) {
 | 
				
			||||||
                if (node.loop === true) { clearInterval(tout); }
 | 
					                if (node.topics[t]) {
 | 
				
			||||||
                else { clearTimeout(tout); }
 | 
					                    if (node.loop === true) { clearInterval(node.topics[t].tout); }
 | 
				
			||||||
                tout = null;
 | 
					                    else { clearTimeout(node.topics[t].tout); }
 | 
				
			||||||
 | 
					                    delete node.topics[t];
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            node.status({});
 | 
					            node.status({});
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -270,6 +270,9 @@
 | 
				
			|||||||
        "wait-reset": "wait to be reset",
 | 
					        "wait-reset": "wait to be reset",
 | 
				
			||||||
        "wait-for": "wait for",
 | 
					        "wait-for": "wait for",
 | 
				
			||||||
        "wait-loop": "resend it every",
 | 
					        "wait-loop": "resend it every",
 | 
				
			||||||
 | 
					        "for": "Handling",
 | 
				
			||||||
 | 
					        "bytopics": "each msg.topic independently",
 | 
				
			||||||
 | 
					        "alltopics": "all messages",
 | 
				
			||||||
        "duration": {
 | 
					        "duration": {
 | 
				
			||||||
            "ms": "Milliseconds",
 | 
					            "ms": "Milliseconds",
 | 
				
			||||||
            "s": "Seconds",
 | 
					            "s": "Seconds",
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user