From 7e30369982fa3089a4f6d4d26f8610e4087ec74d Mon Sep 17 00:00:00 2001 From: appel_c Date: Fri, 15 Aug 2025 11:09:42 +0200 Subject: [PATCH] w --- .../timepix_fly_client/timepix_fly_backend.py | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/superxas_bec/devices/timepix/timepix_fly_client/timepix_fly_backend.py b/superxas_bec/devices/timepix/timepix_fly_client/timepix_fly_backend.py index 9049183..1546d3f 100644 --- a/superxas_bec/devices/timepix/timepix_fly_client/timepix_fly_backend.py +++ b/superxas_bec/devices/timepix/timepix_fly_client/timepix_fly_backend.py @@ -274,17 +274,24 @@ class TimepixFlyBackend: if not chunk: break # Receiving an empty chunk means the connection was closed buffer += chunk.decode("utf-8") # Trailing byte, i.e. -> "}\n" - if "}\n" in buffer: - buffer_chunks = buffer.split("}\n") - processed_chunks = 0 - for entry in buffer_chunks: - if entry and entry.endswith("}\n"): - self._decode_received_data(entry + "}") - processed_chunks += 1 - if processed_chunks == len(buffer_chunks): - buffer = "" - else: - buffer = buffer_chunks[-1] + + # Check if trailing byte present in the buffer + buffer_chunks = buffer.split("}\n") + for entry in buffer_chunks[:-1]: + self._decode_received_data(entry + "}") + buffer = buffer_chunks[-1] # Keep the last incomplete chunk in the buffer + + # if "}\n" in buffer: + # buffer_chunks = buffer.split("}\n") + # processed_chunks = 0 + # for entry in buffer_chunks: + # if entry and entry.endswith("}\n"): + # self._decode_received_data(entry + "}") + # processed_chunks += 1 + # if processed_chunks == len(buffer_chunks): + # buffer = "" + # else: + # buffer = buffer_chunks[-1] def _decode_received_data(self, buffer: str) -> None: """ @@ -311,7 +318,7 @@ class TimepixFlyBackend: finally: # Clear the msg_buffer after processing self.reset_message_buffer() - except json.JSONDecodeError: + except json.JSONDecodeError: # TODO should this be the handling?? logger.warning(f"Failed to decode JSON from buffer: {buffer}") def run_msg_callbacks(self):