Add API Support

This commit is contained in:
Tim Windelschmidt 2018-04-19 20:17:45 +02:00
parent d2cc89dcef
commit 1f73d31d91
4 changed files with 93 additions and 91 deletions

22
node/ctdo-api.js Normal file
View File

@ -0,0 +1,22 @@
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;

View File

@ -6,9 +6,9 @@ var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser'); var bodyParser = require('body-parser');
var cookieSession = require('cookie-session'); var cookieSession = require('cookie-session');
var flash = require('connect-flash'); var flash = require('connect-flash');
//var SnmpMac = require("./snmp-mac");
var IpPoll = require("./ip-poll");
var Flukso = require("./flukso"); var Flukso = require("./flukso");
var IpPoll = require("./ip-poll");
var CTDOApi = require("./ctdo-api");
var routes = require("./routes"); var routes = require("./routes");
var path = require('path'); var path = require('path');
var mqttClient = require('mqtt'); var mqttClient = require('mqtt');
@ -60,17 +60,17 @@ var spaceanswer = {
name: "mains power", name: "mains power",
description: "overall power consumption from our space", description: "overall power consumption from our space",
location: "basement", location: "basement",
value: 0}] value: 0
}]
} }
}; };
var simpleanswer = {state: "unknown", count: 0, names: [], lastchange: 0, power: 0}; var simpleanswer = {state: "unknown", count: 0, names: [], lastchange: 0, power: 0};
var usercountanswer = {at: 0, current_value: 0, datapoints: [ var usercountanswer = {
{at: 0, value: 0} 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 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 flukso = new Flukso("flukso.raum.ctdo.de", "/sensor/cf00e0b22230f4a8870af58f2b8719dd");
var mqtt = mqttClient.connect('mqtt://raum.ctdo.de'); var mqtt = mqttClient.connect('mqtt://raum.ctdo.de');
var connected = false; var connected = false;
@ -78,29 +78,26 @@ var connected = false;
var port = '9911'; var port = '9911';
var address = 'localhost'; var address = 'localhost';
/* ctdoapi.on('done', function (answer) {
snmpMac.on('done', function (res) { simpleanswer.count = answer.count;
simpleanswer.names = res; simpleanswer.names = answer.names;
simpleanswer.lastchange = new Date().getTime();
});
ippoll.on('doneCount', function (num) { if (usercountanswer.datapoints.length > 20) {
simpleanswer.count = num; usercountanswer.datapoints.shift();
simpleanswer.lastchange = new Date().getTime(); }
ippoll.pollState();
usercountanswer.datapoints.push({at: new Date().getTime(), value: simpleanswer.count});
}); });
*/
ippoll.on('doneState', function (state) { ippoll.on('doneState', function (state) {
spaceanswer.state.open = state; spaceanswer.state.open = state;
spaceanswer.state.lastchange = Math.floor(new Date().getTime() / 1000); spaceanswer.state.lastchange = Math.floor(new Date().getTime() / 1000);
simpleanswer.state = state; simpleanswer.state = state;
// simpleanswer.lastchange = spaceanswer.state.lastchange; simpleanswer.lastchange = spaceanswer.state.lastchange;
simpleanswer.lastchange = new Date().getTime(); simpleanswer.lastchange = new Date().getTime();
io.sockets.emit('sdata', {data: simpleanswer}); io.sockets.emit('sdata', {data: simpleanswer});
}); });
flukso.on('done', function (num) { flukso.on('done', function (num) {
simpleanswer.power = num; simpleanswer.power = num;
spaceanswer.sensors.power_consumption[0].value = num; spaceanswer.sensors.power_consumption[0].value = num;
@ -111,24 +108,23 @@ flukso.on('failed', function (error) {
console.log("Flukso: " + error); console.log("Flukso: " + error);
}); });
mqtt.on('connect', function() { mqtt.on('connect', function () {
console.log("connected to mqtt"); console.log("connected to mqtt");
mqtt.subscribe('raumstatus/simple'); mqtt.subscribe('raumstatus/simple');
}); });
mqtt.on('message', function (topic, message) { mqtt.on('message', function (topic, message) {
if(topic === 'raumstatus/simple') { if (topic === 'raumstatus/simple') {
connected = (message.toString() === 'true'); connected = (message.toString() === 'true');
} }
}); });
function sendSimple () { function sendSimple() {
mqtt.publish('raumstatus/simple', JSON.stringify(simpleanswer)); mqtt.publish('raumstatus/simple', JSON.stringify(simpleanswer));
} }
function work() { function work() {
/* snmpMac.poll(); ctdoapi.poll();
ippoll.pollCount();*/
ippoll.pollState(); ippoll.pollState();
sendSimple(); sendSimple();
} }
@ -139,12 +135,10 @@ work();
setInterval(flukso.pollPower, 5000); setInterval(flukso.pollPower, 5000);
flukso.pollPower(); flukso.pollPower();
//app.set('snmpMac', snmpMac);
app.use(express.static('public')); app.use(express.static('public'));
app.use(cookieParser()); app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json({ extended: true })); app.use(bodyParser.json({extended: true}));
app.use(cookieSession({secret: 'ctdo', cookie: {maxAge: 60000}})); app.use(cookieSession({secret: 'ctdo', cookie: {maxAge: 60000}}));
app.use(flash()); app.use(flash());
@ -158,19 +152,17 @@ app.get('/api/spaceapi/v13', function (req, res) {
res.setHeader("Cache-Control", "no-cache"); res.setHeader("Cache-Control", "no-cache");
res.send(spaceanswer); res.send(spaceanswer);
}); });
app.get('/api/simple/v2', function (req, res) { app.get('/api/simple/v2', function (req, res) {
res.setHeader("Access-Control-Allow-Origin", "*"); res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Cache-Control", "no-cache"); res.setHeader("Cache-Control", "no-cache");
res.send(simpleanswer); res.send(simpleanswer);
}); });
app.get('/api/usercount', function (req, res) { app.get('/api/usercount', function (req, res) {
/*ippoll.getHistory("-inf", "+inf", function (data) {
usercountanswer.datapoints.length = 0;
usercountanswer.at = simpleanswer.lastchange; usercountanswer.at = simpleanswer.lastchange;
usercountanswer.current_value = simpleanswer.count; usercountanswer.current_value = simpleanswer.count;
usercountanswer.datapoints = data; */
res.send(usercountanswer); res.send(usercountanswer);
//});
}); });
function getstatusImage(req, res) { function getstatusImage(req, res) {
@ -195,9 +187,8 @@ app.get('/api/simple/image.png', function (req, res) {
//app.post('/form', routes.form); //app.post('/form', routes.form);
app.get('/', routes.index); app.get('/', routes.index);
//server.listen(3000, 'localhost'); var server = server.listen(port, address, function (err) {
var server = server.listen(port, address, function(err) { if (err) {
if(err){
console.log(err); console.log(err);
} }

View File

@ -4,17 +4,11 @@ block content
.intro .intro
p p
//-
| Hier siehst du den aktuellen Status von uns. Die Daten werden vom Router und unserem Flukso | 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 | 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" | 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 | 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. | 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
| 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.
.ink-grid(ng-controller='StatusCtrl') .ink-grid(ng-controller='StatusCtrl')
.column-group.gutters .column-group.gutters
@ -30,20 +24,15 @@ block content
dt letzte Abfrage: dt letzte Abfrage:
dd {{simple.lastchange | date:'dd.MM.yyyy HH:mm:ss'}} dd {{simple.lastchange | date:'dd.MM.yyyy HH:mm:ss'}}
//-
dt aktive Geräte: dt aktive Geräte:
dd {{simple.count}} dd {{simple.count}}
//-
.large-50.medium-80.small-100 .large-50.medium-80.small-100
h2 Anzahl Geräte im LAN: h2 Anzahl Geräte im LAN:
#graph #graph
h2 Personen anwesend: h2 Personen anwesend:
span(ng-repeat="name in simple.names") span(ng-repeat="name in simple.names")

View File

@ -5,8 +5,8 @@ html(ng-app="roomstateapp")
//if IE 7 //if IE 7
link(rel="stylesheet",href="/css/ink-ie7-min.css",type="text/css",media="screen") 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="/js/vendor/angular.min.js")
script(type="text/javascript",src="/socket.io/socket.io.js") script(type="text/javascript", src="/socket.io/socket.io.js")
link(rel='stylesheet', href='/css/ink-min.css') link(rel='stylesheet', href='/css/ink-min.css')
link(rel='stylesheet', href='/css/rickshaw.min.css') link(rel='stylesheet', href='/css/rickshaw.min.css')
@ -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/jquery-1.9.1.min.js")
script(type="text/javascript",src="/js/vendor/ink.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/autoload.js")
script(type="text/javascript",src="/js/vendor/html5shiv.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/prettify.js")
//script(type="text/javascript",src="/js/vendor/modernizr.js") script(type="text/javascript", src="/js/vendor/modernizr.js")
script(type="text/javascript",src="/js/app.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/cheet.min.js")
script(type="text/javascript",src="/js/vendor/notification.js") script(type="text/javascript", src="/js/vendor/notification.js")
script(type="text/javascript",src="/js/easter.js") script(type="text/javascript", src="/js/easter.js")
block scripts block scripts