Commit 1f73d31d authored by fionera's avatar fionera

Add API Support

parent d2cc89dc
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var request = require('request');
var CTDOApi= function(url) {
var self = this;
this.poll = function() {
request({url: "https://" + url}, function(error, res, response) {
if (error) {
self.emit('failed', error)
} else {
var parsedResponse = JSON.parse(response);
self.emit('done', parsedResponse);
}
});
};
};
util.inherits(CTDOApi, EventEmitter);
module.exports = CTDOApi;
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var cookieSession = require('cookie-session');
var flash = require('connect-flash');
//var SnmpMac = require("./snmp-mac");
var IpPoll = require("./ip-poll");
var Flukso = require("./flukso");
var IpPoll = require("./ip-poll");
var CTDOApi = require("./ctdo-api");
var routes = require("./routes");
var path = require('path');
var mqttClient = require('mqtt');
......@@ -56,21 +56,21 @@ var spaceanswer = {
},
sensors: {
power_consumption: [{
unit: "W",
name: "mains power",
description: "overall power consumption from our space",
location: "basement",
value: 0}]
unit: "W",
name: "mains power",
description: "overall power consumption from our space",
location: "basement",
value: 0
}]
}
};
var simpleanswer = {state: "unknown", count: 0, names: [], lastchange: 0, power: 0};
var usercountanswer = {at: 0, current_value: 0, datapoints: [
{at: 0, value: 0}
]};
var usercountanswer = {
at: 0, current_value: 0, datapoints: []
};
//var snmpMac = new SnmpMac("juni.ctdo.de", "ctdo23");
//var ippoll = new IpPoll("flipdot.raum.ctdo.de", "195.160.169.30-120");
var ippoll = new IpPoll("rpi3.raum.ctdo.de");
var ctdoapi = new CTDOApi("api.ctdo.de/names.php");
var flukso = new Flukso("flukso.raum.ctdo.de", "/sensor/cf00e0b22230f4a8870af58f2b8719dd");
var mqtt = mqttClient.connect('mqtt://raum.ctdo.de');
var connected = false;
......@@ -78,29 +78,26 @@ var connected = false;
var port = '9911';
var address = 'localhost';
/*
snmpMac.on('done', function (res) {
simpleanswer.names = res;
simpleanswer.lastchange = new Date().getTime();
});
ctdoapi.on('done', function (answer) {
simpleanswer.count = answer.count;
simpleanswer.names = answer.names;
ippoll.on('doneCount', function (num) {
simpleanswer.count = num;
simpleanswer.lastchange = new Date().getTime();
ippoll.pollState();
if (usercountanswer.datapoints.length > 20) {
usercountanswer.datapoints.shift();
}
usercountanswer.datapoints.push({at: new Date().getTime(), value: simpleanswer.count});
});
*/
ippoll.on('doneState', function (state) {
spaceanswer.state.open = state;
spaceanswer.state.lastchange = Math.floor(new Date().getTime() / 1000);
simpleanswer.state = state;
// simpleanswer.lastchange = spaceanswer.state.lastchange;
simpleanswer.lastchange = spaceanswer.state.lastchange;
simpleanswer.lastchange = new Date().getTime();
io.sockets.emit('sdata', {data: simpleanswer});
});
flukso.on('done', function (num) {
simpleanswer.power = num;
spaceanswer.sensors.power_consumption[0].value = num;
......@@ -111,24 +108,23 @@ flukso.on('failed', function (error) {
console.log("Flukso: " + error);
});
mqtt.on('connect', function() {
console.log("connected to mqtt");
mqtt.subscribe('raumstatus/simple');
mqtt.on('connect', function () {
console.log("connected to mqtt");
mqtt.subscribe('raumstatus/simple');
});
mqtt.on('message', function (topic, message) {
if(topic === 'raumstatus/simple') {
connected = (message.toString() === 'true');
}
mqtt.on('message', function (topic, message) {
if (topic === 'raumstatus/simple') {
connected = (message.toString() === 'true');
}
});
function sendSimple () {
mqtt.publish('raumstatus/simple', JSON.stringify(simpleanswer));
function sendSimple() {
mqtt.publish('raumstatus/simple', JSON.stringify(simpleanswer));
}
function work() {
/* snmpMac.poll();
ippoll.pollCount();*/
ctdoapi.poll();
ippoll.pollState();
sendSimple();
}
......@@ -139,12 +135,10 @@ work();
setInterval(flukso.pollPower, 5000);
flukso.pollPower();
//app.set('snmpMac', snmpMac);
app.use(express.static('public'));
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json({ extended: true }));
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json({extended: true}));
app.use(cookieSession({secret: 'ctdo', cookie: {maxAge: 60000}}));
app.use(flash());
......@@ -158,19 +152,17 @@ app.get('/api/spaceapi/v13', function (req, res) {
res.setHeader("Cache-Control", "no-cache");
res.send(spaceanswer);
});
app.get('/api/simple/v2', function (req, res) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Cache-Control", "no-cache");
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);
//});
usercountanswer.at = simpleanswer.lastchange;
usercountanswer.current_value = simpleanswer.count;
res.send(usercountanswer);
});
function getstatusImage(req, res) {
......@@ -195,12 +187,11 @@ app.get('/api/simple/image.png', function (req, res) {
//app.post('/form', routes.form);
app.get('/', routes.index);
//server.listen(3000, 'localhost');
var server = server.listen(port, address, function(err) {
if(err){
console.log(err);
}
var server = server.listen(port, address, function (err) {
if (err) {
console.log(err);
}
var message = 'Server is running @ http://' + address + ':' + port;
console.log(message);
var message = 'Server is running @ http://' + address + ':' + port;
console.log(message);
});
......@@ -4,14 +4,8 @@ block content
.intro
p
//-
| Hier siehst du den aktuellen Status von uns. Die Daten werden vom Router und unserem Flukso
| eingesammelt. Die Anzahl der Geräte und der Status selber werden Minütlich abgefragt. Unseren
| Energieverbrauch bekommst du alle fünf Sekunden neu. Den Status "geöffnet" oder "geschlossen"
| bestimmt das Programm mit einem ping auf den Switch im Raum. Mit dem Knopf unter dem Stromverbrauch
| kannst du einschalten, dass du eine Benachrichtigung bei einer Änderung bekommst. Der Tab muss dafür offen bleiben.
| Hier siehst du den aktuellen Status von uns. Die Daten werden von unserem Flukso
| eingesammelt. Der Status selber wird Minütlich abgefragt. Unseren
| Hier siehst du den aktuellen Status von uns. Die Daten werden vom Router und unserem Flukso
| eingesammelt. Die Anzahl der Geräte und der Status selber werden Minütlich abgefragt. Unseren
| Energieverbrauch bekommst du alle fünf Sekunden neu. Den Status "geöffnet" oder "geschlossen"
| bestimmt das Programm mit einem ping auf den Switch im Raum. Mit dem Knopf unter dem Stromverbrauch
| kannst du einschalten, dass du eine Benachrichtigung bei einer Änderung bekommst. Der Tab muss dafür offen bleiben.
......@@ -30,27 +24,22 @@ block content
dt letzte Abfrage:
dd {{simple.lastchange | date:'dd.MM.yyyy HH:mm:ss'}}
//-
dt aktive Geräte:
dd {{simple.count}}
.large-50.medium-80.small-100
h2 Anzahl Geräte im LAN:
#graph
//-
.large-50.medium-80.small-100
h2 Anzahl Geräte im LAN:
#graph
h2 Personen anwesend:
h2 Personen anwesend:
span(ng-repeat="name in simple.names")
| {{name}}
span(ng-repeat="name in simple.names")
| {{name}}
span(ng-show=" ! $last ")
| , <!-- -->
span(ng-show=" ! $last ")
| , <!-- -->
.large-30.medium-100.small-100
h2 Energieverbrauch
......
......@@ -5,8 +5,8 @@ html(ng-app="roomstateapp")
//if IE 7
link(rel="stylesheet",href="/css/ink-ie7-min.css",type="text/css",media="screen")
script(type="text/javascript",src="/js/vendor/angular.min.js")
script(type="text/javascript",src="/socket.io/socket.io.js")
script(type="text/javascript", src="/js/vendor/angular.min.js")
script(type="text/javascript", src="/socket.io/socket.io.js")
link(rel='stylesheet', href='/css/ink-min.css')
link(rel='stylesheet', href='/css/rickshaw.min.css')
......@@ -29,7 +29,7 @@ html(ng-app="roomstateapp")
li: a(href="/") Raumstatus
//-
li: a(href='/db') Name verwalten
li: a(href="//www.chaostreff-dortmund.de/") CTDO Webseite
li: a(href="//wiki.ctdo.de/") CTDO Wiki
li: a(href="https://repos.ctdo.de/ctdo/raumstatus/tree/master") Source
......@@ -37,15 +37,15 @@ html(ng-app="roomstateapp")
script(type="text/javascript",src="/js/vendor/jquery-1.9.1.min.js")
script(type="text/javascript",src="/js/vendor/ink.min.js")
script(type="text/javascript",src="/js/vendor/autoload.js")
script(type="text/javascript",src="/js/vendor/html5shiv.js")
//script(type="text/javascript",src="/js/vendor/prettify.js")
//script(type="text/javascript",src="/js/vendor/modernizr.js")
script(type="text/javascript",src="/js/app.js")
script(type="text/javascript",src="/js/vendor/cheet.min.js")
script(type="text/javascript",src="/js/vendor/notification.js")
script(type="text/javascript",src="/js/easter.js")
script(type="text/javascript", src="/js/vendor/jquery-1.9.1.min.js")
script(type="text/javascript", src="/js/vendor/ink.min.js")
script(type="text/javascript", src="/js/vendor/autoload.js")
script(type="text/javascript", src="/js/vendor/html5shiv.js")
script(type="text/javascript", src="/js/vendor/prettify.js")
script(type="text/javascript", src="/js/vendor/modernizr.js")
script(type="text/javascript", src="/js/app.js")
script(type="text/javascript", src="/js/vendor/cheet.min.js")
script(type="text/javascript", src="/js/vendor/notification.js")
script(type="text/javascript", src="/js/easter.js")
block scripts
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment