Actualiser dashboard.js
This commit is contained in:
40
dashboard.js
40
dashboard.js
@@ -69,3 +69,43 @@ if(canvasTemp) {
|
||||
}
|
||||
});
|
||||
}
|
||||
// -------------------------------
|
||||
// 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();
|
||||
Reference in New Issue
Block a user