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:
parent
af5df890a5
commit
4ff6e792cd
@ -26,6 +26,14 @@
|
|||||||
<input type="text" id="node-input-topic">
|
<input type="text" id="node-input-topic">
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="form-row" id="node-once">
|
||||||
|
<label for="node-input-once"> </label>
|
||||||
|
<input type="checkbox" id="node-input-once" style="display:inline-block;width:15px;vertical-align:baseline;">
|
||||||
|
<span data-i18n="inject.onstart"></span>
|
||||||
|
<input type="text" id="node-input-onceDelay" placeholder="0.1" style="width:45px">
|
||||||
|
<span data-i18n="inject.onceDelay"></span>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for=""><i class="fa fa-repeat"></i> <span data-i18n="inject.label.repeat"></span></label>
|
<label for=""><i class="fa fa-repeat"></i> <span data-i18n="inject.label.repeat"></span></label>
|
||||||
<select id="inject-time-type-select">
|
<select id="inject-time-type-select">
|
||||||
@ -106,13 +114,6 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-row" id="node-once">
|
|
||||||
<label> </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">
|
<div class="form-row">
|
||||||
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
|
<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">
|
<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"},
|
payloadType: {value:"date"},
|
||||||
repeat: {value:"", validate:function(v) { return ((v === "") || (RED.validators.number(v) && (v >= 0))) }},
|
repeat: {value:"", validate:function(v) { return ((v === "") || (RED.validators.number(v) && (v >= 0))) }},
|
||||||
crontab: {value:""},
|
crontab: {value:""},
|
||||||
once: {value:false}
|
once: {value:false},
|
||||||
|
onceDelay: {value:0.1}
|
||||||
},
|
},
|
||||||
icon: "inject.png",
|
icon: "inject.png",
|
||||||
inputs:0,
|
inputs:0,
|
||||||
|
@ -26,27 +26,35 @@ module.exports = function(RED) {
|
|||||||
this.repeat = n.repeat;
|
this.repeat = n.repeat;
|
||||||
this.crontab = n.crontab;
|
this.crontab = n.crontab;
|
||||||
this.once = n.once;
|
this.once = n.once;
|
||||||
|
this.onceDelay = (n.onceDelay || 0.1) * 1000;
|
||||||
var node = this;
|
var node = this;
|
||||||
this.interval_id = null;
|
this.interval_id = null;
|
||||||
this.cronjob = null;
|
this.cronjob = null;
|
||||||
|
|
||||||
|
node.repeaterSetup = function () {
|
||||||
if (this.repeat && !isNaN(this.repeat) && this.repeat > 0) {
|
if (this.repeat && !isNaN(this.repeat) && this.repeat > 0) {
|
||||||
this.repeat = this.repeat * 1000;
|
this.repeat = this.repeat * 1000;
|
||||||
if (RED.settings.verbose) { this.log(RED._("inject.repeat",this)); }
|
if (RED.settings.verbose) {
|
||||||
this.interval_id = setInterval( function() {
|
this.log(RED._("inject.repeat", this));
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
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) {
|
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) {
|
this.on("input",function(msg) {
|
||||||
@ -56,7 +64,7 @@ module.exports = function(RED) {
|
|||||||
msg.payload = Date.now();
|
msg.payload = Date.now();
|
||||||
} else if (this.payloadType == null) {
|
} else if (this.payloadType == null) {
|
||||||
msg.payload = this.payload;
|
msg.payload = this.payload;
|
||||||
} else if (this.payloadType == 'none') {
|
} else if (this.payloadType === 'none') {
|
||||||
msg.payload = "";
|
msg.payload = "";
|
||||||
} else {
|
} else {
|
||||||
msg.payload = RED.util.evaluateNodeProperty(this.payload,this.payloadType,this,msg);
|
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")); }
|
if (RED.settings.verbose) { this.log(RED._("inject.stopped")); }
|
||||||
delete this.cronjob;
|
delete this.cronjob;
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
RED.httpAdmin.post("/inject/:id", RED.auth.needsPermission("inject.write"), function(req,res) {
|
RED.httpAdmin.post("/inject/:id", RED.auth.needsPermission("inject.write"), function(req,res) {
|
||||||
var node = RED.nodes.getNode(req.params.id);
|
var node = RED.nodes.getNode(req.params.id);
|
||||||
|
@ -59,7 +59,8 @@
|
|||||||
"Sunday"
|
"Sunday"
|
||||||
],
|
],
|
||||||
"on": "on",
|
"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.",
|
"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__",
|
"success": "Successfully injected: __label__",
|
||||||
"errors": {
|
"errors": {
|
||||||
|
@ -28,17 +28,64 @@ describe('inject node', function() {
|
|||||||
helper.unload();
|
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",
|
it('should inject once with default delay', function(done) {
|
||||||
payload:"payload", topic: "t1",
|
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"]] },
|
once: true, wires:[["n2"]] },
|
||||||
{id:"n2", type:"helper"}],
|
{id:"n2", type:"helper"}],
|
||||||
function() {
|
function() {
|
||||||
var n2 = helper.getNode("n2");
|
var n2 = helper.getNode("n2");
|
||||||
n2.on("input", function(msg) {
|
n2.on("input", function(msg) {
|
||||||
msg.should.have.property('topic', 't1');
|
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();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -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) {
|
it('should inject with cron', function(done) {
|
||||||
helper.load(injectNode, [{id:"n1", type:"inject",
|
helper.load(injectNode, [{id:"n1", type:"inject",
|
||||||
payloadType:"date", topic: "t3",
|
payloadType:"date", topic: "t3",
|
||||||
|
Loading…
Reference in New Issue
Block a user