Change backend call

master
Ringo Watanabe 6 years ago
parent a3dfc4313d
commit d18fde4aa4

1
.gitignore vendored

@ -1 +1,2 @@
backend/ backend/
rtbwn.sock

@ -1,5 +1,4 @@
Server: Server:
!!Move backend call out of request callback for /info!!
Add UNIX socket listener to node server for daemon communication. Add UNIX socket listener to node server for daemon communication.
Add option to save/load state of server. Add option to save/load state of server.

@ -2,6 +2,8 @@ var config = {
PORT: 8001, PORT: 8001,
POLL_INTERVAL: 10000, POLL_INTERVAL: 10000,
//CTL_SOCKET: "rtbwn.sock",
URL_BASE: "/rtbw", URL_BASE: "/rtbw",
PYTHON_EXECUTABLE: "python3", PYTHON_EXECUTABLE: "python3",

@ -0,0 +1,25 @@
var http = require('http');
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");
var _cycle = require("./cycle");
var CycleBuffer = require("./graphBuffer");
var client = new net.Socket(config.CTL_SOCKET, function() {
logger.log("Connected");
client.write("Hello world");
});
client.on('data', function(data) {
});
client.on('close', function() {
console.log("Connection closed");
});

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

Loading…
Cancel
Save