Skip to content

Commit

Permalink
Merge pull request #4 from AndrewLemons/dev
Browse files Browse the repository at this point in the history
v1.1.3
  • Loading branch information
AndrewLemons authored Feb 28, 2024
2 parents 693f683 + 5d565ba commit b0e89f0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bambu-js",
"version": "1.1.2",
"version": "1.1.3",
"description": "Tools to interact with Bambu Lab printers over MQTT and FTP.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
40 changes: 32 additions & 8 deletions src/classes/BambuFTP.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import fs from "node:fs";
import path from "node:path";
import * as ftp from "basic-ftp";
import { PrinterDirectoryUnavailable } from "../utilities/errors";

const TIMEOUT = 15000;
const USERNAME = "bblp";
Expand All @@ -25,7 +27,14 @@ export default class BambuFTP {
await this.client.ensureDir(path);

// Get print jobs
const printJobs = await this.client.list(path);
const printJobs = await this.client.list(path).catch((error) => {
switch (error.code) {
case 550:
throw new PrinterDirectoryUnavailable(path);
default:
throw error;
}
});
return printJobs.map((printJob) => printJob.name);
}

Expand All @@ -40,22 +49,38 @@ export default class BambuFTP {
destinationPath: string,
progressCallback?: (progress: number) => void,
) {
const destinationDir = path.dirname(destinationPath);

// Create directory if it doesn't exist
await this.client.ensureDir(destinationPath);
await this.client.ensureDir(destinationDir).catch((error) => {
switch (error.code) {
case 550:
throw new PrinterDirectoryUnavailable(destinationDir);
default:
throw error;
}
});

// Find the size of the file
const stats = fs.statSync(sourcePath);
const fileSize = stats.size;

// Add progress callback
if (progressCallback) {
this.client.trackProgress((info) =>
progressCallback(info.bytes / fileSize),
);
this.client.trackProgress((info) => {
progressCallback(info.bytes / fileSize);
});
}

// Send print job
await this.client.uploadFrom(sourcePath, destinationPath);
await this.client.uploadFrom(sourcePath, destinationPath).catch((error) => {
switch (error.code) {
case 550:
throw new PrinterDirectoryUnavailable(destinationPath);
default:
throw error;
}
});

// Disconnect progress tracking
this.client.trackProgress();
Expand Down Expand Up @@ -109,7 +134,6 @@ export default class BambuFTP {
) {
const context = new BambuFTP(host, accessCode);
await context.connect();
await callback(context);
await context.disconnect();
await callback(context).finally(() => context.disconnect());
}
}
4 changes: 2 additions & 2 deletions src/classes/BambuPrinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ export default class BambuPrinter extends EventEmitter {
print: {
command: "project_file",
// File info
param: path.resolve("Metadata/", file),
url: "file://" + path.resolve("/sdcard/", projectPath),
param: path.join("Metadata/", file),
url: "file://" + path.join("/sdcard/", projectPath),
subtask_name: name,
md5: hash,
// Options
Expand Down
9 changes: 9 additions & 0 deletions src/utilities/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@ export class PrinterNotConnected extends Error {
super(message ?? "The printer is not connected.");
}
}

export class PrinterDirectoryUnavailable extends Error {
constructor(path?: string) {
let pathString = path ? JSON.stringify(path) + " " : "";
super(
`The directory ${pathString}is not available on the printer's SD card.`,
);
}
}

0 comments on commit b0e89f0

Please sign in to comment.