bambu_ftp.py 27 KB

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