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

View File

@@ -154,6 +154,9 @@
<button class="btn btn-primary btn-sm" onclick="wifi_scan()">Scan</button>
</div>
<div class="card-body">
<div id="wifi-scan-cache-notice" class="alert alert-info py-2 mb-2" style="display:none; font-size: 0.85rem;">
Scan effectue au demarrage du capteur (scan live indisponible en mode hotspot).
</div>
<div id="wifi-scan-empty" class="text-center text-muted py-3">
Cliquez sur "Scan" pour rechercher les reseaux WiFi.
</div>
@@ -510,6 +513,11 @@ function get_internet(){
return;
}
// Show cached scan notice if in hotspot mode
const isCached = response.length > 0 && response[0].cached;
const cacheNotice = document.getElementById('wifi-scan-cache-notice');
if (cacheNotice) cacheNotice.style.display = isCached ? '' : 'none';
document.getElementById('wifi-scan-empty').style.display = 'none';
document.getElementById('wifi-scan-table').style.display = '';
@@ -584,6 +592,8 @@ function get_internet(){
document.getElementById('card-connection-info').style.display = 'none';
document.getElementById('card-hotspot-info').style.display = '';
document.getElementById('card-wifi-scan').style.display = '';
// Auto-load cached scan results in hotspot mode
wifi_scan();
} else {
WIFI_statusElement.textContent = "Unknown";
WIFI_statusElement.className = "badge text-bg-secondary";