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); let connectedClients = []; let lastCommands = []; let lastPings = []; let sharedState = { connectedClients: [], lastCommands: [], lastPings: [] }; const httpsOptions = { "enable": true, 'key': fs.readFileSync('https/localhost.key'), 'cert': fs.readFileSync('https/localhost.pem'), "passphrase": "abcd" } server = https.createServer(httpsOptions, (req, res) => {;}); const wss = new WebSocket.Server({ server }); // Input Connection for WebService 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) { client.send( message ); } }); } 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) { if (client.readyState == WebSocket.OPEN) { client.send( reply ); } }); } 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) { if (client.readyState == WebSocket.OPEN) { client.send( reply ); } }); } // 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) { if (client.readyState == WebSocket.OPEN) { client.send( reply ); } }); } }); }); /* const InputWS = require('./server/InputWS.js') const wss = InputWS(server, robot, GCode, sharedState); */ var TenetSender = require('./robot/TelnetSenderGRBL.js') const baseIP = process.env.GRBL_BASE_IP ?? "fluidNcBase.local"; const elbowIP = process.env.GRBL_ELLBOW_IP ?? "fluidNcEllbow.local"; const handIP = process.env.GRBL_HAND_IP ?? "fluidNcHand.local"; console.log(baseIP, elbowIP, handIP); var telnetSender1 = new TenetSender(urlGRBL = baseIP, maxSpeedF = 2300, xAxisGrbl = "x", yAxisGrbl = "y", zAxisGrbl = "z"); var telnetSender2 = new TenetSender(urlGRBL = elbowIP, maxSpeedF = 5000, xAxisGrbl = "a", yAxisGrbl = null, zAxisGrbl = null); var telnetSender3 = new TenetSender(urlGRBL = handIP, maxSpeedF = 5000, xAxisGrbl = "c", yAxisGrbl = "e", zAxisGrbl = "b"); setTimeout(function() { if(telnetSender1?.tSocket != null) { robot.cmdReceivers.push(telnetSender1); console.log("Socket 1 added");} else{ console.log("Socket 1 is not connected"); console.log(telnetSender1?.tSocket);} if(telnetSender2?.tSocket != null) { robot.cmdReceivers.push(telnetSender2); console.log("Socket 2 added");} else{ console.log("Socket 2 is not connected"); console.log(telnetSender2?.tSocket);} if(telnetSender3?.tSocket != null) { robot.cmdReceivers.push(telnetSender3); console.log("Socket 3 added");} else{ console.log("Socket 3 is not connected"); console.log(telnetSender3?.tSocket);} }, 8000); setTimeout(function(){ if(robot.cmdReceivers.length == 0){ console.log("Kein GRBL Command-Receiver gefunden. Verwende reines Test-Setting.") } },8002) console.log("Works with FluidNc Base"); const port = Number(process.env.PORT); const listenPort = Number.isInteger(port) ? port : 2095; console.log("Starting server on port: " + listenPort.toString()); server.listen(listenPort); console.log("Listen on Port: https://localhost:" + listenPort.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 === '/style.css') { fs.readFile('./public/style.css', (err, data) => { if (err) { res.writeHead(404); res.end('Not found'); } else { res.writeHead(200, {'Content-Type': 'text/css'}); res.end(data); } }); } else if (req.url === '/allApps.css') { fs.readFile('./public/allApps.css', (err, data) => { if (err) { res.writeHead(404); res.end('Not found'); } else { res.writeHead(200, {'Content-Type': 'text/css'}); 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', 15), lastPings: getLastLines('./logs/pings.log', 15) }; res.writeHead(200, {'Content-Type': 'application/json'}); res.end(JSON.stringify(status)); } else if (req.url === '/api/position'){ res.writeHead(200, {'Content-Type': 'application/json'}); res.end(GCode.getM114(robot)); } else { res.writeHead(404); res.end('Not found'); } }); const InfPort = Number(process.env.PORT); const listenInfoPort = Number.isInteger(InfPort) ? InfPort : 2098; console.log("Starting info server on port: " + listenInfoPort.toString()); infoServer.listen(listenInfoPort); console.log("Info server listening on https://localhost:"+ listenInfoPort.toString() + "/");