[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / core_nodes / core / 20-inject.js
1 /**
2  * Copyright 2013, 2014 IBM Corp.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  **/
16
17 module.exports = function(RED) {
18     var cron = require("cron");
19     
20     function InjectNode(n) {
21         RED.nodes.createNode(this,n);
22         this.topic = n.topic;
23         this.payload = n.payload;
24         this.payloadType = n.payloadType;
25         this.repeat = n.repeat;
26         this.crontab = n.crontab;
27         this.once = n.once;
28         var node = this;
29         this.interval_id = null;
30         this.cronjob = null;
31     
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",{});
37             }, this.repeat );
38         } else if (this.crontab) {
39             if (cron) {
40                 this.log("crontab = "+this.crontab);
41                 this.cronjob = new cron.CronJob(this.crontab,
42                     function() {
43                         node.emit("input",{});
44                     },
45                     null,true);
46             } else {
47                 this.error("'cron' module not found");
48             }
49         }
50     
51         if (this.once) {
52             setTimeout( function(){ node.emit("input",{}); }, 100);
53         }
54     
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;
61             } else {
62                 msg.payload = "";
63             }
64             this.send(msg);
65             msg = null;
66         });
67     }
68     
69     RED.nodes.registerType("inject",InjectNode);
70     
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) {
76             this.cronjob.stop();
77             this.log("inject: cronjob stopped");
78             delete this.cronjob;
79         }
80     }
81     
82     RED.httpAdmin.post("/inject/:id", function(req,res) {
83             var node = RED.nodes.getNode(req.params.id);
84             if (node != null) {
85                 try {
86                     node.receive();
87                     res.send(200);
88                 } catch(err) {
89                     res.send(500);
90                     node.error("Inject failed:"+err);
91                     console.log(err.stack);
92                 }
93             } else {
94                 res.send(404);
95             }
96     });
97 }