diff --git a/dashboard.js b/dashboard.js index ff77d04..9075088 100644 --- a/dashboard.js +++ b/dashboard.js @@ -68,4 +68,44 @@ if(canvasTemp) { } } }); -} \ No newline at end of file +} +// ------------------------------- +// 5️⃣ Données réelles depuis l'API +// ------------------------------- +const API_BASE = "http://172.16.60.12/smarthome-api"; + +async function init() { + const res = await fetch(${API_BASE}/auth.php, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ username: "admin", password: "admin123" }) +}); +const data = await res.json(); +const token = data.token; +await chargerClimat(token); +await chargerSecurite(token); +} + +async function chargerClimat(token) { + const res = await fetch(${API_BASE}/routes/climate.php, { + headers: { "Authorization": "Bearer " + token } + }); + const data = await res.json(); + const last = Array.isArray(data) ? data[data.length - 1] : data; + document.getElementById("temperature").textContent = last.temperature + " °C"; + document.getElementById("humidity").textContent = last.humidity + " %"; + document.getElementById("pressure").textContent = last.pressure + " hPa"; + document.getElementById("luminosity").textContent = last.luminosity + " lux"; +} + +async function chargerSecurite(token) { + const res = await fetch(${API_BASE}/routes/security.php, { + headers: { "Authorization": "Bearer " + token } + }); + const data = await res.json(); + const last = Array.isArray(data) ? data[data.length - 1] : data; + document.getElementById("event_type").textContent = last.event_type; + document.getElementById("location").textContent = last.location; +} + +init(); \ No newline at end of file