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

add rate option to queue and timed modes. so a simple check box instead.

This commit is contained in:
Dave Conway-Jones 2021-04-22 11:02:34 +01:00
parent 87e816a7f5
commit 93971537b4
No known key found for this signature in database
GPG Key ID: 88BA2B8A411BE9FF
4 changed files with 30 additions and 19 deletions

View File

@ -63,7 +63,6 @@
<label></label> <label></label>
<select id="node-input-rate-type" style="width:270px !important"> <select id="node-input-rate-type" style="width:270px !important">
<option value="all" data-i18n="delay.limitall"></option> <option value="all" data-i18n="delay.limitall"></option>
<option value="allo" data-i18n="delay.limitallo"></option>
<option value="topic" data-i18n="delay.limittopic"></option> <option value="topic" data-i18n="delay.limittopic"></option>
</select> </select>
</div> </div>
@ -79,8 +78,11 @@
<option value="day" data-i18n="delay.label.units.day.singular"></option> <option value="day" data-i18n="delay.label.units.day.singular"></option>
</select> </select>
</div> </div>
<div class="form-row" id="rate-override">
<label></label><input style="width:30px; vertical-align:baseline;" type="checkbox" id="node-input-allowrate"><label style="width: 250px;" for="node-input-allowrate" data-i18n="delay.allowrate"></label>
</div>
<div class="form-row" id="rate-details-drop"> <div class="form-row" id="rate-details-drop">
<label></label><input style="width: 30px;" type="checkbox" id="node-input-drop"><label style="width: 250px;" for="node-input-drop" data-i18n="delay.dropmsg"></label> <label></label><input style="width:30px;; vertical-align:baseline;" type="checkbox" id="node-input-drop"><label style="width: 250px;" for="node-input-drop" data-i18n="delay.dropmsg"></label>
</div> </div>
<div class="form-row" id="rate-details-per-topic"> <div class="form-row" id="rate-details-per-topic">
<label></label> <label></label>
@ -111,7 +113,8 @@
randomFirst: {value:"1", required:true, validate:function(v) { return RED.validators.number(v) && (v >= 0); }}, 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); }}, randomLast: {value:"5", required:true, validate:function(v) { return RED.validators.number(v) && (v >= 0); }},
randomUnits: {value: "seconds"}, randomUnits: {value: "seconds"},
drop: {value:false} drop: {value:false},
allowrate: {value:false}
}, },
inputs:1, inputs:1,
outputs:1, outputs:1,
@ -130,7 +133,7 @@
return this._("delay.label.random"); return this._("delay.label.random");
} else { } else {
var rate = this.rate+" msg/"+(this.rateUnits ? (this.nbRateUnits > 1 ? this.nbRateUnits : '') + this.rateUnits.charAt(0) : "s"); 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; return this._("delay.label.limit")+" "+rate;
} else if (this.pauseType == "timed") { } else if (this.pauseType == "timed") {
return this._("delay.label.limitTopic")+" "+rate; return this._("delay.label.limitTopic")+" "+rate;
@ -197,9 +200,6 @@
} else if (this.pauseType == "rate") { } else if (this.pauseType == "rate") {
$("#node-input-delay-action").val('rate'); $("#node-input-delay-action").val('rate');
$("#node-input-rate-type").val('all'); $("#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") { } else if (this.pauseType == "queue") {
$("#node-input-delay-action").val('rate'); $("#node-input-delay-action").val('rate');
$("#node-input-rate-type").val('topic'); $("#node-input-rate-type").val('topic');
@ -246,7 +246,7 @@
}).trigger("change"); }).trigger("change");
$("#node-input-rate-type").on("change", function() { $("#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) $("#node-input-drop").attr('disabled',false).next().css("opacity",1)
$("#rate-details-per-topic").hide(); $("#rate-details-per-topic").hide();
} else if (this.value === "topic") { } else if (this.value === "topic") {
@ -263,8 +263,6 @@
action = $("#node-input-rate-type").val(); action = $("#node-input-rate-type").val();
if (action === "all") { if (action === "all") {
this.pauseType = "rate"; this.pauseType = "rate";
} else if (action === "allo") {
this.pauseType = "ratev";
} else { } else {
this.pauseType = $("#node-input-rate-topic-type").val(); this.pauseType = $("#node-input-rate-topic-type").val();
} }

View File

@ -93,6 +93,8 @@ module.exports = function(RED) {
this.lastSent = null; this.lastSent = null;
this.drop = n.drop; this.drop = n.drop;
this.droppedMsgs = 0; this.droppedMsgs = 0;
this.allowrate = n.allowrate|| false;
this.fixedrate = this.rate;
var node = this; var node = this;
function ourTimeout(handler, delay, clearHandler) { function ourTimeout(handler, delay, clearHandler) {
@ -188,8 +190,7 @@ module.exports = function(RED) {
}); });
node.on("close", function() { clearDelayList(); }); node.on("close", function() { clearDelayList(); });
} }
else if ((node.pauseType === "rate")||(node.pauseType === "ratev")) { else if (node.pauseType === "rate") {
var allowrate = (node.pauseType === "ratev") ? true : false;
node.on("input", function(msg, send, done) { node.on("input", function(msg, send, done) {
if (msg.hasOwnProperty("reset")) { if (msg.hasOwnProperty("reset")) {
if (node.intervalID !== -1 ) { if (node.intervalID !== -1 ) {
@ -198,6 +199,7 @@ module.exports = function(RED) {
} }
delete node.lastSent; delete node.lastSent;
node.buffer = []; node.buffer = [];
node.rate = node.fixedrate;
node.status({text:"reset"}); node.status({text:"reset"});
done(); done();
return; return;
@ -207,7 +209,7 @@ module.exports = function(RED) {
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) {
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; node.rate = msg.rate;
clearInterval(node.intervalID); clearInterval(node.intervalID);
node.intervalID = setInterval(sendMsgFromBuffer, node.rate); node.intervalID = setInterval(sendMsgFromBuffer, node.rate);
@ -222,7 +224,7 @@ module.exports = function(RED) {
} }
} }
else { else {
if (allowrate && msg.hasOwnProperty("rate") && !isNaN(parseFloat(msg.rate))) { if (node.allowrate && msg.hasOwnProperty("rate") && !isNaN(parseFloat(msg.rate))) {
node.rate = msg.rate; node.rate = msg.rate;
} }
send(m); send(m);
@ -246,7 +248,7 @@ module.exports = function(RED) {
node.send(msg); node.send(msg);
node.intervalID = setInterval(sendMsgFromBuffer, node.rate); node.intervalID = setInterval(sendMsgFromBuffer, node.rate);
} else { } 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; node.rate = msg.rate;
clearInterval(node.intervalID); clearInterval(node.intervalID);
node.intervalID = setInterval(sendMsgFromBuffer, node.rate); node.intervalID = setInterval(sendMsgFromBuffer, node.rate);
@ -260,7 +262,7 @@ module.exports = function(RED) {
} }
} }
} else { } else {
if (allowrate && msg.hasOwnProperty("rate") && !isNaN(parseFloat(msg.rate))) { if (node.allowrate && msg.hasOwnProperty("rate") && !isNaN(parseFloat(msg.rate))) {
node.rate = msg.rate; node.rate = msg.rate;
} }
var timeSinceLast; var timeSinceLast;
@ -308,6 +310,11 @@ module.exports = function(RED) {
var hit; var hit;
node.on("input", function(msg, send, done) { 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_"; } 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
@ -328,6 +335,7 @@ module.exports = function(RED) {
msgInfo.done(); msgInfo.done();
} }
node.buffer = []; node.buffer = [];
node.rate = node.fixedrate;
node.status({text:"reset"}); node.status({text:"reset"});
done(); done();
} }

View File

@ -25,7 +25,9 @@
<dt class="optional">rate <span class="property-type">number</span></dt> <dt class="optional">rate <span class="property-type">number</span></dt>
<dd>Sets the rate value in milliseconds between messages. <dd>Sets the rate value in milliseconds between messages.
This node overwrites the existing rate value defined in the node configuration This node overwrites the existing rate value defined in the node configuration
when it receives the message which contains <code>msg.rate</code> value.</dd> when it receives the message which contains <code>msg.rate</code> value in milliSeconds.
This option only applies if the node is configured to allow the message to
override the configured default rate interval.</dd>
<dt class="optional">reset</dt> <dt class="optional">reset</dt>
<dd>If the received message has this property set to any value, all <dd>If the received message has this property set to any value, all
outstanding messages held by the node are cleared without being sent.</dd> outstanding messages held by the node are cleared without being sent.</dd>
@ -43,8 +45,10 @@
the configured time period. The status shows the number of messages currently in the queue. the configured time period. The status shows the number of messages currently in the queue.
It can optionally discard intermediate messages as they arrive.</p> It can optionally discard intermediate messages as they arrive.</p>
</p> </p>
<p> 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.</p>
<p>The rate limiting can be applied to all messages, or group them according to <p>The rate limiting can be applied to all messages, or group them according to
their <code>msg.topic</code> value. When grouping, intermerdiate messages are their <code>msg.topic</code> value. When grouping, intermediate messages are
automatically dropped. At each time interval, the node can either release automatically dropped. At each time interval, the node can either release
the most recent message for all topics, or release the most recent message the most recent message for all topics, or release the most recent message
for the next topic. for the next topic.

View File

@ -277,6 +277,7 @@
"rate": "Rate", "rate": "Rate",
"msgper": "msg(s) per", "msgper": "msg(s) per",
"dropmsg": "drop intermediate messages", "dropmsg": "drop intermediate messages",
"allowrate": "allow msg.rate (in ms) to override rate",
"label": { "label": {
"delay": "delay", "delay": "delay",
"variable": "variable", "variable": "variable",