Hotspot: scan WiFi depuis cache CSV + timeout scan live + auto-load

En mode hotspot, le scan live est impossible (wlan0 occupée).
Utilise wifi_list.csv (scan au boot) avec notice explicative.
Ajout timeout 10s sur le scan live pour eviter blocage.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
PaulVua
2026-03-17 17:54:44 +01:00
parent a0f8b4b8eb
commit 83d854b596
2 changed files with 48 additions and 22 deletions

View File

@@ -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);
}