MCP 3D Printer Server
by DMontgomery40
Verified
- node_modules
- bambu-js
- dist
- classes
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_events_1 = __importDefault(require("node:events"));
const mqtt_1 = __importDefault(require("mqtt"));
const MQTT_USERNAME = "bblp";
/**
* A class for interfacing with a Bambu Lab printers over MQTT.
* @emits update - Emitted when the printer's state is updated.
* @emits connect - Emitted when the printer is connected.
* @emits disconnect - Emitted when the printer is disconnected.
*/
class BambuMQTT extends node_events_1.default {
/**
* Create a new BambuMQTT instance.
* @param host - The host of the printer.
* @param accessCode - The access code for the printer.
* @param serial - The serial number of the printer.
*/
constructor(host, accessCode, serial) {
super();
this.host = host;
this.accessCode = accessCode;
this.serial = serial;
}
/**
* Connect to the printer.
*/
connect() {
return __awaiter(this, void 0, void 0, function* () {
this.client = mqtt_1.default.connect(`mqtt://${this.host}:8883`, {
username: MQTT_USERNAME,
password: this.accessCode,
clientId: MQTT_USERNAME,
protocol: "mqtts",
rejectUnauthorized: false,
reconnectPeriod: 5000,
});
this.client.on("connect", this.onConnect.bind(this));
this.client.on("close", this.onClose.bind(this));
this.client.on("message", this.onMessage.bind(this));
this.client.on("error", (error) => __awaiter(this, void 0, void 0, function* () {
this.emit("error", new Error("Client error"));
yield this.disconnect();
}));
this.client.stream.on("error", (error) => __awaiter(this, void 0, void 0, function* () {
this.emit("error", new Error("Stream error"));
yield this.disconnect();
}));
});
}
/**
* Disconnect from the printer.
*/
disconnect() {
return __awaiter(this, void 0, void 0, function* () {
this.client.end();
this.client.emit("close");
this.client.removeAllListeners();
this.emit("disconnect");
});
}
/**
* Send a request to the device.
* @param payload - The payload to send to the device.
*/
sendRequest(payload) {
this.client.publish(`device/${this.serial}/request`, JSON.stringify(payload));
}
/**
* Handle the connection event.
*/
onConnect() {
return __awaiter(this, void 0, void 0, function* () {
// Subscribe to the printer
this.client.subscribe(`device/${this.serial}/report`);
// Request the printer's complete state
this.sendRequest({ pushing: { sequence_id: "0", command: "pushall" } });
this.emit("connect");
});
}
/**
* Handle the close event.
*/
onClose() {
this.emit("disconnect");
}
/**
* Handle an incoming message.
* @param topic - The topic the message was sent to.
* @param message - The message payload.
*/
onMessage(topic, message) {
const payload = JSON.parse(message.toString());
if (topic === `device/${this.serial}/report`) {
if (payload.print) {
if (payload.print.command === "push_status") {
// Remove message metadata
let state = Object.assign(Object.assign({}, payload.print), { command: undefined, msg: undefined, sequence_id: undefined });
// Emit the update event
this.emit("update", state);
}
}
}
}
/**
* Get the current connection status.
*/
get isConnected() {
return this.client.connected;
}
}
exports.default = BambuMQTT;
//# sourceMappingURL=BambuMQTT.js.map