Handle AP mode monitoring for endless loop with improved recovery
(self, current_time)
| 1021 | if not self.check_wifi_connection(): |
| 1022 | validation_failures += 1 |
| 1023 | self.logger.warning(f"Endless Loop: WiFi validation failed ({validation_failures}/{self.wifi_validation_retries})") |
| 1024 | else: |
| 1025 | self.logger.info(f"Endless Loop: WiFi validation passed ({i+1}/{self.wifi_validation_retries})") |
| 1026 | |
| 1027 | # Don't wait after the last check |
| 1028 | if i < self.wifi_validation_retries - 1: |
| 1029 | time.sleep(self.wifi_validation_retry_interval) |
| 1030 | |
| 1031 | # Update validation time |
| 1032 | self.last_wifi_validation = time.time() |
| 1033 | |
| 1034 | # If ALL 5 validations failed, disconnect and start endless loop |
| 1035 | if validation_failures == self.wifi_validation_retries: |
| 1036 | self.consecutive_validation_cycles_failed += 1 |
| 1037 | self.logger.warning(f"Endless Loop: All {self.wifi_validation_retries} WiFi validations failed! Switching to AP mode") |
| 1038 | self.wifi_connected = False |
| 1039 | self.shared_data.wifi_connected = False |
| 1040 | self.disconnect_wifi() # Disconnect from current network |
| 1041 | self._endless_loop_start_ap_mode() |
| 1042 | else: |
| 1043 | # Reset consecutive failure counter if any check passed |
| 1044 | self.consecutive_validation_cycles_failed = 0 |
| 1045 | self.logger.info(f"Endless Loop: WiFi validation completed - {self.wifi_validation_retries - validation_failures}/{self.wifi_validation_retries} passed") |
| 1046 | |
| 1047 | def _strong_wifi_presence_check(self): |
| 1048 | """Perform a strong connectivity check (ping external host) to verify real internet access""" |
| 1049 | try: |
| 1050 | self.logger.debug("Performing strong WiFi presence check (ping 8.8.8.8)") |
| 1051 | result = subprocess.run(['ping', '-c', '2', '-W', '3', '8.8.8.8'], |
| 1052 | capture_output=True, timeout=8) |
| 1053 | if result.returncode == 0: |
| 1054 | self.logger.info("Strong check: Successfully pinged 8.8.8.8 - WiFi connected") |
| 1055 | return True |
| 1056 | else: |
| 1057 | self.logger.warning("Strong check: Failed to ping 8.8.8.8") |
| 1058 | return False |
| 1059 | except Exception as e: |
| 1060 | self.logger.error(f"Strong check error: {e}") |
| 1061 | return False |
| 1062 | |
| 1063 | def _failsafe_reboot(self): |
| 1064 | """Reboot the system as a last resort failsafe mechanism""" |
| 1065 | try: |
| 1066 | self.logger.critical("FAILSAFE: Initiating system reboot due to persistent connectivity issues") |
| 1067 | self.ap_logger.critical("FAILSAFE: System reboot triggered - persistent connectivity failures") |
| 1068 | |
| 1069 | # Save state before reboot |
| 1070 | self._save_connection_state(None, False) |
| 1071 | |
| 1072 | # Log the failsafe trigger |
| 1073 | try: |
| 1074 | with open('/var/log/ragnar_failsafe.log', 'a') as f: |
| 1075 | timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S') |
| 1076 | f.write(f"{timestamp} - Failsafe reboot triggered: {self.no_connection_cycles} consecutive connection failures\n") |
| 1077 | except: |
| 1078 | pass |
| 1079 | |
| 1080 | # Clean shutdown of services |
no test coverage detected