Main class for Ragnar on Pineapple Pager.
| 143 | |
| 144 | |
| 145 | class PagerRagnar: |
| 146 | """Main class for Ragnar on Pineapple Pager.""" |
| 147 | |
| 148 | def __init__(self, shared_data): |
| 149 | self.shared_data = shared_data |
| 150 | self.orchestrator_thread = None |
| 151 | self.orchestrator = None |
| 152 | self._orchestrator_lock = threading.Lock() |
| 153 | |
| 154 | self.shared_data.ragnar_instance = self |
| 155 | self.shared_data.headless_mode = False |
| 156 | |
| 157 | def run(self): |
| 158 | """Main loop - waits for Wi-Fi connection and starts Orchestrator.""" |
| 159 | if hasattr(self.shared_data, 'startup_delay') and self.shared_data.startup_delay > 0: |
| 160 | logger.info(f"Waiting for startup delay: {self.shared_data.startup_delay} seconds") |
| 161 | time.sleep(self.shared_data.startup_delay) |
| 162 | |
| 163 | while not self.shared_data.should_exit: |
| 164 | if not self.shared_data.manual_mode: |
| 165 | self.check_and_start_orchestrator() |
| 166 | time.sleep(10) |
| 167 | |
| 168 | def check_and_start_orchestrator(self): |
| 169 | if self.is_wifi_connected(): |
| 170 | self.shared_data.wifi_connected = True |
| 171 | if self.orchestrator_thread is None or not self.orchestrator_thread.is_alive(): |
| 172 | self.start_orchestrator() |
| 173 | else: |
| 174 | self.shared_data.wifi_connected = False |
| 175 | logger.info("Waiting for Wi-Fi connection to start Orchestrator...") |
| 176 | |
| 177 | def start_orchestrator(self): |
| 178 | with self._orchestrator_lock: |
| 179 | if self.is_wifi_connected(): |
| 180 | self.shared_data.wifi_connected = True |
| 181 | if self.orchestrator_thread is None or not self.orchestrator_thread.is_alive(): |
| 182 | logger.info("Starting Orchestrator thread...") |
| 183 | self.shared_data.orchestrator_should_exit = False |
| 184 | self.shared_data.manual_mode = False |
| 185 | from orchestrator import Orchestrator |
| 186 | self.orchestrator = Orchestrator() |
| 187 | self.orchestrator_thread = threading.Thread(target=self.orchestrator.run) |
| 188 | self.orchestrator_thread.start() |
| 189 | logger.info("Orchestrator thread started, automatic mode activated.") |
| 190 | |
| 191 | def stop_orchestrator(self): |
| 192 | self.shared_data.manual_mode = True |
| 193 | logger.info("Stopping Orchestrator...") |
| 194 | if self.orchestrator_thread is not None and self.orchestrator_thread.is_alive(): |
| 195 | self.shared_data.orchestrator_should_exit = True |
| 196 | self.orchestrator_thread.join() |
| 197 | self.shared_data.ragnarorch_status = "IDLE" |
| 198 | self.shared_data.ragnarstatustext2 = "" |
| 199 | |
| 200 | def is_wifi_connected(self): |
| 201 | """Check Wi-Fi connectivity (Pager + Pi compatible).""" |
| 202 | try: |