feat(printer): 新增局域网发送3MF至A1打印机功能
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
"""打印机发送模块:通过局域网将 3MF 发送到 Bambu A1 并开始打印。"""
|
||||
|
||||
from __future__ import annotations
|
||||
from pathlib import Path
|
||||
from dataclasses import dataclass
|
||||
|
||||
@dataclass
|
||||
class PrinterConfig:
|
||||
"""打印机连接配置。"""
|
||||
ip: str = "" # 打印机 IP 地址
|
||||
access_code: str = "" # 局域网访问码
|
||||
serial: str = "" # 打印机序列号
|
||||
|
||||
def send_to_printer(file_path: Path, config: PrinterConfig) -> bool:
|
||||
"""将 3MF 文件上传至打印机并开始打印。
|
||||
|
||||
Args:
|
||||
file_path: 要发送的 3MF 文件路径
|
||||
config: 打印机连接配置
|
||||
|
||||
Returns:
|
||||
True 表示成功,False 表示失败
|
||||
|
||||
Raises:
|
||||
ImportError: bambulabs_api 未安装
|
||||
Exception: 连接或上传失败
|
||||
"""
|
||||
# 延迟导入,避免在没有 bambulabs_api 时报错
|
||||
from bambulabs_api import Printer as BambuPrinter
|
||||
printer = BambuPrinter(config.ip, config.access_code, config.serial)
|
||||
try:
|
||||
printer.connect()
|
||||
file_name = file_path.name
|
||||
with open(file_path, "rb") as f:
|
||||
import io
|
||||
result = printer.upload_file(io.BytesIO(f.read()), file_name)
|
||||
if "226" not in result:
|
||||
raise RuntimeError(f"上传文件失败: {result}")
|
||||
printer.start_print(file_name, plate_number=1)
|
||||
return True
|
||||
finally:
|
||||
printer.disconnect()
|
||||
Reference in New Issue
Block a user