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

Inject node - let once delay be editable (#1541)

* inject once with delay

* test for inject delay at once works

* give access to the once delay of the inject node

* change event not needed in HTML

* code review with Dave

* rename test

* tests for default and optional delay

* test once with delay and repeat
This commit is contained in:
Klaus Landsdorf 2018-01-11 22:50:53 +01:00 committed by Nick O'Leary
parent af5df890a5
commit 4ff6e792cd
4 changed files with 111 additions and 29 deletions

View File

@ -26,6 +26,14 @@
<input type="text" id="node-input-topic">
</div>
<div class="form-row" id="node-once">
<label for="node-input-once">&nbsp;</label>
<input type="checkbox" id="node-input-once" style="display:inline-block;width:15px;vertical-align:baseline;">
<span data-i18n="inject.onstart"></span>&nbsp;
<input type="text" id="node-input-onceDelay" placeholder="0.1" style="width:45px">&nbsp;
<span data-i18n="inject.onceDelay"></span>
</div>
<div class="form-row">
<label for=""><i class="fa fa-repeat"></i> <span data-i18n="inject.label.repeat"></span></label>
<select id="inject-time-type-select">
@ -106,13 +114,6 @@
</div>
</div>
</div>
<div class="form-row" id="node-once">
<label>&nbsp;</label>
<input type="checkbox" id="node-input-once" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-once" style="width: 70%;" data-i18n="inject.onstart"></label>
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
@ -183,7 +184,8 @@ If you want every 20 minutes from now - use the <i>"interval"</i> option.</p>
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,

View File

@ -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);

View File

@ -59,7 +59,8 @@
"Sunday"
],
"on": "on",
"onstart": "Inject once at start?",
"onstart": "Inject once after",
"onceDelay": "seconds, then",
"tip": "<b>Note:</b> \"interval between times\" and \"at a specific time\" will use cron.<br/>See info box for details.",
"success": "Successfully injected: __label__",
"errors": {

View File

@ -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",