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

Merge branch 'master' of github.com:node-red/node-red

This commit is contained in:
Nicholas O'Leary 2013-11-13 15:42:58 +00:00
commit 851c2ab089
2 changed files with 204 additions and 103 deletions

View File

@ -14,12 +14,15 @@
limitations under the License. limitations under the License.
--> -->
<!-- First, the content of the edit dialog is defined. -->
<script type="text/x-red" data-template-name="delay"> <script type="text/x-red" data-template-name="delay">
<div class="form-row"> <div class="form-row">
<label for="node-input-pauseType"><i class="icon-tasks"></i> Action</label> <label for="node-input-pauseType"><i class="icon-tasks"></i> Action</label>
<select id="node-input-pauseType" style="width:270px !important"> <select id="node-input-pauseType" style="width:270px !important">
<option value="delay">Delay message</option> <option value="delay">Delay message</option>
<option value="rate">Limit message rate</option> <option value="rate">Limit rate to</option>
<option value="random">Random delay</option>
</select> </select>
</div> </div>
<div id="delay-details" class="form-row"> <div id="delay-details" class="form-row">
@ -33,10 +36,11 @@
<option value="days">Days</option> <option value="days">Days</option>
</select> </select>
</div> </div>
<div id="rate-details" class="form-row"> <div id="rate-details" class="form-row">
<label for="node-input-rate"><i class="icon-time"></i> To</label> <label for="node-input-rate"><i class="icon-time"></i> To</label>
<input type="text" id="node-input-rate" placeholder="1" style="direction:rtl; width:40px !important"> <input type="text" id="node-input-rate" placeholder="1" style="direction:rtl; width:50px !important">
<label for="node-input-rateUnits" style="width:120px !important">message(s) per</label> <label for="node-input-reateUnits">message(s) per</label>
<select id="node-input-rateUnits" style="width:140px !important"> <select id="node-input-rateUnits" style="width:140px !important">
<option value="second">Second</option> <option value="second">Second</option>
<option value="minute">Minute</option> <option value="minute">Minute</option>
@ -44,57 +48,87 @@
<option value="day">Day</option> <option value="day">Day</option>
</select> </select>
</div> </div>
<div id="random-details" class="form-row">
<label for="node-input-randomFirst"><i class="icon-time"></i> Between</label>
<input type="text" id="node-input-randomFirst" placeholder="" style="directon:rtl; width:30px !important">
<label for="node-input-randomLast" style="width:20px"> & </label>
<input type="text" id="node-input-randomLast" placeholder="" style="directon:rtl; width:30px !important">
<select id="node-input-randomUnits" style="width:140px !important">
<option value="milliseconds">Milliseconds</option>
<option value="seconds">Seconds</option>
<option value="minutes">Minutes</option>
<option value="hours">Hours</option>
<option value="days">Days</option>
</select>
</div>
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label> <label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name"> <input type="text" id="node-input-name" placeholder="Name">
</div> </div>
</script> </script>
<!-- Next, some simple help text is provided for the node. -->
<script type="text/x-red" data-help-name="delay"> <script type="text/x-red" data-help-name="delay">
<p>Introduces a delay into a flow or rate limits messages</p> <p>Introduces a delay into a flow or rate limts messges</p>
<p>Default delay is 5 seconds or rate limit of 1 message/second, but both can be configured</p> <p>Default delay is 5 seconds and rate limit of 1 msg/second, but both can be configured</p>
<p>The rate limiter will delay messages by buffering in a time released queue. A warning is generated if the buffer is larger than 1000 items.</p>
</script> </script>
<!-- Finally, the node type is registered along with all of its properties -->
<script type="text/javascript"> <script type="text/javascript">
RED.nodes.registerType('delay',{ RED.nodes.registerType('delay',{
category: 'function', category: 'function', // the palette category
color:"#E6E0F8", color:"#E6E0F8",
defaults: { defaults: { // defines the editable properties of the node
name: {value:""}, name: {value:""}, // along with default values.
pauseType: {value:"delay", required:true}, pauseType: {value:"delay", required:true},
timeout: {value:"5", required:true, validate:RED.validators.number()}, timeout: {value:"5", required:true, validate:RED.validators.number()},
timeoutUnits: {value:"seconds"}, timeoutUnits: {value:"seconds"},
rate: {value:"1", required:true, validate:RED.validators.number()}, rate: {value:"1", required:true, validate:RED.validators.number()},
rateUnits: {value: "second"} rateUnits: {value: "second"},
randomFirst: {value:"1", required:true, validate:RED.validators.number()},
randomLast: {value:"5", required:true, validate:RED.validators.number()},
randomUnits: {value: "seconds"}
}, },
inputs:1, inputs:1, // set the number of inputs - only 0 or 1
outputs:1, outputs:1, // set the number of outputs - 0 to n
icon: "timer.png", icon: "timer.png", // set the icon (held in public/icons)
label: function() { label: function() { // sets the default label contents
if (this.pauseType == "delay") { if (this.pauseType == "delay") {
var units = this.timeoutUnits ? this.timeoutUnits.charAt(0) : "s"; var units = this.timeoutUnits ? this.timeoutUnits.charAt(0) : "s";
return this.name||"delay "+this.timeout+" " + units; return this.name||"delay "+this.timeout+" " + units;
} else if (this.pauseType == "rate") { } else if (this.pauseType == "rate") {
var units = this.rateUnits ? this.rateUnits.charAt(0) : "s"; var units = this.rateUnits ? this.rateUnits.charAt(0) : "s";
return this.name||"limit "+this.rate+" msg/"+ units; return this.name||"limit "+this.rate+" msg/"+ units;
} else if (this.pauseType == "random") {
return "random";
} }
return ""; return "foo";
}, },
labelStyle: function() { labelStyle: function() { // sets the class to apply to the label
return this.name?"node_label_italic":""; return this.name?"node_label_italic":"";
}, },
oneditprepare: function() { oneditprepare: function() {
$( "#node-input-timeout" ).spinner({min:1,max:60}); $( "#node-input-timeout" ).spinner({min:1,max:60});
$( "#node-input-rate" ).spinner({min:1}); $( "#node-input-rate" ).spinner({min:1});
$( "#node-input-randomFirst" ).spinner({min:0});
$( "#node-input-randomLast" ).spinner({min:1});
if (this.pauseType == "delay") { if (this.pauseType == "delay") {
$("#delay-details").show(); $("#delay-details").show();
$("#rate-details").hide(); $("#rate-details").hide();
$("#random-details").hide();
} else if (this.pauseType == "rate") { } else if (this.pauseType == "rate") {
$("#delay-details").hide(); $("#delay-details").hide();
$("#rate-details").show(); $("#rate-details").show();
$("#random-details").hide();
} else if (this.pauseType == "random") {
$("#delay-details").hide();
$("#rate-details").hide();
$("#random-details").show();
} }
if (!this.timeoutUnits) { if (!this.timeoutUnits) {
@ -103,13 +137,25 @@
}).attr('selected', true); }).attr('selected', true);
} }
if (!this.randomUnits) {
$("#node-input-randomUnits option").filter(function() {
return $(this).val() == 'seconds';
}).attr('selected', true);
}
$("#node-input-pauseType").on("change",function() { $("#node-input-pauseType").on("change",function() {
if (this.value == "delay") { if (this.value == "delay") {
$("#delay-details").show(); $("#delay-details").show();
$("#rate-details").hide(); $("#rate-details").hide();
$("#random-details").hide();
} else if (this.value == "rate") { } else if (this.value == "rate") {
$("#delay-details").hide(); $("#delay-details").hide();
$("#rate-details").show(); $("#rate-details").show();
$("#random-details").hide();
} else if (this.value == "random") {
$("#delay-details").hide();
$("#rate-details").hide();
$("#random-details").show();
} }
}); });
} }

