diff --git a/src/ui/test_vision/test_vision.py b/src/ui/test_vision/test_vision.py index 9d55bbc..2e14879 100644 --- a/src/ui/test_vision/test_vision.py +++ b/src/ui/test_vision/test_vision.py @@ -1,12 +1,58 @@ import sys +import logging -from PyQt5.QtCore import pyqtSignal +from PyQt5.QtCore import pyqtSignal, QTimer from PyQt5.QtGui import QColor, QImage, QPalette, QPixmap -from PyQt5.QtWidgets import QHeaderView, QProgressBar, QTableWidgetItem +from PyQt5.QtWidgets import QHeaderView, QProgressBar, QTableWidgetItem, QDialog, QVBoxLayout, QLabel, QProgressBar -from lib.helpers.blocking_dialog import BlockingDialog -from ui.helpers import calc_foreground_color -from ui.test_test import Test_Test +# Import relative to the project structure +from src.lib.helpers.blocking_dialog import BlockingDialog +from src.ui.helpers import calc_foreground_color +from src.ui.test_test import Test_Test + + +class SchemeProgressDialog(QDialog): + """ + Dialog to show scheme switching progress and block the UI until complete. + """ + def __init__(self, parent=None): + super().__init__(parent) + self.setWindowTitle("Switching Camera Scheme") + self.setModal(True) + self.setMinimumWidth(400) + + layout = QVBoxLayout() + self.setLayout(layout) + + self.label = QLabel("Waiting for camera scheme to switch. Please wait...") + layout.addWidget(self.label) + + self.progress_bar = QProgressBar() + self.progress_bar.setMinimum(0) + self.progress_bar.setMaximum(100) + layout.addWidget(self.progress_bar) + + self.status_label = QLabel("Status: Initializing...") + layout.addWidget(self.status_label) + + # Set a timeout in case the progress signal doesn't complete + self.timer = QTimer(self) + self.timer.setSingleShot(True) + self.timer.timeout.connect(self.handle_timeout) + self.timer.start(30000) # 30 second timeout + + def update_progress(self, progress, status): + self.progress_bar.setValue(progress) + self.status_label.setText(f"Status: {status}") + + if progress >= 100 and status == "Success": + self.accept() + elif status == "Failed": + self.reject() + + def handle_timeout(self): + self.status_label.setText("Status: Timeout - Continuing anyway") + self.accept() # Continue with the cycle even if timeout occurs class Test_Vision(Test_Test): @@ -18,6 +64,8 @@ class Test_Vision(Test_Test): self.ok_counter_limit = 3 else: self.ok_counter_limit = 1 + # Get logger + self.log = logging.getLogger("Test_Vision") # DETECTIONS TABLE self.results_table_headers = { "class": "RILEVATO", @@ -46,6 +94,27 @@ class Test_Vision(Test_Test): wait_time = 60 dialog = BlockingDialog(self.ui,wait_time=wait_time, message=f"COLLEGARE CAVO ETHERNET E ATTENDERE {wait_time} SECONDI PER IL CORRETTO AVVIO DELLE TELECAMERE") dialog.exec_() + + # Wait for scheme switching to complete before proceeding + if self.components[cam_type].current_operation == "switch_scheme": + # Create and show the progress dialog + progress_dialog = SchemeProgressDialog(self) + + # Connect to the progress signal + progress_connection = self.components[cam_type].progress_signal.connect( + progress_dialog.update_progress + ) + + # Show the dialog and wait for it to complete + result = progress_dialog.exec_() + + # Disconnect from the progress signal + self.components[cam_type].progress_signal.disconnect(progress_connection) + + if result == QDialog.Rejected: + # Scheme switching failed, but we'll continue anyway + self.log.warning("Scheme switching failed, continuing with cycle") + self.components[cam_type].resume() else: return @@ -235,3 +304,38 @@ class Test_Vision(Test_Test): super().emit_ko() if "neo_pixels" in self.components and self.components["neo_pixels"].ready: self.components["neo_pixels"].set_all_pixel_color("#000000") + + def test_scheme_switching(self): + """ + Test method to verify that the scheme switching wait mechanism works correctly. + This can be called manually for testing purposes. + """ + if "vision" not in self.components or "hikrobot_sc" not in self.components: + self.log.error("Cannot test scheme switching: vision or hikrobot_sc component not available") + return False + + # Get the current scheme + current_scheme = self.components["hikrobot_sc"].get_current_scheme() + self.log.info(f"Current scheme: {current_scheme}") + + # Set a test recipe to trigger scheme switching + test_recipe = "test_absence" # Use an existing recipe name + self.log.info(f"Setting test recipe: {test_recipe}") + self.components["vision"].set_recipe(test_recipe) + + # Wait for the scheme switching dialog to appear and complete + self.log.info("Waiting for scheme switching to complete...") + + # The dialog should appear automatically in the start() method + # when the hikrobot_sc component's current_operation is "switch_scheme" + + # Verify the scheme was switched + new_scheme = self.components["hikrobot_sc"].get_current_scheme() + self.log.info(f"New scheme: {new_scheme}") + + if new_scheme == test_recipe: + self.log.info("Scheme switching test passed!") + return True + else: + self.log.error("Scheme switching test failed: scheme was not switched correctly") + return False