Allow trigger node to use other than msg.topic to separate streams

and add test
This commit is contained in:
Dave Conway-Jones 2020-01-30 21:39:34 +00:00
parent bb12ec702a
commit 3f756aac21
No known key found for this signature in database
GPG Key ID: 302A6725C594817F
6 changed files with 71 additions and 10 deletions

View File

@ -62,10 +62,13 @@
<br/>
<div class="form-row">
<label data-i18n="trigger.for" for="node-input-bytopic"></label>
<select id="node-input-bytopic">
<select id="node-input-bytopic" style="width:120px;">
<option value="all" data-i18n="trigger.alltopics"></option>
<option value="topic" data-i18n="trigger.bytopics"></option>
</select>
<span class="form-row" id="node-trigger-property">
<input type="text" id="node-input-property" style="width:46%;"/>
</span>
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
@ -78,6 +81,7 @@
category: 'function',
color:"#E6E0F8",
defaults: {
name: {value:""},
op1: {value:"1", validate: RED.validators.typedInput("op1type")},
op2: {value:"0", validate: RED.validators.typedInput("op2type")},
op1type: {value:"val"},
@ -89,7 +93,7 @@
reset: {value:""},
bytopic: {value:"all"},
outputs: {value:1},
name: {value:""}
property: {value:"topic",required:true}
},
inputs:1,
outputs:1,
@ -110,6 +114,19 @@
},
oneditprepare: function() {
var that = this;
if (this.property === undefined) {
$("#node-input-property").val("topic");
}
$("#node-input-property").typedInput({default:'msg',types:['msg']});
$("#node-input-bytopic").on("change", function() {
console.log("BYT",$("#node-input-bytopic").val());
if ($("#node-input-bytopic").val() === "all") {
$("#node-trigger-property").hide();
} else {
$("#node-trigger-property").show();
}
});
$("#node-input-second").change(function() {
if ($("#node-input-second").is(":checked")) {
that.outputs = 2;
@ -191,8 +208,6 @@
if ($("#node-then-type").val() == "loop") {
$("#node-input-duration").val($("#node-input-duration").val() * -1);
}
}
});
</script>

View File

@ -25,6 +25,7 @@ module.exports = function(RED) {
this.op1type = n.op1type || "str";
this.op2type = n.op2type || "str";
this.second = n.second || false;
this.property = n.property || "topic";
if (this.op1type === 'val') {
if (this.op1 === 'true' || this.op1 === 'false') {
@ -112,7 +113,7 @@ module.exports = function(RED) {
});
var processMessage = function(msg) {
var topic = msg.topic || "_none";
var topic = RED.util.getMessageProperty(msg,node.property) || "_none";
var promise;
if (node.bytopic === "all") { topic = "_none"; }
node.topics[topic] = node.topics[topic] || {};

View File

@ -14,7 +14,7 @@
limitations under the License.
-->
<script type="text/x-red" data-help-name="trigger">
<script type="text/html" data-help-name="trigger">
<p>When triggered, can send a message, and then optionally a second message, unless extended or reset.</p>
<h3>Inputs</h3>
@ -40,6 +40,6 @@
progress will be cleared and no message triggered.</p>
<p>The node can be configured to resend a message at a regular interval until it
is reset by a received message.</p>
<p>Optionally, the node can be configured to treat messages with <code>msg.topic</code> as if they
are separate streams.</p>
<p>Optionally, the node can be configured to treat messages as if they are separate streams,
using a msg property to identify each stream. Default <code>msg.topic</code>.</p>
</script>

View File

@ -302,7 +302,7 @@
"wait-for": "wait for",
"wait-loop": "resend it every",
"for": "Handling",
"bytopics": "each msg.topic independently",
"bytopics": "each",
"alltopics": "all messages",
"duration": {
"ms": "Milliseconds",

View File

@ -302,7 +302,7 @@
"wait-for": "指定した時間待機",
"wait-loop": "指定した時間間隔毎に送信を繰り返す",
"for": "処理対象",
"bytopics": "msg.topic毎",
"bytopics": "毎",
"alltopics": "全メッセージ",
"duration": {
"ms": "ミリ秒",

View File

@ -378,6 +378,51 @@ describe('trigger node', function() {
});
});
it('should handle multiple other properties individually if asked to do so', function(done) {
var flow = [{"id":"n1", "type":"trigger", "name":"triggerNode", bytopic:"topic", property:"foo", op1:"1", op2:"0", op1type:"num", op2type:"num", duration:"30", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(triggerNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
var c = 0;
n2.on("input", function(msg) {
try {
c += 1;
if (c === 1) {
msg.should.have.a.property("payload", 1);
msg.should.have.a.property("foo", "A");
}
else if (c === 2) {
msg.should.have.a.property("payload", 1);
msg.should.have.a.property("foo", "B");
}
else if (c === 3) {
msg.should.have.a.property("payload", 1);
msg.should.have.a.property("foo", "C");
}
else if (c === 4) {
msg.should.have.a.property("payload", 0);
msg.should.have.a.property("foo", "A");
}
else if (c === 5) {
msg.should.have.a.property("payload", 0);
msg.should.have.a.property("foo", "B");
}
else if (c === 6) {
msg.should.have.a.property("payload", 0);
msg.should.have.a.property("foo", "C");
done();
}
} catch(err) {
done(err);
}
});
n1.emit("input", {payload:1,foo:"A"});
n1.emit("input", {payload:2,foo:"B"});
n1.emit("input", {payload:3,foo:"C"});
});
});
it('should be able to return things from flow and global context variables', function(done) {
var spy = sinon.stub(RED.util, 'evaluateNodeProperty',
function(arg1, arg2, arg3, arg4, arg5) { if (arg5) { arg5(null, arg1) } else { return arg1; } }