From 93971537b4009e8da2f47d5c1ef2ace328c03e09 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Thu, 22 Apr 2021 11:02:34 +0100 Subject: [PATCH] add rate option to queue and timed modes. so a simple check box instead. --- .../nodes/core/function/89-delay.html | 18 ++++++++--------- .../@node-red/nodes/core/function/89-delay.js | 20 +++++++++++++------ .../locales/en-US/function/89-delay.html | 10 +++++++--- .../nodes/locales/en-US/messages.json | 1 + 4 files changed, 30 insertions(+), 19 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/function/89-delay.html b/packages/node_modules/@node-red/nodes/core/function/89-delay.html index 550f69e28..9b7ad6c58 100644 --- a/packages/node_modules/@node-red/nodes/core/function/89-delay.html +++ b/packages/node_modules/@node-red/nodes/core/function/89-delay.html @@ -63,7 +63,6 @@ @@ -79,8 +78,11 @@ +
+ +
- +
@@ -111,7 +113,8 @@ randomFirst: {value:"1", required:true, validate:function(v) { return RED.validators.number(v) && (v >= 0); }}, randomLast: {value:"5", required:true, validate:function(v) { return RED.validators.number(v) && (v >= 0); }}, randomUnits: {value: "seconds"}, - drop: {value:false} + drop: {value:false}, + allowrate: {value:false} }, inputs:1, outputs:1, @@ -130,7 +133,7 @@ return this._("delay.label.random"); } else { var rate = this.rate+" msg/"+(this.rateUnits ? (this.nbRateUnits > 1 ? this.nbRateUnits : '') + this.rateUnits.charAt(0) : "s"); - if ((this.pauseType == "rate")||(this.pauseType == "ratev")) { + if (this.pauseType == "rate") { return this._("delay.label.limit")+" "+rate; } else if (this.pauseType == "timed") { return this._("delay.label.limitTopic")+" "+rate; @@ -197,9 +200,6 @@ } else if (this.pauseType == "rate") { $("#node-input-delay-action").val('rate'); $("#node-input-rate-type").val('all'); - } else if (this.pauseType == "ratev") { - $("#node-input-delay-action").val('rate'); - $("#node-input-rate-type").val('allo'); } else if (this.pauseType == "queue") { $("#node-input-delay-action").val('rate'); $("#node-input-rate-type").val('topic'); @@ -246,7 +246,7 @@ }).trigger("change"); $("#node-input-rate-type").on("change", function() { - if ((this.value === "all") || (this.value === "allo")) { + if (this.value === "all") { $("#node-input-drop").attr('disabled',false).next().css("opacity",1) $("#rate-details-per-topic").hide(); } else if (this.value === "topic") { @@ -263,8 +263,6 @@ action = $("#node-input-rate-type").val(); if (action === "all") { this.pauseType = "rate"; - } else if (action === "allo") { - this.pauseType = "ratev"; } else { this.pauseType = $("#node-input-rate-topic-type").val(); } diff --git a/packages/node_modules/@node-red/nodes/core/function/89-delay.js b/packages/node_modules/@node-red/nodes/core/function/89-delay.js index a8b1e34c9..748421862 100644 --- a/packages/node_modules/@node-red/nodes/core/function/89-delay.js +++ b/packages/node_modules/@node-red/nodes/core/function/89-delay.js @@ -93,6 +93,8 @@ module.exports = function(RED) { this.lastSent = null; this.drop = n.drop; this.droppedMsgs = 0; + this.allowrate = n.allowrate|| false; + this.fixedrate = this.rate; var node = this; function ourTimeout(handler, delay, clearHandler) { @@ -188,8 +190,7 @@ module.exports = function(RED) { }); node.on("close", function() { clearDelayList(); }); } - else if ((node.pauseType === "rate")||(node.pauseType === "ratev")) { - var allowrate = (node.pauseType === "ratev") ? true : false; + else if (node.pauseType === "rate") { node.on("input", function(msg, send, done) { if (msg.hasOwnProperty("reset")) { if (node.intervalID !== -1 ) { @@ -198,6 +199,7 @@ module.exports = function(RED) { } delete node.lastSent; node.buffer = []; + node.rate = node.fixedrate; node.status({text:"reset"}); done(); return; @@ -207,7 +209,7 @@ module.exports = function(RED) { var m = RED.util.cloneMessage(msg); delete m.flush; if (node.intervalID !== -1) { - if (allowrate && msg.hasOwnProperty("rate") && !isNaN(parseFloat(msg.rate)) && node.rate !== msg.rate) { + if (node.allowrate && msg.hasOwnProperty("rate") && !isNaN(parseFloat(msg.rate)) && node.rate !== msg.rate) { node.rate = msg.rate; clearInterval(node.intervalID); node.intervalID = setInterval(sendMsgFromBuffer, node.rate); @@ -222,7 +224,7 @@ module.exports = function(RED) { } } else { - if (allowrate && msg.hasOwnProperty("rate") && !isNaN(parseFloat(msg.rate))) { + if (node.allowrate && msg.hasOwnProperty("rate") && !isNaN(parseFloat(msg.rate))) { node.rate = msg.rate; } send(m); @@ -246,7 +248,7 @@ module.exports = function(RED) { node.send(msg); node.intervalID = setInterval(sendMsgFromBuffer, node.rate); } else { - if (allowrate && msg.hasOwnProperty("rate") && !isNaN(parseFloat(msg.rate)) && node.rate !== msg.rate) { + if (node.allowrate && msg.hasOwnProperty("rate") && !isNaN(parseFloat(msg.rate)) && node.rate !== msg.rate) { node.rate = msg.rate; clearInterval(node.intervalID); node.intervalID = setInterval(sendMsgFromBuffer, node.rate); @@ -260,7 +262,7 @@ module.exports = function(RED) { } } } else { - if (allowrate && msg.hasOwnProperty("rate") && !isNaN(parseFloat(msg.rate))) { + if (node.allowrate && msg.hasOwnProperty("rate") && !isNaN(parseFloat(msg.rate))) { node.rate = msg.rate; } var timeSinceLast; @@ -308,6 +310,11 @@ module.exports = function(RED) { var hit; node.on("input", function(msg, send, done) { + if (node.allowrate && msg.hasOwnProperty("rate") && !isNaN(parseFloat(msg.rate)) && node.rate !== msg.rate) { + node.rate = msg.rate; + clearInterval(node.intervalID); + node.intervalID = setInterval(sendMsgFromBuffer, node.rate); + } if (!msg.hasOwnProperty("topic")) { msg.topic = "_none_"; } hit = false; for (var b in node.buffer) { // check if already in queue @@ -328,6 +335,7 @@ module.exports = function(RED) { msgInfo.done(); } node.buffer = []; + node.rate = node.fixedrate; node.status({text:"reset"}); done(); } diff --git a/packages/node_modules/@node-red/nodes/locales/en-US/function/89-delay.html b/packages/node_modules/@node-red/nodes/locales/en-US/function/89-delay.html index 9c949bc7a..632cd9429 100644 --- a/packages/node_modules/@node-red/nodes/locales/en-US/function/89-delay.html +++ b/packages/node_modules/@node-red/nodes/locales/en-US/function/89-delay.html @@ -25,7 +25,9 @@
rate number
Sets the rate value in milliseconds between messages. This node overwrites the existing rate value defined in the node configuration - when it receives the message which contains msg.rate value.
+ when it receives the message which contains msg.rate value in milliSeconds. + This option only applies if the node is configured to allow the message to + override the configured default rate interval.
reset
If the received message has this property set to any value, all outstanding messages held by the node are cleared without being sent.
@@ -36,15 +38,17 @@

Details

When configured to delay messages, the delay interval can be a fixed value, a random value within a range or dynamically set for each message. - Each message is delayed independently of any other message, based on + Each message is delayed independently of any other message, based on the time of its arrival.

When configured to rate limit messages, their delivery is spread across the configured time period. The status shows the number of messages currently in the queue. It can optionally discard intermediate messages as they arrive.

+

If set to allow override of the rate, the new rate will be applied immediately, + and will remain in effect until changed again, the node is reset, or the flow is restarted.

The rate limiting can be applied to all messages, or group them according to - their msg.topic value. When grouping, intermerdiate messages are + their msg.topic value. When grouping, intermediate messages are automatically dropped. At each time interval, the node can either release the most recent message for all topics, or release the most recent message for the next topic. diff --git a/packages/node_modules/@node-red/nodes/locales/en-US/messages.json b/packages/node_modules/@node-red/nodes/locales/en-US/messages.json index ff22746e9..0f14e906d 100755 --- a/packages/node_modules/@node-red/nodes/locales/en-US/messages.json +++ b/packages/node_modules/@node-red/nodes/locales/en-US/messages.json @@ -277,6 +277,7 @@ "rate": "Rate", "msgper": "msg(s) per", "dropmsg": "drop intermediate messages", + "allowrate": "allow msg.rate (in ms) to override rate", "label": { "delay": "delay", "variable": "variable",