Skip to content

Commit

Permalink
Merge pull request #11 from AndrewLemons/dev
Browse files Browse the repository at this point in the history
v2.1.3
  • Loading branch information
AndrewLemons authored Sep 17, 2024
2 parents 8ad5824 + 28a2531 commit 8154aa9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
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": "2.0.3",
"version": "2.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
28 changes: 28 additions & 0 deletions src/classes/BambuPrinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class BambuPrinter extends EventEmitter {
serial: string;
accessCode: string;
state: RawPrinterState;
hasInitialUpdate: boolean;

mqtt: BambuMQTT;

Expand All @@ -29,6 +30,7 @@ class BambuPrinter extends EventEmitter {
this.state = {
timestamp: Date.now(),
};
this.hasInitialUpdate = false;

this.mqtt = new BambuMQTT(host, accessCode, serial);
}
Expand Down Expand Up @@ -73,6 +75,32 @@ class BambuPrinter extends EventEmitter {
return convertState(this.state);
}

/**
* Await the printer to send its initial state.
* @param timeout - The maximum time to wait for the initial state.
* @returns The initial state of the printer.
*/
async awaitInitialState(timeout: number = 30000): Promise<PrinterState> {
return new Promise((resolve, reject) => {
// Timeout if the initial state is not received
let timeoutId = setTimeout(() => {
reject(new Error("Timed out waiting for initial state"));
}, timeout);

// Resolve when the initial state is received
this.once("update", (state) => {
clearTimeout(timeoutId);
resolve(state);
});

// If the initial state has already been received, resolve immediately
if (this.hasInitialUpdate) {
clearTimeout(timeoutId);
resolve(this.getState());
}
});
}

/**
* Pause the current print job.
*/
Expand Down

0 comments on commit 8154aa9

Please sign in to comment.