Add websocket wifi updates
This commit is contained in:
parent
c443fce24f
commit
1477d909b0
11 changed files with 755 additions and 10 deletions
|
|
@ -4,7 +4,9 @@ const appState = {
|
|||
hotspotEnabled: true,
|
||||
autoScrollLogs: true,
|
||||
ws: null,
|
||||
wifiWs: null,
|
||||
reconnectAttempts: 0,
|
||||
wifiReconnectAttempts: 0,
|
||||
maxReconnectAttempts: 5,
|
||||
connectedSSID: null,
|
||||
networks: [],
|
||||
|
|
@ -28,8 +30,9 @@ async function initializeApp() {
|
|||
loadLogs()
|
||||
]);
|
||||
|
||||
// Set up WebSocket for real-time logs
|
||||
// Set up WebSockets for real-time updates
|
||||
connectWebSocket();
|
||||
connectWifiWebSocket();
|
||||
|
||||
// Start periodic updates
|
||||
startPeriodicUpdates();
|
||||
|
|
@ -72,8 +75,20 @@ async function scanWifi() {
|
|||
showToast('success', 'Scan WiFi', `${networks.length} réseau(x) trouvé(s)`);
|
||||
} catch (error) {
|
||||
console.error('Error scanning WiFi:', error);
|
||||
wifiList.innerHTML = '<div class="wifi-item loading"><span style="color: var(--danger-color);">Erreur lors du scan</span></div>';
|
||||
showToast('error', 'Erreur', 'Échec du scan WiFi');
|
||||
|
||||
// Fallback to cached networks
|
||||
try {
|
||||
const fallbackResponse = await fetch('/api/wifi/networks');
|
||||
const cachedNetworks = await fallbackResponse.json();
|
||||
|
||||
appState.networks = cachedNetworks;
|
||||
displayWifiNetworks(cachedNetworks);
|
||||
showToast('warning', 'Scan échoué', `Affichage des réseaux en cache (${cachedNetworks.length})`);
|
||||
} catch (fallbackError) {
|
||||
console.error('Error loading cached networks:', fallbackError);
|
||||
wifiList.innerHTML = '<div class="wifi-item loading"><span style="color: var(--danger-color);">Erreur lors du scan</span></div>';
|
||||
showToast('error', 'Erreur', 'Échec du scan WiFi');
|
||||
}
|
||||
} finally {
|
||||
if (scanBtn) {
|
||||
scanBtn.disabled = false;
|
||||
|
|
@ -491,6 +506,108 @@ function connectWebSocket() {
|
|||
}
|
||||
}
|
||||
|
||||
function connectWifiWebSocket() {
|
||||
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||
const wsUrl = `${protocol}//${window.location.host}/ws/wifi`;
|
||||
|
||||
try {
|
||||
appState.wifiWs = new WebSocket(wsUrl);
|
||||
|
||||
appState.wifiWs.onopen = function() {
|
||||
console.log('WiFi WebSocket connected');
|
||||
appState.wifiReconnectAttempts = 0;
|
||||
};
|
||||
|
||||
appState.wifiWs.onmessage = function(event) {
|
||||
try {
|
||||
const msg = JSON.parse(event.data);
|
||||
handleWifiEvent(msg);
|
||||
} catch (error) {
|
||||
console.error('Error parsing WiFi WebSocket message:', error);
|
||||
}
|
||||
};
|
||||
|
||||
appState.wifiWs.onerror = function(error) {
|
||||
console.error('WiFi WebSocket error:', error);
|
||||
};
|
||||
|
||||
appState.wifiWs.onclose = function() {
|
||||
console.log('WiFi WebSocket disconnected');
|
||||
|
||||
// Attempt to reconnect
|
||||
if (appState.wifiReconnectAttempts < appState.maxReconnectAttempts) {
|
||||
appState.wifiReconnectAttempts++;
|
||||
setTimeout(connectWifiWebSocket, 5000);
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error creating WiFi WebSocket:', error);
|
||||
}
|
||||
}
|
||||
|
||||
function handleWifiEvent(event) {
|
||||
console.log('WiFi event received:', event.type, event);
|
||||
|
||||
switch (event.type) {
|
||||
case 'scan_update':
|
||||
handleScanUpdate(event.data);
|
||||
break;
|
||||
case 'state_change':
|
||||
handleStateChange(event.data);
|
||||
break;
|
||||
case 'signal_update':
|
||||
handleSignalUpdate(event.data);
|
||||
break;
|
||||
default:
|
||||
console.warn('Unknown WiFi event type:', event.type);
|
||||
}
|
||||
}
|
||||
|
||||
function handleScanUpdate(data) {
|
||||
// Update the network list in real-time
|
||||
appState.networks = data.networks;
|
||||
displayWifiNetworks(data.networks);
|
||||
console.log(`Scan update: ${data.networks.length} network(s) found`);
|
||||
}
|
||||
|
||||
function handleStateChange(data) {
|
||||
// Update WiFi status badge
|
||||
const wifiStatus = document.getElementById('wifiStatus');
|
||||
const wifiDot = wifiStatus.querySelector('.status-dot');
|
||||
const wifiText = wifiStatus.querySelector('.status-text');
|
||||
|
||||
if (data.state === 'connected') {
|
||||
wifiDot.className = 'status-dot active';
|
||||
wifiText.textContent = `Connecté: ${data.ssid}`;
|
||||
appState.connectedSSID = data.ssid;
|
||||
|
||||
// Refresh network list to show connected network
|
||||
if (appState.networks.length > 0) {
|
||||
displayWifiNetworks(appState.networks);
|
||||
}
|
||||
} else if (data.state === 'disconnected') {
|
||||
wifiDot.className = 'status-dot offline';
|
||||
wifiText.textContent = 'Déconnecté';
|
||||
appState.connectedSSID = null;
|
||||
|
||||
// Refresh network list to remove connected highlighting
|
||||
if (appState.networks.length > 0) {
|
||||
displayWifiNetworks(appState.networks);
|
||||
}
|
||||
} else if (data.state === 'connecting') {
|
||||
wifiDot.className = 'status-dot';
|
||||
wifiText.textContent = `Connexion à ${data.ssid}...`;
|
||||
}
|
||||
|
||||
console.log(`WiFi state changed: ${data.previous_state} → ${data.state}`, data.ssid);
|
||||
}
|
||||
|
||||
function handleSignalUpdate(data) {
|
||||
// Update signal strength display if needed
|
||||
console.log(`Signal update for ${data.ssid}: ${data.signal}/5 (${data.dbm} dBm)`);
|
||||
// Could update the network list to reflect new signal strength
|
||||
}
|
||||
|
||||
// ===== Utility Functions =====
|
||||
|
||||
function generateSignalBars(strength) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue