raumstatus/node/statusbot.js

152 lines
4.4 KiB
JavaScript
Raw Normal View History

2013-10-02 12:04:01 +00:00
var express = require('express'),
2013-10-20 22:04:58 +00:00
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
2013-10-14 19:26:54 +00:00
var flash = require('connect-flash');
var moment = require('moment');
2013-10-02 12:04:01 +00:00
var SnmpMac = require("./snmp-mac");
var IpPoll = require("./ip-poll");
var Flukso = require("./flukso");
2013-10-20 22:04:58 +00:00
var routes = require("./routes");
2014-07-05 23:42:11 +00:00
var path = require('path');
2013-10-02 12:04:01 +00:00
var spaceanswer = {
api: "0.13",
space: "Chaostreff Dortmund",
2014-07-05 23:11:45 +00:00
logo: "http://www.chaostreff-dortmund.de/logo/logo_ctdo.svg",
icon: {
open: "http://status.ctdo.de/img/green.png",
closed: "http://status.ctdo.de/img/red.png"
2014-07-05 23:11:45 +00:00
},
url: "http://www.chaostreff-dortmund.de/",
location: {
address: "Braunschweiger Str 22, 44145 Dortmund, Germany",
lon: 7.4649449,
lat: 51.527611
2013-10-02 12:04:01 +00:00
},
contact: {
phone: "+49 231 8 404 777",
irc: "irc://irc.hackint.eu/#ctdo",
ml: "ccc@chaostreff-dortmund.de",
twitter: "@ctdo",
issue_mail: "server-admin@chaostreff-dortmund.de"
2013-10-02 12:04:01 +00:00
},
2014-07-05 23:11:45 +00:00
issue_report_channels: [ "issue_mail" ],
state: {
open: null,
lastchange: 0,
icon: {
open: "http://status.ctdo.de/img/green.png",
closed: "http://status.ctdo.de/img/red.png"
}
},
sensors: {
power_consumption: [ {
unit: "W",
name: "mains power",
description: "overall power consumption from our space",
location: "basement",
value: 0 } ]
2013-10-02 12:04:01 +00:00
}
};
var simpleanswer = { state: "unknown", count: 0, names: [], lastchange: 0, power: 0 };
var usercountanswer = { at: 0, current_value: 0, datapoints: [
{ at: 0, value: 0 }
]};
2013-10-02 12:04:01 +00:00
2013-10-14 19:26:54 +00:00
var snmpMac = new SnmpMac("juni.ctdo.de", "ctdo23");
var ippoll = new IpPoll("switch2.raum.ctdo.de", "195.160.169.20-62 195.160.169.70-126");
var flukso = new Flukso("flukso.raum.ctdo.de", "/sensor/cf00e0b22230f4a8870af58f2b8719dd");
2013-10-14 19:26:54 +00:00
snmpMac.on('done', function (res) {
simpleanswer.names = res;
2014-07-05 23:24:49 +00:00
simpleanswer.lastchange = new Date().getTime();
2013-10-14 19:26:54 +00:00
});
ippoll.on('doneCount', function (num) {
simpleanswer.count = num;
2014-07-05 23:24:49 +00:00
simpleanswer.lastchange = new Date().getTime();
ippoll.pollState();
2013-10-14 19:26:54 +00:00
});
ippoll.on('doneState', function (state) {
spaceanswer.state.open = state;
2014-07-05 23:24:49 +00:00
spaceanswer.state.lastchange = new Date().getTime();
2013-10-21 11:39:52 +00:00
simpleanswer.state = state;
2013-10-14 19:26:54 +00:00
simpleanswer.lastchange = spaceanswer.state.lastchange;
io.sockets.emit('sdata', { data: simpleanswer });
2013-10-20 22:04:58 +00:00
});
flukso.on('done', function (num) {
simpleanswer.power = num;
spaceanswer.sensors.power_consumption[0].value = num;
io.sockets.emit('sdata', { data: simpleanswer });
});
2013-10-20 22:04:58 +00:00
io.configure(function () {
io.set('log level', 0);
});
2013-10-14 19:26:54 +00:00
function work() {
snmpMac.poll();
ippoll.pollCount();
2013-10-02 12:04:01 +00:00
}
2013-10-14 19:26:54 +00:00
setInterval(work, 60000);
work();
2013-10-14 19:26:54 +00:00
setInterval(flukso.pollPower, 5000);
flukso.pollPower();
2013-10-20 22:04:58 +00:00
app.set('snmpMac', snmpMac); //TODO: wie kann man das schoener machen? (Modul in die Routes reintun)
2013-10-14 19:26:54 +00:00
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
2013-10-20 22:04:58 +00:00
app.disable('x-powered-by');
app.configure('development', function () {
app.use(express.errorHandler());
app.locals.pretty = true;
});
2013-10-14 19:26:54 +00:00
app.use(express.static(__dirname + '/public'));
app.use(express.bodyParser());
app.use(express.cookieParser('chaostreff'));
app.use(express.session({ cookie: { maxAge: 60000 }}));
app.use(flash());
2013-10-20 22:04:58 +00:00
2013-10-14 19:26:54 +00:00
app.get('/api/spaceapi/v13', function (req, res) {
res.send(spaceanswer);
});
app.get('/api/simple/v2', function (req, res) {
res.send(simpleanswer);
});
app.get('/api/usercount', function (req, res) {
ippoll.getHistory("-inf", "+inf", function (data) {
usercountanswer.datapoints.length = 0;
usercountanswer.at = simpleanswer.lastchange;
usercountanswer.current_value = simpleanswer.count;
usercountanswer.datapoints = data;
res.send(usercountanswer);
});
});
2014-07-05 23:24:49 +00:00
app.get('/api/simple/image', function (req, res) {
if (spaceanswer.state.open == true) {
res.sendfile(path.resolve(__dirname, 'public/img/green.png'));
} else if (spaceanswer.state.open == false) {
res.sendfile(path.resolve(__dirname, 'public/img/red.png'));
} else {
res.sendfile(path.resolve(__dirname, 'public/img/yellow.png'));
}
2014-07-05 23:24:49 +00:00
});
2013-10-14 19:26:54 +00:00
2013-10-20 22:04:58 +00:00
app.get('/db', routes.db);
app.post('/form', routes.form);
app.get('/', routes.index);
2013-10-14 19:26:54 +00:00
server.listen(3000, 'localhost');
2013-10-14 19:26:54 +00:00
2013-10-02 12:04:01 +00:00