Driver Info Page

This commit is contained in:
ChK
2026-04-01 18:09:48 +02:00
parent 8a32009889
commit a9fc78305c
8 changed files with 703 additions and 2 deletions

View File

@@ -2,12 +2,27 @@ const fs = require('fs');
const https = require('https');
const WebSocket = require('ws');
function getLastLines(filePath, numLines) {
try {
const data = fs.readFileSync(filePath, 'utf8');
const lines = data.split('\n').filter(line => line.trim());
return lines.slice(-numLines);
} catch (e) {
return [];
}
}
const Robot = require('./robot/Robot.js')
const GCode = require('./robot/GCode.js')
// 238 + 1 + 25 = 264
let robot = new Robot(250,264,100); //(300,290,10);
// Tracking variables for info page
let connectedClients = [];
let lastCommands = [];
let lastPings = [];
const httpsOptions = {
"enable": true,
@@ -24,10 +39,19 @@ const wss = new WebSocket.Server({ server });
wss.on('connection', function connection(ws)
{
console.log("WebServer for Input is connected");
const clientIP = ws._socket.remoteAddress || 'unknown';
connectedClients.push(clientIP);
ws.on('close', () => {
connectedClients = connectedClients.filter(client => client !== clientIP);
});
ws.on('message', function incoming(message)
{
if(message == "Ping"){
fs.appendFileSync('./logs/pings.log', `${new Date().toISOString()} ${clientIP}: ${message}\n`);
lastPings.push(new Date().toISOString() + ': ' + message);
if (lastPings.length > 10) lastPings.shift();
wss.clients.forEach(function (client)
{
if (client.readyState == WebSocket.OPEN)
@@ -38,6 +62,9 @@ wss.on('connection', function connection(ws)
}
if(GCode.containsCommand(message)){
fs.appendFileSync('./logs/gcode_commands.log', `${new Date().toISOString()} ${clientIP}: ${message}\n`);
lastCommands.push(new Date().toISOString() + ': ' + message);
if (lastCommands.length > 10) lastCommands.shift();
GCode.receiveGCode(robot,message);
reply = GCode.getM114(robot);
wss.clients.forEach(function (client)
@@ -50,6 +77,9 @@ wss.on('connection', function connection(ws)
}
if(GCode.ContainsFilesCommand(message)){
fs.appendFileSync('./logs/gcode_commands.log', `${new Date().toISOString()} ${clientIP}: ${message}\n`);
lastCommands.push(new Date().toISOString() + ': ' + message);
if (lastCommands.length > 10) lastCommands.shift();
reply = GCode.receiveFC(robot, message);
wss.clients.forEach(function (client)
{
@@ -62,6 +92,9 @@ wss.on('connection', function connection(ws)
// Request for Status
if(message == "M114"){
fs.appendFileSync('./logs/gcode_commands.log', `${new Date().toISOString()} ${clientIP}: ${message}\n`);
lastCommands.push(new Date().toISOString() + ': ' + message);
if (lastCommands.length > 10) lastCommands.shift();
reply = GCode.getM114(robot);
wss.clients.forEach(function (client)
{
@@ -114,3 +147,50 @@ console.log("Works with FluidNc Base");
port = 2095
server.listen(port);
console.log("Listen on Port: https://localhost:" + port.toString()+ "/")
// Info server on port 2098
const infoServer = https.createServer(httpsOptions, (req, res) => {
if (req.url === '/') {
fs.readFile('./public/index.html', (err, data) => {
if (err) {
res.writeHead(404);
res.end('Not found');
} else {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(data);
}
});
} else if (req.url === '/app.js') {
fs.readFile('./public/app.js', (err, data) => {
if (err) {
res.writeHead(404);
res.end('Not found');
} else {
res.writeHead(200, {'Content-Type': 'application/javascript'});
res.end(data);
}
});
} else if (req.url === '/api/status') {
const status = {
clients: connectedClients,
senders: [
{name: 'Base', status: telnetSender1?.tSocket ? 'connected' : 'disconnected'},
{name: 'Elbow', status: telnetSender2?.tSocket ? 'connected' : 'disconnected'},
{name: 'Hand', status: telnetSender3?.tSocket ? 'connected' : 'disconnected'}
],
lastCommands: getLastLines('./logs/gcode_commands.log', 10),
lastPings: getLastLines('./logs/pings.log', 10)
};
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(status));
} else {
res.writeHead(404);
res.end('Not found');
}
});
infoServer.listen(2098);
console.log("Info server listening on https://localhost:2098/");