102 lines
3.0 KiB
JavaScript
102 lines
3.0 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 sendersStatus = senders.map(({ name, instance }) => {
|
|
const status = instance?.getStatus ? instance.getStatus() : {
|
|
state: instance?.isTestMode ? 'connected' : instance?.tSocket ? 'connected' : 'disconnected',
|
|
url: instance?.url || null,
|
|
error: instance?.error || null,
|
|
isTestMode: !!instance?.isTestMode,
|
|
reconnectAttempt: instance?.reconnectAttempt || 0,
|
|
reconnectTimer: !!instance?.reconnectTimer
|
|
};
|
|
|
|
const state = status.state || (instance?.tSocket ? 'connected' : 'disconnected');
|
|
const health = state === 'connected'
|
|
? 'ok'
|
|
: state === 'reconnecting'
|
|
? 'warning'
|
|
: 'disconnected';
|
|
const reason = state === 'disconnected'
|
|
? status.error || 'no active socket connection'
|
|
: undefined;
|
|
|
|
return {
|
|
name,
|
|
state,
|
|
url: status.url || null,
|
|
isTestMode: !!status.isTestMode,
|
|
error: status.error || null,
|
|
reconnectAttempt: status.reconnectAttempt || 0,
|
|
reconnectTimer: !!status.reconnectTimer,
|
|
health,
|
|
reason
|
|
};
|
|
});
|
|
|
|
const connectedSenders = sendersStatus.filter(s => s.health === 'ok').length;
|
|
const health = {
|
|
ok: sendersStatus.length > 0 && sendersStatus.every(s => s.health === 'ok'),
|
|
connectedSenders,
|
|
totalSenders: sendersStatus.length
|
|
};
|
|
|
|
const status = {
|
|
generatedAt: new Date().toISOString(),
|
|
health,
|
|
clients: sharedState.connectedClients,
|
|
senders: sendersStatus,
|
|
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; |