2 * Copyright 2013, 2014 IBM Corp.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 module.exports = function(RED) {
18 var cron = require("cron");
20 function InjectNode(n) {
21 RED.nodes.createNode(this,n);
23 this.payload = n.payload;
24 this.payloadType = n.payloadType;
25 this.repeat = n.repeat;
26 this.crontab = n.crontab;
29 this.interval_id = null;
32 if (this.repeat && !isNaN(this.repeat) && this.repeat > 0) {
33 this.repeat = this.repeat * 1000;
34 this.log("repeat = "+this.repeat);
35 this.interval_id = setInterval( function() {
36 node.emit("input",{});
38 } else if (this.crontab) {
40 this.log("crontab = "+this.crontab);
41 this.cronjob = new cron.CronJob(this.crontab,
43 node.emit("input",{});
47 this.error("'cron' module not found");
52 setTimeout( function(){ node.emit("input",{}); }, 100);
55 this.on("input",function(msg) {
56 var msg = {topic:this.topic};
57 if ( (this.payloadType == null && this.payload == "") || this.payloadType == "date") {
58 msg.payload = Date.now();
59 } else if (this.payloadType == null || this.payloadType == "string") {
60 msg.payload = this.payload;
69 RED.nodes.registerType("inject",InjectNode);
71 InjectNode.prototype.close = function() {
72 if (this.interval_id != null) {
73 clearInterval(this.interval_id);
74 this.log("inject: repeat stopped");
75 } else if (this.cronjob != null) {
77 this.log("inject: cronjob stopped");
82 RED.httpAdmin.post("/inject/:id", function(req,res) {
83 var node = RED.nodes.getNode(req.params.id);
90 node.error("Inject failed:"+err);
91 console.log(err.stack);