diff --git a/html/launcher.php b/html/launcher.php index c6dc756..53bdbaa 100755 --- a/html/launcher.php +++ b/html/launcher.php @@ -1202,38 +1202,54 @@ if ($type == "wifi_forget") { if ($type == "wifi_scan") { - // Perform live WiFi scan instead of reading stale CSV file - $output = shell_exec('nmcli -f SSID,SIGNAL,SECURITY device wifi list ifname wlan0 2>/dev/null'); - - // Initialize an array to hold the JSON data $jsonData = []; - if ($output) { - // Split the output into lines - $lines = explode("\n", trim($output)); + // Check if wlan0 is in hotspot mode — if so, use cached CSV from boot scan + // (live scan is impossible while wlan0 is serving the hotspot) + $wlan0_connection = trim(shell_exec("nmcli -t -f GENERAL.CONNECTION device show wlan0 2>/dev/null | cut -d: -f2")); + $is_hotspot = (strpos(strtolower($wlan0_connection), 'hotspot') !== false); - // Skip the header line and process each network - for ($i = 1; $i < count($lines); $i++) { - $line = trim($lines[$i]); - if (empty($line)) continue; + if ($is_hotspot) { + // Read cached scan from boot (wifi_list.csv) + $csv_path = '/var/www/nebuleair_pro_4g/wifi_list.csv'; + if (file_exists($csv_path)) { + $lines = file($csv_path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); + // Skip header line (SSID,SIGNAL,SECURITY) + for ($i = 1; $i < count($lines); $i++) { + $parts = str_getcsv($lines[$i]); + if (count($parts) >= 2 && !empty(trim($parts[0]))) { + $jsonData[] = [ + 'SSID' => trim($parts[0]), + 'SIGNAL' => trim($parts[1]), + 'SECURITY' => isset($parts[2]) ? trim($parts[2]) : '--', + 'cached' => true + ]; + } + } + } + } else { + // Live scan (wlan0 is free) + $output = shell_exec('timeout 10 nmcli -f SSID,SIGNAL,SECURITY device wifi list ifname wlan0 2>/dev/null'); - // Split by multiple spaces (nmcli uses column formatting) - $parts = preg_split('/\s{2,}/', $line, 3); + if ($output) { + $lines = explode("\n", trim($output)); + for ($i = 1; $i < count($lines); $i++) { + $line = trim($lines[$i]); + if (empty($line)) continue; - if (count($parts) >= 2) { - $jsonData[] = [ - 'SSID' => trim($parts[0]), - 'SIGNAL' => trim($parts[1]), - 'SECURITY' => isset($parts[2]) ? trim($parts[2]) : '--' - ]; + $parts = preg_split('/\s{2,}/', $line, 3); + if (count($parts) >= 2) { + $jsonData[] = [ + 'SSID' => trim($parts[0]), + 'SIGNAL' => trim($parts[1]), + 'SECURITY' => isset($parts[2]) ? trim($parts[2]) : '--' + ]; + } } } } - // Set the content type to JSON header('Content-Type: application/json'); - - // Convert the array to JSON format and output it echo json_encode($jsonData, JSON_PRETTY_PRINT); } diff --git a/html/wifi.html b/html/wifi.html index cbbc6d0..cfd19fd 100755 --- a/html/wifi.html +++ b/html/wifi.html @@ -154,6 +154,9 @@