Skip to content

Commit 7264e2d

Browse files
author
guoping.liu
committed
fixed connection check when wifi offline
1 parent 796814f commit 7264e2d

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

supervisor/network.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
get_wlan0_mac,
2222
is_interface_existing,
2323
is_network_connected,
24-
has_active_connection,
24+
has_saved_connection,
2525
get_wlan0_ip,
2626
get_active_connection_name
2727
)
@@ -241,7 +241,7 @@ def _handle_disconnect_status(self):
241241
# self.supervisor.onNetworkDisconnect()
242242

243243
if self.supervisor:
244-
if has_active_connection():
244+
if has_saved_connection():
245245
self.supervisor.set_led_state(LedState.SYS_OFFLINE)
246246
# 14400 seconds, 4 Hours, 10 seconds = 1 tick, 1440 ticks = 4 hours
247247
# 1800 seconds, 30 minutes, 10 seconds = 1 tick, 180 ticks = 30 minutes

supervisor/utils/wifi_utils.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,30 +129,44 @@ def get_active_connection_name():
129129
return None
130130

131131

132-
def has_active_connection():
133-
"""
134-
Check if there is an active network connection
135132

133+
134+
135+
def has_saved_connection():
136+
"""
137+
Check if there is any saved (historical) NetworkManager connection profile.
138+
136139
Returns:
137-
bool: True if there is an active connection, False otherwise
140+
bool: True if there is at least one saved connection, False otherwise
138141
"""
142+
# Prefer nmcli; fallback to checking NM config directory
139143
try:
140144
result = subprocess.run(
141-
['nmcli', '-t', '-f', 'TYPE,STATE,NAME', 'connection', 'show', '--active'],
145+
['nmcli', '-t', '-f', 'NAME,TYPE', 'connection', 'show'],
142146
check=True,
143147
stdout=subprocess.PIPE,
144148
stderr=subprocess.PIPE,
145149
text=True
146150
)
147-
return bool(result.stdout.strip())
151+
# Consider any saved connection; optionally prioritize wifi
152+
lines = [l for l in result.stdout.strip().split('\n') if l]
153+
if any(lines):
154+
return True
148155
except subprocess.CalledProcessError as e:
149-
logging.error(f"Command 'nmcli' failed with exit code {e.returncode}")
150-
logging.error(e.stderr)
151-
return False
156+
logging.warning(f"nmcli connection show failed: {e.returncode}, fallback to config dir")
152157
except Exception as e:
153-
logging.error(f"An unexpected error occurred: {e}")
154-
return False
158+
logging.warning(f"nmcli connection show unexpected error: {e}")
155159

160+
# Fallback: check NetworkManager system-connections directory
161+
config_dir = '/etc/NetworkManager/system-connections/'
162+
try:
163+
if os.path.isdir(config_dir):
164+
for entry in os.scandir(config_dir):
165+
if entry.is_file():
166+
return True
167+
except Exception as e:
168+
logging.warning(f"Failed to scan saved connections in {config_dir}: {e}")
169+
return False
156170
def _get_info_nmcli():
157171
"""Get WiFi info using nmcli"""
158172
try:

0 commit comments

Comments
 (0)