2026-05-12 16:40:39 +02:00
|
|
|
let chart = null;
|
2026-05-12 23:24:31 +02:00
|
|
|
document.addEventListener("DOMContentLoaded", init);
|
2026-05-12 16:40:39 +02:00
|
|
|
|
2026-05-12 23:24:31 +02:00
|
|
|
function showChartError() {
|
2026-05-12 16:40:39 +02:00
|
|
|
|
|
|
|
|
}
|
2026-05-11 23:17:43 +02:00
|
|
|
|
|
|
|
|
async function loadData() {
|
2026-05-12 16:40:39 +02:00
|
|
|
const start = document.getElementById("date-start").value;
|
|
|
|
|
const end = document.getElementById("date-end").value;
|
|
|
|
|
const granularity = document.getElementById("granularity").value;
|
2026-05-12 22:30:21 +02:00
|
|
|
const device_eui = document.getElementById("device").value;
|
2026-05-12 16:40:39 +02:00
|
|
|
|
2026-05-12 23:24:31 +02:00
|
|
|
if (!device_eui || !start || !end) return;
|
2026-05-12 16:40:39 +02:00
|
|
|
|
2026-05-12 23:24:31 +02:00
|
|
|
try {
|
|
|
|
|
const points = await fetchConsumption({
|
|
|
|
|
device_eui: device_eui,
|
|
|
|
|
start: start,
|
|
|
|
|
end: end,
|
|
|
|
|
granularity: granularity,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (chart) { chart.destroy(); }
|
|
|
|
|
|
|
|
|
|
chart = renderChart(points, granularity);
|
|
|
|
|
updateKPIs(points);
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (chart) { chart.destroy(); }
|
|
|
|
|
showMessage("Impossible de charger les données : " + err.message);
|
|
|
|
|
updateKPIs(null);
|
2026-05-12 16:40:39 +02:00
|
|
|
}
|
2026-05-12 17:15:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function updateKPIs(points) {
|
|
|
|
|
const totalDisplay = document.getElementById("kpi-total");
|
|
|
|
|
const avgDisplay = document.getElementById("kpi-avg");
|
|
|
|
|
const countDisplay = document.getElementById("kpi-count");
|
|
|
|
|
|
|
|
|
|
if (!points || points.length === 0) {
|
2026-05-12 23:24:31 +02:00
|
|
|
totalDisplay.textContent = "-";
|
|
|
|
|
avgDisplay.textContent = "-";
|
|
|
|
|
countDisplay.textContent = "-";
|
2026-05-12 17:15:42 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const totalValue = points.reduce((acc, p) => acc + p.delta_m3, 0);
|
|
|
|
|
const avgValue = totalValue / points.length;
|
|
|
|
|
|
|
|
|
|
totalDisplay.textContent = totalValue.toFixed(3);
|
|
|
|
|
avgDisplay.textContent = avgValue.toFixed(3);
|
|
|
|
|
countDisplay.textContent = points.length;
|
2026-05-12 16:40:39 +02:00
|
|
|
}
|
2026-05-12 22:30:21 +02:00
|
|
|
|
|
|
|
|
async function loadDevices() {
|
|
|
|
|
const selectDeviceElmt = document.getElementById("device");
|
|
|
|
|
|
2026-05-12 23:24:31 +02:00
|
|
|
try {
|
|
|
|
|
const devices = await fetchDevices();
|
|
|
|
|
if (!devices.length) {
|
|
|
|
|
selectDeviceElmt.disabled = true;
|
|
|
|
|
selectDeviceElmt.add(new Option("Aucun appareil disponible", ""));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-05-12 22:30:21 +02:00
|
|
|
|
2026-05-12 23:24:31 +02:00
|
|
|
devices.forEach((device) => {
|
|
|
|
|
selectDeviceElmt.add(new Option(device.device_eui));
|
|
|
|
|
});
|
2026-05-12 22:30:21 +02:00
|
|
|
|
2026-05-12 23:24:31 +02:00
|
|
|
selectDeviceElmt.value = devices[0].device_eui;
|
|
|
|
|
|
|
|
|
|
} catch(err) {
|
|
|
|
|
selectDeviceElmt.disabled = true;
|
|
|
|
|
selectDeviceElmt.add(new Option("Erreur de chargement", ""));
|
|
|
|
|
console.error("loadDevices :", err.message);
|
|
|
|
|
}
|
2026-05-12 22:30:21 +02:00
|
|
|
}
|
2026-05-12 23:24:31 +02:00
|
|
|
|
|
|
|
|
async function init() {
|
|
|
|
|
let start = new Date();
|
|
|
|
|
start.setDate(start.getDate() - 7);
|
|
|
|
|
const end = new Date();
|
|
|
|
|
|
|
|
|
|
document.getElementById("date-start").valueAsDate = start;
|
|
|
|
|
document.getElementById("date-end").valueAsDate = end;
|
|
|
|
|
document.getElementById("btn-refresh").addEventListener("click", loadData);
|
|
|
|
|
|
|
|
|
|
await loadDevices();
|
|
|
|
|
|
|
|
|
|
const device_eui = document.getElementById("device").value;
|
|
|
|
|
if (device_eui) await loadData();
|
|
|
|
|
}
|