initial
This commit is contained in:
105
server/fluidnc/FluidNCClient.js
Normal file
105
server/fluidnc/FluidNCClient.js
Normal file
@@ -0,0 +1,105 @@
|
||||
|
||||
const config = require("../config/config");
|
||||
const net = require("net");
|
||||
const { resolve } = require("path");
|
||||
const TelnetSocket = require("telnet-stream");
|
||||
|
||||
|
||||
|
||||
class FluidNCClient {
|
||||
constructor() {
|
||||
console.log("[FluidNCClient] Initializing...");
|
||||
|
||||
this.url = config.fluidnc.host;
|
||||
this.port = config.fluidnc.port || 23;
|
||||
this.tSocket = null;
|
||||
this.connected = false;
|
||||
this.state = { x:0, z:0, state:"unknown" };
|
||||
this.listeners = [];
|
||||
|
||||
this._connect();
|
||||
}
|
||||
|
||||
_connect() {
|
||||
console.log(`[FluidNCClient] Connecting to FluidNC: ${this.url}`);
|
||||
|
||||
let socket = null;
|
||||
|
||||
new Promise((resolve, reject) => {
|
||||
socket = net.createConnection({host:this.url, port:this.port}, () => {
|
||||
resolve(socket);
|
||||
}).on("error", reject);
|
||||
}).then(connection => {
|
||||
console.log("[FluidNCClient] Telnet socket connected");
|
||||
this.connected = true;
|
||||
|
||||
connection.on("data", data => {
|
||||
const msg = data.toString().replace(/\r/g,"").trim();
|
||||
if(!msg) return;
|
||||
console.log("[FluidNCClient] Received:", msg);
|
||||
|
||||
if(msg.startsWith("<") && msg.includes("MPos:")){
|
||||
const match = msg.match(/MPos:([\d\.\-]+),[\d\.\-]+,([\d\.\-]+)/);
|
||||
if(match){
|
||||
this.state.x = parseFloat(match[1]);
|
||||
this.state.z = parseFloat(match[2]);
|
||||
this.state.state = msg.split(",")[0].replace("<","");
|
||||
this._broadcast(this.state);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if(socket != null){
|
||||
this.tSocket = TelnetSocket(socket);
|
||||
|
||||
this.tSocket.on("close", () => {
|
||||
console.log("[FluidNCClient] Telnet Closed");
|
||||
this.tSocket = null;
|
||||
this.connected = false;
|
||||
setTimeout(()=>this._connect(), 30000);
|
||||
});
|
||||
}
|
||||
|
||||
}, error => {
|
||||
console.log("[FluidNCClient] Telnet Connection Error:", error.toString());
|
||||
this.tSocket = null;
|
||||
this.connected = false;
|
||||
setTimeout(()=>this._connect(), 30000);
|
||||
});
|
||||
}
|
||||
|
||||
_broadcast(msg){
|
||||
this.listeners.forEach(fn => fn(msg));
|
||||
}
|
||||
|
||||
onMessage(fn){
|
||||
this.listeners.push(fn);
|
||||
}
|
||||
|
||||
send(cmd){
|
||||
if(this.tSocket){
|
||||
console.log("[FluidNCClient] Sending:", cmd);
|
||||
this.tSocket.write(cmd + "\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
jog(axis, value){
|
||||
if(!this.connected) return;
|
||||
this.send("G91");
|
||||
this.send(`G0 ${axis.toUpperCase()}${value}`);
|
||||
this.send("G90");
|
||||
}
|
||||
|
||||
sendGcode(cmd){
|
||||
if(!this.connected) return;
|
||||
this.send("G90");
|
||||
this.send(cmd);
|
||||
}
|
||||
|
||||
setZero(){
|
||||
if(!this.connected) return;
|
||||
this.send("G10 L20 P1 X0 Z0");
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = FluidNCClient;
|
||||
Reference in New Issue
Block a user