raumstatus/node/statusbot.js

198 lines
5.6 KiB
JavaScript
Raw Normal View History

2013-10-02 12:04:01 +00:00
var express = require('express'),
2018-04-19 18:17:45 +00:00
app = express(),
server = require('http').createServer(app),
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');
var Flukso = require("./flukso");
2018-04-19 18:17:45 +00:00
var IpPoll = require("./ip-poll");
var CTDOApi = require("./ctdo-api");
2013-10-20 22:04:58 +00:00
var routes = require("./routes");
2014-07-05 23:42:11 +00:00
var path = require('path');
2016-11-28 10:15:23 +00:00
var mqttClient = require('mqtt');
2013-10-02 12:04:01 +00:00
var spaceanswer = {
api: "0.13",
ext_ccc: "erfa",
space: "Chaostreff Dortmund",
2018-04-20 14:07:56 +00:00
logo: "http://www.chaostreff-dortmund.de/presse/logo/logo_ctdo.png",
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",
2016-11-09 13:47:38 +00:00
irc: "irc://irc.hackint.eu/#ccc.do",
ml: "discuss@lists.chaostreff-dortmund.de",
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"],
state: {
open: null,
lastchange: 0,
icon: {
open: "http://status.ctdo.de/img/green.png",
closed: "http://status.ctdo.de/img/red.png"
}
},
feeds: {
blog: {
type: "application/rss+xml",
url: "https://www.chaostreff-dortmund.de/feed/"
},
calendar: {
type: "ical",
2018-01-14 15:52:22 +00:00
url: "https://www.chaostreff-dortmund.de/events.ics"
}
},
sensors: {
2015-09-05 10:18:35 +00:00
power_consumption: [{
2018-04-19 18:17:45 +00:00
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};
2018-04-19 18:17:45 +00:00
var usercountanswer = {
at: 0, current_value: 0, datapoints: []
};
2013-10-02 12:04:01 +00:00
2017-11-25 14:06:54 +00:00
var ippoll = new IpPoll("rpi3.raum.ctdo.de");
2018-04-19 18:17:45 +00:00
var ctdoapi = new CTDOApi("api.ctdo.de/names.php");
var flukso = new Flukso("flukso.raum.ctdo.de", "/sensor/cf00e0b22230f4a8870af58f2b8719dd");
2016-11-28 10:15:23 +00:00
var mqtt = mqttClient.connect('mqtt://raum.ctdo.de');
var connected = false;
2013-10-14 19:26:54 +00:00
var port = '9911';
var address = 'localhost';
2018-04-19 18:17:45 +00:00
ctdoapi.on('done', function (answer) {
simpleanswer.count = answer.count;
simpleanswer.names = answer.names;
2013-10-14 19:26:54 +00:00
2018-04-19 18:17:45 +00:00
if (usercountanswer.datapoints.length > 20) {
usercountanswer.datapoints.shift();
}
usercountanswer.datapoints.push({at: new Date().getTime(), value: simpleanswer.count});
2013-10-14 19:26:54 +00:00
});
ippoll.on('doneState', function (state) {
spaceanswer.state.open = state;
2018-02-22 17:21:46 +00:00
spaceanswer.state.lastchange = Math.floor(new Date().getTime() / 1000);
2013-10-21 11:39:52 +00:00
simpleanswer.state = state;
2018-04-19 18:17:45 +00:00
simpleanswer.lastchange = spaceanswer.state.lastchange;
simpleanswer.lastchange = new Date().getTime();
2015-09-05 10:18:35 +00:00
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;
2015-09-05 10:18:35 +00:00
io.sockets.emit('sdata', {data: simpleanswer});
});
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
});
2018-04-19 18:17:45 +00:00
mqtt.on('connect', function () {
console.log("connected to mqtt");
mqtt.subscribe('raumstatus/simple');
2016-11-28 10:15:23 +00:00
});
2018-04-19 18:17:45 +00:00
mqtt.on('message', function (topic, message) {
if (topic === 'raumstatus/simple') {
connected = (message.toString() === 'true');
}
2016-11-28 10:15:23 +00:00
});
2018-04-19 18:17:45 +00:00
function sendSimple() {
mqtt.publish('raumstatus/simple', JSON.stringify(simpleanswer));
2016-11-28 10:15:23 +00:00
}
2013-10-14 19:26:54 +00:00
function work() {
2018-04-19 18:17:45 +00:00
ctdoapi.poll();
ippoll.pollState();
2016-11-28 10:15:23 +00:00
sendSimple();
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();
2016-01-08 16:47:28 +00:00
app.use(express.static('public'));
app.use(cookieParser());
2018-04-19 18:17:45 +00:00
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
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) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Cache-Control", "no-cache");
2013-10-14 19:26:54 +00:00
res.send(spaceanswer);
});
2018-04-19 18:17:45 +00:00
2013-10-14 19:26:54 +00:00
app.get('/api/simple/v2', function (req, res) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Cache-Control", "no-cache");
2013-10-14 19:26:54 +00:00
res.send(simpleanswer);
});
2018-04-19 18:17:45 +00:00
app.get('/api/usercount', function (req, res) {
2018-04-19 18:17:45 +00:00
usercountanswer.at = simpleanswer.lastchange;
usercountanswer.current_value = simpleanswer.count;
res.send(usercountanswer);
});
2016-09-27 19:33:30 +00:00
function getstatusImage(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'));
}
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
//app.get('/db', routes.db);
//app.post('/form', routes.form);
2013-10-20 22:04:58 +00:00
app.get('/', routes.index);
2013-10-14 19:26:54 +00:00
2018-04-19 18:17:45 +00:00
var server = server.listen(port, address, function (err) {
if (err) {
console.log(err);
}
2018-04-19 18:17:45 +00:00
var message = 'Server is running @ http://' + address + ':' + port;
console.log(message);
});