freifunk-bot

Modular parser for the hackerspace door status with handlers for multiple services
git clone git://archive.git.mtrnord.blog/MTRNord/freifunk-bot.git
Log | Files | Refs | README | LICENSE

main.js (4554B)


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