|
|
|
@ -3,7 +3,8 @@ var process = require('child_process');
|
|
|
|
|
var WebSocketServer = require("ws").Server;
|
|
|
|
|
var events = require("events");
|
|
|
|
|
var jsoncompress = require('jsoncompress');
|
|
|
|
|
|
|
|
|
|
var net = require('net');
|
|
|
|
|
var fs = require('fs');
|
|
|
|
|
|
|
|
|
|
var config = require('./config');
|
|
|
|
|
var logger = require("./logger");
|
|
|
|
@ -75,6 +76,7 @@ var allTimeAverage = new _cycle.Cycle();
|
|
|
|
|
var allTimeAverageN = 0;
|
|
|
|
|
var buffer = new CycleBuffer(config.BUFFER_PURGE_TIME);
|
|
|
|
|
var superbuffer = new CycleBuffer(config.SUPERBUFFER_PURGE_TIME);
|
|
|
|
|
var beInfo = null;
|
|
|
|
|
|
|
|
|
|
var upSince = new Date();
|
|
|
|
|
|
|
|
|
@ -96,6 +98,16 @@ superbuffer.onclear = function(bu) {
|
|
|
|
|
logger.log("Buffer set to clear every "+(config.BUFFER_PURGE_TIME/1000/60)+" minutes");
|
|
|
|
|
logger.log("Superbuffer set to clear every "+(config.SUPERBUFFER_PURGE_TIME/1000/60)+" minutes");
|
|
|
|
|
|
|
|
|
|
process.execFile(config.PYTHON_EXECUTABLE, [config.RTBWCTL_FILENAME, config.BACKEND_SOCKET, "info"], function(error,stdout,stderr) {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
beInfo = JSON.parse(stdout);
|
|
|
|
|
} catch(e) {
|
|
|
|
|
logger.log("Recieved invalid JSON from info backend call.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
logger.log("Backend info received");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var server = http.createServer(function(req, res) {
|
|
|
|
|
if(req.url == config.URL_BASE+"/cycle") {
|
|
|
|
@ -106,27 +118,22 @@ var server = http.createServer(function(req, res) {
|
|
|
|
|
}
|
|
|
|
|
else if(req.url == config.URL_BASE+"/info") {
|
|
|
|
|
logger.log("Recieved info request");
|
|
|
|
|
process.execFile(config.PYTHON_EXECUTABLE, [config.RTBWCTL_FILENAME, config.BACKEND_SOCKET, "info"], function(error,stdout,stderr) {
|
|
|
|
|
var js = undefined;
|
|
|
|
|
try {
|
|
|
|
|
js = JSON.parse(stdout);
|
|
|
|
|
} catch(e) {
|
|
|
|
|
logger.log("Recieved invalid JSON from info backend call");
|
|
|
|
|
if(!beInfo) {
|
|
|
|
|
res.writeHead(500);
|
|
|
|
|
res.end("");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
res.setHeader("Content-Type", "application/json");
|
|
|
|
|
res.writeHead(200);
|
|
|
|
|
res.end(JSON.stringify({
|
|
|
|
|
interval: config.POLL_INTERVAL,
|
|
|
|
|
purgeInterval: config.BUFFER_PURGE_TIME,
|
|
|
|
|
superPurgeInterval: config.SUPERBUFFER_PURGE_TIME,
|
|
|
|
|
backendInterval: js.timeout,
|
|
|
|
|
board: js.board,
|
|
|
|
|
backendInterval: beInfo.timeout,
|
|
|
|
|
board: beInfo.board,
|
|
|
|
|
upSince: upSince
|
|
|
|
|
}));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if(req.url == config.URL_BASE+"/template")
|
|
|
|
|
{
|
|
|
|
@ -190,6 +197,8 @@ var server = http.createServer(function(req, res) {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var wss = new WebSocketServer({
|
|
|
|
|
server: server,
|
|
|
|
|
autoAcceptConnections: false
|
|
|
|
@ -274,6 +283,7 @@ var backend = setInterval(function() {
|
|
|
|
|
else if(bein(json[i], "trip"))
|
|
|
|
|
cycle.trips+=1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cycle.interval = timeAverage(timeInters);
|
|
|
|
|
cycle.last = lastNumber;
|
|
|
|
|
|
|
|
|
@ -281,9 +291,28 @@ var backend = setInterval(function() {
|
|
|
|
|
buffer.add(cycle);
|
|
|
|
|
allTime = _cycle.Cycle.accumulate([allTime, cycle]);
|
|
|
|
|
allTimeAverage = _cycle.Cycle.average(allTimeAverage, [cycle], allTimeAverageN);
|
|
|
|
|
allTimeAverageN +=1;
|
|
|
|
|
allTimeAverageN += 1;
|
|
|
|
|
cycleEmitter.emit("newCycle", lastCycle);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}, config.POLL_INTERVAL);
|
|
|
|
|
|
|
|
|
|
/*var ctlServer = net.createServer();
|
|
|
|
|
|
|
|
|
|
if(!!config.CTL_SOCKET) {
|
|
|
|
|
ctlServer.listen(config.CTL_SOCKET, function() {
|
|
|
|
|
logger.log("Control server up on socket "+config.CTL_SOCKET);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ctlServer.on('connection', function(con) {
|
|
|
|
|
logger.log("Connection");
|
|
|
|
|
con.on('data', function (data) {
|
|
|
|
|
logger.log("CTL: "+data);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ctlServer.on('error', function(e) {
|
|
|
|
|
logger.log("Control server error ["+config.CTL_SOCKET+"]: "+e.code);
|
|
|
|
|
ctlServer.close();
|
|
|
|
|
});
|
|
|
|
|
}*/
|
|
|
|
|