Skip to content

Commit fee5433

Browse files
committed
feat: enhance BambuPrinter with additional 3MF file metadata and plate type handling
1 parent 4d250da commit fee5433

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

src/bpm/bambuprinter.py

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
import logging
55
import math
6+
import re
67
import ssl
78
import threading
89
import time
@@ -78,7 +79,9 @@ def __init__(self, config: BambuConfig | None = None):
7879
* _gcode_state `READ ONLY` State reported for job status (FAILED/RUNNING/PAUSE/IDLE/FINISH).
7980
* _gcode_file `READ ONLY` The name of the current or last printed gcode file.
8081
* _3mf_file `READ ONLY` The name of the 3mf file currently being printed.
82+
* _3mf_file_md5 `READ ONLY` The md5 of the 3mf file currently being printed.
8183
* _plate_num `READ ONLY` The selected plate # for the current 3mf file.
84+
* _plate_type `READ ONLY` The selected plate type for the current 3mf file.
8285
* _subtask_name `READ ONLY` The name of the active subtask.
8386
* _print_type `READ ONLY` Not entirely sure. Reports "idle" when no job is active.
8487
* _percent_complete `READ ONLY` Percentage complete for the current active job.
@@ -152,7 +155,9 @@ class level attributes are marked private.
152155
self._gcode_state = ""
153156
self._gcode_file = ""
154157
self._3mf_file = ""
158+
self._3mf_file_md5 = ""
155159
self._plate_num = 0
160+
self._plate_type = PlateType.NONE
156161
self._subtask_name = ""
157162
self._print_type = ""
158163
self._percent_complete = 0
@@ -456,6 +461,7 @@ def print_3mf_file(
456461
"""
457462
self._3mf_file = f"{name}"
458463
self._plate_num = int(plate)
464+
self._plate_type = bed
459465

460466
file = copy.deepcopy(bambucommands.PRINT_3MF_FILE)
461467

@@ -961,20 +967,32 @@ def _on_message(self, message: str):
961967
elif "print" in message:
962968
status = message["print"]
963969

964-
if "command" in status and status["command"] == "project_file":
970+
if (
971+
status.get("command") == "project_file"
972+
and status.get("result") == "success"
973+
):
965974
self._start_time = 0
966-
if self._3mf_file:
967-
logger.debug("project_file request acknowledged")
968-
else:
969-
url = status.get("url", None)
970-
subtask = status.get("subtask_name", None)
971-
if url and subtask and url.startswith("https://"):
972-
self._3mf_file = f"/cache/{subtask}.3mf"
973-
else:
974-
if "file" in status:
975-
self._3mf_file = status.get("file", "")
976-
else:
977-
logger.warning("unable to determine file being printed")
975+
976+
url = status.get("url", "")
977+
parts = url.split("://", 1)
978+
if len(parts) == 2:
979+
self._3mf_file = parts[1]
980+
981+
self._3mf_file_md5 = status.get("md5", "")
982+
self._subtask_name = status.get("subtask_name", "")
983+
984+
param = status.get("param", None)
985+
if param:
986+
match = re.search(r"plate_(\d{1,2})", param)
987+
if match:
988+
self._plate_num = int(match.group(1))
989+
990+
bed_type = status.get("bed_type", None)
991+
self._plate_type = (
992+
PlateType[bed_type.upper()]
993+
if bed_type and bed_type.upper() in PlateType.__members__
994+
else PlateType.NONE
995+
)
978996

979997
# let's sleep for a couple seconds and do a full refresh
980998
# if ams filament settings have changed
@@ -1025,6 +1043,10 @@ def _on_message(self, message: str):
10251043
self._gcode_state = status["gcode_state"]
10261044
if self._gcode_state in ("FINISH", "FAILED") and self._3mf_file:
10271045
self._3mf_file = ""
1046+
self._3mf_file_md5 = ""
1047+
self._plate_num = None
1048+
self._plate_type = PlateType.NONE
1049+
self._subtask_name = ""
10281050

10291051
if "subtask_name" in status:
10301052
self._subtask_name = status["subtask_name"]
@@ -1453,10 +1475,18 @@ def subtask_name(self):
14531475
def current_3mf_file(self):
14541476
return self._3mf_file
14551477

1478+
@property
1479+
def current_3mf_file_md5(self):
1480+
return self._3mf_file_md5
1481+
14561482
@property
14571483
def current_plate_num(self):
14581484
return self._plate_num
14591485

1486+
@property
1487+
def current_plate_type(self):
1488+
return self._plate_type
1489+
14601490
@property
14611491
def gcode_file(self):
14621492
return self._gcode_file

src/bpm/bambutools.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ class PlateType(Enum):
193193
ENG_PLATE = 2
194194
HOT_PLATE = 3
195195
TEXTURED_PLATE = 4
196+
NONE = 999
196197

197198

198199
class PrintOption(Enum):

0 commit comments

Comments
 (0)