diff --git a/gui/app.py b/gui/app.py index 607abf1..89128ac 100644 --- a/gui/app.py +++ b/gui/app.py @@ -252,6 +252,7 @@ def scanner_navigation_guard(): allowed_endpoints = { "index", "api_status", + "api_network_wifi_devices", "scanner_stop", "api_gps_test", "gps_test_stop", @@ -367,6 +368,31 @@ def get_gps_test_status(): "error": service.error } +def network_actions_busy(): + + with scanner_lock: + + if ( + scanner_starting + or + ( + scanner_process is not None + and + scanner_process.poll() is None + ) + ): + return True + + with gps_test_lock: + + return ( + gps_test_service is not None + and + gps_test_service.thread is not None + and + gps_test_service.thread.is_alive() + ) + def get_scanner_status(): global scanner_health @@ -488,6 +514,104 @@ def read_scanner_output(process): pass +def get_nm_wifi_devices(): + + command = [ + "nmcli", + "--terse", + "--escape", + "yes", + "--fields", + "DEVICE,TYPE,STATE", + "device", + "status" + ] + + try: + + result = subprocess.run( + command, + capture_output=True, + text=True, + timeout=10, + env={ + **os.environ, + "LC_ALL": "C" + } + ) + + except Exception as error: + + return [], f"NetworkManager query failed: {error}" + + if result.returncode != 0: + + error = ( + result.stderr.strip() + or + f"nmcli exited with code {result.returncode}" + ) + + return [], error + + devices = [] + + for line in result.stdout.splitlines(): + + parts = line.split(":", 2) + + if len(parts) != 3: + continue + + device, device_type, state = parts + + if device_type != "wifi": + continue + + devices.append( + { + "device": device, + "state": state, + "managed": None + } + ) + + for device in devices: + + try: + + managed_result = subprocess.run( + [ + "nmcli", + "--get-values", + "GENERAL.NM-MANAGED", + "device", + "show", + device["device"] + ], + capture_output=True, + text=True, + timeout=10, + env={ + **os.environ, + "LC_ALL": "C" + } + ) + + if managed_result.returncode == 0: + + value = managed_result.stdout.strip().lower() + + if value == "yes": + device["managed"] = True + + elif value == "no": + device["managed"] = False + + except Exception: + pass + + return devices, None def get_wifi_interfaces(): @@ -2062,6 +2186,7 @@ def api_status(): status = get_scanner_status() gps_test = get_gps_test_status() + busy = network_actions_busy() with scanner_lock: @@ -2073,6 +2198,7 @@ def api_status(): { "status": status, "gps_test": gps_test, + "network_actions_busy": busy, "output": output } ) @@ -2231,6 +2357,88 @@ def api_gps_test(): get_gps_test_status() ) +@app.route( + "/api/network/wifi-devices" +) +def api_network_wifi_devices(): + + devices, error = get_nm_wifi_devices() + + if error: + + return jsonify( + { + "ok": False, + "devices": [], + "selected_device": None, + "error": error + } + ), 503 + + for device in devices: + + state = device["state"] + managed = device["managed"] + + device["available"] = False + device["reason"] = None + + if managed is False: + + device["reason"] = ( + "NetworkManager is not managing this device." + ) + + elif managed is None: + + device["reason"] = ( + "Could not determine whether " + "NetworkManager manages this device." + ) + + elif state == "unavailable": + + device["reason"] = ( + "Wi-Fi device is unavailable. " + "Check hardware switch, rfkill or driver." + ) + + elif state == "unmanaged": + + device["reason"] = ( + "Wi-Fi device is unmanaged." + ) + + elif state not in ("connected", "disconnected"): + + device["reason"] = ( + f"Wi-Fi device is not ready. State: {state}" + ) + + else: + + device["available"] = True + + selected_device = None + + if len(devices) == 1: + + selected_device = devices[0]["device"] + + return jsonify( + { + "ok": True, + "devices": devices, + "selected_device": selected_device, + "selection_required": len(devices) > 1, + "error": ( + "No Wi-Fi interfaces found." + if not devices + else None + ) + } + ) + @app.route( "/api/wifi/channels" )