Skip to content
Merged
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
124 changes: 116 additions & 8 deletions bambulabs_api/mqtt_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
import ssl
import datetime
from typing import Any, Callable
from typing import Any, Callable, Union
from re import match

import paho.mqtt.client as mqtt
Expand All @@ -13,12 +13,24 @@
from paho.mqtt.enums import CallbackAPIVersion

from bambulabs_api.ams import AMS, AMSHub
from bambulabs_api.printer_info import NozzleType
from bambulabs_api.printer_info import (
NozzleType,
PrinterFirmwareInfo,
PrinterType)

from .filament_info import Filament, FilamentTray
from .states_info import GcodeState, PrintStatus


def set_temperature_support(printer_info: PrinterFirmwareInfo):
printer_type = printer_info.printer_type
if printer_type in (PrinterType.P1P, PrinterType.P1S,
PrinterType.X1E, PrinterType.X1C, ):
return printer_info.firmware_version < "01.06"
elif printer_type in (PrinterType.A1, PrinterType.A1_MINI):
return printer_info.firmware_version <= "01.04"


def is_valid_gcode(line: str):
"""
Check if a line is a valid G-code command
Expand Down Expand Up @@ -83,6 +95,30 @@ def __init__(

self._client.on_connect = self._on_connect
self._client.on_message = self._on_message
self._client.on_disconnect = self._on_disconnect

self.on_connect_handler: Callable[[
'PrinterMQTTClient',
mqtt.Client,
Any,
mqtt.ConnectFlags,
paho.mqtt.reasoncodes.ReasonCode,
paho.mqtt.properties.Properties | None
], None] = lambda a, b, c, d, e, f: None
self.on_message_handler: Callable[[
'PrinterMQTTClient',
mqtt.Client,
Any,
mqtt.MQTTMessage
], None] = lambda a, b, c, d: None
self.on_disconnect_handler: Callable[[
'PrinterMQTTClient',
mqtt.Client,
Any,
mqtt.DisconnectFlags,
paho.mqtt.reasoncodes.ReasonCode,
Union[paho.mqtt.properties.Properties, None],
], None] = lambda a, b, c, d, e, f: None

self.pushall_timeout: int = pushall_timeout
self.pushall_aggressive = pushall_on_connect
Expand All @@ -95,6 +131,11 @@ def __init__(
self.ams_hub: AMSHub = AMSHub()
self.strict = strict

self.printer_info: PrinterFirmwareInfo = PrinterFirmwareInfo(
PrinterType.P1S,
"01.04.00.00"
)

def is_connected(self):
"""
Check if the mqtt client is connected
Expand Down Expand Up @@ -123,6 +164,24 @@ def wrapper(
def ready(self) -> bool:
return bool(self._data)

def _on_disconnect(
self,
client: mqtt.Client,
userdata: Any,
disconnect_flags: mqtt.DisconnectFlags,
reason_code: paho.mqtt.reasoncodes.ReasonCode,
properties: Union[paho.mqtt.properties.Properties, None],
) -> None:
logging.info(f"Client Disconnected: {client} {userdata} "
f"{disconnect_flags} {reason_code} {properties}")
self.on_disconnect_handler(
self,
client,
userdata,
disconnect_flags,
reason_code,
properties)

def _on_message(
self,
client: mqtt.Client,
Expand All @@ -133,6 +192,8 @@ def _on_message(
doc = json.loads(msg.payload)
self.manual_update(doc)

self.on_message_handler(self, client, userdata, msg)

def manual_update(self, doc: dict[str, Any]) -> None:
if "print" in doc:
self._data |= doc["print"]
Expand Down Expand Up @@ -173,6 +234,15 @@ def _on_connect(
else:
logging.warning(f"Connection failed with result code {rc}")

self.on_connect_handler(
self,
client,
userdata,
flags,
rc,
properties
)

def connect(self) -> None:
"""
Connects to the MQTT server asynchronously
Expand Down Expand Up @@ -485,17 +555,35 @@ def send_gcode(
raise ValueError("Invalid G-code command")
return self.__send_gcode_line("\n".join(gcode_command))

def set_bed_temperature(self, temperature: int) -> bool:
def set_bed_temperature(
self,
temperature: int,
override: bool = False
) -> bool:
"""
Set the bed temperature
Set the bed temperature. Note P1 firmware version above 01.06 does not
support M140. M190 is used instead (set and wait for temperature).
To prevent long wait times, if temperature is set to below 40 deg cel,
no temperature is set, override flag is provided to circumvent this.

Args:
temperature (int): The temperature to set the bed to
override (bool): Whether to override guards. Default to False

Returns:
bool: success of setting the bed temperature
"""
return self.__send_gcode_line(f"M140 S{temperature}\n")
if set_temperature_support(self.printer_info):
return self.__send_gcode_line(f"M140 S{temperature}\n")
else:
if temperature < 40 and not override:
logging.warning(
"Attempting to set low bed temperature not recommended. "
"Set override flag to true to if you're sure you want to "
f"run M190 S{temperature};"
)
return False
return self.__send_gcode_line(f"M190 S{temperature}\n")

def get_fan_gear(self):
"""
Expand Down Expand Up @@ -644,17 +732,37 @@ def set_print_speed_lvl(self, speed_lvl: int = 1) -> bool:
{"print": {"command": "print_speed", "param": f"{speed_lvl}"}}
)

def set_nozzle_temperature(self, temperature: int) -> bool:
def set_nozzle_temperature(
self,
temperature: int,
override: bool = False
) -> bool:
"""
Set the nozzle temperature
Set the nozzle temperature. Note P1 firmware version above 01.06 does
not support M104. M109 is used instead (set and wait for temperature).
To prevent long wait times, if temperature is set to below 40 deg cel,
no temperature is set, override flag is provided to circumvent this.

Args:
temperature (int): The temperature to set the bed to
override (bool): Whether to override guards. Default to False
Args:
temperature (int): temperature to set the nozzle to

Returns:
bool: success of setting the nozzle temperature
"""
return self.__send_gcode_line(f"M104 S{temperature}\n")
if set_temperature_support(self.printer_info):
return self.__send_gcode_line(f"M104 S{temperature}\n")
else:
if temperature < 60 and not override:
logging.warning(
"Attempting to set low bed temperature not recommended. "
"Set override flag to true to if you're sure you want to "
f"run M109 S{temperature};"
)
return False
return self.__send_gcode_line(f"M109 S{temperature}\n")

def set_printer_filament(
self,
Expand Down
45 changes: 45 additions & 0 deletions bambulabs_api/printer_info.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
__all__ = [
"NozzleType",
"P1FirmwareVersion",
]

from dataclasses import dataclass
from enum import Enum

NOZZLE_DIAMETER = {
Expand All @@ -8,6 +14,45 @@
}


class PrinterType(str, Enum):
"""
Enum class for the printer type

Attributes
----------
P1S: P1S printer
P1P: P1P printer
A1: A1P printer
A1_MINI: A1_MINI printer
X1C: X1C printer
X1E: X1E printer
"""
P1S = "P1S"
P1P = "P1P"
A1 = "A1"
A1_MINI = "A1_MINI"
X1C = "X1C"
X1E = "X1E"


@dataclass
class PrinterFirmwareInfo:
printer_type: PrinterType
firmware_version: str


class P1FirmwareVersion(str, Enum):
V_01_07_00_00 = "01.07.00.00"
V_01_06_01_02 = "01.06.01.02"
V_01_06_01_00 = "01.06.01.00"
V_01_06_00_00 = "01.06.00.00"
V_01_05_02_00 = "01.05.02.00"
V_01_05_01_00 = "01.05.01.00"
V_01_04_02_00 = "01.04.02.00"
V_01_04_01_00 = "01.04.01.00"
V_01_04_00_00 = "01.04.00.00"


class NozzleType(str, Enum):
"""
Enum class for the nozzle type
Expand Down
1 change: 1 addition & 0 deletions docs/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Release notes for each release here:
.. toctree::
:maxdepth: 1

2.5.12 <release/2.5.12-notes.rst>
2.5.10 <release/2.5.10-notes.rst>
2.5.9 <release/2.5.9-notes.rst>
2.5.8 <release/2.5.8-notes.rst>
Expand Down
8 changes: 8 additions & 0 deletions docs/release/2.5.12-notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Bambulabs API 2.5.12 Release Notes
==================================

* Add workaround for setting temperature for P1S
* Add get fan speeds by [@Stephen Tafoya](https://github.com/YOUR-WORST-TACO)
* Updated print 3mf api to accept custom filepaths - updated print gcode example scripts
* Add gcode validation bypass
* Add mqtt on_message, on_connect, and on_disconnect handlers
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "bambulabs_api"
version = "2.5.12-rc1"
version = "2.5.12"
authors = [
{ name="Chris Ioannidis", email="[email protected]" },
{ name="Haotian Wu", email="[email protected]"}
Expand Down