diff --git a/nodes/core/core/20-inject.html b/nodes/core/core/20-inject.html index 9273cceae..ac653b3f9 100644 --- a/nodes/core/core/20-inject.html +++ b/nodes/core/core/20-inject.html @@ -26,6 +26,14 @@ +
+ + +   +   + +
+
- -
-
@@ -183,7 +184,8 @@ If you want every 20 minutes from now - use the "interval" option.

payloadType: {value:"date"}, repeat: {value:"", validate:function(v) { return ((v === "") || (RED.validators.number(v) && (v >= 0))) }}, crontab: {value:""}, - once: {value:false} + once: {value:false}, + onceDelay: {value:0.1} }, icon: "inject.png", inputs:0, diff --git a/nodes/core/core/20-inject.js b/nodes/core/core/20-inject.js index 85913480b..a04944d24 100644 --- a/nodes/core/core/20-inject.js +++ b/nodes/core/core/20-inject.js @@ -26,27 +26,35 @@ module.exports = function(RED) { this.repeat = n.repeat; this.crontab = n.crontab; this.once = n.once; + this.onceDelay = (n.onceDelay || 0.1) * 1000; var node = this; this.interval_id = null; this.cronjob = null; - if (this.repeat && !isNaN(this.repeat) && this.repeat > 0) { + node.repeaterSetup = function () { + if (this.repeat && !isNaN(this.repeat) && this.repeat > 0) { this.repeat = this.repeat * 1000; - if (RED.settings.verbose) { this.log(RED._("inject.repeat",this)); } - this.interval_id = setInterval( function() { - node.emit("input",{}); - }, this.repeat ); - } else if (this.crontab) { - if (RED.settings.verbose) { this.log(RED._("inject.crontab",this)); } - this.cronjob = new cron.CronJob(this.crontab, - function() { - node.emit("input",{}); - }, - null,true); - } + if (RED.settings.verbose) { + this.log(RED._("inject.repeat", this)); + } + this.interval_id = setInterval(function() { + node.emit("input", {}); + }, this.repeat); + } else if (this.crontab) { + if (RED.settings.verbose) { + this.log(RED._("inject.crontab", this)); + } + this.cronjob = new cron.CronJob(this.crontab, function() { node.emit("input", {}); }, null, true); + } + }; if (this.once) { - setTimeout( function() { node.emit("input",{}); }, 100 ); + setTimeout( function() { + node.emit("input",{}); + node.repeaterSetup(); + }, this.onceDelay); + } else { + node.repeaterSetup(); } this.on("input",function(msg) { @@ -56,7 +64,7 @@ module.exports = function(RED) { msg.payload = Date.now(); } else if (this.payloadType == null) { msg.payload = this.payload; - } else if (this.payloadType == 'none') { + } else if (this.payloadType === 'none') { msg.payload = ""; } else { msg.payload = RED.util.evaluateNodeProperty(this.payload,this.payloadType,this,msg); @@ -80,7 +88,7 @@ module.exports = function(RED) { if (RED.settings.verbose) { this.log(RED._("inject.stopped")); } delete this.cronjob; } - } + }; RED.httpAdmin.post("/inject/:id", RED.auth.needsPermission("inject.write"), function(req,res) { var node = RED.nodes.getNode(req.params.id); diff --git a/nodes/core/locales/en-US/messages.json b/nodes/core/locales/en-US/messages.json index d201174d9..346bcbbd0 100644 --- a/nodes/core/locales/en-US/messages.json +++ b/nodes/core/locales/en-US/messages.json @@ -59,7 +59,8 @@ "Sunday" ], "on": "on", - "onstart": "Inject once at start?", + "onstart": "Inject once after", + "onceDelay": "seconds, then", "tip": "Note: \"interval between times\" and \"at a specific time\" will use cron.
See info box for details.", "success": "Successfully injected: __label__", "errors": { diff --git a/test/nodes/core/core/20-inject_spec.js b/test/nodes/core/core/20-inject_spec.js index 1dbb4cd35..2137f2967 100644 --- a/test/nodes/core/core/20-inject_spec.js +++ b/test/nodes/core/core/20-inject_spec.js @@ -28,22 +28,69 @@ describe('inject node', function() { helper.unload(); }); - it('should inject once', function(done) { + it('should inject once with default delay property', function(done) { + helper.load(injectNode, [{id:"n1", type:"inject", topic: "t1", + payload:"",payloadType:"date", + once: true, wires:[["n2"]] }, + {id:"n2", type:"helper"}], + function() { + var n1 = helper.getNode("n1"); + n1.should.have.property('onceDelay', 100); + done(); + }); + }); - helper.load(injectNode, [{id:"n1", type:"inject", - payload:"payload", topic: "t1", + it('should inject once with default delay', function(done) { + var timestamp = new Date(); + timestamp.setSeconds(timestamp.getSeconds() + 1); + + helper.load(injectNode, [{id:"n1", type:"inject", topic: "t1", + payload:"",payloadType:"date", once: true, wires:[["n2"]] }, {id:"n2", type:"helper"}], function() { var n2 = helper.getNode("n2"); n2.on("input", function(msg) { msg.should.have.property('topic', 't1'); - msg.should.have.property('payload', 'payload'); + msg.should.have.property('payload'); + should(msg.payload).be.lessThan(timestamp.getTime()); done(); }); }); }); + it('should inject once with 500 msec. delay', function(done) { + helper.load(injectNode, [{id:"n1", type:"inject", topic: "t1", + payload:"",payloadType:"date", + once: true, onceDelay: 0.5, wires:[["n2"]] }, + {id:"n2", type:"helper"}], + function() { + var n1 = helper.getNode("n1"); + n1.should.have.property('onceDelay', 500); + done(); + }); + }); + + it('should inject once with delay of two seconds', function(done) { + this.timeout(2700); // have to wait for the inject with delay of two seconds + + var timestamp = new Date(); + timestamp.setSeconds(timestamp.getSeconds() + 1); + + helper.load(injectNode, [{id:"n1", type:"inject", topic: "t1", + payload:"",payloadType:"date", + once: true, onceDelay: 2, wires:[["n2"]] }, + {id:"n2", type:"helper"}], + function() { + var n2 = helper.getNode("n2"); + n2.on("input", function(msg) { + msg.should.have.property('topic', 't1'); + should(msg.payload).be.greaterThan(timestamp.getTime()); + done(); + }); + }); + }); + it('should inject repeatedly', function(done) { helper.load(injectNode, [{id:"n1", type:"inject", @@ -66,6 +113,30 @@ describe('inject node', function() { }); }); + it('should inject once with delay of two seconds and repeatedly', function(done) { + var timestamp = new Date(); + timestamp.setSeconds(timestamp.getSeconds() + 1); + + helper.load(injectNode, [{id:"n1", type:"inject", topic: "t1", + payload:"",payloadType:"date", repeat: 0.2, + once: true, onceDelay: 1.2, wires:[["n2"]] }, + {id:"n2", type:"helper"}], + function() { + var n2 = helper.getNode("n2"); + var count = 0; + n2.on("input", function(msg) { + msg.should.have.property('topic', 't1'); + should(msg.payload).be.greaterThan(timestamp.getTime()); + count += 1; + if (count > 2) { + helper.clearFlows().then(function() { + done(); + }); + } + }); + }); + }); + it('should inject with cron', function(done) { helper.load(injectNode, [{id:"n1", type:"inject", payloadType:"date", topic: "t3",