Skip to content

Commit 7f1fdc2

Browse files
authored
Merge pull request #69 from SimplyPrint/fix-extract-image-issue
Fix crash + add more logging for future similar issues.
2 parents e870496 + 5b4e350 commit 7f1fdc2

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

octoprint_simplyprint/websocket/simplyprint.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -314,28 +314,35 @@ def _monotonic(self) -> float:
314314
return self._aioloop.time()
315315

316316
async def _initialize(self) -> None:
317-
self._loop = IOLoop.current()
318-
for (func, args) in self.cached_events:
319-
func(*args)
320-
self.cached_events = []
321-
self.update_timer.start(delay=60.)
322-
self.cpu_timer.start()
323-
self.ping_timer.start()
324-
if self.printer.is_operational():
325-
self._on_printer_connected()
326-
else:
327-
self._update_state_from_octo()
328-
self.cache.machine_data = await self._get_machine_data()
329-
await self.webcam_stream.test_connection()
330-
await self._connect()
317+
try:
318+
self._loop = IOLoop.current()
319+
for (func, args) in self.cached_events:
320+
func(*args)
321+
self.cached_events = []
322+
self.update_timer.start(delay=60.)
323+
self.cpu_timer.start()
324+
self.ping_timer.start()
325+
if self.printer.is_operational():
326+
self._on_printer_connected()
327+
else:
328+
self._update_state_from_octo()
329+
self.cache.machine_data = await self._get_machine_data()
330+
await self.webcam_stream.test_connection()
331+
await self._connect()
332+
except Exception as e:
333+
self._logger.error("Failed to initialize SimplyPrint Plugin!", exc_info=e)
331334

332335
async def _get_machine_data(self) -> Dict[str, Any]:
333-
sys_query = SystemQuery(self.settings)
334-
data = await self._loop.run_in_executor(None, sys_query.get_system_info)
335-
data["ui"] = "OctoPrint"
336-
data["api"] = "OctoPrint"
337-
data["sp_version"] = self.plugin._plugin_version
338-
return data
336+
try:
337+
sys_query = SystemQuery(self.settings)
338+
data = await self._loop.run_in_executor(None, sys_query.get_system_info)
339+
data["ui"] = "OctoPrint"
340+
data["api"] = "OctoPrint"
341+
data["sp_version"] = self.plugin._plugin_version
342+
return data
343+
except Exception as e:
344+
self._logger.warning("Failed to get machine data", exc_info=e)
345+
return {}
339346

340347
async def _connect(self) -> None:
341348
log_connect = True

octoprint_simplyprint/websocket/webcam.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,13 @@ def webcam_connected(self):
5757
return self._connection_test_passed
5858

5959
async def test_connection(self) -> None:
60-
if not hasattr(self, "_loop"):
61-
self._loop = IOLoop.current()
62-
img = await self._loop.run_in_executor(None, self.extract_image)
63-
self._connection_test_passed = img is not None
60+
try:
61+
if not hasattr(self, "_loop"):
62+
self._loop = IOLoop.current()
63+
img = await self._loop.run_in_executor(None, self.extract_image)
64+
self._connection_test_passed = img is not None
65+
except Exception as e:
66+
self._logger.warning("Failed to test webcam connection", exc_info=e)
6467

6568
def extract_image(self) -> Optional[str]:
6669
headers = {"Accept": "image/jpeg"}
@@ -75,11 +78,13 @@ def extract_image(self) -> Optional[str]:
7578
self.url, headers=headers, verify=False, timeout=4
7679
)
7780
resp.raise_for_status()
81+
82+
return self._encode_image(resp.content)
7883
else:
7984
self._logger.warning("Unable to determine snapshot URL")
8085
except Exception:
81-
return None
82-
return self._encode_image(resp.content)
86+
pass
87+
return None
8388

8489
def _encode_image(self, image: bytes) -> str:
8590
return base64.b64encode(image).decode()

0 commit comments

Comments
 (0)