Skip to content

Commit

Permalink
0.1.8rc12
Browse files Browse the repository at this point in the history
update pybambu module from upstream HA project
  • Loading branch information
jneilliii committed Dec 1, 2024
1 parent 98ab94b commit 52ba3ff
Show file tree
Hide file tree
Showing 8 changed files with 456 additions and 1,316 deletions.
19 changes: 10 additions & 9 deletions octoprint_bambu_printer/printer/bambu_virtual_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,19 +259,20 @@ def _create_client_connection(self):
f"connecting via local mqtt: {self._settings.get_boolean(['local_mqtt'])}"
)
bambu_client = BambuClient(
device_type=self._settings.get(["device_type"]),
serial=self._settings.get(["serial"]),
host=self._settings.get(["host"]),
username=(
{"device_type": self._settings.get(["device_type"]),
"serial": self._settings.get(["serial"]),
"host": self._settings.get(["host"]),
"username": (
"bblp"
if self._settings.get_boolean(["local_mqtt"])
else self._settings.get(["username"])
),
access_code=self._settings.get(["access_code"]),
local_mqtt=self._settings.get_boolean(["local_mqtt"]),
region=self._settings.get(["region"]),
email=self._settings.get(["email"]),
auth_token=self._settings.get(["auth_token"]) if self._settings.get_boolean(["local_mqtt"]) is False else "",
"access_code": self._settings.get(["access_code"]),
"local_mqtt": self._settings.get_boolean(["local_mqtt"]),
"region": self._settings.get(["region"]),
"email": self._settings.get(["email"]),
"auth_token": self._settings.get(["auth_token"]) if self._settings.get_boolean(["local_mqtt"]) is False else "",
}
)
bambu_client.on_disconnect = self.on_disconnect(bambu_client.on_disconnect)
bambu_client.on_connect = self.on_connect(bambu_client.on_connect)
Expand Down
98 changes: 63 additions & 35 deletions octoprint_bambu_printer/printer/pybambu/bambu_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def run(self):

# Reset buffer
img = None
# else:
# else:
# Otherwise we need to continue looping without reseting the buffer to receive the remaining data
# and without delaying.

Expand Down Expand Up @@ -237,7 +237,7 @@ def run(self):
exceptionSeen = ""
while True:
try:
host = self._client.host if self._client.local_mqtt else self._client.bambu_cloud.cloud_mqtt_host
host = self._client.host if self._client._local_mqtt else self._client.bambu_cloud.cloud_mqtt_host
LOGGER.debug(f"Connect: Attempting Connection to {host}")
self._client.client.connect(host, self._client._port, keepalive=5)

Expand Down Expand Up @@ -285,25 +285,32 @@ class BambuClient:
_camera = None
_usage_hours: float

def __init__(self, device_type: str, serial: str, host: str, local_mqtt: bool, region: str, email: str,
username: str, auth_token: str, access_code: str, usage_hours: float = 0, manual_refresh_mode: bool = False, chamber_image: bool = True):
self.callback = None
self.host = host
self.local_mqtt = local_mqtt
self._serial = serial
self._auth_token = auth_token
self._access_code = access_code
self._username = username
def __init__(self, config):
self.host = config['host']
self._callback = None

self._access_code = config.get('access_code', '')
self._auth_token = config.get('auth_token', '')
self._device_type = config.get('device_type', 'unknown')
self._local_mqtt = config.get('local_mqtt', False)
self._manual_refresh_mode = config.get('manual_refresh_mode', False)
self._serial = config.get('serial', '')
self._usage_hours = config.get('usage_hours', 0)
self._username = config.get('username', '')
self._enable_camera = config.get('enable_camera', True)

self._connected = False
self._device_type = device_type
self._usage_hours = usage_hours
self._port = 1883
self._refreshed = False
self._manual_refresh_mode = manual_refresh_mode

self._device = Device(self)
self.bambu_cloud = BambuCloud(region, email, username, auth_token)
self.bambu_cloud = BambuCloud(
config.get('region', ''),
config.get('email', ''),
config.get('username', ''),
config.get('auth_token', '')
)
self.slicer_settings = SlicerSettings(self)
self.use_chamber_image = chamber_image

@property
def connected(self):
Expand All @@ -322,7 +329,22 @@ async def set_manual_refresh_mode(self, on):
self.disconnect()
else:
# Reconnect normally
self.connect(self.callback)
self.connect(self._callback)

@property
def camera_enabled(self):
return self._enable_camera

def callback(self, event: str):
if self._callback is not None:
self._callback(event)

def set_camera_enabled(self, enable):
self._enable_camera = enable
if self._enable_camera:
self._start_camera()
else:
self._stop_camera()

def setup_tls(self):
self.client.tls_set(tls_version=ssl.PROTOCOL_TLS, cert_reqs=ssl.CERT_NONE)
Expand All @@ -331,7 +353,7 @@ def setup_tls(self):
def connect(self, callback):
"""Connect to the MQTT Broker"""
self.client = mqtt.Client()
self.callback = callback
self._callback = callback
self.client.on_connect = self.on_connect
self.client.on_disconnect = self.on_disconnect
self.client.on_message = self.on_message
Expand All @@ -342,7 +364,7 @@ def connect(self, callback):
self.setup_tls()

self._port = 8883
if self.local_mqtt:
if self._local_mqtt:
self.client.username_pw_set("bblp", password=self._access_code)
else:
self.client.username_pw_set(self._username, password=self._auth_token)
Expand Down Expand Up @@ -371,6 +393,22 @@ def on_connect(self,
LOGGER.info("On Connect: Connected to printer")
self._on_connect()

def _start_camera(self):
if not self._device.supports_feature(Features.CAMERA_RTSP):
if self._device.supports_feature(Features.CAMERA_IMAGE):
if self._enable_camera:
LOGGER.debug("Starting Chamber Image thread")
self._camera = ChamberImageThread(self)
self._camera.start()
elif (self.host == "") or (self._access_code == ""):
LOGGER.debug("Skipping camera setup as local access details not provided.")

def _stop_camera(self):
if self._camera is not None:
LOGGER.debug("Stopping camera thread")
self._camera.stop()
self._camera.join()

def _on_connect(self):
self._connected = True
self.subscribe_and_request_info()
Expand All @@ -379,14 +417,7 @@ def _on_connect(self):
self._watchdog = WatchdogThread(self)
self._watchdog.start()

if not self._device.supports_feature(Features.CAMERA_RTSP):
if self._device.supports_feature(Features.CAMERA_IMAGE):
if self.use_chamber_image:
LOGGER.debug("Starting Chamber Image thread")
self._camera = ChamberImageThread(self)
self._camera.start()
elif (self.host == "") or (self._access_code == ""):
LOGGER.debug("Skipping camera setup as local access details not provided.")
self._start_camera()

def try_on_connect(self,
client_: mqtt.Client,
Expand All @@ -410,7 +441,7 @@ def on_disconnect(self,
"""Called when MQTT Disconnects"""
LOGGER.warn(f"On Disconnect: Printer disconnected with error code: {result_code}")
self._on_disconnect()

def _on_disconnect(self):
LOGGER.debug("_on_disconnect: Lost connection to the printer")
self._connected = False
Expand All @@ -419,10 +450,7 @@ def _on_disconnect(self):
LOGGER.debug("Stopping watchdog thread")
self._watchdog.stop()
self._watchdog.join()
if self._camera is not None:
LOGGER.debug("Stopping camera thread")
self._camera.stop()
self._camera.join()
self._stop_camera()

def _on_watchdog_fired(self):
LOGGER.info("Watch dog fired")
Expand Down Expand Up @@ -487,7 +515,7 @@ async def refresh(self):
"""Force refresh data"""

if self._manual_refresh_mode:
self.connect(self.callback)
self.connect(self._callback)
else:
LOGGER.debug("Force Refresh: Getting Version Info")
self._refreshed = True
Expand Down Expand Up @@ -531,8 +559,8 @@ def on_message(client, userdata, message):
# Run the blocking tls_set method in a separate thread
loop = asyncio.get_event_loop()
await loop.run_in_executor(None, self.setup_tls)

if self.local_mqtt:
if self._local_mqtt:
self.client.username_pw_set("bblp", password=self._access_code)
else:
self.client.username_pw_set(self._username, password=self._auth_token)
Expand Down
Loading

0 comments on commit 52ba3ff

Please sign in to comment.