1
0
mirror of https://github.com/bec-project/bec_widgets.git synced 2025-12-30 18:51:19 +01:00

refactor: use client callback for scan history reload

This commit is contained in:
2025-07-29 15:24:00 +02:00
committed by Christian Appel
parent 6df1d0c31f
commit d22a3317ba

View File

@@ -32,26 +32,18 @@ class BECHistoryManager(QtCore.QObject):
super().__init__(parent)
self._load_attempt = 0
self.client = client
self._cb_id = self.client.callbacks.register(
event_type=EventType.SCAN_HISTORY_UPDATE, callback=self._on_scan_history_update
self._cb_id: dict[str, int] = {}
self._cb_id["update_scan_history"] = self.client.callbacks.register(
EventType.SCAN_HISTORY_UPDATE, self._on_scan_history_update
)
self._cb_id["scan_history_loaded"] = self.client.callbacks.register(
EventType.SCAN_HISTORY_LOADED, self._on_scan_history_reloaded
)
def refresh_scan_history(self) -> None:
"""Refresh the scan history from the client."""
all_messages = []
# pylint: disable=protected-access
self.client.history._scan_history_loaded_event.wait(timeout=1)
if not self.client.history._scan_history_loaded_event.is_set():
self._load_attempt += 1
if self._load_attempt < 3:
logger.warning(
f"Scan history not loaded yet, retrying ({self._load_attempt}/3 attempts)."
)
QtCore.QTimer.singleShot(1000, self.refresh_scan_history)
else:
logger.error("Failed to load scan history after 3 attempts.")
return
self._load_attempt = 0
for scan_id in self.client.history._scan_ids: # pylint: disable=protected-access
history_msg = self.client.history._scan_data.get(scan_id, None)
if history_msg is None:
@@ -60,14 +52,23 @@ class BECHistoryManager(QtCore.QObject):
all_messages.append(history_msg.model_dump())
self.scan_history_refreshed.emit(all_messages)
def _on_scan_history_reloaded(self, history_msgs: list[ScanHistoryMessage]) -> None:
"""Handle scan history reloaded event from the client."""
if not history_msgs:
logger.warning("Scan history reloaded with no messages.")
return
self.scan_history_refreshed.emit([msg.model_dump() for msg in history_msgs])
def _on_scan_history_update(self, history_msg: ScanHistoryMessage) -> None:
"""Handle scan history updates from the client."""
self.scan_history_updated.emit(history_msg.model_dump())
def cleanup(self) -> None:
"""Clean up the manager by disconnecting callbacks."""
self.client.callbacks.remove(self._cb_id)
for cb_id in self._cb_id.values():
self.client.callbacks.remove(cb_id)
self.scan_history_updated.disconnect()
self.scan_history_refreshed.disconnect()
class ScanHistoryView(BECWidget, QtWidgets.QTreeWidget):
@@ -111,7 +112,7 @@ class ScanHistoryView(BECWidget, QtWidgets.QTreeWidget):
header.setToolTip(f"Last {self.max_length} scans in history.")
self.bec_scan_history_manager.scan_history_updated.connect(self.update_history)
self.bec_scan_history_manager.scan_history_refreshed.connect(self.update_full_history)
QtCore.QTimer.singleShot(500, self.refresh)
self.refresh()
def _set_policies(self):
"""Set the policies for the tree widget."""
@@ -172,9 +173,15 @@ class ScanHistoryView(BECWidget, QtWidgets.QTreeWidget):
@SafeSlot()
def refresh(self):
"""Refresh the scan history view."""
while len(self.scan_history) > 0:
self.remove_scan(index=0)
self.bec_scan_history_manager.refresh_scan_history()
if (
self.client.history._scan_history_loaded_event.is_set()
): # pylint: disable=protected-access
while len(self.scan_history) > 0:
self.remove_scan(index=0)
self.bec_scan_history_manager.refresh_scan_history()
return
else:
logger.info("Scan history not loaded yet, waiting for it to be loaded.")
@SafeSlot(dict)
def update_history(self, msg_dump: dict):