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
10 changes: 6 additions & 4 deletions bambulabs_api/camera_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ def __init__(
self.__hostname = str(hostname)
self.__port = port

self.__thread = Thread(target=self.retriever)
self.__thread.daemon = True

self.__thread = None
self.last_frame = None
self.alive = False

Expand All @@ -44,6 +42,8 @@ def start(self):
"""
if not self.alive:
self.alive = True
self.__thread = Thread(target=self.retriever)
self.__thread.daemon = True
self.__thread.start()
return True
else:
Expand All @@ -54,7 +54,9 @@ def stop(self):
Stop the camera client
"""
self.alive = False
self.__thread.join()
if self.__thread is not None:
self.__thread.join()
self.__thread = None

def get_frame(self):
if self.last_frame is None:
Expand Down
17 changes: 17 additions & 0 deletions tests/test_camera_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from unittest.mock import patch

from bambulabs_api.camera_client import PrinterCamera


def test_camera_is_reusable():
camera = PrinterCamera('1.2.3.4', 'fake_code')

# Mock the retriever method so it doesn't do any network I/O.
with patch.object(camera, 'retriever', return_value=None):
# First cycle
camera.start()
camera.stop()

# Second cycle
camera.start() # This was failing before.
camera.stop()