bambu_ftp.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. import asyncio
  2. import ftplib
  3. import logging
  4. import os
  5. import socket
  6. import ssl
  7. from collections.abc import Awaitable, Callable
  8. from ftplib import FTP, FTP_TLS
  9. from io import BytesIO
  10. from pathlib import Path
  11. from typing import TypeVar
  12. logger = logging.getLogger(__name__)
  13. T = TypeVar("T")
  14. class ImplicitFTP_TLS(FTP_TLS):
  15. """FTP_TLS subclass for implicit FTPS (port 990) with model-specific SSL handling.
  16. X1C/P1S printers (vsFTPd) require SSL with session reuse on the data channel.
  17. A1/A1 Mini printers have issues with SSL on the data channel entirely and
  18. timeout waiting for transfer completion. Set skip_session_reuse=True for A1
  19. printers to skip SSL on the data channel (control channel remains encrypted).
  20. """
  21. def __init__(self, *args, skip_session_reuse: bool = False, **kwargs):
  22. super().__init__(*args, **kwargs)
  23. self._sock = None
  24. self.skip_session_reuse = skip_session_reuse
  25. self.ssl_context = ssl.create_default_context()
  26. self.ssl_context.check_hostname = False
  27. self.ssl_context.verify_mode = ssl.CERT_NONE
  28. def connect(self, host="", port=990, timeout=-999, source_address=None):
  29. """Connect to host, wrapping socket in TLS immediately (implicit FTPS)."""
  30. if host:
  31. self.host = host
  32. if port > 0:
  33. self.port = port
  34. if timeout != -999:
  35. self.timeout = timeout
  36. if source_address:
  37. self.source_address = source_address
  38. # Create and wrap socket immediately (implicit TLS)
  39. self.sock = socket.create_connection((self.host, self.port), self.timeout, source_address=self.source_address)
  40. self.sock = self.ssl_context.wrap_socket(self.sock, server_hostname=self.host)
  41. self.af = self.sock.family
  42. self.file = self.sock.makefile("r", encoding=self.encoding)
  43. self.welcome = self.getresp()
  44. return self.welcome
  45. def ntransfercmd(self, cmd, rest=None):
  46. """Override to wrap data connection in SSL for X1C/P1S only.
  47. X1C/P1S printers (vsFTPd) require SSL session reuse on the data channel.
  48. A1/A1 Mini printers have issues with SSL on the data channel entirely -
  49. they timeout waiting for the transfer completion response. For A1, we
  50. skip SSL wrapping on the data channel (control channel remains encrypted).
  51. """
  52. conn, size = FTP.ntransfercmd(self, cmd, rest)
  53. if self._prot_p and not self.skip_session_reuse:
  54. # X1C/P1S: Wrap data channel with SSL session reuse (required by vsFTPd)
  55. conn = self.ssl_context.wrap_socket(
  56. conn,
  57. server_hostname=self.host,
  58. session=self.sock.session,
  59. )
  60. # A1/A1 Mini (skip_session_reuse=True): Don't wrap data channel in SSL
  61. # The control channel remains encrypted via implicit FTPS
  62. return conn, size
  63. class BambuFTPClient:
  64. """FTP client for retrieving files from Bambu Lab printers."""
  65. FTP_PORT = 990
  66. DEFAULT_TIMEOUT = 30 # Default timeout in seconds (increased for A1 printers)
  67. # Models that need SSL session reuse disabled (A1 series has FTP issues with session reuse)
  68. # P2S should use normal session reuse like X1C/P1S, not skip it
  69. SKIP_SESSION_REUSE_MODELS = ("A1", "A1 Mini", "P1S", "P1P")
  70. def __init__(
  71. self,
  72. ip_address: str,
  73. access_code: str,
  74. timeout: float | None = None,
  75. printer_model: str | None = None,
  76. ):
  77. self.ip_address = ip_address
  78. self.access_code = access_code
  79. self.timeout = timeout if timeout is not None else self.DEFAULT_TIMEOUT
  80. self.printer_model = printer_model
  81. self._ftp: ImplicitFTP_TLS | None = None
  82. def _should_skip_session_reuse(self) -> bool:
  83. """Check if this printer model needs SSL session reuse disabled."""
  84. if not self.printer_model:
  85. return False
  86. return self.printer_model in self.SKIP_SESSION_REUSE_MODELS
  87. def connect(self) -> bool:
  88. """Connect to the printer FTP server (implicit FTPS on port 990)."""
  89. try:
  90. skip_reuse = self._should_skip_session_reuse()
  91. logger.debug(
  92. f"FTP connecting to {self.ip_address}:{self.FTP_PORT} "
  93. f"(timeout={self.timeout}s, model={self.printer_model}, skip_session_reuse={skip_reuse})"
  94. )
  95. self._ftp = ImplicitFTP_TLS(skip_session_reuse=skip_reuse)
  96. self._ftp.connect(self.ip_address, self.FTP_PORT, timeout=self.timeout)
  97. logger.debug("FTP connected, logging in as bblp")
  98. self._ftp.login("bblp", self.access_code)
  99. logger.debug("FTP logged in, setting prot_p and passive mode")
  100. self._ftp.prot_p()
  101. self._ftp.set_pasv(True)
  102. # Log welcome message for debugging
  103. if hasattr(self._ftp, "welcome") and self._ftp.welcome:
  104. logger.debug(f"FTP server welcome: {self._ftp.welcome}")
  105. logger.info(f"FTP connected successfully to {self.ip_address} (model={self.printer_model})")
  106. return True
  107. except ftplib.error_perm as e:
  108. logger.warning(f"FTP connection permission error to {self.ip_address}: {e}")
  109. self._ftp = None
  110. return False
  111. except TimeoutError as e:
  112. logger.warning(f"FTP connection timed out to {self.ip_address}: {e}")
  113. self._ftp = None
  114. return False
  115. except ssl.SSLError as e:
  116. logger.warning(f"FTP SSL error connecting to {self.ip_address}: {e}")
  117. self._ftp = None
  118. return False
  119. except Exception as e:
  120. logger.warning(f"FTP connection failed to {self.ip_address}: {e} (type: {type(e).__name__})")
  121. self._ftp = None
  122. return False
  123. def disconnect(self):
  124. """Disconnect from the FTP server."""
  125. if self._ftp:
  126. try:
  127. self._ftp.quit()
  128. except Exception:
  129. pass
  130. self._ftp = None
  131. def list_files(self, path: str = "/") -> list[dict]:
  132. """List files in a directory."""
  133. if not self._ftp:
  134. return []
  135. files = []
  136. try:
  137. self._ftp.cwd(path)
  138. items = []
  139. self._ftp.retrlines("LIST", items.append)
  140. for item in items:
  141. parts = item.split()
  142. if len(parts) >= 9:
  143. name = " ".join(parts[8:])
  144. is_dir = item.startswith("d")
  145. size = int(parts[4]) if not is_dir else 0
  146. # Parse modification time from FTP listing
  147. # Format: "Nov 30 10:15" or "Nov 30 2024"
  148. mtime = None
  149. try:
  150. from datetime import datetime
  151. month = parts[5]
  152. day = parts[6]
  153. time_or_year = parts[7]
  154. # Determine if it's time (HH:MM) or year
  155. if ":" in time_or_year:
  156. # Recent file: "Nov 30 10:15" - assume current year
  157. year = datetime.now().year
  158. time_str = f"{month} {day} {year} {time_or_year}"
  159. mtime = datetime.strptime(time_str, "%b %d %Y %H:%M")
  160. # If parsed date is in the future, use last year
  161. if mtime > datetime.now():
  162. mtime = mtime.replace(year=year - 1)
  163. else:
  164. # Older file: "Nov 30 2024" - no time, just date
  165. time_str = f"{month} {day} {time_or_year}"
  166. mtime = datetime.strptime(time_str, "%b %d %Y")
  167. except (ValueError, IndexError):
  168. pass
  169. file_entry = {
  170. "name": name,
  171. "is_directory": is_dir,
  172. "size": size,
  173. "path": f"{path.rstrip('/')}/{name}",
  174. }
  175. if mtime:
  176. file_entry["mtime"] = mtime
  177. files.append(file_entry)
  178. logger.debug(f"Listed {len(files)} files in {path}")
  179. except Exception as e:
  180. logger.info(f"FTP list_files failed for {path}: {e}")
  181. return files
  182. def download_file(self, remote_path: str) -> bytes | None:
  183. """Download a file from the printer."""
  184. if not self._ftp:
  185. return None
  186. try:
  187. buffer = BytesIO()
  188. self._ftp.retrbinary(f"RETR {remote_path}", buffer.write)
  189. return buffer.getvalue()
  190. except Exception:
  191. return None
  192. def download_to_file(self, remote_path: str, local_path: Path) -> bool:
  193. """Download a file from the printer to local filesystem."""
  194. if not self._ftp:
  195. logger.warning("download_to_file called but FTP not connected")
  196. return False
  197. try:
  198. local_path.parent.mkdir(parents=True, exist_ok=True)
  199. with open(local_path, "wb") as f:
  200. self._ftp.retrbinary(f"RETR {remote_path}", f.write)
  201. f.flush()
  202. os.fsync(f.fileno())
  203. file_size = local_path.stat().st_size if local_path.exists() else 0
  204. logger.info(f"Successfully downloaded {remote_path} to {local_path} ({file_size} bytes)")
  205. return True
  206. except Exception as e:
  207. # Log at INFO level so we can see failures in normal logs
  208. logger.info(f"FTP download failed for {remote_path}: {e}")
  209. # Clean up partial file if it exists
  210. if local_path.exists():
  211. try:
  212. local_path.unlink()
  213. except Exception:
  214. pass
  215. return False
  216. def diagnose_storage(self) -> dict:
  217. """Run storage diagnostics and return results. For debugging upload issues."""
  218. results = {
  219. "connected": self._ftp is not None,
  220. "can_list_root": False,
  221. "root_files": [],
  222. "can_list_cache": False,
  223. "storage_info": None,
  224. "pwd": None,
  225. "errors": [],
  226. }
  227. if not self._ftp:
  228. results["errors"].append("FTP not connected")
  229. return results
  230. # Try to get current directory
  231. try:
  232. results["pwd"] = self._ftp.pwd()
  233. logger.debug(f"FTP current directory: {results['pwd']}")
  234. except Exception as e:
  235. results["errors"].append(f"PWD failed: {e}")
  236. logger.debug(f"FTP PWD failed: {e}")
  237. # Try to list root directory
  238. try:
  239. self._ftp.cwd("/")
  240. items = []
  241. self._ftp.retrlines("LIST", items.append)
  242. results["can_list_root"] = True
  243. results["root_files"] = items[:10] # First 10 entries
  244. logger.debug(f"FTP root listing ({len(items)} items): {items[:5]}")
  245. except Exception as e:
  246. results["errors"].append(f"LIST / failed: {e}")
  247. logger.debug(f"FTP LIST / failed: {e}")
  248. # Try to list /cache (should exist on all printers)
  249. try:
  250. self._ftp.cwd("/cache")
  251. items = []
  252. self._ftp.retrlines("LIST", items.append)
  253. results["can_list_cache"] = True
  254. logger.debug(f"FTP /cache listing: {len(items)} items")
  255. except Exception as e:
  256. results["errors"].append(f"LIST /cache failed: {e}")
  257. logger.debug(f"FTP LIST /cache failed: {e}")
  258. # Try to get storage info
  259. try:
  260. results["storage_info"] = self.get_storage_info()
  261. logger.debug(f"FTP storage info: {results['storage_info']}")
  262. except Exception as e:
  263. results["errors"].append(f"Storage info failed: {e}")
  264. return results
  265. def upload_file(
  266. self,
  267. local_path: Path,
  268. remote_path: str,
  269. progress_callback: Callable[[int, int], None] | None = None,
  270. ) -> bool:
  271. """Upload a file to the printer with optional progress callback."""
  272. if not self._ftp:
  273. logger.warning("upload_file: FTP not connected")
  274. return False
  275. try:
  276. file_size = local_path.stat().st_size if local_path.exists() else 0
  277. logger.info(f"FTP uploading {local_path} ({file_size} bytes) to {remote_path}")
  278. # Run storage diagnostics before upload (debug)
  279. logger.debug("Running pre-upload storage diagnostics...")
  280. diag = self.diagnose_storage()
  281. logger.info(
  282. f"FTP storage diagnostics: can_list_root={diag['can_list_root']}, "
  283. f"can_list_cache={diag['can_list_cache']}, "
  284. f"storage={diag['storage_info']}, errors={diag['errors']}"
  285. )
  286. if diag["root_files"]:
  287. logger.debug(f"FTP root directory contents: {diag['root_files']}")
  288. uploaded = 0
  289. def on_block(block: bytes):
  290. nonlocal uploaded
  291. uploaded += len(block)
  292. if progress_callback:
  293. progress_callback(uploaded, file_size)
  294. with open(local_path, "rb") as f:
  295. if self._should_skip_session_reuse():
  296. ftplib._SSLSocket = None
  297. logger.debug(f"FTP STOR command starting for {remote_path}")
  298. self._ftp.storbinary(f"STOR {remote_path}", f, callback=on_block)
  299. logger.info(f"FTP upload complete: {remote_path}")
  300. return True
  301. except ftplib.error_perm as e:
  302. # Permanent FTP error (4xx/5xx response)
  303. error_code = str(e)[:3] if str(e) else "unknown"
  304. logger.error(f"FTP upload failed for {remote_path}: {e} (error code: {error_code})")
  305. if error_code == "553":
  306. logger.error(
  307. "FTP 553 error - Could not create file. Possible causes: "
  308. "1) No SD card inserted, 2) SD card full, 3) SD card not formatted correctly (needs FAT32/exFAT), "
  309. "4) Printer busy/not ready, 5) File path issue"
  310. )
  311. elif error_code == "550":
  312. logger.error("FTP 550 error - File/directory not found or permission denied")
  313. elif error_code == "552":
  314. logger.error("FTP 552 error - Storage quota exceeded (SD card full?)")
  315. return False
  316. except Exception as e:
  317. logger.error(f"FTP upload failed for {remote_path}: {e} (type: {type(e).__name__})")
  318. return False
  319. def upload_bytes(self, data: bytes, remote_path: str) -> bool:
  320. """Upload bytes to the printer."""
  321. if not self._ftp:
  322. return False
  323. try:
  324. buffer = BytesIO(data)
  325. self._ftp.storbinary(f"STOR {remote_path}", buffer)
  326. return True
  327. except Exception:
  328. return False
  329. def delete_file(self, remote_path: str) -> bool:
  330. """Delete a file from the printer."""
  331. if not self._ftp:
  332. return False
  333. try:
  334. self._ftp.delete(remote_path)
  335. return True
  336. except Exception as e:
  337. logger.warning(f"Failed to delete {remote_path}: {e}")
  338. return False
  339. def get_file_size(self, remote_path: str) -> int | None:
  340. """Get the size of a file."""
  341. if not self._ftp:
  342. return None
  343. try:
  344. return self._ftp.size(remote_path)
  345. except Exception:
  346. return None
  347. def get_storage_info(self) -> dict | None:
  348. """Get storage information from the printer."""
  349. if not self._ftp:
  350. return None
  351. result = {}
  352. # Try AVBL command (available space) - some FTP servers support this
  353. try:
  354. response = self._ftp.sendcmd("AVBL")
  355. logger.debug(f"AVBL response: {response}")
  356. # Response format: "213 <bytes available>"
  357. if response.startswith("213"):
  358. parts = response.split()
  359. if len(parts) >= 2:
  360. result["free_bytes"] = int(parts[1])
  361. except Exception as e:
  362. logger.debug(f"AVBL command not supported: {e}")
  363. # Try STAT command as fallback
  364. try:
  365. response = self._ftp.sendcmd("STAT")
  366. logger.debug(f"STAT response: {response}")
  367. except Exception:
  368. pass
  369. # Calculate used space by listing root directories
  370. try:
  371. total_used = 0
  372. dirs_to_scan = ["/cache", "/timelapse", "/model", "/data", "/data/Metadata", "/"]
  373. for dir_path in dirs_to_scan:
  374. try:
  375. self._ftp.cwd(dir_path)
  376. items = []
  377. self._ftp.retrlines("LIST", items.append)
  378. for item in items:
  379. parts = item.split()
  380. if len(parts) >= 5 and not item.startswith("d"):
  381. try:
  382. total_used += int(parts[4])
  383. except ValueError:
  384. pass
  385. except Exception:
  386. pass
  387. result["used_bytes"] = total_used
  388. except Exception:
  389. pass
  390. return result if result else None
  391. async def download_file_async(
  392. ip_address: str,
  393. access_code: str,
  394. remote_path: str,
  395. local_path: Path,
  396. timeout: float = 60.0,
  397. socket_timeout: float | None = None,
  398. printer_model: str | None = None,
  399. ) -> bool:
  400. """Async wrapper for downloading a file with timeout.
  401. Args:
  402. ip_address: Printer IP address
  403. access_code: Printer access code
  404. remote_path: Remote file path on printer
  405. local_path: Local path to save file
  406. timeout: Overall operation timeout (asyncio)
  407. socket_timeout: FTP socket timeout for slow connections (e.g., A1 printers)
  408. printer_model: Printer model for A1-specific workarounds
  409. """
  410. loop = asyncio.get_event_loop()
  411. def _download():
  412. client = BambuFTPClient(ip_address, access_code, timeout=socket_timeout, printer_model=printer_model)
  413. if client.connect():
  414. try:
  415. return client.download_to_file(remote_path, local_path)
  416. finally:
  417. client.disconnect()
  418. return False
  419. try:
  420. return await asyncio.wait_for(loop.run_in_executor(None, _download), timeout=timeout)
  421. except TimeoutError:
  422. logger.warning(f"FTP download timed out after {timeout}s for {remote_path}")
  423. return False
  424. async def download_file_try_paths_async(
  425. ip_address: str,
  426. access_code: str,
  427. remote_paths: list[str],
  428. local_path: Path,
  429. socket_timeout: float | None = None,
  430. printer_model: str | None = None,
  431. ) -> bool:
  432. """Try downloading a file from multiple paths using a single connection.
  433. Args:
  434. socket_timeout: FTP socket timeout for slow connections (e.g., A1 printers)
  435. printer_model: Printer model for A1-specific workarounds
  436. """
  437. loop = asyncio.get_event_loop()
  438. def _download():
  439. client = BambuFTPClient(ip_address, access_code, timeout=socket_timeout, printer_model=printer_model)
  440. if not client.connect():
  441. return False
  442. try:
  443. return any(client.download_to_file(remote_path, local_path) for remote_path in remote_paths)
  444. finally:
  445. client.disconnect()
  446. return await loop.run_in_executor(None, _download)
  447. async def upload_file_async(
  448. ip_address: str,
  449. access_code: str,
  450. local_path: Path,
  451. remote_path: str,
  452. timeout: float = 600.0,
  453. progress_callback: Callable[[int, int], None] | None = None,
  454. socket_timeout: float | None = None,
  455. printer_model: str | None = None,
  456. ) -> bool:
  457. """Async wrapper for uploading a file with timeout and progress callback.
  458. Args:
  459. ip_address: Printer IP address
  460. access_code: Printer access code
  461. local_path: Local file path to upload
  462. remote_path: Remote path on printer
  463. timeout: Overall operation timeout (asyncio)
  464. progress_callback: Optional callback for progress updates
  465. socket_timeout: FTP socket timeout for slow connections (e.g., A1 printers)
  466. printer_model: Printer model for A1-specific workarounds
  467. """
  468. loop = asyncio.get_event_loop()
  469. def _upload():
  470. logger.info(
  471. f"FTP connecting to {ip_address} for upload (model={printer_model}, socket_timeout={socket_timeout}s)..."
  472. )
  473. client = BambuFTPClient(ip_address, access_code, timeout=socket_timeout, printer_model=printer_model)
  474. if client.connect():
  475. logger.info(f"FTP connected to {ip_address}")
  476. try:
  477. return client.upload_file(local_path, remote_path, progress_callback)
  478. finally:
  479. client.disconnect()
  480. logger.warning(f"FTP connection failed to {ip_address}")
  481. return False
  482. try:
  483. return await asyncio.wait_for(loop.run_in_executor(None, _upload), timeout=timeout)
  484. except TimeoutError:
  485. logger.warning(f"FTP upload timed out after {timeout}s for {remote_path}")
  486. return False
  487. async def list_files_async(
  488. ip_address: str,
  489. access_code: str,
  490. path: str = "/",
  491. timeout: float = 30.0,
  492. socket_timeout: float | None = None,
  493. printer_model: str | None = None,
  494. ) -> list[dict]:
  495. """Async wrapper for listing files with timeout.
  496. Args:
  497. socket_timeout: FTP socket timeout for slow connections (e.g., A1 printers)
  498. printer_model: Printer model for A1-specific workarounds
  499. """
  500. loop = asyncio.get_event_loop()
  501. def _list():
  502. client = BambuFTPClient(ip_address, access_code, timeout=socket_timeout, printer_model=printer_model)
  503. if client.connect():
  504. try:
  505. return client.list_files(path)
  506. finally:
  507. client.disconnect()
  508. return []
  509. try:
  510. return await asyncio.wait_for(loop.run_in_executor(None, _list), timeout=timeout)
  511. except TimeoutError:
  512. logger.warning(f"FTP list_files timed out after {timeout}s for {path}")
  513. return []
  514. async def delete_file_async(
  515. ip_address: str,
  516. access_code: str,
  517. remote_path: str,
  518. socket_timeout: float | None = None,
  519. printer_model: str | None = None,
  520. ) -> bool:
  521. """Async wrapper for deleting a file.
  522. Args:
  523. socket_timeout: FTP socket timeout for slow connections (e.g., A1 printers)
  524. printer_model: Printer model for A1-specific workarounds
  525. """
  526. loop = asyncio.get_event_loop()
  527. def _delete():
  528. client = BambuFTPClient(ip_address, access_code, timeout=socket_timeout, printer_model=printer_model)
  529. if client.connect():
  530. try:
  531. return client.delete_file(remote_path)
  532. finally:
  533. client.disconnect()
  534. return False
  535. return await loop.run_in_executor(None, _delete)
  536. async def download_file_bytes_async(
  537. ip_address: str,
  538. access_code: str,
  539. remote_path: str,
  540. socket_timeout: float | None = None,
  541. printer_model: str | None = None,
  542. ) -> bytes | None:
  543. """Async wrapper for downloading file as bytes.
  544. Args:
  545. socket_timeout: FTP socket timeout for slow connections (e.g., A1 printers)
  546. printer_model: Printer model for A1-specific workarounds
  547. """
  548. loop = asyncio.get_event_loop()
  549. def _download():
  550. client = BambuFTPClient(ip_address, access_code, timeout=socket_timeout, printer_model=printer_model)
  551. if client.connect():
  552. try:
  553. return client.download_file(remote_path)
  554. finally:
  555. client.disconnect()
  556. return None
  557. return await loop.run_in_executor(None, _download)
  558. async def get_storage_info_async(
  559. ip_address: str,
  560. access_code: str,
  561. socket_timeout: float | None = None,
  562. printer_model: str | None = None,
  563. ) -> dict | None:
  564. """Async wrapper for getting storage info.
  565. Args:
  566. socket_timeout: FTP socket timeout for slow connections (e.g., A1 printers)
  567. printer_model: Printer model for A1-specific workarounds
  568. """
  569. loop = asyncio.get_event_loop()
  570. def _get_storage():
  571. client = BambuFTPClient(ip_address, access_code, timeout=socket_timeout, printer_model=printer_model)
  572. if client.connect():
  573. try:
  574. return client.get_storage_info()
  575. finally:
  576. client.disconnect()
  577. return None
  578. return await loop.run_in_executor(None, _get_storage)
  579. async def get_ftp_retry_settings() -> tuple[bool, int, float, float]:
  580. """Get FTP retry settings from database.
  581. Returns:
  582. Tuple of (retry_enabled, retry_count, retry_delay, timeout)
  583. """
  584. from backend.app.api.routes.settings import get_setting
  585. from backend.app.core.database import async_session
  586. async with async_session() as db:
  587. enabled = (await get_setting(db, "ftp_retry_enabled") or "true") == "true"
  588. count = int(await get_setting(db, "ftp_retry_count") or "3")
  589. delay = float(await get_setting(db, "ftp_retry_delay") or "2")
  590. timeout = float(await get_setting(db, "ftp_timeout") or "30")
  591. return enabled, count, delay, timeout
  592. async def with_ftp_retry(
  593. operation: Callable[..., Awaitable[T]],
  594. *args,
  595. max_retries: int = 3,
  596. retry_delay: float = 2.0,
  597. operation_name: str = "FTP operation",
  598. **kwargs,
  599. ) -> T | None:
  600. """Execute FTP operation with retry logic.
  601. Args:
  602. operation: Async function to execute
  603. *args: Positional arguments for the operation
  604. max_retries: Number of retry attempts (default: 3)
  605. retry_delay: Seconds to wait between retries (default: 2.0)
  606. operation_name: Name for logging purposes
  607. **kwargs: Keyword arguments for the operation
  608. Returns:
  609. Result of the operation, or None if all attempts fail
  610. """
  611. last_error = None
  612. for attempt in range(max_retries + 1):
  613. try:
  614. result = await operation(*args, **kwargs)
  615. # Check for "falsy" success indicators
  616. if result not in (False, None, []):
  617. if attempt > 0:
  618. logger.info(f"{operation_name} succeeded on attempt {attempt + 1}/{max_retries + 1}")
  619. return result
  620. # Operation returned failure indicator
  621. if attempt > 0:
  622. logger.info(f"{operation_name} attempt {attempt + 1}/{max_retries + 1} returned failure")
  623. except Exception as e:
  624. last_error = e
  625. logger.warning(f"{operation_name} attempt {attempt + 1}/{max_retries + 1} failed: {e}")
  626. # Don't wait after the last attempt
  627. if attempt < max_retries:
  628. logger.info(f"{operation_name} will retry in {retry_delay}s...")
  629. await asyncio.sleep(retry_delay)
  630. logger.error(f"{operation_name} failed after {max_retries + 1} attempts")
  631. if last_error:
  632. logger.debug(f"Last error: {last_error}")
  633. return None