Horrible G91 G90 mistake
This commit is contained in:
311
startRobot.js
311
startRobot.js
@@ -1,241 +1,70 @@
|
||||
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() + "/");
|
||||
const fs = require('fs');
|
||||
const https = require('https');
|
||||
|
||||
const Robot = require('./robot/Robot');
|
||||
const GCode = require('./robot/GCode');
|
||||
|
||||
const initInputWS = require('./server/InputWS');
|
||||
const createInfoServer = require('./server/InfoServer');
|
||||
|
||||
const TenetSender = require('./robot/TelnetSenderGRBL');
|
||||
|
||||
/* ---------- Robot ---------- */
|
||||
const robot = new Robot(250, 264, 100);
|
||||
|
||||
/* ---------- Shared State ---------- */
|
||||
const sharedState = {
|
||||
connectedClients: [],
|
||||
lastCommands: [],
|
||||
lastPings: []
|
||||
};
|
||||
|
||||
/* ---------- HTTPS ---------- */
|
||||
const httpsOptions = {
|
||||
enable: true,
|
||||
key: fs.readFileSync('https/localhost.key'),
|
||||
cert: fs.readFileSync('https/localhost.pem'),
|
||||
passphrase: 'abcd'
|
||||
};
|
||||
|
||||
const httpsServer = https.createServer(httpsOptions);
|
||||
|
||||
/* ---------- WebSocket Input ---------- */
|
||||
initInputWS(httpsServer, robot, GCode, sharedState);
|
||||
|
||||
/* ---------- GRBL Sender ---------- */
|
||||
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";
|
||||
|
||||
const telnetSender1 = new TenetSender(baseIP, 2300, "x", "y", "z");
|
||||
const telnetSender2 = new TenetSender(elbowIP, 5000, "a", null, null);
|
||||
const telnetSender3 = new TenetSender(handIP, 5000, "c", "e", "b");
|
||||
|
||||
setTimeout(() => {
|
||||
[telnetSender1, telnetSender2, telnetSender3].forEach(s => {
|
||||
if (s?.tSocket) robot.cmdReceivers.push(s);
|
||||
});
|
||||
}, 5000);
|
||||
|
||||
/* ---------- Start Input Server ---------- */
|
||||
const port = Number(process.env.PORT) || 2095;
|
||||
httpsServer.listen(port);
|
||||
console.log(`Input HTTPS/WebSocket on https://localhost:${port}`);
|
||||
|
||||
/* ---------- Info Server ---------- */
|
||||
const infoServer = createInfoServer(
|
||||
httpsOptions,
|
||||
sharedState,
|
||||
robot,
|
||||
GCode,
|
||||
[
|
||||
{ name: "Base", instance: telnetSender1 },
|
||||
{ name: "Elbow", instance: telnetSender2 },
|
||||
{ name: "Hand", instance: telnetSender3 }
|
||||
]
|
||||
);
|
||||
|
||||
const infoPort = 2098;
|
||||
infoServer.listen(infoPort);
|
||||
console.log(`Info server on https://localhost:${infoPort}`);
|
||||
Reference in New Issue
Block a user