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
2 changed files with 146 additions and 26 deletions

View File

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