forked from MTRNord/freifunk-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
186 lines (176 loc) · 4.45 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* Main-functionality for handling the Modules :)
*
* @main Main
* @module Main
* @author Marcel Radzio
*/
//Load needed Features
var request = require('request');
var git = require('simple-git');
var schedule = require('node-schedule');
var argv = require('yargs').argv;
var params_config = require("./configs/ircServer.json");
var autoupdate = params_config["autoupdate"];
var async = require("async");
var _ = require("lodash");
var child = require('child_process')
//LOAD MODULES DOWN HERE
require('./helper/heroku.js');
var pushbullet = require('./handlers/pushbullet.js');
var irc = require('./handlers/irc.js');
//var slack = require('./handlers/slack.js');
var getNodes = require('./handlers/parseNodes.js');
var telegram = require('./handlers/telegram.js');
//Constants
var SIGINT = "SIGINT";
var door_status2 = "1";
/**
* Get door-status from the Web
*
* @class GetData
* @return {String} Door Status
*/
function GetData(){
/**
* Actual pulled Door Status
*
* @property door_status
* @type String
*/
request.get('http://www.nordlab-ev.de/doorstate/status.txt', function (error, response, body) {
if (!error && response.statusCode === 200) {
/**
* Content of status_page
*
* @property body
* @type String
*/
door_status = body;
}else{
/**
* Fired when an error occurs...
*
* @property error
* @type String
*/
//TODO Add real Error Handler
door_status = error;
throw new Error(err)
}
//If no error -> go on
if (!error && response.statusCode === 200) {
if (door_status2 !== door_status){
//Handle first run
if (door_status2 !== "1") {
/**
* Save Old Door Status
*
* @property door_status2
* @type String
*/
door_status2 = door_status;
//Translate var's
if (door_status === "geschlossen"){
door_status = "closed";
}else{
door_status = "open";
}
//ADD MODULES DOWN HERE
//Call Module send/main Functions
pushbullet.pushbulletSend(door_status);
//irc.ircSend(door_status);
return door_status;
}else{
//Save Last Status
door_status2 = door_status;
return door_status;
}
}
}
}).setMaxListeners(0);
//Rerun Function after 10 seconds
setTimeout(function() {}, 10000);
}
//Handle "[CTRL]+[C]"
if (process.platform === "win32") {
var rl = require("readline").createInterface({
input: process.stdin,
output: process.stdout
}).setMaxListeners(0);
rl.on(SIGINT, function () {
process.emit(SIGINT);
}).setMaxListeners(0);
}
process.on(SIGINT, function () {
irc.ircStopp();
process.exit();
}).setMaxListeners(0);
/**
* Update - Pull last master from Github
*
* @method update
*/
function update(){
git.pull("origin", "master", function(err, update) {
if (err) {throw new Error(err)}
if(update && update.summary.changes) {
console.log('Start Update!');
restart();
}
});
}
/**
* Restart - Restart the Bot completly
*
* @method restart
*/
function restart(){
console.log('Daily restart!');
irc.ircEndCustom('Restart! Coming back in a few Seconds!');
child.exec('npm restart');
}
// var j = schedule.scheduleJob('59 3 * * *', function(){
// if (!argv.noupdate && autoupdate == 1) {
// //Run update at 3am and 59min
// update();
// }else{
// restart();
// }
// });
//Startup
async.auto({
ircPreload: function (callback) {
// callback has to be called by `uploadImage` when it's done
irc.ircPreload(callback)
},
ircBotCommands: function (callback) {
//Activate IRC-Bot Command Handler
irc.ircBotCommands(callback)
},
saveNodes: function (callback) {
getNodes.saveNodes(callback)
},
telegramStart: function (callback) {
telegram.start(callback)
},
GetData: function (callback) {
async.forever(
function(next) {
GetData()
callback(next)
},
function(err) {
// if next is called with a value in its first parameter, it will appear
// in here as 'err', and execution will stop.
callback(err)
}
);
}
},
function(err, result) {
if (err) {
console.log(err)
}
}
);