Files
appRobotDriver/server/InfoServer.js
2026-04-23 17:27:52 +02:00

63 lines
1.6 KiB
JavaScript

// server/InfoServer.js
const fs = require('fs');
const https = require('https');
function createInfoServer(httpsOptions, sharedState, robot, GCode, senders) {
return https.createServer(httpsOptions, (req, res) => {
if (req.url === '/') {
return serveFile(res, './public/index.html', 'text/html');
}
if (req.url === '/app.js') {
return serveFile(res, './public/app.js', 'application/javascript');
}
if (req.url === '/style.css') {
return serveFile(res, './public/style.css', 'text/css');
}
if (req.url === '/allApps.css') {
return serveFile(res, './public/allApps.css', 'text/css');
}
/* ---------- API ---------- */
if (req.url === '/api/status') {
const status = {
clients: sharedState.connectedClients,
senders: senders.map(s => ({
name: s.name,
status: s.instance?.tSocket ? 'connected' : 'disconnected'
})),
lastCommands: sharedState.lastCommands,
lastPings: sharedState.lastPings
};
res.writeHead(200, {'Content-Type': 'application/json'});
return res.end(JSON.stringify(status));
}
if (req.url === '/api/position') {
res.writeHead(200, {'Content-Type': 'application/json'});
return res.end(GCode.getM114(robot));
}
res.writeHead(404);
res.end('Not found');
});
}
/* ---------- Helper ---------- */
function serveFile(res, path, type) {
fs.readFile(path, (err, data) => {
if (err) {
res.writeHead(404);
return res.end('Not found');
}
res.writeHead(200, {'Content-Type': type});
res.end(data);
});
}
module.exports = createInfoServer;