WS eigene Datei
This commit is contained in:
63
server/InfoServer.js
Normal file
63
server/InfoServer.js
Normal file
@@ -0,0 +1,63 @@
|
||||
// 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;
|
||||
83
server/InputWS.js
Normal file
83
server/InputWS.js
Normal file
@@ -0,0 +1,83 @@
|
||||
// server/InputWS.js
|
||||
const fs = require('fs');
|
||||
const WebSocket = require('ws');
|
||||
|
||||
function initInputWS(server, robot, GCode, sharedState) {
|
||||
const wss = new WebSocket.Server({ server });
|
||||
|
||||
wss.on('connection', (ws) => {
|
||||
console.log("WebSocket Input connected");
|
||||
|
||||
const clientIP = ws._socket.remoteAddress || 'unknown';
|
||||
sharedState.connectedClients.push(clientIP);
|
||||
|
||||
ws.on('close', () => {
|
||||
sharedState.connectedClients =
|
||||
sharedState.connectedClients.filter(ip => ip !== clientIP);
|
||||
});
|
||||
|
||||
ws.on('message', (msg) => {
|
||||
const message = msg.toString();
|
||||
|
||||
/* ---------- Ping ---------- */
|
||||
if (message === "Ping") {
|
||||
logPing(sharedState, clientIP);
|
||||
broadcast(wss, message);
|
||||
return;
|
||||
}
|
||||
|
||||
/* ---------- GCode ---------- */
|
||||
if (GCode.containsCommand(message)) {
|
||||
logCommand(sharedState, clientIP, message);
|
||||
GCode.receiveGCode(robot, message);
|
||||
broadcast(wss, GCode.getM114(robot));
|
||||
return;
|
||||
}
|
||||
|
||||
/* ---------- File Commands ---------- */
|
||||
if (GCode.ContainsFilesCommand(message)) {
|
||||
logCommand(sharedState, clientIP, message);
|
||||
broadcast(wss, GCode.receiveFC(robot, message));
|
||||
return;
|
||||
}
|
||||
|
||||
/* ---------- Status ---------- */
|
||||
if (message === "M114") {
|
||||
logCommand(sharedState, clientIP, message);
|
||||
broadcast(wss, GCode.getM114(robot));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return wss;
|
||||
}
|
||||
|
||||
/* ---------- Helpers ---------- */
|
||||
|
||||
function broadcast(wss, payload) {
|
||||
wss.clients.forEach(client => {
|
||||
if (client.readyState === WebSocket.OPEN) {
|
||||
client.send(payload);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function logCommand(state, ip, message) {
|
||||
fs.appendFileSync(
|
||||
'./logs/gcode_commands.log',
|
||||
`${new Date().toISOString()} ${ip}: ${message}\n`
|
||||
);
|
||||
state.lastCommands.push(`${new Date().toISOString()}: ${message}`);
|
||||
if (state.lastCommands.length > 10) state.lastCommands.shift();
|
||||
}
|
||||
|
||||
function logPing(state, ip) {
|
||||
fs.appendFileSync(
|
||||
'./logs/pings.log',
|
||||
`${new Date().toISOString()} ${ip}: Ping\n`
|
||||
);
|
||||
state.lastPings.push(`${new Date().toISOString()}: Ping`);
|
||||
if (state.lastPings.length > 10) state.lastPings.shift();
|
||||
}
|
||||
|
||||
module.exports = initInputWS;
|
||||
Reference in New Issue
Block a user