Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions bambulabs_api/ams.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
__all__ = ["AMS", "AMSHub"]

from typing import Any
from typing import Any, Iterator
from bambulabs_api.filament_info import FilamentTray


class AMSHub:
"""
Represents the Bambulabs AMS (Automated Material System) system hub.

Returns:
AMSHub: AMSHub object containing all AMS objects.
"""
def __init__(self) -> None:
self.ams_hub: dict[int, AMS] = {}

Expand All @@ -21,18 +27,20 @@ def __getitem__(self, ind: int) -> "AMS":
def __setitem__(self, ind: int, item: "AMS"):
self.ams_hub[ind] = item

def __iter__(self) -> Iterator["AMS"]:
return iter(self.ams_hub.values())


class AMS:
"""
Represents the Bambulab's AMS (Automated Material System) system.
"""

def __init__(
self, humidity: int, temperature: float, **kwargs: dict[str, Any]
) -> None:
def __init__(self, humidity: int, humidity_pct: int, temperature: float, **kwargs: dict[str, Any]) -> None:
self.filament_trays: dict[int, FilamentTray] = {}

self.humidity = humidity
self.humidity_pct = humidity_pct
self.temperature = temperature

if "tray" in kwargs:
Expand All @@ -41,7 +49,7 @@ def __init__(
def process_trays(self, trays: list[dict[str, Any]]):
for t in trays:
id = t.get("id")
tray_n: Any | None = t.get("n", None)
tray_n: Any | None = t.get("tray_info_idx", None)
if id and tray_n is not None:
id = int(id)
self.filament_trays[id] = FilamentTray.from_dict(t)
Expand Down Expand Up @@ -79,3 +87,7 @@ def __getitem__(self, index: int):

def __setitem__(self, index: int, filament_tray: FilamentTray):
self.filament_trays[index] = filament_tray

def __iter__(self) -> Iterator[FilamentTray]:
"""Iterate over the FilamentTray objects inside this AMS."""
return iter(self.filament_trays.values())
21 changes: 16 additions & 5 deletions bambulabs_api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ def mqtt_client_connected(self):
"""
return self.mqtt_client.is_connected()

def mqtt_client_ready(self):
"""
Get the mqtt client is ready to send commands.

Returns
-------
bool
Check if the mqtt client is ready.
"""
return self.mqtt_client.ready()

def current_layer_num(self):
"""
Get current layer number
Expand Down Expand Up @@ -348,17 +359,17 @@ def start_print(self, filename: str,

Parameters
----------
filename : str
filename: str
The name of the file to be printed.
plate_number : (int | str)
plate_number: (int | str)
The plate number of the file to be printed (assuming the 3mf file
is created with Bambustudio/Orcaslicer). Or the path as a string.
use_ams : bool, optional
use_ams: bool, optional
Whether to use the AMS system, by default True.
ams_mapping : list[int], optional
ams_mapping: list[int], optional
The mapping of the filament trays to the plate numbers,
by default [0].
skip_objects (list[int] | None, optional): List of gcode objects to
skip_objects: (list[int] | None, optional) List of gcode objects to
skip. Defaults to None.

Returns
Expand Down
75 changes: 67 additions & 8 deletions bambulabs_api/filament_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,36 @@ class Filament(AMSFilamentSettings, Enum):
SUPPORT_G: The Support G filament settings.
SUPPORT_W: The Support W filament settings.
BAMBU_TPU_95A: The Bambu TPU 95A filament settings.
BAMBU_ASA_AERO: The Bambu ASA AERO filament settings.
BAMBU_PLA_METAL: The Bambu PLA Metal filament settings.
BAMBU_PETG_TRANSLUCENT: The Bambu PETG Translucent filament settings.
BAMBU_PLA_MARBLE: The Bambu PLA Marble filament settings.
BAMBU_PLA_WOOD: The Bambu PLA Wood filament settings.
BAMBU_PLA_SILK_PLUS: The Bambu PLA Silk Plus filament settings.
BAMBU_PETG_HF: The Bambu PETG HF filament settings.
BAMBU_TPU_FOR_AMS: The Bambu TPU for AMS filament settings.
BAMBU_SUPPORT_FOR_ABS: The Bambu Support for ABS filament settings.
BAMBU_PC_FR: The Bambu PC FR filament settings.
BAMBU_PLA_GALAXY: The Bambu PLA Galaxy filament settings.
BAMBU_PA6_GF: The Bambu PA6 GF filament settings.
BAMBU_PLA_AERO: The Bambu PLA AERO filament settings.
BAMBU_ASA_CF: The Bambu ASA CF filament settings.
BAMBU_PETG_CF: The Bambu PETG CF filament settings.
BAMBU_SUPPORT_FOR_PA_PET: The Bambu Support for PA PET filament settings.
BAMBU_PLA_SPARKLE: The Bambu PLA Sparkle filament settings.
BAMBU_ABS_GF: The Bambu ABS GF filament settings.
BAMBU_PAHT_CF: The Bambu PAHT CF filament settings.
BAMBU_PLA_BASIC: The Bambu PLA Basic filament settings.
BAMBU_PLA_MATTE: The Bambu PLA Matte filament settings.
BAMBU_PA6_CF: The Bambu PA6 CF filament settings.
BAMBU_PLA_SILK: The Bambu PLA Silk filament settings.
BAMBU_PVA: The Bambu PVA filament settings.
BAMBU_PLA_CF: The Bambu PLA CF filament settings.
BAMBU_SUPPORT_FOR_PLA_PETG: The Bambu Support for PLA PETG filament settings.
BAMBU_TPU_95A_HF: The Bambu TPU 95A HF filament settings.
BAMBU_PPA_CF: The Bambu PPA CF filament settings.
BAMBU_ASA: The Bambu ASA filament settings.
BAMBU_PLA_GLOW: The Bambu PLA Glow filament settings.
ABS: The ABS filament settings.
ASA: The ASA filament settings.
PA: The PA filament settings.
Expand All @@ -68,6 +98,37 @@ class Filament(AMSFilamentSettings, Enum):
SUPPORT_W = "GFS00", 190, 250, "PLA-S"
BAMBU_TPU_95A = "GFU01", 200, 250, "TPU"

BAMBU_ASA_AERO = "GFB02", 240, 280, "ASA"
BAMBU_PLA_METAL = "GFA02", 190, 230, "PLA"
BAMBU_PETG_TRANSLUCENT = "GFG01", 230, 260, "PETG"
BAMBU_PLA_MARBLE = "GFA07", 190, 230, "PLA"
BAMBU_PLA_WOOD = "GFA16", 190, 240, "PLA"
BAMBU_PLA_SILK_PLUS = "GFA06", 210, 240, "PLA"
BAMBU_PETG_HF = "GFG02", 230, 260, "PETG"
BAMBU_TPU_FOR_AMS = "GFU02", 230, 230, "TPU"
BAMBU_SUPPORT_FOR_ABS = "GFS06", 190, 220, "Support"
BAMBU_PC_FR = "GFC01", 260, 280, "PC"
BAMBU_PLA_GALAXY = "GFA15", 190, 230, "PLA"
BAMBU_PA6_GF = "GFN08", 260, 290, "PA6"
BAMBU_PLA_AERO = "GFA11", 220, 260, "PLA"
BAMBU_ASA_CF = "GFB51", 250, 280, "ASA"
BAMBU_PETG_CF = "GFG50", 240, 270, "PETG"
BAMBU_SUPPORT_FOR_PA_PET = "GFS03", 280, 300, "Support"
BAMBU_PLA_SPARKLE = "GFA08", 190, 230, "PLA"
BAMBU_ABS_GF = "GFB50", 240, 270, "ABS"
BAMBU_PAHT_CF = "GFN04", 260, 290, "PAHT"
BAMBU_PLA_BASIC = "GFA00", 190, 230, "PLA"
BAMBU_PLA_MATTE = "GFA01", 190, 230, "PLA"
BAMBU_PA6_CF = "GFN05", 260, 290, "PA6"
BAMBU_PLA_SILK = "GFA05", 210, 230, "PLA"
BAMBU_PVA = "GFS04", 220, 250, "PVA"
BAMBU_PLA_CF = "GFA50", 210, 240, "PLA"
BAMBU_SUPPORT_FOR_PLA_PETG = "GFS05", 190, 220, "Support"
BAMBU_TPU_95A_HF = "GFU00", 230, 230, "TPU"
BAMBU_PPA_CF = "GFN06", 280, 310, "PPA"
BAMBU_ASA = "GFB01", 240, 270, "ASA"
BAMBU_PLA_GLOW = "GFA12", 190, 230, "PLA"

ABS = "GFB99", 240, 270, "ABS"
ASA = "GFB98", 240, 270, "ASA"
PA = "GFN99", 270, 300, "PA"
Expand Down Expand Up @@ -97,8 +158,7 @@ class FilamentTray:
Attributes
----------

k: The k value.
n: The n value.
id: The ID of the tray.
tag_uid: The tag UID.
tray_id_name: The tray ID name.
tray_info_idx: The tray info index.
Expand All @@ -107,17 +167,16 @@ class FilamentTray:
tray_color: The filament color of the tray.
tray_weight: The tray weight.
tray_diameter: The tray diameter.
tray_temp: The tray temperature.
tray_time: The tray time.
drying_temp: The tray temperature.
drying_time: The tray time.
bed_temp_type: The bed temperature type.
bed_temp: The bed temperature.
nozzle_temp_max: The maximum nozzle temperature for the filament.
nozzle_temp_min: The minimum nozzle temperature for the filament.
xcam_info: The XCam information.
tray_uuid: The tray UUID.
"""
k: float
n: int
id: int
tag_uid: str
tray_id_name: str
tray_info_idx: str
Expand All @@ -126,8 +185,8 @@ class FilamentTray:
tray_color: str
tray_weight: str
tray_diameter: str
tray_temp: str
tray_time: str
drying_temp: str
drying_time: str
bed_temp_type: str
bed_temp: str
nozzle_temp_max: int
Expand Down
46 changes: 33 additions & 13 deletions bambulabs_api/mqtt_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,14 +817,14 @@ def get_access_code(self) -> str:

def set_auto_step_recovery(self, auto_step_recovery: bool = True) -> bool:
"""
Set whether or not to set auto step recovery
Set whether to set auto step recovery or not.

Args:
auto_step_recovery (bool): flag to set auto step recovery.
Default True.

Returns:
bool: success of the auto step recovery command command
bool: success of the auto step recovery command
"""
return self.__publish_command({"print": {
"command": "gcode_line", "auto_recovery": auto_step_recovery
Expand Down Expand Up @@ -1036,7 +1036,24 @@ def get_chamber_temperature(self) -> float:
Returns:
float: chamber temperature
"""
return float(self.__get_print("chamber_temper", 0.0))
primary = self.__get_print("chamber_temper", None)
try:
return float(primary)
except (TypeError, ValueError):
pass # fall back

device = self.__get_print("device", {})
if isinstance(device, dict):
ctc = device.get("ctc", {})
if isinstance(ctc, dict):
info = ctc.get("info", {})
if isinstance(info, dict):
try:
return float(info.get("temp", 0.0))
except (TypeError, ValueError):
pass

return 0.0

def current_layer_num(self) -> int:
"""
Expand Down Expand Up @@ -1209,24 +1226,27 @@ def process_ams(self):

for k, v in enumerate(ams_units):
humidity = int(v.get("humidity", 0))
humidity_pct = int(v.get("humidity_raw", 0))
temp = float(v.get("temp", 0.0))
id = int(v.get("id", k))

ams = AMS(humidity=humidity, temperature=temp)
ams = AMS(humidity=humidity, humidity_pct=humidity_pct, temperature=temp)

trays: list[dict[str, Any]] = v.get("tray", [])

if trays:
for tray_id, tray in enumerate(trays):
tray_id = int(tray.get("id", tray_id))
tray_n: Any | None = tray.get("n", None)
if tray_n:
ams.set_filament_tray(
tray_index=tray_id,
filament_tray=FilamentTray.from_dict(tray))
ams.process_trays(trays)

self.ams_hub[id] = ams

def get_ams_hub(self) -> AMSHub:
"""
Get the AMS hub

Returns:
AMSHub: AMS hub
"""
self.process_ams()
return self.ams_hub

def vt_tray(self) -> FilamentTray:
"""
Get Filament Tray of the external spool.
Expand Down