Change backend call

master
Ringo Watanabe 6 years ago
parent a3dfc4313d
commit d18fde4aa4

1
.gitignore vendored

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

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

@ -2,6 +2,8 @@ var config = {
PORT: 8001,
POLL_INTERVAL: 10000,
//CTL_SOCKET: "rtbwn.sock",
URL_BASE: "/rtbw",
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 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;
@ -287,3 +297,22 @@ var backend = setInterval(function() {
}, 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