You've already forked MicroPythonOS
mirror of
https://github.com/m5stack/MicroPythonOS.git
synced 2026-05-20 11:51:27 -07:00
Move wifi busy logic to wifi service
This commit is contained in:
@@ -68,8 +68,7 @@ class WiFi(Activity):
|
||||
WifiService.get_saved_networks()
|
||||
|
||||
if len(self.scanned_ssids) == 0:
|
||||
if WifiService.wifi_busy == False:
|
||||
WifiService.wifi_busy = True
|
||||
if not WifiService.is_busy():
|
||||
self.start_scan_networks()
|
||||
else:
|
||||
self.show_error("Wifi is busy, please try again later.")
|
||||
@@ -93,9 +92,8 @@ class WiFi(Activity):
|
||||
except Exception as e:
|
||||
print(f"scan_networks: Scan failed: {e}")
|
||||
self.show_error("Wi-Fi scan failed")
|
||||
# scan done:
|
||||
# scan done - WifiService.scan_networks() manages wifi_busy flag internally
|
||||
self.busy_scanning = False
|
||||
WifiService.wifi_busy = False
|
||||
self.update_ui_threadsafe_if_foreground(self.scan_button_label.set_text, self.scan_button_scan_text)
|
||||
self.update_ui_threadsafe_if_foreground(self.scan_button.remove_state, lv.STATE.DISABLED)
|
||||
self.update_ui_threadsafe_if_foreground(self.refresh_list)
|
||||
|
||||
@@ -36,7 +36,7 @@ class WifiService:
|
||||
"""
|
||||
|
||||
# Class-level lock to prevent concurrent WiFi operations
|
||||
# Used by WiFi app when scanning to avoid conflicts with connection attempts
|
||||
# Use is_busy() to check state; operations like scan_networks() manage this automatically
|
||||
wifi_busy = False
|
||||
|
||||
# Dictionary of saved access points {ssid: {password: "..."}}
|
||||
@@ -312,6 +312,19 @@ class WifiService:
|
||||
#print(f"WifiService: Error disconnecting: {e}") # probably "Wifi Not Started" so harmless
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def is_busy():
|
||||
"""
|
||||
Check if WiFi operations are currently in progress.
|
||||
|
||||
Use this to check if scanning or other WiFi operations can be started.
|
||||
Operations like scan_networks() manage the busy flag automatically.
|
||||
|
||||
Returns:
|
||||
bool: True if WiFi is busy, False if available
|
||||
"""
|
||||
return WifiService.wifi_busy
|
||||
|
||||
@staticmethod
|
||||
def get_saved_networks():
|
||||
"""
|
||||
@@ -356,22 +369,35 @@ class WifiService:
|
||||
def scan_networks(network_module=None):
|
||||
"""
|
||||
Scan for available WiFi networks.
|
||||
|
||||
This method manages the wifi_busy flag internally. If WiFi is already busy,
|
||||
returns an empty list. The busy flag is automatically cleared when scanning
|
||||
completes (even on error).
|
||||
|
||||
Args:
|
||||
network_module: Network module for dependency injection (testing)
|
||||
|
||||
Returns:
|
||||
list: List of SSIDs found, or mock data on desktop
|
||||
list: List of SSIDs found, empty list if busy, or mock data on desktop
|
||||
"""
|
||||
# Desktop mode - return mock SSIDs (no busy flag needed)
|
||||
if not HAS_NETWORK_MODULE and network_module is None:
|
||||
# Desktop mode - return mock SSIDs
|
||||
time.sleep(1)
|
||||
return ["Home WiFi", "Pretty Fly for a Wi Fi", "Winternet is coming", "The Promised LAN"]
|
||||
|
||||
networks = WifiService._scan_networks_raw(network_module)
|
||||
# Return unique SSIDs, filtering out empty ones and invalid lengths
|
||||
ssids = list(set(n[0].decode() for n in networks if n[0]))
|
||||
return [s for s in ssids if 0 < len(s) <= 32]
|
||||
# Check if already busy
|
||||
if WifiService.wifi_busy:
|
||||
print("WifiService: scan_networks() - WiFi is busy, returning empty list")
|
||||
return []
|
||||
|
||||
WifiService.wifi_busy = True
|
||||
try:
|
||||
networks = WifiService._scan_networks_raw(network_module)
|
||||
# Return unique SSIDs, filtering out empty ones and invalid lengths
|
||||
ssids = list(set(n[0].decode() for n in networks if n[0]))
|
||||
return [s for s in ssids if 0 < len(s) <= 32]
|
||||
finally:
|
||||
WifiService.wifi_busy = False
|
||||
|
||||
@staticmethod
|
||||
def get_current_ssid(network_module=None):
|
||||
|
||||
Reference in New Issue
Block a user