1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Merge pull request #2733 from node-red-hitachi/delay-node-mapi

Messaging API support in Delay node
This commit is contained in:
Nick O'Leary 2020-12-07 13:53:51 +00:00 committed by GitHub
commit b80a7459cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 146 additions and 26 deletions

View File

@ -80,10 +80,10 @@ module.exports = function(RED) {
this.drop = n.drop; this.drop = n.drop;
var node = this; var node = this;
function ourTimeout(handler, delay) { function ourTimeout(handler, delay, clearHandler) {
var toutID = setTimeout(handler, delay); var toutID = setTimeout(handler, delay);
return { return {
clear: function() { clearTimeout(toutID); }, clear: function() { clearTimeout(toutID); clearHandler(); },
trigger: function() { clearTimeout(toutID); return handler(); } trigger: function() { clearTimeout(toutID); return handler(); }
}; };
} }
@ -113,14 +113,15 @@ module.exports = function(RED) {
} }
if (node.pauseType === "delay") { if (node.pauseType === "delay") {
node.on("input", function(msg) { node.on("input", function(msg, send, done) {
if (msg.hasOwnProperty("flush")) { flushDelayList(); } if (msg.hasOwnProperty("flush")) { flushDelayList(); done(); }
else { else {
var id = ourTimeout(function() { var id = ourTimeout(function() {
node.idList.splice(node.idList.indexOf(id),1); node.idList.splice(node.idList.indexOf(id),1);
if (node.idList.length === 0) { node.status({}); } if (node.idList.length === 0) { node.status({}); }
node.send(msg); send(msg);
}, node.timeout); done();
}, node.timeout, () => done());
node.idList.push(id); node.idList.push(id);
if ((node.timeout > 1000) && (node.idList.length !== 0)) { if ((node.timeout > 1000) && (node.idList.length !== 0)) {
node.status({fill:"blue",shape:"dot",text:" "}); node.status({fill:"blue",shape:"dot",text:" "});
@ -131,7 +132,7 @@ module.exports = function(RED) {
node.on("close", function() { clearDelayList(); }); node.on("close", function() { clearDelayList(); });
} }
else if (node.pauseType === "delayv") { else if (node.pauseType === "delayv") {
node.on("input", function(msg) { node.on("input", function(msg, send, done) {
var delayvar = Number(node.timeout); var delayvar = Number(node.timeout);
if (msg.hasOwnProperty("delay") && !isNaN(parseFloat(msg.delay))) { if (msg.hasOwnProperty("delay") && !isNaN(parseFloat(msg.delay))) {
delayvar = parseFloat(msg.delay); delayvar = parseFloat(msg.delay);
@ -140,8 +141,9 @@ module.exports = function(RED) {
var id = ourTimeout(function() { var id = ourTimeout(function() {
node.idList.splice(node.idList.indexOf(id),1); node.idList.splice(node.idList.indexOf(id),1);
if (node.idList.length === 0) { node.status({}); } if (node.idList.length === 0) { node.status({}); }
node.send(msg); send(msg);
}, delayvar); done();
}, delayvar, () => done());
node.idList.push(id); node.idList.push(id);
if ((delayvar >= 0) && (node.idList.length !== 0)) { if ((delayvar >= 0) && (node.idList.length !== 0)) {
node.status({fill:"blue",shape:"dot",text:delayvar/1000+"s"}); node.status({fill:"blue",shape:"dot",text:delayvar/1000+"s"});
@ -152,7 +154,7 @@ module.exports = function(RED) {
node.on("close", function() { clearDelayList(); }); node.on("close", function() { clearDelayList(); });
} }
else if (node.pauseType === "rate") { else if (node.pauseType === "rate") {
node.on("input", function(msg) { node.on("input", function(msg, send, done) {
if (msg.hasOwnProperty("reset")) { if (msg.hasOwnProperty("reset")) {
if (node.intervalID !== -1 ) { if (node.intervalID !== -1 ) {
clearInterval(node.intervalID); clearInterval(node.intervalID);
@ -161,17 +163,18 @@ module.exports = function(RED) {
delete node.lastSent; delete node.lastSent;
node.buffer = []; node.buffer = [];
node.status({text:"reset"}); node.status({text:"reset"});
done();
return; return;
} }
if (!node.drop) { if (!node.drop) {
var m = RED.util.cloneMessage(msg); var m = RED.util.cloneMessage(msg);
delete m.flush; delete m.flush;
if (node.intervalID !== -1) { if (node.intervalID !== -1) {
node.buffer.push(m); node.buffer.push({msg: m, send: send, done: done});
node.reportDepth(); node.reportDepth();
} }
else { else {
node.send(m); send(m);
node.reportDepth(); node.reportDepth();
node.intervalID = setInterval(function() { node.intervalID = setInterval(function() {
if (node.buffer.length === 0) { if (node.buffer.length === 0) {
@ -179,16 +182,22 @@ module.exports = function(RED) {
node.intervalID = -1; node.intervalID = -1;
} }
if (node.buffer.length > 0) { if (node.buffer.length > 0) {
node.send(node.buffer.shift()); const msgInfo = node.buffer.shift();
msgInfo.send(msgInfo.msg);
msgInfo.done();
} }
node.reportDepth(); node.reportDepth();
}, node.rate); }, node.rate);
done();
} }
if (msg.hasOwnProperty("flush")) { if (msg.hasOwnProperty("flush")) {
while (node.buffer.length > 0) { while (node.buffer.length > 0) {
node.send(node.buffer.shift()); const msgInfo = node.buffer.shift();
msgInfo.send(msgInfo.msg);
msgInfo.done();
} }
node.status({}); node.status({});
done();
} }
} }
else { else {
@ -198,17 +207,19 @@ module.exports = function(RED) {
} }
if (!node.lastSent) { // ensuring that we always send the first message if (!node.lastSent) { // ensuring that we always send the first message
node.lastSent = process.hrtime(); node.lastSent = process.hrtime();
node.send(msg); send(msg);
} }
else if ( ( (timeSinceLast[0] * SECONDS_TO_NANOS) + timeSinceLast[1] ) > (node.rate * MILLIS_TO_NANOS) ) { else if ( ( (timeSinceLast[0] * SECONDS_TO_NANOS) + timeSinceLast[1] ) > (node.rate * MILLIS_TO_NANOS) ) {
node.lastSent = process.hrtime(); node.lastSent = process.hrtime();
node.send(msg); send(msg);
} }
done();
} }
}); });
node.on("close", function() { node.on("close", function() {
clearInterval(node.intervalID); clearInterval(node.intervalID);
clearTimeout(node.busy); clearTimeout(node.busy);
node.buffer.forEach((msgInfo) => msgInfo.done());
node.buffer = []; node.buffer = [];
node.status({}); node.status({});
}); });
@ -217,57 +228,75 @@ module.exports = function(RED) {
node.intervalID = setInterval(function() { node.intervalID = setInterval(function() {
if (node.pauseType === "queue") { if (node.pauseType === "queue") {
if (node.buffer.length > 0) { if (node.buffer.length > 0) {
node.send(node.buffer.shift()); // send the first on the queue const msgInfo = node.buffer.shift();
msgInfo.send(msgInfo.msg); // send the first on the queue
msgInfo.done();
} }
} }
else { else {
while (node.buffer.length > 0) { // send the whole queue while (node.buffer.length > 0) { // send the whole queue
node.send(node.buffer.shift()); const msgInfo = node.buffer.shift();
msgInfo.send(msgInfo.msg);
msgInfo.done();
} }
} }
node.reportDepth(); node.reportDepth();
},node.rate); },node.rate);
var hit; var hit;
node.on("input", function(msg) { node.on("input", function(msg, send, done) {
if (!msg.hasOwnProperty("topic")) { msg.topic = "_none_"; } if (!msg.hasOwnProperty("topic")) { msg.topic = "_none_"; }
hit = false; hit = false;
for (var b in node.buffer) { // check if already in queue for (var b in node.buffer) { // check if already in queue
if (msg.topic === node.buffer[b].topic) { if (msg.topic === node.buffer[b].msg.topic) {
node.buffer[b] = msg; // if so - replace existing entry node.buffer[b].done();
node.buffer[b] = {msg, send, done}; // if so - replace existing entry
hit = true; hit = true;
break; break;
} }
} }
if (!hit) { if (!hit) {
node.buffer.push(msg); // if not add to end of queue node.buffer.push({msg, send, done}); // if not add to end of queue
node.reportDepth(); node.reportDepth();
} }
if (msg.hasOwnProperty("reset")) { if (msg.hasOwnProperty("reset")) {
while (node.buffer.length > 0) {
const msgInfo = node.buffer.shift();
msgInfo.done();
}
node.buffer = []; node.buffer = [];
node.status({text:"reset"}); node.status({text:"reset"});
done();
} }
if (msg.hasOwnProperty("flush")) { if (msg.hasOwnProperty("flush")) {
while (node.buffer.length > 0) { while (node.buffer.length > 0) {
node.send(node.buffer.shift()); const msgInfo = node.buffer.shift();
msgInfo.send(msgInfo.msg);
msgInfo.done();
} }
node.status({}); node.status({});
done();
} }
}); });
node.on("close", function() { node.on("close", function() {
clearInterval(node.intervalID); clearInterval(node.intervalID);
while (node.buffer.length > 0) {
const msgInfo = node.buffer.shift();
msgInfo.done();
}
node.buffer = []; node.buffer = [];
node.status({}); node.status({});
}); });
} }
else if (node.pauseType === "random") { else if (node.pauseType === "random") {
node.on("input", function(msg) { node.on("input", function(msg, send, done) {
var wait = node.randomFirst + (node.diff * Math.random()); var wait = node.randomFirst + (node.diff * Math.random());
var id = ourTimeout(function() { var id = ourTimeout(function() {
node.idList.splice(node.idList.indexOf(id),1); node.idList.splice(node.idList.indexOf(id),1);
node.send(msg); send(msg);
node.status({}); node.status({});
}, wait); done();
}, wait, () => done());
node.idList.push(id); node.idList.push(id);
if ((node.timeout >= 1000) && (node.idList.length !== 0)) { if ((node.timeout >= 1000) && (node.idList.length !== 0)) {
node.status({fill:"blue",shape:"dot",text:parseInt(wait/10)/100+"s"}); node.status({fill:"blue",shape:"dot",text:parseInt(wait/10)/100+"s"});

View File

@ -719,4 +719,95 @@ describe('delay Node', function() {
setImmediate( function() { delayNode1.receive({reset:true}); }); // reset the queue setImmediate( function() { delayNode1.receive({reset:true}); }); // reset the queue
}); });
}); });
/* Messaging API support */
function mapiDoneTestHelper(done, pauseType, drop, msgAndTimings) {
const completeNode = require("nr-test-utils").require("@node-red/nodes/core/common/24-complete.js");
const flow = [{id:"delayNode1",type:"delay",name:"delayNode", pauseType:pauseType, timeout:"1", timeoutUnits: "seconds",
rate: "1", nbRateUnits: "1", rateUnits: "second", randomFirst:"950", randomLast:"1050",randomUnits:"milliseconds",
drop: drop, wires: [[]]},
{id:"completeNode1",type:"complete",scope: ["delayNode1"],uncaught:false,wires:[["helperNode1"]]},
{id:"helperNode1",type:"helper", wires:[[]]}];
const numMsgs = msgAndTimings.length;
helper.load([delayNode, completeNode], flow, function () {
const delayNode1 = helper.getNode("delayNode1");
const helperNode1 = helper.getNode("helperNode1");
const t = Date.now();
let c = 0;
helperNode1.on("input", function (msg) {
msg.should.have.a.property('payload', msgAndTimings[c].msg.payload);
(Date.now() - t).should.be.approximately(msgAndTimings[c].avr, msgAndTimings[c].var);
c += 1;
if ( c === numMsgs) {
done();
}
});
for (let i = 0; i < numMsgs; i++) {
setImmediate( function() { delayNode1.receive(msgAndTimings[i].msg); } );
}
});
}
it('calls done when queued message is emitted (type: delay)', function(done) {
mapiDoneTestHelper(done, "delay", false, [{msg:{payload:1}, avr:1000, var:100}]);
});
it('calls done when queued message is emitted (type: delayv)', function(done) {
mapiDoneTestHelper(done, "delayv", false, [{msg:{payload:1, delay:1000}, avr:1000, var:100}]);
});
it('calls done when queued message is emitted (type: delay)', function(done) {
mapiDoneTestHelper(done, "random", false, [{msg:{payload:1}, avr:1000, var:100}]);
});
it('calls done when queued message is cleared (type: delay)', function(done) {
mapiDoneTestHelper(done, "delay", false, [{msg:{payload:1}, avr:100, var:100},
{msg:{payload:2,reset:true}, avr:100, var:100}]);
});
it('calls done when queued message is cleared (type: delayv)', function(done) {
mapiDoneTestHelper(done, "delayv", false, [{msg:{payload:1, delay:1000}, avr:100, var:100},
{msg:{payload:2, reset:true}, avr:100, var:100}]);
});
it('calls done when queued message is cleared (type: random)', function(done) {
mapiDoneTestHelper(done, "random", false, [{msg:{payload:1}, avr:100, var:100},
{msg:{payload:2,reset:true}, avr:100, var:100}]);
});
it('calls done when queued message is flushed (type: delay)', function(done) {
mapiDoneTestHelper(done, "delay", false, [{msg:{payload:1}, avr:100, var:100},
{msg:{payload:2,flush:true}, avr:100, var:100}]);
});
it('calls done when queued message is flushed (type: delayv)', function(done) {
mapiDoneTestHelper(done, "delayv", false, [{msg:{payload:1, delay:1000}, avr:100, var:100},
{msg:{payload:2, flush:true}, avr:100, var:100}]);
});
it('calls done when queued message is flushed (type: random)', function(done) {
mapiDoneTestHelper(done, "random", false, [{msg:{payload:1}, avr:100, var:100},
{msg:{payload:2,flush:true}, avr:100, var:100}]);
});
it('calls done when rated message is emitted (drop: false)', function(done) {
mapiDoneTestHelper(done, "rate", false, [{msg:{payload:1}, avr:0, var:100},
{msg:{payload:2}, avr:1000, var:100}]);
});
it('calls done when rated message is emitted (drop: true)', function(done) {
mapiDoneTestHelper(done, "rate", true, [{msg:{payload:1}, avr:0, var:100},
{msg:{payload:2}, avr:0, var:100}]);
});
it('calls done when rated message is flushed', function(done) {
mapiDoneTestHelper(done, "rate", false, [{msg:{payload:1}, avr:0, var:100},
{msg:{payload:2}, avr:0, var:100},
{msg:{payload:3,flush:true}, avr:0, var:100}]);
});
it('calls done when queued messages are sent (queue)', function(done) {
mapiDoneTestHelper(done, "queue", false, [{msg:{payload:1,topic:"a"}, avr:500, var:700},
{msg:{payload:2, topic:"b"}, avr:1500, var:700}]);
});
it('calls done when queued messages are sent (timed)', function(done) {
mapiDoneTestHelper(done, "timed", false, [{msg:{payload:1,topic:"a"}, avr:500, var:700},
{msg:{payload:2,topic:"b"}, avr:500, var:700}]);
});
it('calls done when queue is reset (queue/timed)', function(done) {
mapiDoneTestHelper(done, "timed", false, [{msg:{payload:1,topic:"a"}, avr:0, var:500},
{msg:{payload:2,reset:true}, avr:0, var:500}]);
});
it('calls done when queue is flushed (queue/timed)', function(done) {
mapiDoneTestHelper(done, "timed", false, [{msg:{payload:1,topic:"a"}, avr:0, var:500},
{msg:{payload:2,flush:true}, avr:0, var:500}]);
});
}); });