bambu_ftp.py 32 KB

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