Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trouble with disconnect() and UnhandledPromiseRejection #3

Open
b-fitzpatrick opened this issue Jan 12, 2025 · 2 comments
Open

Trouble with disconnect() and UnhandledPromiseRejection #3

b-fitzpatrick opened this issue Jan 12, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@b-fitzpatrick
Copy link

First off, thanks for this!

I'm trying to create a simple web API to allow dynamic flow percentage changes. I'm able to connect and send a GCodeCommand successfully, even multiple times, but after a few minutes, it always errors out as shown below. The client:disconnect message is never received.

MQTT Connected
New print message: {
  sequence_id: 3,
  command: 'gcode_line',
  param: 'M221 S96\n',
  reason: 'success',
  result: 'success'
}
Trying to disconnect
node:internal/process/promises:392
      new UnhandledPromiseRejection(reason);
      ^

UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "Error publishing to topic 'device/03919C440902089/request': client disconnecting".
    at throwUnhandledRejectionsMode (node:internal/process/promises:392:7)
    at processPromiseRejections (node:internal/process/promises:475:17)
    at process.processTicksAndRejections (node:internal/process/task_queues:106:32) {
  code: 'ERR_UNHANDLED_REJECTION'
}

Node.js v23.5.0

Here's my code. Any idea what my problem is?

import { BambuClient, GCodeLineCommand } from "bambu-node";
import express from "express";
import printers from "./printers.js";

const app = express();
const PORT = process.env.port || 3331;

app.get("/setFlowPct", async function (req, res) {

  const device = req.query.device;
  const flowPct = req.query.flowPct;

  console.log("Device :", device);
  console.log("FlowPct :", flowPct);

  if (device == undefined) {
    console.log("device not provided");
    res.send("device not provided");
  }

  if (flowPct == undefined) {
    console.log("flowPct not provided");
    res.send("flowPct not provided");
  }

  const printer = printers[device];
  if (printer == undefined) {
    console.log("device not found");
    res.send("device not found");
  }
    
  const gcode = "M221 S" + flowPct;
  console.log("gcode :", gcode);

  const client = new BambuClient({
    host: printers[device].host,
    accessToken: printers[device].accessToken,
    serialNumber: printers[device].serialNumber
  });

  // Send gcode command on connect
  client.on("client:connect", async () => {
    console.log("MQTT Connected");
    try {
      await client.executeCommand(new GCodeLineCommand({ gcodes: [gcode] }));
    }
    catch (err) {
      console.error(err);
      res.send(err);
    };
  })

  client.on("message", async (topic, key, data) => {
    console.log(`New ${key} message:`, data)
    
    // Result of GCodeLineCommand
    if (key == "print" && data.param == gcode + "\n") {
      try {
        console.log("Trying to disconnect");
        await client.disconnect();
      }
      catch (err) {
        console.error(err);
        res.send(err);
      };
      res.send("gcode: " + gcode + ", result: " + data.result);
    }
  });

  client.on("client:disconnect", (isDisconnectOffline) => {
    console.log("MQTT Disconnected");
  })

  try {
    await client.connect();
  }
  catch (err) {
    console.error(err);
    res.send(err);
  };

});

app.listen(PORT, function (err) {
  if (err) console.error(err);
  console.log("Server created Successfully on PORT", PORT);
});
@THE-SIMPLE-MARK THE-SIMPLE-MARK added the bug Something isn't working label Jan 12, 2025
@THE-SIMPLE-MARK
Copy link
Owner

Hey! Unfortunately, I no longer have the bandwidth nor a printer to troubleshoot this anymore.

I suspect that Bambu has changed some things around regarding the MQTT channels, which is causing the bug.

I'm honestly not sure what to do with this library. PRs are accepted, but I am sadly no longer able to fulfill my role as a maintainer. I'll ask around in the Bambu discord if anyone wants to take this lib over...

@b-fitzpatrick
Copy link
Author

OK, thanks for letting me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants