MCP 3D Printer Server
by DMontgomery40
Verified
- node_modules
- bambu-js
- dist
- classes
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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_fs_1 = __importDefault(require("node:fs"));
const node_path_1 = __importDefault(require("node:path"));
const ftp = __importStar(require("basic-ftp"));
const errors_1 = require("../utilities/errors");
const TIMEOUT = 15000;
const USERNAME = "bblp";
class BambuFTP {
constructor(host, accessCode) {
this.host = host;
this.accessCode = accessCode;
this.client = new ftp.Client();
}
/**
* Read files in a given directory on the printer's SD card.
* @param path - The absolute path to the directory on the printer's SD card.
* @throws {errors.PrinterNotConnected} - If the printer is not connected.
*/
readDir(path) {
return __awaiter(this, void 0, void 0, function* () {
// Create directory if it doesn't exist
yield this.client.ensureDir(path);
// Get print jobs
const printJobs = yield this.client.list(path).catch((error) => {
switch (error.code) {
case 550:
throw new errors_1.PrinterDirectoryUnavailable(path);
default:
throw error;
}
});
return printJobs.map((printJob) => printJob.name);
});
}
/**
* Send a file to the printer's SD card.
* @param sourcePath - The absolute path to the file on the local machine.
* @param destinationPath - The absolute path to the file on the printer's SD card.
* @param progressCallback - A callback function that is called with the progress of the file transfer.
*/
sendFile(sourcePath, destinationPath, progressCallback) {
return __awaiter(this, void 0, void 0, function* () {
const destinationDir = node_path_1.default.dirname(destinationPath);
// Create directory if it doesn't exist
yield this.client.ensureDir(destinationDir).catch((error) => {
switch (error.code) {
case 550:
throw new errors_1.PrinterDirectoryUnavailable(destinationDir);
default:
throw error;
}
});
// Find the size of the file
const stats = node_fs_1.default.statSync(sourcePath);
const fileSize = stats.size;
// Add progress callback
if (progressCallback) {
this.client.trackProgress((info) => {
progressCallback(info.bytes / fileSize);
});
}
// Send print job
yield this.client.uploadFrom(sourcePath, destinationPath).catch((error) => {
switch (error.code) {
case 550:
throw new errors_1.PrinterDirectoryUnavailable(destinationPath);
default:
throw error;
}
});
// Disconnect progress tracking
this.client.trackProgress();
});
}
/**
* Remove a file from the printer's SD card.
* @param path - The absolute path to the file on the printer's SD card.
*/
removeFile(path) {
return __awaiter(this, void 0, void 0, function* () {
yield this.client.remove(path);
});
}
/**
* Connect to the printer.
*/
connect() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.client)
this.client = new ftp.Client(TIMEOUT);
yield this.client.access({
host: this.host,
port: 990,
user: USERNAME,
password: this.accessCode,
secureOptions: {
rejectUnauthorized: false,
},
secure: "implicit",
});
});
}
/**
* Disconnect from the printer.
*/
disconnect() {
return __awaiter(this, void 0, void 0, function* () {
this.client.close();
});
}
/**
* Create a context for manipulating files on the printer's SD card.
* This function will connect to the printer, execute the callback, and then disconnect from the printer.
* The goal is to prevent multiple connections to the printer at the same time.
* @param host - The IP address of the printer.
* @param accessCode - The access code for the printer.
* @param callback - Callback to manipulate files within the context.
*/
static createContext(host, accessCode, callback) {
return __awaiter(this, void 0, void 0, function* () {
const context = new BambuFTP(host, accessCode);
yield context.connect();
yield callback(context).finally(() => context.disconnect());
});
}
}
exports.default = BambuFTP;
//# sourceMappingURL=BambuFTP.js.map