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

372 lines
16 KiB
HTML
Raw Normal View History

2013-09-05 16:02:48 +02:00
<!--
Copyright 2013 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<script type="text/x-red" data-template-name="inject">
<div class="form-row node-input-payload">
<label for="node-input-payload"><i class="icon-envelope"></i> Payload</label>
<input type="text" id="node-input-payload" placeholder="Payload">
</div>
2013-09-12 18:21:46 +02:00
<div class="form-row">
<label for="node-input-topic"><i class="icon-tasks"></i> Topic</label>
<input type="text" id="node-input-topic" placeholder="Topic">
</div>
2013-09-05 16:02:48 +02:00
<div class="form-row">
<label>&nbsp;</label>
<input type="checkbox" id="node-input-once" placeholder="once" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-once" style="width: 70%;">Fire once at start ?</label>
</div>
2013-09-12 18:21:46 +02:00
<div class="form-row">
<label for=""><i class="icon-repeat"></i> Repeat</label>
<select id="inject-time-type-select"><option value="none">None</option><option value="interval">interval</option><option value="interval-time">interval between times</option><option value="time">at a specific time</option></select>
<input type="hidden" id="node-input-repeat" placeholder="Payload">
<input type="hidden" id="node-input-crontab" placeholder="Payload">
</div>
2013-09-12 18:21:46 +02:00
<div class="form-row inject-time-row hidden" id="inject-time-row-interval">
every <input id="inject-time-interval-count" class="inject-time-count" value="1"></input>
<select style="width: 100px" id="inject-time-interval-units"><option value="s">seconds</option><option value="m">minutes</option><option value="h">hours</option></select><br/>
on <select disabled id="inject-time-interval-days" class="inject-time-days"></select>
</div>
2013-09-12 18:21:46 +02:00
<div class="form-row inject-time-row hidden" id="inject-time-row-interval-time">
every <input id="inject-time-interval-time-units" class="inject-time-count" value="1"></input> minutes<br/>
2013-09-12 18:21:46 +02:00
between <select id="inject-time-interval-time-start" class="inject-time-times"></select>
and <select id="inject-time-interval-time-end" class="inject-time-times"></select><br/>
on <select id="inject-time-interval-time-days" class="inject-time-days"></select>
</div>
2013-09-12 18:21:46 +02:00
<div class="form-row inject-time-row hidden" id="inject-time-row-time">
at <input id="inject-time-time" value="12:00"></input><br/>
on <select id="inject-time-time-days" class="inject-time-days"></select>
</div>
2013-09-05 16:02:48 +02:00
<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>
2013-09-12 18:21:46 +02:00
<div class="form-tips">Tip: Injects Date.now() if no payload set</div>
<script>
{
2013-09-12 18:21:46 +02:00
$("#inject-time-type-select").change(function() {
var id = $("#inject-time-type-select option:selected").val();
$(".inject-time-row").hide();
$("#inject-time-row-"+id).show();
});
2013-09-12 18:21:46 +02:00
var days = [
{v:"*",t:"every day"},
{v:"1-5",t:"Mondays to Fridays"},
{v:"6-7",t:"Saturdays and Sundays"},
{v:"1",t:"Mondays"},
{v:"2",t:"Tuesdays"},
{v:"3",t:"Wednesdays"},
{v:"4",t:"Thursdays"},
{v:"5",t:"Fridays"},
{v:"6",t:"Saturdays"},
{v:"7",t:"Sundays"}
];
$(".inject-time-days").each(function() {
for (var d in days) {
$(this).append($("<option></option>").val(days[d].v).text(days[d].t));
}
});
2013-09-12 18:21:46 +02:00
$(".inject-time-times").each(function() {
for (var i=0;i<24;i++) {
var l = (i<10?"0":"")+i+":00";
$(this).append($("<option></option>").val(i).text(l));
}
});
$(".inject-time-count").spinner({
min:1,
max:60
});
2013-09-12 18:21:46 +02:00
$("#inject-time-interval-units").change(function() {
var units = $("#inject-time-interval-units option:selected").val();
$("#inject-time-interval-days").prop("disabled",(units == "s")?"disabled":false);
$(".inject-time-count").spinner("option","max",(units == "h")?24:60);
2013-09-12 18:21:46 +02:00
});
2013-09-12 18:21:46 +02:00
$.widget( "ui.injecttimespinner", $.ui.spinner, {
options: {
// seconds
step: 60 * 1000,
// hours
page: 60,
min:0,
max:(23*60+59)*60*1000
},
_parse: function( value ) {
if ( typeof value === "string" ) {
// already a timestamp
if ( Number( value ) == value ) {
return Number( value );
}
var p = value.split(":");
return ((p[0]*60)+Number(p[1]))*60*1000;
}
return value;
},
_format: function( value ) {
var d = new Date(value);
var h = d.getHours();
var m = d.getMinutes();
2013-09-12 18:21:46 +02:00
return ((h<10)?"0":"")+h+":"+((m<10)?"0":"")+m;
}
});
2013-09-12 18:21:46 +02:00
$("#inject-time-time").injecttimespinner();
};
2013-09-05 16:02:48 +02:00
2013-09-12 18:21:46 +02:00
</script>
</script>
<style>
.inject-time-row {
padding-left: 110px;
}
.inject-time-row select {
margin: 3px 0;
}
.inject-time-days {
width: 225px;
}
.inject-time-times {
width: 90px;
}
.inject-time-row > .ui-spinner {
height: 28px;
margin: 3px 0;
border-color: rgb(204, 204, 204);
}
#inject-time-time {
margin-top: 3px;
width: 75px;
}
.inject-time-count {
width: 30px !important;
}
.
2013-09-12 18:21:46 +02:00
</style>
2013-09-05 16:02:48 +02:00
<script type="text/x-red" data-help-name="inject">
<p>Pressing the button on the left side of the node allows a message on a topic to be injected into the flow. This is mainly for test purposes.</p>
<p>If no payload is specified the payload is set to the current time in millisecs since 1970. This allows subsequent functions to perform time based actions.</p>
<p>The repeat function does what it says on the tin and continuously sends the payload every x seconds.</p>
<p>The Fire once at start option actually waits 50mS before firing to give other nodes a chance to instantiate properly.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('inject',{
category: 'input',
color:"#a6bbcf",
defaults: {
name: {value:""},
topic: {value:""},
payload: {value:""},
repeat: {value:""},
2013-09-12 18:21:46 +02:00
crontab: {value:""},
2013-09-05 16:02:48 +02:00
once: {value:false}
},
inputs:0,
outputs:1,
icon: "inject.png",
label: function() {
return this.name||this.topic||this.payload||"inject";
2013-09-05 16:02:48 +02:00
},
labelStyle: function() {
return this.name?"node_label_italic":"";
},
2013-09-12 18:21:46 +02:00
oneditprepare: function() {
var repeattype = "none";
if (this.repeat != "") {
2013-09-12 18:21:46 +02:00
repeattype = "interval";
$("#inject-time-interval-units option").filter(function() {return $(this).val() == "s";}).attr('selected',true);
$("#inject-time-interval-count").val(this.repeat);
$("#inject-time-interval-days").prop("disabled","disabled");
} else if (this.crontab) {
var cronparts = this.crontab.split(" ");
var days = cronparts[4];
if (!isNaN(cronparts[0]) && !isNaN(cronparts[1])) {
2013-09-12 18:21:46 +02:00
repeattype = "time";
// Fixed time
var time = cronparts[1]+":"+cronparts[0];
$("#inject-time-time").val(time);
$("#inject-time-type-select option").filter(function() {return $(this).val() == "s";}).attr('selected',true);
$("#inject-time-time-days option").filter(function() {return $(this).val() == days;}).attr('selected',true);
2013-09-12 18:21:46 +02:00
} else if (cronparts[0] == "0") {
// interval - hours
var hours = cronparts[1].slice(2);
repeattype = "interval";
$("#inject-time-interval-days").prop("disabled",false);
$("#inject-time-interval-days option").filter(function() {return $(this).val() == days;}).attr('selected',true);
$("#inject-time-interval-count").val(hours)
$("#inject-time-interval-units option").filter(function() {return $(this).val() == "h";}).attr('selected',true);
} else if (cronparts[1] == "*") {
// interval - minutes
var minutes = cronparts[0].slice(2);
repeattype = "interval";
$("#inject-time-interval-days").prop("disabled",false);
$("#inject-time-interval-days option").filter(function() {return $(this).val() == days;}).attr('selected',true);
$("#inject-time-interval-count").val(minutes)
$("#inject-time-interval-units option").filter(function() {return $(this).val() == "m";}).attr('selected',true);
} else {
repeattype = "interval-time";
// interval - time period
var minutes = cronparts[0].slice(2);
$("#inject-time-interval-time-units").val(minutes);
$("#inject-time-interval-time-days option").filter(function() {return $(this).val() == days;}).attr('selected',true);
var time = cronparts[1];
var timeparts = time.split(",");
var start;
var end;
if (timeparts.length == 1) {
// 0 or 0-10
var hours = timeparts[0].split("-");
if (hours.length == 1) {
start = hours[0];
end = Number(hours[0])+1;
} else {
start = hours[0];
end = (Number(hours[1])+1)%24;
}
} else {
// 23,0 or 17-23,0-10 or 23,0-2 or 17-23,0
var startparts = timeparts[0].split("-");
start = startparts[0];
2013-09-12 18:21:46 +02:00
var endparts = timeparts[1].split("-");
if (endparts.length == 1) {
end = Number(endparts[0])+1;
} else {
end = Number(endparts[1])+1;
}
}
$("#inject-time-interval-time-start option").filter(function() {return $(this).val() == start;}).attr('selected',true);
$("#inject-time-interval-time-end option").filter(function() {return $(this).val() == end;}).attr('selected',true);
}
} else {
$("#inject-time-type-select option").filter(function() {return $(this).val() == "none";}).attr('selected',true);
}
2013-09-12 18:21:46 +02:00
$(".inject-time-row").hide();
$("#inject-time-type-select option").filter(function() {return $(this).val() == repeattype;}).attr('selected',true);
$("#inject-time-row-"+repeattype).show();
2013-09-12 18:21:46 +02:00
},
oneditsave: function() {
var repeat = "";
var crontab = "";
var type = $("#inject-time-type-select option:selected").val();
if (type == "none") {
// nothing
} else if (type == "interval") {
var count = $("#inject-time-interval-count").val();
var units = $("#inject-time-interval-units option:selected").val();
var days = $("#inject-time-interval-days option:selected").val();
if (units == "s") {
repeat = count;
} else {
if (units == "m") {
crontab = "*/"+count+" * * * "+days;
} else if (units == "h") {
crontab = "0 */"+count+" * * "+days;
}
}
2013-09-12 18:21:46 +02:00
} else if (type == "interval-time") {
var count = $("#inject-time-interval-time-units").val();
var startTime = Number($("#inject-time-interval-time-start option:selected").val());
var endTime = Number($("#inject-time-interval-time-end option:selected").val());
var days = $("#inject-time-interval-time-days option:selected").val();
var timerange = "";
if (startTime == endTime) {
//TODO: invalid
repeat = 0;
crontab = "";
} else if (endTime == 0) {
timerange = startTime+"-23";
} else if (startTime+1 < endTime) {
timerange = startTime+"-"+(endTime-1);
} else if (startTime+1 == endTime) {
timerange = startTime;
} else {
var startpart = "";
var endpart = "";
if (startTime == 23) {
startpart = "23";
} else {
startpart = startTime+"-23";
}
if (endTime == 1) {
endpart = "0";
} else {
endpart = "0-"+(endTime-1);
}
timerange = startpart+","+endpart;
}
repeat = 0;
crontab = "*/"+count+" "+timerange+" * * "+days;
} else if (type == "time") {
var time = $("#inject-time-time").val();
var days = $("#inject-time-time-days option:selected").val();
var parts = time.split(":");
repeat = 0;
crontab = parts[1]+" "+parts[0]+" * * "+days;
}
2013-09-12 18:21:46 +02:00
$("#node-input-repeat").val(repeat);
$("#node-input-crontab").val(crontab);
2013-09-12 18:21:46 +02:00
},
2013-09-05 16:02:48 +02:00
button: {
onclick: function() {
var label = this.name||this.payload;
d3.xhr("inject/"+this.id).post(function(err,resp) {
if (err) {
if (err.status == 404) {
RED.notify("<strong>Error</strong>: inject node not deployed","error");
2013-09-07 15:27:41 +02:00
} else if (err.status == 500) {
RED.notify("<strong>Error</strong>: inject failed, see log for details.","error");
} else if (err.status == 0) {
2013-09-06 22:03:51 +02:00
RED.notify("<strong>Error</strong>: no response from server","error");
2013-09-07 15:27:41 +02:00
} else {
RED.notify("<strong>Error</strong>: unexpected error: ("+err.status+")"+err.response,"error");
2013-09-05 16:02:48 +02:00
}
} else if (resp.status == 200) {
RED.notify("Successfully injected: "+label,"success");
} else {
2013-09-07 15:27:41 +02:00
RED.notify("<strong>Error</strong>: unexpected response: ("+resp.status+") "+resp.response,"error");
2013-09-05 16:02:48 +02:00
}
});
}
}
});
</script>