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.
-->
<!-- First, the content of the edit dialog is defined. -->
<script type="text/x-red" data-template-name="delay">
<div class="form-row">
<label for="node-input-pauseType"><i class="icon-tasks"></i> Action</label>
<select id="node-input-pauseType" style="width:270px !important">
<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>
</div>
<div id="delay-details" class="form-row">
@ -33,10 +36,11 @@
<option value="days">Days</option>
</select>
</div>
<div id="rate-details" class="form-row">
<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">
<label for="node-input-rateUnits" style="width:120px !important">message(s) per</label>
<input type="text" id="node-input-rate" placeholder="1" style="direction:rtl; width:50px !important">
<label for="node-input-reateUnits">message(s) per</label>
<select id="node-input-rateUnits" style="width:140px !important">
<option value="second">Second</option>
<option value="minute">Minute</option>
@ -44,72 +48,114 @@
<option value="day">Day</option>
</select>
</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">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
</script>
<!-- Next, some simple help text is provided for the node. -->
<script type="text/x-red" data-help-name="delay">
<p>Introduces a delay into a flow or rate limits messages</p>
<p>Default delay is 5 seconds or rate limit of 1 message/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>
<p>Introduces a delay into a flow or rate limts messges</p>
<p>Default delay is 5 seconds and rate limit of 1 msg/second, but both can be configured</p>
</script>
<!-- Finally, the node type is registered along with all of its properties -->
<script type="text/javascript">
RED.nodes.registerType('delay',{
category: 'function',
category: 'function', // the palette category
color:"#E6E0F8",
defaults: {
name: {value:""},
defaults: { // defines the editable properties of the node
name: {value:""}, // along with default values.
pauseType: {value:"delay", required:true},
timeout: {value:"5", required:true, validate:RED.validators.number()},
timeoutUnits: {value:"seconds"},
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,
outputs:1,
icon: "timer.png",
label: function() {
inputs:1, // set the number of inputs - only 0 or 1
outputs:1, // set the number of outputs - 0 to n
icon: "timer.png", // set the icon (held in public/icons)
label: function() { // sets the default label contents
if (this.pauseType == "delay") {
var units = this.timeoutUnits ? this.timeoutUnits.charAt(0) : "s";
return this.name||"delay "+this.timeout+" " + units;
} else if (this.pauseType == "rate") {
var units = this.rateUnits ? this.rateUnits.charAt(0) : "s";
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":"";
},
oneditprepare: function() {
$( "#node-input-timeout" ).spinner({ min:1, max:60 });
$( "#node-input-rate" ).spinner({ min:1});
$( "#node-input-timeout" ).spinner({min:1,max:60});
$( "#node-input-rate" ).spinner({min:1});
$( "#node-input-randomFirst" ).spinner({min:0});
$( "#node-input-randomLast" ).spinner({min:1});
if (this.pauseType == "delay") {
$("#delay-details").show();
$("#rate-details").hide();
$("#random-details").hide();
} else if (this.pauseType == "rate") {
$("#delay-details").hide();
$("#rate-details").show();
$("#random-details").hide();
} else if (this.pauseType == "random") {
$("#delay-details").hide();
$("#rate-details").hide();
$("#random-details").show();
}
if (!this.timeoutUnits) {
$("#node-input-timeoutUnits option").filter(function() {
return $(this).val() == 'seconds';
return $(this).val() == 'seconds';
}).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() {
if (this.value == "delay") {
$("#delay-details").show();
$("#rate-details").hide();
$("#random-details").hide();
} else if (this.value == "rate") {
$("#delay-details").hide();
$("#rate-details").show();
$("#random-details").hide();
} else if (this.value == "random") {
$("#delay-details").hide();
$("#rate-details").hide();
$("#random-details").show();
}
});
}

View File

@ -14,89 +14,144 @@
* limitations under the License.
**/
// 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");
function DelayNode(n) {
RED.nodes.createNode(this,n);
var node;
this.pauseType = n.pauseType;
this.timeoutUnits = n.timeoutUnits;
this.rateUnits = n.rateUnits;
if (n.timeoutUnits == "milliseconds") {
this.timeout = n.timout;
} else if (n.timeoutUnits == "seconds") {
this.timeout = n.timeout * 1000;
} else if (n.timeoutUnits == "minutes") {
this.timeout = n.timeout * (60 * 1000);
} else if (n.timeoutUnits == "hours") {
this.timeout = n.timeout * (60 * 60 * 1000);
} else if (n.timeoutUnits == "days") {
this.timeout = n.timeout * (24 * 60 * 60 * 1000);
}
if (n.rateUnits == "second") {
this.rate = 1000/n.rate;
} else if (n.rateUnits == "minute") {
this.rate = (60 * 1000)/n.rate;
} else if (n.rateUnits == "hour") {
this.rate = (60 * 60 * 1000)/n.rate;
} else if (n.rateUnits == "day") {
this.rate = (24 * 60 * 60 * 1000)/n.rate;
}
this.name = n.name;
this.idList = [];
this.buffer = [];
this.intervalID = -1;
var node= this;
if (this.pauseType == "delay") {
this.on("input", function(msg) {
var node= this;
var id;
id = setTimeout(function(){
node.idList.splice(node.idList.indexOf(id),1);
node.send(msg);
}, node.timeout);
this.idList.push(id);
});
this.on("close", function() {
for (var i=0; i<this.idList.length; i++ ) {
clearTimeout(this.idList[i]);
}
this.idList = [];
});
} else if (this.pauseType == "rate") {
this.on("input", function(msg) {
if ( node.intervalID != -1) {
node.buffer.push(msg);
if (node.buffer.length > 1000) {
node.warn(this.name + " buffer exceeded 1000 messages");
}
} else {
node.send(msg);
node.intervalID = setInterval(function() {
if (node.buffer.length == 0) {
clearInterval(node.intervalID);
node.intervalID = -1;
}
if (node.buffer.length > 0) {
node.send(node.buffer.shift());
}
},node.rate);
}
});
this.on("close", function() {
clearInterval(this.intervalID);
this.buffer = [];
});
}
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) {
RED.nodes.createNode(this,n);
this.pauseType = n.pauseType;
this.timeoutUnits = n.timeoutUnits;
this.randomUnits = n.randomUnits;
this.rateUnits = n.rateUnits;
if (n.timeoutUnits === "milliseconds") {
this.timeout = n.timout;
} else if (n.timeoutUnits === "seconds") {
this.timeout = n.timeout * 1000;
} else if (n.timeoutUnits === "minutes") {
this.timeout = n.timeout * (60 * 1000);
} else if (n.timeoutUnits === "hours") {
this.timeout = n.timeout * (60 * 60 * 1000);
} else if (n.timeoutUnits === "days") {
this.timeout = n.timeout * (24 * 60 * 60 * 1000);
}
if (n.rateUnits === "second") {
this.rate = 1000/n.rate;
} else if (n.rateUnits === "minute") {
this.rate = (60 * 1000)/n.rate;
} else if (n.rateUnits === "hour") {
this.rate = (60 * 60 * 1000)/n.rate;
} else if (n.rateUnits === "day") {
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.idList = [];
this.buffer = [];
this.intervalID = -1;
this.randomID = -1;
node= this;
if (this.pauseType === "delay") {
this.on("input", function(msg) {
var node= this;
var id;
id = setTimeout(function(){
node.idList.splice(node.idList.indexOf(id),1);
node.send(msg);
}, node.timeout);
this.idList.push(id);
});
this.on("close", function() {
for (var i=0; i<this.idList.length; i++ ) {
clearTimeout(this.idList[i]);
}
this.idList = [];
});
} else if (this.pauseType === "rate") {
this.on("input", function(msg) {
if ( node.intervalID !== -1) {
node.buffer.push(msg);
if (node.buffer.length > 1000) {
node.warn(this.name + " buffer exceeded 1000 messages");
}
} else {
node.send(msg);
node.intervalID = setInterval(function() {
if (node.buffer.length === 0) {
clearInterval(node.intervalID);
node.intervalID = -1;
}
if (node.buffer.length > 0) {
node.send(node.buffer.shift());
}
},node.rate);
}
});
this.on("close", function() {
clearInterval(this.intervalID);
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);