View File

@ -16,44 +16,80 @@
//Simple node to introduce a pause into a flow //Simple node to introduce a pause into a flow
//Require main module
var RED = require("../../red/red"); var RED = require("../../red/red");
var node;
function random() {
var wait = node.randomFirst + (node.diff * Math.random());
if (node.buffer.length > 0) {
node.send(node.buffer.pop());
node.randomID = setTimeout(random,wait);
} else {
node.randomID = -1;
}
}
//main node definition
function DelayNode(n) { function DelayNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.pauseType = n.pauseType; this.pauseType = n.pauseType;
this.timeoutUnits = n.timeoutUnits; this.timeoutUnits = n.timeoutUnits;
this.randomUnits = n.randomUnits;
this.rateUnits = n.rateUnits; this.rateUnits = n.rateUnits;
if (n.timeoutUnits == "milliseconds") { if (n.timeoutUnits === "milliseconds") {
this.timeout = n.timout; this.timeout = n.timout;
} else if (n.timeoutUnits == "seconds") { } else if (n.timeoutUnits === "seconds") {
this.timeout = n.timeout * 1000; this.timeout = n.timeout * 1000;
} else if (n.timeoutUnits == "minutes") { } else if (n.timeoutUnits === "minutes") {
this.timeout = n.timeout * (60 * 1000); this.timeout = n.timeout * (60 * 1000);
} else if (n.timeoutUnits == "hours") { } else if (n.timeoutUnits === "hours") {
this.timeout = n.timeout * (60 * 60 * 1000); this.timeout = n.timeout * (60 * 60 * 1000);
} else if (n.timeoutUnits == "days") { } else if (n.timeoutUnits === "days") {
this.timeout = n.timeout * (24 * 60 * 60 * 1000); this.timeout = n.timeout * (24 * 60 * 60 * 1000);
} }
if (n.rateUnits == "second") { if (n.rateUnits === "second") {
this.rate = 1000/n.rate; this.rate = 1000/n.rate;
} else if (n.rateUnits == "minute") { } else if (n.rateUnits === "minute") {
this.rate = (60 * 1000)/n.rate; this.rate = (60 * 1000)/n.rate;
} else if (n.rateUnits == "hour") { } else if (n.rateUnits === "hour") {
this.rate = (60 * 60 * 1000)/n.rate; this.rate = (60 * 60 * 1000)/n.rate;
} else if (n.rateUnits == "day") { } else if (n.rateUnits === "day") {
this.rate = (24 * 60 * 60 * 1000)/n.rate; this.rate = (24 * 60 * 60 * 1000)/n.rate;
} }
if (n.randomUnits === "milliseconds") {
this.randomFirst = n.randomFirst;
this.randomLast = n.randomLast;
} else if (n.randomUnits === "seconds") {
this.randomFirst = n.randomFirst * 1000;
this.randomLast = n.randomLast * 1000;
} else if (n.randomUnits === "minutes") {
this.randomFirst = n.randomFirst * (60 * 1000);
this.randomLast = n.randomLast * (60 * 1000);
} else if (n.randomUnits === "hours") {
this.randomFirst = n.randomFirst * (60 * 60 * 1000);
this.randomLast = n.randomLast * (60 * 60 * 1000);
} else if (n.randomUnits === "days") {
this.randomFirst = n.randomFirst * (24 * 60 * 60 * 1000);
this.randomLast = n.randomLast * (24 * 60 * 60 * 1000);
}
this.diff = this.randomLast - this.randomFirst;
this.name = n.name; this.name = n.name;
this.idList = []; this.idList = [];
this.buffer = []; this.buffer = [];
this.intervalID = -1; this.intervalID = -1;
var node= this; this.randomID = -1;
node= this;
if (this.pauseType == "delay") { if (this.pauseType === "delay") {
this.on("input", function(msg) { this.on("input", function(msg) {
var node= this; var node= this;
var id; var id;
@ -70,10 +106,10 @@ function DelayNode(n) {
} }
this.idList = []; this.idList = [];
}); });
} else if (this.pauseType == "rate") { } else if (this.pauseType === "rate") {
this.on("input", function(msg) { this.on("input", function(msg) {
if ( node.intervalID != -1) { if ( node.intervalID !== -1) {
node.buffer.push(msg); node.buffer.push(msg);
if (node.buffer.length > 1000) { if (node.buffer.length > 1000) {
node.warn(this.name + " buffer exceeded 1000 messages"); node.warn(this.name + " buffer exceeded 1000 messages");
@ -81,7 +117,7 @@ function DelayNode(n) {
} else { } else {
node.send(msg); node.send(msg);
node.intervalID = setInterval(function() { node.intervalID = setInterval(function() {
if (node.buffer.length == 0) { if (node.buffer.length === 0) {
clearInterval(node.intervalID); clearInterval(node.intervalID);
node.intervalID = -1; node.intervalID = -1;
} }
@ -97,6 +133,25 @@ function DelayNode(n) {
clearInterval(this.intervalID); clearInterval(this.intervalID);
this.buffer = []; this.buffer = [];
}); });
} else if (this.pauseType === "random") {
this.on("input",function(msg){
node.buffer.push(msg);
if (node.randomID === -1) {
var wait = node.randomFirst + (node.diff * Math.random());
node.randomID = setTimeout(random,wait);
}
});
this.on("close", function (){
if (this.randomID !== -1) {
clearTimeout(node.randomID);
}
});
} }
} }
//register node
RED.nodes.registerType("delay",DelayNode); RED.nodes.registerType("delay",DelayNode);