Skip to content

Commit 19b987e

Browse files
authored
Merge pull request #22 from buglss/19-request-interval-between-times-with-variables
Extended has been msg property.
2 parents a9c9edf + 4792d1a commit 19b987e

6 files changed

Lines changed: 138 additions & 27 deletions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"id":"090a8ed6537fc771","type":"comment","z":"7cabc8e221eacc0f","name":"15 [msg]: Every at 15 seconds","info":"","x":600,"y":1980,"wires":[]},{"id":"c9134d07f9d664bd","type":"crontinject","z":"7cabc8e221eacc0f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","crontiMethod":"","crontiArgs":"[]","inputs":1,"hasButton":false,"x":770,"y":2060,"wires":[["820a49842ef3d64f"]]},{"id":"3f969eddb2d921f1","type":"crontinject","z":"7cabc8e221eacc0f","name":"Create Schedule","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"crontiMethod\":\"interval\",\"count\":\"15\",\"units\":\"s\"}","payloadType":"json","crontiMethod":"","crontiArgs":"[]","inputs":0,"hasButton":true,"x":590,"y":2020,"wires":[["c9134d07f9d664bd"]]},{"id":"e47e87616dbdb148","type":"crontinject","z":"7cabc8e221eacc0f","name":"Cancel Schedule","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"crontiSchedulePayload","payloadType":"flow","crontiMethod":"","crontiArgs":"[]","inputs":0,"hasButton":true,"x":590,"y":2100,"wires":[["c9134d07f9d664bd"]]},{"id":"820a49842ef3d64f","type":"function","z":"7cabc8e221eacc0f","name":"save schedule id","func":"if(msg.schedule && !msg.schedule.cancel) {\n if(msg.schedule.cancel) {\n // Canceled Schedule\n /* Silent is gold */\n } else {\n // Created Schedule\n flow.set(\"crontiSchedulePayload\", {\"_id\": msg.schedule.self._id})\n } \n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":940,"y":2060,"wires":[["14cb1881501d310e"]]},{"id":"14cb1881501d310e","type":"debug","z":"7cabc8e221eacc0f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1100,"y":2060,"wires":[]}]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"id":"fcc952480045ea50","type":"comment","z":"7cabc8e221eacc0f","name":"16 [msg]: Every 1 minutes in between 00:00 and 01:00 hours of everyday","info":"","x":740,"y":2220,"wires":[]},{"id":"953059a53f75c78d","type":"crontinject","z":"7cabc8e221eacc0f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","crontiMethod":"","crontiArgs":"[]","inputs":1,"hasButton":false,"x":770,"y":2300,"wires":[["f9f1f88a70a693e2"]]},{"id":"0c7f532560d9d80b","type":"crontinject","z":"7cabc8e221eacc0f","name":"Create Schedule","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"crontiMethod\":\"interval-time\",\"count\":\"1\",\"startTime\":0,\"endTime\":1,\"days\":[0,1,2,3,4,5,6]}","payloadType":"json","crontiMethod":"","crontiArgs":"[]","inputs":0,"hasButton":true,"x":590,"y":2260,"wires":[["953059a53f75c78d"]]},{"id":"3069088a95d9ecab","type":"debug","z":"7cabc8e221eacc0f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1100,"y":2300,"wires":[]},{"id":"355d505925575861","type":"crontinject","z":"7cabc8e221eacc0f","name":"Cancel Schedule","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"crontiSchedulePayload","payloadType":"flow","crontiMethod":"","crontiArgs":"[]","inputs":0,"hasButton":true,"x":590,"y":2340,"wires":[["953059a53f75c78d"]]},{"id":"f9f1f88a70a693e2","type":"function","z":"7cabc8e221eacc0f","name":"save schedule id","func":"if(msg.schedule && !msg.schedule.cancel) {\n if(msg.schedule.cancel) {\n // Canceled Schedule\n /* Silent is gold */\n } else {\n // Created Schedule\n flow.set(\"crontiSchedulePayload\", {\"_id\": msg.schedule.self._id})\n } \n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":940,"y":2300,"wires":[["3069088a95d9ecab"]]}]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"id":"633d55222fef3b54","type":"comment","z":"7cabc8e221eacc0f","name":"17 [msg]: Everyday at 12:00 clock","info":"","x":620,"y":2440,"wires":[]},{"id":"5b358aa1d98580a6","type":"crontinject","z":"7cabc8e221eacc0f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","crontiMethod":"","crontiArgs":"[]","inputs":1,"hasButton":false,"x":770,"y":2520,"wires":[["47c26bd9c40e406e"]]},{"id":"3eaca72b64e5fec4","type":"crontinject","z":"7cabc8e221eacc0f","name":"Create Schedule","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"crontiMethod\":\"time\",\"time\":\"12:00\",\"days\":[0,1,2,3,4,5,6]}","payloadType":"json","crontiMethod":"","crontiArgs":"[]","inputs":0,"hasButton":true,"x":590,"y":2480,"wires":[["5b358aa1d98580a6"]]},{"id":"7dee3ac30a28d513","type":"debug","z":"7cabc8e221eacc0f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1100,"y":2520,"wires":[]},{"id":"1983ba77f6dba9c9","type":"crontinject","z":"7cabc8e221eacc0f","name":"Cancel Schedule","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"crontiSchedulePayload","payloadType":"flow","crontiMethod":"","crontiArgs":"[]","inputs":0,"hasButton":true,"x":590,"y":2560,"wires":[["5b358aa1d98580a6"]]},{"id":"47c26bd9c40e406e","type":"function","z":"7cabc8e221eacc0f","name":"save schedule id","func":"if(msg.schedule && !msg.schedule.cancel) {\n if(msg.schedule.cancel) {\n // Canceled Schedule\n /* Silent is gold */\n } else {\n // Created Schedule\n flow.set(\"crontiSchedulePayload\", {\"_id\": msg.schedule.self._id})\n } \n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":940,"y":2520,"wires":[["7dee3ac30a28d513"]]}]

nodes/crontinject.js

Lines changed: 129 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -94,33 +94,139 @@ module.exports = function(RED) {
9494
node.status({ fill: "green", shape: "dot", text: context.crontab + " (" + context.cronjob.fireDate().toLocaleString() + ")" + " (SCNT:" + node.__repeaters__[node.id].length + ")" });
9595
} else if(context.crontiMethod) {
9696
try {
97-
let crontiArgs = context.crontiArgs
98-
try { crontiArgs = JSON.parse(crontiArgs) } catch(error) { /* Silent is gold */ }
99-
context.cronjob = cronjo({
100-
method: context.crontiMethod,
101-
job() {
102-
if(node.crontiMethod === "onIntervalTime") {
103-
let startDate = new Date(crontiArgs[0])
104-
let endDate = new Date(crontiArgs[1])
105-
if(new Date() >= endDate) {
106-
node.cronjob.cancel();
107-
delete node.cronjob;
108-
return
109-
} else if(new Date <= startDate) {
110-
return
97+
if(context.crontiMethod === "interval") {
98+
let repeat = "";
99+
let count = context.count;
100+
let units = context.units;
101+
if(units == "s") {
102+
repeat = count;
103+
} else {
104+
if(units == "m") {
105+
//crontab = "*/"+count+" * * * "+days;
106+
repeat = count * 60;
107+
} else if(units == "h") {
108+
//crontab = "0 */"+count+" * * "+days;
109+
repeat = count * 60 * 60;
110+
}
111+
}
112+
if(repeat && !isNaN(repeat) && repeat > 0) {
113+
repeat = repeat * 1000;
114+
node.debug("repeat = " + repeat);
115+
context.interval_id = setInterval(function() {
116+
node.emit("input", msg);
117+
}, repeat);
118+
node.__repeaters__[node.id].push({ _id: repeaterId, interval_id: context.interval_id })
119+
node.status({ fill: "green", shape: "dot", text: repeat + " (SCNT:" + node.__repeaters__[node.id].length + ")" });
120+
} else {
121+
node.status({ fill: "red", shape: "dot", text: "Repeat:" + repeat });
122+
}
123+
} else if(context.crontiMethod === "interval-time") {
124+
let crontab = "";
125+
let count = context.count;
126+
let startTime = Number(context.startTime);
127+
let endTime = Number(context.endTime);
128+
let days = context.days;
129+
if(days.length == 0) {
130+
crontab = "";
131+
} else {
132+
if(days.length == 7) {
133+
days = "*";
134+
} else {
135+
days = days.join(",");
136+
}
137+
let timerange = "";
138+
if(endTime == 0) {
139+
timerange = startTime + "-23";
140+
} else if(startTime + 1 < endTime) {
141+
timerange = startTime + "-" + (endTime - 1);
142+
} else if(startTime + 1 == endTime) {
143+
timerange = startTime;
144+
} else {
145+
let startpart = "";
146+
let endpart = "";
147+
if(startTime == 23) {
148+
startpart = "23";
149+
} else {
150+
startpart = startTime + "-23";
151+
}
152+
if(endTime == 1) {
153+
endpart = "0";
154+
} else {
155+
endpart = "0-" + (endTime - 1);
156+
}
157+
timerange = startpart + "," + endpart;
158+
}
159+
if(count === "0") {
160+
crontab = count + " " + timerange + " * * " + days;
161+
} else {
162+
crontab = "*/" + count + " " + timerange + " * * " + days;
163+
}
164+
}
165+
if(crontab) {
166+
node.debug("crontab = " + crontab);
167+
context.cronjob = cronjo(() => { node.emit("input", msg) }, crontab)
168+
node.__repeaters__[node.id].push({ _id: repeaterId, cronjob: context.cronjob })
169+
node.status({ fill: "green", shape: "dot", text: crontab + " (" + context.cronjob.fireDate().toLocaleString() + ")" + " (SCNT:" + node.__repeaters__[node.id].length + ")" });
170+
} else {
171+
node.status({ fill: "red", shape: "dot", text: "Crontab:" + crontab });
172+
}
173+
} else if(context.crontiMethod === "time") {
174+
let crontab = "";
175+
let time = context.time;
176+
let days = context.days;
177+
if(days.length == 0) {
178+
crontab = "";
179+
} else {
180+
if(days.length == 7) {
181+
days = "*";
182+
} else {
183+
days = days.join(",");
184+
}
185+
let parts = time.split(":");
186+
if(parts.length === 2) {
187+
parts[1] = ("00" + (parseInt(parts[1]) % 60)).substr(-2);
188+
parts[0] = ("00" + (parseInt(parts[0]) % 24)).substr(-2);
189+
crontab = parts[1] + " " + parts[0] + " * * " + days;
190+
}
191+
else { crontab = ""; }
192+
}
193+
if(crontab) {
194+
node.debug("crontab = " + crontab);
195+
context.cronjob = cronjo(() => { node.emit("input", msg) }, crontab)
196+
node.__repeaters__[node.id].push({ _id: repeaterId, cronjob: context.cronjob })
197+
node.status({ fill: "green", shape: "dot", text: crontab + " (" + context.cronjob.fireDate().toLocaleString() + ")" + " (SCNT:" + node.__repeaters__[node.id].length + ")" });
198+
} else {
199+
node.status({ fill: "red", shape: "dot", text: "Crontab:" + crontab });
200+
}
201+
} else {
202+
let crontiArgs = context.crontiArgs
203+
try { crontiArgs = JSON.parse(crontiArgs) } catch(error) { /* Silent is gold */ }
204+
context.cronjob = cronjo({
205+
method: context.crontiMethod,
206+
job() {
207+
if(node.crontiMethod === "onIntervalTime") {
208+
let startDate = new Date(crontiArgs[0])
209+
let endDate = new Date(crontiArgs[1])
210+
if(new Date() >= endDate) {
211+
node.cronjob.cancel();
212+
delete node.cronjob;
213+
return
214+
} else if(new Date <= startDate) {
215+
return
216+
}
111217
}
218+
node.emit("input", msg)
112219
}
113-
node.emit("input", msg)
220+
}, ...crontiArgs)
221+
node.__repeaters__[node.id].push({ _id: repeaterId, cronjob: context.cronjob })
222+
let crontime = context.cronjob.expression
223+
node.debug("crontab = " + crontime);
224+
let dateText = ""
225+
if(context.crontiMethod === "onIntervalTime") {
226+
dateText = "ST:" + new Date(crontiArgs[0]).toLocaleString() + " | ET:" + new Date(crontiArgs[1]).toLocaleString()
114227
}
115-
}, ...crontiArgs)
116-
node.__repeaters__[node.id].push({ _id: repeaterId, cronjob: context.cronjob })
117-
let crontime = context.cronjob.expression
118-
node.debug("crontab = " + crontime);
119-
let dateText = ""
120-
if(context.crontiMethod === "onIntervalTime") {
121-
dateText = "ST:" + new Date(crontiArgs[0]).toLocaleString() + " | ET:" + new Date(crontiArgs[1]).toLocaleString()
228+
node.status({ fill: "green", shape: "dot", text: crontime + " (" + context.cronjob.fireDate().toLocaleString() + ")" + (dateText ? (" | " + dateText) : "") + " (SCNT:" + node.__repeaters__[node.id].length + ")" });
122229
}
123-
node.status({ fill: "green", shape: "dot", text: crontime + " (" + context.cronjob.fireDate().toLocaleString() + ")" + (dateText ? (" | " + dateText) : "") + " (SCNT:" + node.__repeaters__[node.id].length + ")" });
124230
} catch(error) {
125231
node.error(error, {})
126232
}

nodes/locales/en-US/crontinject.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ <h3>Outputs</h3>
3939
<dt>schedule.self<span class="property-type">object</span></dt>
4040
<dd>It is the scheduled job itself.</dd>
4141
</dl>
42-
<h3>Inputs (msg)</h3>
42+
<h3>Inputs (msg.payload)</h3>
4343
<dl class="message-properties">
4444
<dt>_id<span class="property-type">number</span></dt>
4545
<dd>The id of the scheduled job. When this value is filled, cancellation is performed. Other parameters is ignored.</dd>
@@ -49,7 +49,8 @@ <h3>Inputs (msg)</h3>
4949
<dd>It is a valid crontime expression. If the <i>"repeat"</i> field is full, this field is not used.</dd>
5050
<dt>crontiMethod <span class="property-type">string|number</span></dt>
5151
<dd>
52-
The methods supported by the <a href="https://www.npmjs.com/package/cronti" target="_blank">cronti</a> package.
52+
count and units values ​​for the "interval" method; count, startTime, endTime and days values ​​for the "interval-time" method; For the "time" method, time and days values ​​can be used.
53+
Additionally the methods supported by the <a href="https://www.npmjs.com/package/cronti" target="_blank">cronti</a> package.
5354
The <a href="https://www.npmjs.com/package/cronjo" target="_blank">cronjo</a> package also uses the cronti package.
5455
If the <i>"crontab"</i> field is full, this field is not used.
5556
For detailed information <a href="https://www.npmjs.com/package/cronti" target="_blank">cronti</a> or <a href="https://www.npmjs.com/package You can refer to the package doc /cronjo" target="_blank">cronjo</a>.

nodes/locales/tr/crontinject.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ <h3>Çıktılar</h3>
3939
<dt>schedule.self<span class="property-type">object</span></dt>
4040
<dd>Zamanlanmış işin kendisidir.</dd>
4141
</dl>
42-
<h3>Girdiler (msg)</h3>
42+
<h3>Girdiler (msg.payload)</h3>
4343
<dl class="message-properties">
4444
<dt>_id<span class="property-type">number</span></dt>
4545
<dd>Zamanlanmış işin id numarasıdır. Bu değer doldurulduğunda iptal işlemi gerçekleştirilir. Diğer parametrelere bakılmaz.</dd>
@@ -49,7 +49,8 @@ <h3>Girdiler (msg)</h3>
4949
<dd>Geçerli bir crontime ifadesidir. Eğer <i>"repeat"</i> alanı dolu ise bu alan kullanılmaz.</dd>
5050
<dt>crontiMethod <span class="property-type">string|number</span></dt>
5151
<dd>
52-
<a href="https://www.npmjs.com/package/cronti" target="_blank">cronti</a> paketinin desteklediği metodlardır.
52+
"interval" metodu için count ve units değerleri; "interval-time" metodu için count, startTime, endTime ve days değerleri; "time" metodu için ise time ve days değerleri kullanılabilir.
53+
Bunlara ek olarak <a href="https://www.npmjs.com/package/cronti" target="_blank">cronti</a> paketinin desteklediği metodlardır.
5354
<a href="https://www.npmjs.com/package/cronjo" target="_blank">cronjo</a> paketi de cronti paketini kullanır.
5455
Eğer <i>"crontab"</i> alanı dolu ise bu alan kullanılmaz.
5556
Detaylı bilgi için <a href="https://www.npmjs.com/package/cronti" target="_blank">cronti</a> veya <a href="https://www.npmjs.com/package/cronjo" target="_blank">cronjo</a> paket dokümanına bakabilirsiniz.

0 commit comments

Comments
 (0)