Fix binary io flush logic.
This commit is contained in:
		| @@ -272,12 +272,7 @@ class BambuVirtualPrinter: | |||||||
|         return self._serial_io.readline() |         return self._serial_io.readline() | ||||||
|  |  | ||||||
|     def readlines(self) -> list[bytes]: |     def readlines(self) -> list[bytes]: | ||||||
|         result = [] |         return self._serial_io.readlines() | ||||||
|         next_line = self._serial_io.readline() |  | ||||||
|         while next_line != b"": |  | ||||||
|             result.append(next_line) |  | ||||||
|             next_line = self._serial_io.readline() |  | ||||||
|         return result |  | ||||||
|  |  | ||||||
|     def sendIO(self, line: str): |     def sendIO(self, line: str): | ||||||
|         self._serial_io.send(line) |         self._serial_io.send(line) | ||||||
| @@ -504,24 +499,10 @@ class BambuVirtualPrinter: | |||||||
|         self._writingToSdFile = None |         self._writingToSdFile = None | ||||||
|         self.sendIO("Done saving file") |         self.sendIO("Done saving file") | ||||||
|  |  | ||||||
|     def _setMainThreadBusy(self, reason="processing"): |  | ||||||
|         def loop(): |  | ||||||
|             while self._busy_reason is not None: |  | ||||||
|                 self.sendIO(f"echo:busy {self._busy_reason}") |  | ||||||
|                 time.sleep(self._busy_interval) |  | ||||||
|             self._serial_io.sendOk() |  | ||||||
|  |  | ||||||
|         self._busy_reason = reason |  | ||||||
|         self._busy_loop = threading.Thread(target=loop) |  | ||||||
|         self._busy_loop.daemon = True |  | ||||||
|         self._busy_loop.start() |  | ||||||
|  |  | ||||||
|     def _setMainThreadIdle(self): |  | ||||||
|         self._busy_reason = None |  | ||||||
|  |  | ||||||
|     def close(self): |     def close(self): | ||||||
|         if self.bambu_client.connected: |         if self.bambu_client.connected: | ||||||
|             self.bambu_client.disconnect() |             self.bambu_client.disconnect() | ||||||
|  |         self.change_state(self._state_idle) | ||||||
|         self._serial_io.close() |         self._serial_io.close() | ||||||
|  |  | ||||||
|     def _showPrompt(self, text, choices): |     def _showPrompt(self, text, choices): | ||||||
|   | |||||||
| @@ -1,5 +1,6 @@ | |||||||
| from __future__ import annotations | from __future__ import annotations | ||||||
|  |  | ||||||
|  | from io import BufferedIOBase | ||||||
| import logging | import logging | ||||||
| import queue | import queue | ||||||
| import re | import re | ||||||
| @@ -13,7 +14,7 @@ from serial import SerialTimeoutException | |||||||
| from .char_counting_queue import CharCountingQueue | from .char_counting_queue import CharCountingQueue | ||||||
|  |  | ||||||
|  |  | ||||||
| class PrinterSerialIO(threading.Thread): | class PrinterSerialIO(threading.Thread, BufferedIOBase): | ||||||
|     command_regex = re.compile(r"^([GM])(\d+)") |     command_regex = re.compile(r"^([GM])(\d+)") | ||||||
|  |  | ||||||
|     def __init__( |     def __init__( | ||||||
| @@ -90,8 +91,7 @@ class PrinterSerialIO(threading.Thread): | |||||||
|         self.join() |         self.join() | ||||||
|  |  | ||||||
|     def flush(self): |     def flush(self): | ||||||
|         with self.input_bytes.all_tasks_done: |         self.input_bytes.join() | ||||||
|             self.input_bytes.all_tasks_done.wait() |  | ||||||
|  |  | ||||||
|     def write(self, data: bytes) -> int: |     def write(self, data: bytes) -> int: | ||||||
|         data = to_bytes(data, errors="replace") |         data = to_bytes(data, errors="replace") | ||||||
| @@ -126,6 +126,14 @@ class PrinterSerialIO(threading.Thread): | |||||||
|             # queue empty? return empty line |             # queue empty? return empty line | ||||||
|             return b"" |             return b"" | ||||||
|  |  | ||||||
|  |     def readlines(self): | ||||||
|  |         result = [] | ||||||
|  |         next_line = self.readline() | ||||||
|  |         while next_line != b"": | ||||||
|  |             result.append(next_line) | ||||||
|  |             next_line = self.readline() | ||||||
|  |         return result | ||||||
|  |  | ||||||
|     def send(self, line: str) -> None: |     def send(self, line: str) -> None: | ||||||
|         if self.output_bytes is not None: |         if self.output_bytes is not None: | ||||||
|             self.output_bytes.put(line) |             self.output_bytes.put(line) | ||||||
|   | |||||||
| @@ -12,8 +12,5 @@ class PrintFinishedState(APrinterState): | |||||||
|     def _finishSdPrint(self): |     def _finishSdPrint(self): | ||||||
|         if self._printer.is_running: |         if self._printer.is_running: | ||||||
|             self._printer.sendIO("Done printing file") |             self._printer.sendIO("Done printing file") | ||||||
|             self._selectedSdFilePos = 0 |  | ||||||
|             self._selectedSdFileSize = 0 |         self._printer.change_state(self._printer._state_idle) | ||||||
|             self._sdPrinting = False |  | ||||||
|             self._sdPrintStarting = False |  | ||||||
|             self._sdPrinter = None |  | ||||||
|   | |||||||
| @@ -133,7 +133,8 @@ def printer(output_test_folder, settings, profile_manager, log_test, ftps_sessio | |||||||
|         faked_baudrate=115200, |         faked_baudrate=115200, | ||||||
|     ) |     ) | ||||||
|     serial_obj._bambu_client = MagicMock() |     serial_obj._bambu_client = MagicMock() | ||||||
|     return serial_obj |     yield serial_obj | ||||||
|  |     serial_obj.close() | ||||||
|  |  | ||||||
|  |  | ||||||
| def test_initial_state(printer: BambuVirtualPrinter): | def test_initial_state(printer: BambuVirtualPrinter): | ||||||
| @@ -142,7 +143,7 @@ def test_initial_state(printer: BambuVirtualPrinter): | |||||||
|  |  | ||||||
| def test_list_sd_card(printer: BambuVirtualPrinter): | def test_list_sd_card(printer: BambuVirtualPrinter): | ||||||
|     printer.write(b"M20\n")  # GCode for listing SD card |     printer.write(b"M20\n")  # GCode for listing SD card | ||||||
|     printer.close() |     printer.flush() | ||||||
|     result = printer.readlines() |     result = printer.readlines() | ||||||
|     assert result == ""  # Replace with the actual expected result |     assert result == ""  # Replace with the actual expected result | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user