2013-10-02 12:04:01 +00:00
|
|
|
var express = require('express'),
|
2015-09-05 10:18:35 +00:00
|
|
|
app = express(),
|
|
|
|
server = require('http').createServer(app),
|
2015-09-16 14:04:33 +00:00
|
|
|
io = require('socket.io').listen(server);
|
2016-01-08 16:47:28 +00:00
|
|
|
var cookieParser = require('cookie-parser');
|
|
|
|
var bodyParser = require('body-parser');
|
|
|
|
var cookieSession = require('cookie-session');
|
2013-10-14 19:26:54 +00:00
|
|
|
var flash = require('connect-flash');
|
2013-10-02 12:04:01 +00:00
|
|
|
var SnmpMac = require("./snmp-mac");
|
|
|
|
var IpPoll = require("./ip-poll");
|
2014-07-23 00:16:15 +00:00
|
|
|
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 = {
|
2013-10-21 23:40:57 +00:00
|
|
|
api: "0.13",
|
2016-10-30 12:13:43 +00:00
|
|
|
ext_ccc: "chaostreff",
|
2013-10-21 23:40:57 +00:00
|
|
|
space: "Chaostreff Dortmund",
|
2014-07-05 23:11:45 +00:00
|
|
|
logo: "http://www.chaostreff-dortmund.de/logo/logo_ctdo.svg",
|
2014-07-23 00:16:15 +00:00
|
|
|
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
|
|
|
},
|
2013-10-21 23:40:57 +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
|
|
|
},
|
2013-10-21 23:40:57 +00:00
|
|
|
contact: {
|
2014-07-23 00:16:15 +00:00
|
|
|
phone: "+49 231 8 404 777",
|
2013-10-21 23:40:57 +00:00
|
|
|
irc: "irc://irc.hackint.eu/#ctdo",
|
2016-07-16 16:39:32 +00:00
|
|
|
ml: "discuss@lists.chaostreff-dortmund.de",
|
2013-10-21 23:40:57 +00:00
|
|
|
twitter: "@ctdo",
|
|
|
|
issue_mail: "server-admin@chaostreff-dortmund.de"
|
2013-10-02 12:04:01 +00:00
|
|
|
},
|
2015-09-05 10:18:35 +00:00
|
|
|
issue_report_channels: ["issue_mail"],
|
2013-10-21 23:40:57 +00:00
|
|
|
state: {
|
|
|
|
open: null,
|
2014-07-23 00:16:15 +00:00
|
|
|
lastchange: 0,
|
|
|
|
icon: {
|
|
|
|
open: "http://status.ctdo.de/img/green.png",
|
|
|
|
closed: "http://status.ctdo.de/img/red.png"
|
|
|
|
}
|
|
|
|
},
|
2016-10-29 13:24:33 +00:00
|
|
|
feeds: {
|
|
|
|
blog: {
|
|
|
|
type: "application/rss+xml",
|
|
|
|
url: "https://www.chaostreff-dortmund.de/feed/"
|
|
|
|
},
|
|
|
|
calendar: {
|
2016-10-30 12:13:43 +00:00
|
|
|
type: "ical",
|
|
|
|
url: "https://calendar.google.com/calendar/ical/chaostreffdortmund%40googlemail.com/public/basic.ics"
|
2016-10-29 13:24:33 +00:00
|
|
|
}
|
|
|
|
},
|
2014-07-23 00:16:15 +00:00
|
|
|
sensors: {
|
2015-09-05 10:18:35 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
};
|
2015-09-05 10:18:35 +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");
|
2016-04-28 15:58:12 +00:00
|
|
|
var ippoll = new IpPoll("flipdot.raum.ctdo.de", "195.160.169.30-120");
|
2014-07-23 00:16:15 +00:00
|
|
|
var flukso = new Flukso("flukso.raum.ctdo.de", "/sensor/cf00e0b22230f4a8870af58f2b8719dd");
|
2013-10-14 19:26:54 +00:00
|
|
|
|
2016-07-16 16:39:32 +00:00
|
|
|
var port = '9911';
|
2015-09-11 19:10:21 +00:00
|
|
|
var address = 'localhost';
|
|
|
|
|
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();
|
2013-10-22 23:49:08 +00:00
|
|
|
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;
|
2015-09-05 10:18:35 +00:00
|
|
|
io.sockets.emit('sdata', {data: simpleanswer});
|
2013-10-20 22:04:58 +00:00
|
|
|
});
|
|
|
|
|
2014-07-23 00:16:15 +00:00
|
|
|
flukso.on('done', function (num) {
|
|
|
|
simpleanswer.power = num;
|
|
|
|
spaceanswer.sensors.power_consumption[0].value = num;
|
2015-09-05 10:18:35 +00:00
|
|
|
io.sockets.emit('sdata', {data: simpleanswer});
|
2014-07-23 00:16:15 +00:00
|
|
|
});
|
|
|
|
|
2016-01-08 16:47:28 +00:00
|
|
|
flukso.on('failed', function (error) {
|
|
|
|
console.log("Flukso: " + error);
|
2013-10-20 22:04:58 +00:00
|
|
|
});
|
|
|
|
|
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
|
|
|
|
2014-07-23 00:16:15 +00:00
|
|
|
setInterval(work, 60000);
|
2013-10-20 23:15:35 +00:00
|
|
|
work();
|
2013-10-14 19:26:54 +00:00
|
|
|
|
2014-07-23 00:16:15 +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)
|
|
|
|
|
2016-01-08 16:47:28 +00:00
|
|
|
app.use(express.static('public'));
|
|
|
|
app.use(cookieParser());
|
|
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
|
|
|
app.use(bodyParser.json({ extended: true }));
|
2016-09-27 19:11:28 +00:00
|
|
|
app.use(cookieSession({secret: 'ctdo', cookie: {maxAge: 60000}}));
|
2013-10-14 19:26:54 +00:00
|
|
|
app.use(flash());
|
2016-01-08 16:47:28 +00:00
|
|
|
|
2016-01-08 17:30:09 +00:00
|
|
|
app.set('/views');
|
2016-01-08 16:47:28 +00:00
|
|
|
app.set('view engine', 'jade');
|
|
|
|
app.disable('x-powered-by');
|
2015-10-07 10:23:39 +00:00
|
|
|
app.enable('trust proxy');
|
2013-10-14 19:26:54 +00:00
|
|
|
|
|
|
|
app.get('/api/spaceapi/v13', function (req, res) {
|
2014-07-23 12:18:11 +00:00
|
|
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
|
|
res.setHeader("Cache-Control", "no-cache");
|
2013-10-14 19:26:54 +00:00
|
|
|
res.send(spaceanswer);
|
|
|
|
});
|
|
|
|
app.get('/api/simple/v2', function (req, res) {
|
2014-07-23 12:18:11 +00:00
|
|
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
|
|
res.setHeader("Cache-Control", "no-cache");
|
2013-10-14 19:26:54 +00:00
|
|
|
res.send(simpleanswer);
|
|
|
|
});
|
2013-10-21 23:40:57 +00:00
|
|
|
app.get('/api/usercount', function (req, res) {
|
2014-07-23 00:16:15 +00:00
|
|
|
ippoll.getHistory("-inf", "+inf", function (data) {
|
2013-10-22 12:01:50 +00:00
|
|
|
usercountanswer.datapoints.length = 0;
|
|
|
|
usercountanswer.at = simpleanswer.lastchange;
|
|
|
|
usercountanswer.current_value = simpleanswer.count;
|
|
|
|
usercountanswer.datapoints = data;
|
|
|
|
res.send(usercountanswer);
|
|
|
|
});
|
2013-10-21 23:40:57 +00:00
|
|
|
});
|
2016-09-27 19:33:30 +00:00
|
|
|
|
|
|
|
function getstatusImage(req, res) {
|
2014-07-23 00:16:15 +00:00
|
|
|
if (spaceanswer.state.open == true) {
|
2016-01-08 17:14:07 +00:00
|
|
|
res.sendFile(path.resolve(__dirname, 'public/img/green.png'));
|
2014-07-23 00:16:15 +00:00
|
|
|
} else if (spaceanswer.state.open == false) {
|
2016-01-08 17:14:07 +00:00
|
|
|
res.sendFile(path.resolve(__dirname, 'public/img/red.png'));
|
2014-07-23 00:16:15 +00:00
|
|
|
} else {
|
2016-01-08 17:14:07 +00:00
|
|
|
res.sendFile(path.resolve(__dirname, 'public/img/yellow.png'));
|
2014-07-23 00:16:15 +00:00
|
|
|
}
|
2016-09-27 19:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
app.get('/api/simple/image', function (req, res) {
|
|
|
|
getstatusImage(req, res);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/api/simple/image.png', function (req, res) {
|
|
|
|
getstatusImage(req, res);
|
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
|
|
|
|
2015-09-11 19:10:21 +00:00
|
|
|
//server.listen(3000, 'localhost');
|
|
|
|
var server = server.listen(port, address, function(err) {
|
2016-01-06 07:11:55 +00:00
|
|
|
if(err){
|
|
|
|
console.log(err);
|
|
|
|
}
|
2015-09-11 19:10:21 +00:00
|
|
|
|
|
|
|
var message = 'Server is running @ http://' + address + ':' + port;
|
|
|
|
console.log(message);
|
|
|
|
});
|