# FlightMonitor/controller/app_controller.py # Import LiveFetcher from the data module from .data.live_fetcher import LiveFetcher # We will need to instantiate it, so no need to import the entire module if only class is used class AppController: """ Coordinates operations between the GUI and the data/logic modules. Manages the application's state and flow. """ def __init__(self, main_window=None): # main_window can be set later """ Initializes the AppController. Args: main_window: The main GUI window instance. """ self.main_window = main_window self.live_fetcher = None # Will be an instance of LiveFetcher self.is_live_monitoring_active = False def set_main_window(self, main_window): """ Sets the main window instance after initialization, if needed. Args: main_window: The main GUI window instance. """ self.main_window = main_window def start_live_monitoring(self, bounding_box: dict): """ Starts the live flight data monitoring process. Args: bounding_box (dict): The geographical bounding box for filtering flights. """ if not self.main_window: print("Error: Main window not set in controller.") return if self.is_live_monitoring_active: self.main_window.update_status("Live monitoring is already active.") return self.main_window.update_status(f"Attempting to fetch live flights for bbox: {bounding_box}...") self.is_live_monitoring_active = True # Set flag before potential blocking call if self.live_fetcher is None: self.live_fetcher = LiveFetcher(timeout_seconds=15) # Initialize with a timeout # --- This is a SYNCHRONOUS call for now --- # In a later phase, this will be moved to a separate thread. flights_data = self.live_fetcher.fetch_flights(bounding_box) if flights_data is not None: # fetch_flights returns None on error, [] if no flights self.main_window.update_status(f"Fetched {len(flights_data)} flights. Displaying...") self.main_window.display_flights_on_canvas(flights_data, bounding_box) if not flights_data: # If list is empty self.main_window.update_status("No flights found in the selected area.") else: error_msg = "Failed to fetch live flight data. Check console for details." self.main_window.update_status(error_msg) self.main_window.show_error_message("API Error", error_msg) # Since fetching failed, we should probably allow user to try again # or revert the UI state to "stopped" self.stop_live_monitoring(from_error=True) # Call stop to reset UI # For now, live monitoring is a one-shot fetch. # We'll implement periodic polling later. # So, let's simulate it stopping after one fetch for now, or the UI will stay "stuck" # self.stop_live_monitoring() # Commented out: let user stop it manually or we'll add timer def stop_live_monitoring(self, from_error=False): """Stops the live flight data monitoring process.""" if not self.is_live_monitoring_active and not from_error: # if called not due to an error, and not active, do nothing. # if from_error is true, we might want to proceed to update UI regardless of this flag if not from_error: self.main_window.update_status("Live monitoring is not active.") return self.is_live_monitoring_active = False # In the future, if there's a running thread/task, it would be stopped here. if self.main_window and not from_error: # only update status if not already handled by an error message self.main_window.update_status("Live monitoring stopped.") # If called from an error in start_live_monitoring, main_window.stop_monitoring() # will be called by main_window itself to reset buttons. # If we want controller to explicitly tell window to reset UI: if self.main_window and from_error: self.main_window._stop_monitoring() # Call the UI method to reset buttons etc. def start_history_monitoring(self): """Starts the historical flight data analysis process.""" if not self.main_window: print("Error: Main window not set in controller.") return self.main_window.update_status("History monitoring started (placeholder).") # Logic for loading and displaying historical data will go here. # For now, main_window._start_monitoring handles the placeholder display. def stop_history_monitoring(self): """Stops the historical flight data analysis process.""" if not self.main_window: # print("Error: Main window not set in controller.") # Can be noisy if not active return self.main_window.update_status("History monitoring stopped.") # Clean up historical data display, etc.