prusa3d / Prusa-Connect-SDK-Printer

Python printer library for Prusa Connect
31 stars 4 forks source link

Unable to get it running with Readme examples #270

Open Skaronator opened 1 year ago

Skaronator commented 1 year ago

I'm trying to get it running, but the examples from the Readme are all outdated and / or the error handling appears to be wrong?

My use case is basic. My Prusa XL is already connected to Prusa Connect and I just want to get the current printer state.

My odyssey ---- Let's just go through what I did and why it's frustrating. Picking up the very first example from the Readme: ```python from prusa.connect.printer import Printer, const SERVER = "https://connect.prusa3d.com" SN = 'SERIAL_NUMBER_FROM_PRINTER' FINGERPRINT = 'Printer fingerprint' TOKEN = 'secret token from prusa_printer_settings.ini' printer = Printer(const.PrinterType.I3MK3, SN, SERVER, TOKEN) printer.loop() # communication loop ``` Running this will result in the first error: ``` File "/home/niklas/Workspace/prusa-demo/./main.py", line 14, in printer = Printer(const.PrinterType.I3MK3, SN, SERVER, TOKEN) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/niklas/Workspace/prusa-demo/venv/lib/python3.11/site-packages/prusa/connect/printer/__init__.py", line 116, in __init__ if max_retries > 1: ^^^^^^^^^^^^^^^ TypeError: '>' not supported between instances of 'str' and 'int' ``` The arguments from Printer are different now. The 3rd Argument is now finterprint (instead of SERVER) and the 4th argument is now max_retries (instead of TOKEN) ---- The next issue, the Prusa XL doesn't exist as PrinterType. https://github.com/prusa3d/Prusa-Connect-SDK-Printer/blob/b3e8f6d32e689a41e3188920550b61bdc38e7211/prusa/connect/printer/const.py#L47-L57 Okay, maybe it's too new, but Prusa Mini (and MK4) don't even exist. Looking at the code, the PrinterType is optional, so I'll leave it empty: https://github.com/prusa3d/Prusa-Connect-SDK-Printer/blob/b3e8f6d32e689a41e3188920550b61bdc38e7211/prusa/connect/printer/__init__.py#L85-L89 ---- Upnext I tried the following code: While I know the SerialNumber I have no clue what fingerprint is. So I used the Printer UUID from the Prusa Connect settings page of my printer. ```python printer = Printer(None, "", "", 10) thread = Thread(target=printer.loop) thread.start() while True: print(printer.state) sleep(1) ``` Running that looks good! I got a State back! ``` $ ./main.py State.BUSY State.BUSY ``` But wait a second.. HOW? I didn't even enter an API Token and just the SN and UUID shouldn't be enough. So I tried setting stupid data like this: ```diff - printer = Printer(None, "", "", 10) + printer = Printer(None, "dummystring", "dummystring", 10) ``` Running that looks good as well? I got a state back and no error whatsoever. ``` $ ./main.py State.BUSY State.BUSY ``` ---- Digging deeper into the SDK Code I have to call `printer.set_connection`. The function want a Server and a Token but not the API Token, a different token. I did some research and looks like downloading the "LAN settings" from Prusa Connect contains both, the Server and the token. Furthermore, the API Key is still not set, so just set this as well. Running that code now, which looks like this: ```python SN = '10381-xyxy' printer = Printer(None, SN, None, 10) printer.api_key = "xyz" printer.set_connection("https://connect.prusa3d.com", "xyz") thread = Thread(target=printer.loop) thread.start() while True: print(printer.state) sleep(1) ``` Running it? ``` $ ./main.py State.BUSY State.BUSY ``` Which is still wrong! It should be PRINTING

Would it be possible to provide an up2date Readme example that shows the current printer status, nozzle temperature, print time and document which (API) tokens and fingerprints I need to get it running?

CuriousTimo commented 1 year ago

I think you are mixing up a few things.

Since you want to connect locally to an XL you should look into PrusaLink v2. It is the same as what the Home Assistant integration uses for example.

From what I can tell, based on the python, you are looking into the V1 source code. This is not correct for the Prusa XL. Also you should look into the part of the API that handles the local API directly to the printer, not the Prusa Connect stuff.

Skaronator commented 1 year ago

I'm specially looking for to accessing the Print Queue and Set Ready command for my home assistant. Both are Prusa Connect features and not available/part of Prusa Link Vx. Hence, why I'm looking at this SDK.

Looks like this library is mostly meant for a Printer that communicates with Prusa Connect and not a User/Script that reads data from Prusa Connect?

CuriousTimo commented 1 year ago

In that case it might be much easier to just use the dev tools on the browser when working on connect.prusa.com and look at what is happening there on the network tab in dev tools

Skaronator commented 1 year ago

Yeah, I was really hoping this "Python printer library for Prusa Connect" from Prusa directly could do most of the heavy lifting.

UmBsublime commented 3 months ago

I'm somewhat confused by this thread. I'm also trying the example code from the readme and not really getting expected results.

I currently own a prusaXL which is registered to connect.prusa3d.com. I have not found any way instantiate a prusa.connect.printer.Printer that allows me to query my printer.

The constructor for prusa.connect.printer.Printer does not even seem to match the examples in the readme

class Printer:
 ...
def __init__(self,
                 type_: Optional[const.PrinterType] = None,
                 sn: Optional[str] = None,
                 fingerprint: Optional[str] = None,
                 max_retries: int = 1,
                 mmu_supported: bool = True):

And here is the example from readme

from prusa.connect.printer import Printer, const

SERVER = "https://connect.prusa3d.com"
SN = 'SERIAL_NUMBER_FROM_PRINTER'
FINGERPRINT = 'Printer fingerprint'
TOKEN = 'secret token from prusa_printer_settings.ini'
printer = Printer(const.PrinterType.I3MK3, SN, SERVER, TOKEN)

This leaves me with 2 questions:

  1. My end goal is to fetch telemetry for my printer through connect.prusa3d.com is this something possible with this module ?
  2. How do I instantiate the Printer object to allow this and where can I find the printer fingerpint ?