115 lines
4.6 KiB
JavaScript
115 lines
4.6 KiB
JavaScript
// --- VARIABLES D'ÉTAT ---
|
|
let isOnline = true;
|
|
let lastHeartbeat = Date.now();
|
|
const TIMEOUT_MS = 5000; // 5 secondes pour les tests
|
|
|
|
// --- ÉLÉMENTS DU DOM ---
|
|
const statusDot = document.getElementById('system-status');
|
|
const statusText = document.getElementById('status-text');
|
|
|
|
const tempEl = document.getElementById('temp-val');
|
|
const humEl = document.getElementById('hum-val');
|
|
|
|
const apiTempEl = document.getElementById('api-temp');
|
|
const apiDescEl = document.getElementById('api-desc');
|
|
const apiIconEl = document.getElementById('api-icon');
|
|
|
|
// =================================================================
|
|
// --- NOUVEAU : GESTION DE LA VRAIE API MÉTÉO (OPEN-METEO) ---
|
|
// =================================================================
|
|
|
|
// 1. Traduction des codes météo de l'API en Icônes et Textes
|
|
function getWeatherDetails(code) {
|
|
if (code === 0) return { desc: "Dégagé", icon: "bi-brightness-high", color: "text-warning" };
|
|
if (code === 1 || code === 2 || code === 3) return { desc: "Nuageux", icon: "bi-cloud", color: "text-light" };
|
|
if (code >= 45 && code <= 48) return { desc: "Brouillard", icon: "bi-cloud-haze", color: "text-secondary" };
|
|
if (code >= 51 && code <= 67) return { desc: "Pluie", icon: "bi-cloud-rain", color: "text-info" };
|
|
if (code >= 71 && code <= 77) return { desc: "Neige", icon: "bi-cloud-snow", color: "text-white" };
|
|
if (code >= 95) return { desc: "Orage", icon: "bi-cloud-lightning", color: "text-danger" };
|
|
return { desc: "Inconnu", icon: "bi-thermometer", color: "text-muted" };
|
|
}
|
|
|
|
// 2. Fonction qui va chercher les données sur Internet
|
|
async function fetchRealWeather() {
|
|
try {
|
|
// --- METS LES COORDONNÉES DE TA VILLE ICI ---
|
|
const lat = 48.8566; // Latitude (Ex: Paris)
|
|
const lon = 2.3522; // Longitude (Ex: Paris)
|
|
|
|
// L'URL magique de l'API Open-Meteo
|
|
const url = `https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lon}¤t=temperature_2m,weather_code`;
|
|
|
|
// On lance la requête
|
|
const response = await fetch(url);
|
|
const data = await response.json(); // On convertit la réponse en JSON
|
|
|
|
// On extrait la température et le code météo
|
|
const temp = data.current.temperature_2m;
|
|
const code = data.current.weather_code;
|
|
|
|
// On récupère la bonne icône et le bon texte
|
|
const weather = getWeatherDetails(code);
|
|
|
|
// On met à jour l'interface HTML !
|
|
apiTempEl.innerText = temp + "°C";
|
|
apiDescEl.innerText = weather.desc;
|
|
apiIconEl.className = "bi " + weather.icon + " " + weather.color + " display-4 me-3";
|
|
|
|
} catch (error) {
|
|
console.error("Erreur avec l'API Météo:", error);
|
|
apiDescEl.innerText = "Erreur Web";
|
|
apiIconEl.className = "bi bi-wifi-off text-danger display-4 me-3";
|
|
}
|
|
}
|
|
|
|
// =================================================================
|
|
|
|
// --- FONCTION DE MISE À JOUR DES DONNÉES LOCALES (Ton DHT11) ---
|
|
function fetchLocalData() {
|
|
if (!isOnline) return;
|
|
|
|
// Simulation de ton capteur DHT11 local
|
|
tempEl.innerText = (22 + (Math.random() * 2 - 1)).toFixed(1);
|
|
humEl.innerText = Math.floor(45 + Math.random() * 5);
|
|
|
|
lastHeartbeat = Date.now();
|
|
}
|
|
|
|
// --- FONCTION WATCHDOG (LE HEARTBEAT) ---
|
|
function checkSystemStatus() {
|
|
if (Date.now() - lastHeartbeat > TIMEOUT_MS) {
|
|
statusDot.className = "status-dot rounded-circle pulse-offline";
|
|
statusText.innerText = "HORS LIGNE";
|
|
statusText.style.color = "#ef4444";
|
|
} else {
|
|
statusDot.className = "status-dot rounded-circle pulse-online";
|
|
statusText.innerText = "EN LIGNE";
|
|
statusText.style.color = "#e2e8f0";
|
|
}
|
|
}
|
|
|
|
// --- LANCEMENT DES PROGRAMMES ---
|
|
|
|
// 1. Lancer la météo Web une première fois tout de suite
|
|
fetchRealWeather();
|
|
|
|
// 2. Les boucles automatiques
|
|
setInterval(fetchLocalData, 3000); // Mise à jour de ton capteur local toutes les 3s
|
|
setInterval(checkSystemStatus, 1000); // Surveillance de la connexion
|
|
|
|
// ATTENTION: On ne met à jour la météo Web que toutes les 15 minutes (900 000 ms)
|
|
// pour ne pas saturer l'API gratuite et se faire bloquer !
|
|
setInterval(fetchRealWeather, 900000);
|
|
|
|
// --- GESTION DU BOUTON DE TEST ---
|
|
document.getElementById('btn-test').addEventListener('click', function () {
|
|
isOnline = !isOnline;
|
|
if (isOnline) {
|
|
this.innerHTML = '<i class="bi bi-wifi-off"></i> Simuler Coupure';
|
|
this.classList.replace('btn-outline-success', 'btn-outline-danger');
|
|
lastHeartbeat = Date.now();
|
|
} else {
|
|
this.innerHTML = '<i class="bi bi-wifi"></i> Rétablir Connexion';
|
|
this.classList.replace('btn-outline-danger', 'btn-outline-success');
|
|
}
|
|
}); |