Tests und Mock
This commit is contained in:
68
test/helpers/TestFluidNCClient.js
Normal file
68
test/helpers/TestFluidNCClient.js
Normal file
@@ -0,0 +1,68 @@
|
||||
const WebSocket = require("ws");
|
||||
const EventEmitter = require("events");
|
||||
|
||||
class TestFluidNCClient extends EventEmitter {
|
||||
constructor(cfg) {
|
||||
super();
|
||||
|
||||
this.host = cfg.host;
|
||||
this.port = cfg.port || 81;
|
||||
|
||||
this.ws = null;
|
||||
this.connect();
|
||||
}
|
||||
|
||||
connect() {
|
||||
const url = `ws://${this.host}:${this.port}`;
|
||||
|
||||
this.ws = new WebSocket(url);
|
||||
|
||||
this.ws.on("open", () => {
|
||||
console.log("[TestFluidNC] Connected (WS)");
|
||||
});
|
||||
|
||||
this.ws.on("message", (msg) => {
|
||||
this.emit("message", msg.toString());
|
||||
});
|
||||
|
||||
this.ws.on("close", () => {
|
||||
console.log("[TestFluidNC] Disconnected");
|
||||
});
|
||||
|
||||
this.ws.on("error", (err) => {
|
||||
console.log("[TestFluidNC] WS Error:", err.message);
|
||||
});
|
||||
}
|
||||
|
||||
sendLine(cmd) {
|
||||
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
||||
this.ws.send(cmd + "\n");
|
||||
}
|
||||
}
|
||||
|
||||
requestStatus() {
|
||||
this.sendLine("?");
|
||||
}
|
||||
|
||||
jog(relative, axis, value) {
|
||||
const cmd = relative? `$J=G91 G1 ${axis}${value} F2000\r\n`: `$J=G90 G1 ${axis}${value} F2000\r\n`;
|
||||
this.sendLine(cmd);
|
||||
}
|
||||
|
||||
sendGcode(cmd) {
|
||||
this.sendLine(cmd);
|
||||
}
|
||||
|
||||
onMessage(fn) {
|
||||
this.on("message", fn);
|
||||
}
|
||||
|
||||
close() {
|
||||
if (this.ws) {
|
||||
this.ws.close();
|
||||
this.ws = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = TestFluidNCClient;
|
||||
Reference in New Issue
Block a user