mattcar15 / bambu-connect

Connect to the stats, controls, and camera of your bambu printer
Other
47 stars 8 forks source link

executeClient.send_command - only the first command is working #4

Closed lixxbox closed 8 months ago

lixxbox commented 8 months ago

As a beginner in Python, I'm facing an issue with controlling a printer lamp.

Although I can successfully execute either the 'on' or 'off' command individually, it seems that only the first command in my code is working. If I rearrange the order of these commands, still only the first one gets executed.

I've verified that the payload is correct. Do you have any tips for me?

from bambu_connect import BambuClient
import json, time
import config_helper

def toggle_led(toggle):
    payload = {
        "system": {
            "command": "ledctrl",
            "led_node": "chamber_light",
            "led_mode": toggle,
            "led_on_time": 500,
            "led_off_time": 500,
            "loop_times": 0,
            "interval_time": 0,
        },
        "user_id": "1234567890",
    }
    print("toggle:", toggle)
    return json.dumps(payload)

bambu_client = BambuClient(
    config_helper.hostname, config_helper.access_code, config_helper.serial
)

bambu_client.executeClient.send_command(toggle_led("on"))
time.sleep(5)
bambu_client.executeClient.send_command(toggle_led("off"))
lixxbox commented 8 months ago

https://github.com/mattcar15/bambu-connect/blob/7afb59f1ec6959642cf02490a66078a744931dc7/bambu_connect/ExecuteClient.py#L27

There is a self.client.disconnect() in the send_command function. Not sure, if it is needed for something, but if I comment that out, it works.

mattcar15 commented 8 months ago

Funny timing, I found this two days ago too haha, had fixed it but hadn't merged it. Just merged it so should be good now. The disconnect is indeed the issue, changed it so it disconnects when the object is destroyed. https://github.com/mattcar15/bambu-connect/commit/bcd83f375798d10954b98f48adfcf4180d7e0c0d

I'll leave the issue open so you can confirm too, lemme know

lixxbox commented 8 months ago

It's working now.

My example script throws the following error after the two send_commands have been called. I think the error is triggered by exiting the script and probably has more to do with how I (not) handle it. Edit: Adding bambu_client.executeClient.disconnect() at the bottom solves this.

But the light goes on and off now. ☺️ Thank you very much!

toggle: on
toggle: off
Exception ignored in: <function BambuClient.__del__ at 0x000001C523D3C680>
Traceback (most recent call last):
  File "C:\Python311\Lib\site-packages\bambu_connect\BambuClient.py", line 17, in __del__
  File "C:\Python311\Lib\site-packages\bambu_connect\ExecuteClient.py", line 24, in disconnect
  File "C:\Python311\Lib\site-packages\paho\mqtt\client.py", line 1358, in disconnect
  File "C:\Python311\Lib\site-packages\paho\mqtt\client.py", line 2853, in _send_disconnect
  File "C:\Python311\Lib\site-packages\paho\mqtt\client.py", line 3016, in _packet_queue
  File "C:\Python311\Lib\site-packages\paho\mqtt\client.py", line 1577, in loop_write
  File "C:\Python311\Lib\site-packages\paho\mqtt\client.py", line 2464, in _packet_write
  File "C:\Python311\Lib\site-packages\paho\mqtt\client.py", line 649, in _sock_send
  File "C:\Python311\Lib\ssl.py", line 1210, in send
ssl.SSLError: Underlying socket connection gone (_ssl.c:2321)
mattcar15 commented 8 months ago

If your Python script ends normally, Python's garbage collector will destroy the objects that are no longer needed and del should be called, which is where the disconnect happens. If you exit it (with ctrl+c) then this would be an expected error yeah

lixxbox commented 8 months ago

Hm okay, I did not quit with ctrl+c. The script just ran bambu_client.executeClient.send_command(toggle_led("on")) & bambu_client.executeClient.send_command(toggle_led("off")) and quit itself.

So, it looks like del was not called?

mattcar15 commented 8 months ago

Can you share the script?

lixxbox commented 8 months ago

It's in the first post.