Main class for Ragnar. Manages the primary operations of the application.
| 27 | |
| 28 | |
| 29 | class Ragnar: |
| 30 | """Main class for Ragnar. Manages the primary operations of the application.""" |
| 31 | |
| 32 | def __init__(self, shared_data_obj): |
| 33 | self.shared_data = shared_data_obj |
| 34 | self.commentaire_ia = Commentaireia() |
| 35 | self.orchestrator_thread = None |
| 36 | self.orchestrator = None |
| 37 | self.wifi_manager = WiFiManager(self.shared_data) |
| 38 | |
| 39 | # Expose this instance to other modules |
| 40 | self.shared_data.ragnar_instance = self |
| 41 | self.shared_data.headless_mode = True |
| 42 | |
| 43 | # --------------------------------------------------------------------- |
| 44 | # Main loop |
| 45 | # --------------------------------------------------------------------- |
| 46 | def run(self): |
| 47 | """Main loop for Ragnar. Starts Wi-Fi manager and orchestrator as needed.""" |
| 48 | logger.info("=" * 70) |
| 49 | logger.info("RAGNAR MAIN THREAD STARTING (no EPD display)") |
| 50 | logger.info("=" * 70) |
| 51 | |
| 52 | # Initialize Wi-Fi management system |
| 53 | logger.info("Starting Wi-Fi management system...") |
| 54 | self.wifi_manager.start() |
| 55 | logger.info("Wi-Fi management system started") |
| 56 | |
| 57 | # Main loop to keep Ragnar running |
| 58 | logger.info("Entering main Ragnar loop...") |
| 59 | loop_count = 0 |
| 60 | while not self.shared_data.should_exit: |
| 61 | loop_count += 1 |
| 62 | if loop_count % 6 == 1: # roughly every 60 seconds if sleep(10) |
| 63 | logger.info( |
| 64 | f"Ragnar main loop iteration {loop_count}, " |
| 65 | f"manual_mode={self.shared_data.manual_mode}" |
| 66 | ) |
| 67 | |
| 68 | if not self.shared_data.manual_mode: |
| 69 | self.check_and_start_orchestrator() |
| 70 | |
| 71 | time.sleep(10) |
| 72 | |
| 73 | logger.info("Ragnar main loop exited") |
| 74 | |
| 75 | # --------------------------------------------------------------------- |
| 76 | # Orchestrator control |
| 77 | # --------------------------------------------------------------------- |
| 78 | def check_and_start_orchestrator(self): |
| 79 | """Check connectivity and start the orchestrator if connected.""" |
| 80 | network_connected = self.wifi_manager.check_network_connectivity() |
| 81 | connection_type = getattr(self.wifi_manager, "last_connection_type", None) |
| 82 | logger.debug(f"Network connection check: {network_connected} (type={connection_type})") |
| 83 | |
| 84 | self.shared_data.network_connected = network_connected |
| 85 | self.shared_data.wifi_connected = connection_type == "wifi" |
| 86 | self.shared_data.lan_connected = connection_type == "ethernet" |