Skip to content

Commit 67592c7

Browse files
Move wifi busy logic to wifi service
1 parent 73cba70 commit 67592c7

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

internal_filesystem/builtin/apps/com.micropythonos.wifi/assets/wifi.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ def onResume(self, screen):
6868
WifiService.get_saved_networks()
6969

7070
if len(self.scanned_ssids) == 0:
71-
if WifiService.wifi_busy == False:
72-
WifiService.wifi_busy = True
71+
if not WifiService.is_busy():
7372
self.start_scan_networks()
7473
else:
7574
self.show_error("Wifi is busy, please try again later.")
@@ -93,9 +92,8 @@ def scan_networks_thread(self):
9392
except Exception as e:
9493
print(f"scan_networks: Scan failed: {e}")
9594
self.show_error("Wi-Fi scan failed")
96-
# scan done:
95+
# scan done - WifiService.scan_networks() manages wifi_busy flag internally
9796
self.busy_scanning = False
98-
WifiService.wifi_busy = False
9997
self.update_ui_threadsafe_if_foreground(self.scan_button_label.set_text, self.scan_button_scan_text)
10098
self.update_ui_threadsafe_if_foreground(self.scan_button.remove_state, lv.STATE.DISABLED)
10199
self.update_ui_threadsafe_if_foreground(self.refresh_list)

internal_filesystem/lib/mpos/net/wifi_service.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class WifiService:
3636
"""
3737

3838
# Class-level lock to prevent concurrent WiFi operations
39-
# Used by WiFi app when scanning to avoid conflicts with connection attempts
39+
# Use is_busy() to check state; operations like scan_networks() manage this automatically
4040
wifi_busy = False
4141

4242
# Dictionary of saved access points {ssid: {password: "..."}}
@@ -312,6 +312,19 @@ def disconnect(network_module=None):
312312
#print(f"WifiService: Error disconnecting: {e}") # probably "Wifi Not Started" so harmless
313313
pass
314314

315+
@staticmethod
316+
def is_busy():
317+
"""
318+
Check if WiFi operations are currently in progress.
319+
320+
Use this to check if scanning or other WiFi operations can be started.
321+
Operations like scan_networks() manage the busy flag automatically.
322+
323+
Returns:
324+
bool: True if WiFi is busy, False if available
325+
"""
326+
return WifiService.wifi_busy
327+
315328
@staticmethod
316329
def get_saved_networks():
317330
"""
@@ -356,22 +369,35 @@ def _scan_networks_raw(network_module=None):
356369
def scan_networks(network_module=None):
357370
"""
358371
Scan for available WiFi networks.
372+
373+
This method manages the wifi_busy flag internally. If WiFi is already busy,
374+
returns an empty list. The busy flag is automatically cleared when scanning
375+
completes (even on error).
359376
360377
Args:
361378
network_module: Network module for dependency injection (testing)
362379
363380
Returns:
364-
list: List of SSIDs found, or mock data on desktop
381+
list: List of SSIDs found, empty list if busy, or mock data on desktop
365382
"""
383+
# Desktop mode - return mock SSIDs (no busy flag needed)
366384
if not HAS_NETWORK_MODULE and network_module is None:
367-
# Desktop mode - return mock SSIDs
368385
time.sleep(1)
369386
return ["Home WiFi", "Pretty Fly for a Wi Fi", "Winternet is coming", "The Promised LAN"]
370387

371-
networks = WifiService._scan_networks_raw(network_module)
372-
# Return unique SSIDs, filtering out empty ones and invalid lengths
373-
ssids = list(set(n[0].decode() for n in networks if n[0]))
374-
return [s for s in ssids if 0 < len(s) <= 32]
388+
# Check if already busy
389+
if WifiService.wifi_busy:
390+
print("WifiService: scan_networks() - WiFi is busy, returning empty list")
391+
return []
392+
393+
WifiService.wifi_busy = True
394+
try:
395+
networks = WifiService._scan_networks_raw(network_module)
396+
# Return unique SSIDs, filtering out empty ones and invalid lengths
397+
ssids = list(set(n[0].decode() for n in networks if n[0]))
398+
return [s for s in ssids if 0 < len(s) <= 32]
399+
finally:
400+
WifiService.wifi_busy = False
375401

376402
@staticmethod
377403
def get_current_ssid(network_module=None):

0 commit comments

Comments
 (0)