76 lines
2.5 KiB
JavaScript
76 lines
2.5 KiB
JavaScript
const resultEl = document.getElementById('result');
|
|
const espIn = document.getElementById('espId');
|
|
const sensorIn = document.getElementById('sensorNumber');
|
|
|
|
document.getElementById('saveBtn').addEventListener('click', async () => {
|
|
const espId = espIn.value.trim();
|
|
const sensorNumber = sensorIn.value.trim();
|
|
if (!espId || !sensorNumber) {
|
|
resultEl.innerText = 'Bitte ESP-ID und Sensornummer eingeben.';
|
|
return;
|
|
}
|
|
resultEl.innerText = 'Speichere...';
|
|
try {
|
|
const r = await fetch('/api/save', {
|
|
method: 'POST',
|
|
headers: {'Content-Type':'application/json'},
|
|
body: JSON.stringify({ espId, sensorNumber })
|
|
});
|
|
const j = await r.json();
|
|
if (j.ok) {
|
|
resultEl.innerHTML = `<strong>Gespeichert:</strong> ESP-ID = ${j.entry.espId}, Sensor = ${j.entry.sensorNumber}`;
|
|
espIn.value = '';
|
|
sensorIn.value = '';
|
|
loadList();
|
|
} else {
|
|
resultEl.innerText = 'Fehler: ' + (j.error || 'Unbekannt');
|
|
}
|
|
} catch {
|
|
resultEl.innerText = 'Netzwerkfehler';
|
|
}
|
|
});
|
|
|
|
async function loadList() {
|
|
const page = document.getElementById('page').value || 1;
|
|
const limit = document.getElementById('limit').value || 25;
|
|
const listEl = document.getElementById('list');
|
|
listEl.innerText = 'Lade...';
|
|
try {
|
|
const r = await fetch(`/api/list?page=${encodeURIComponent(page)}&limit=${encodeURIComponent(limit)}`);
|
|
const j = await r.json();
|
|
if (!j.ok) { listEl.innerText = 'Fehler beim Laden'; return; }
|
|
if (j.items.length === 0) { listEl.innerText = 'Keine Einträge'; return;}
|
|
let html = `<div>Ergebnis: ${j.items.length} von ${j.total} (Seite ${j.page})</div>`;
|
|
html += '<table><thead><tr><th>Datum</th><th>ESP-ID</th><th>Sensor</th><th></th></tr></thead><tbody>';
|
|
j.items.forEach(it => {
|
|
html += `<tr>
|
|
<td>${it.createdAt}</td>
|
|
<td>${it.espId}</td>
|
|
<td>${it.sensorNumber}</td>
|
|
<td><button onclick="deleteEntry('${it._id}')">Löschen</button></td>
|
|
</tr>`;
|
|
});
|
|
html += '</tbody></table>';
|
|
listEl.innerHTML = html;
|
|
} catch {
|
|
listEl.innerText = 'Netzwerkfehler beim Laden';
|
|
}
|
|
}
|
|
|
|
async function deleteEntry(id) {
|
|
if (!confirm('Diesen Eintrag wirklich löschen?')) return;
|
|
try {
|
|
const r = await fetch(`/api/entry/${id}`, { method: 'DELETE' });
|
|
const j = await r.json();
|
|
if (j.ok) {
|
|
loadList();
|
|
} else {
|
|
alert('Fehler beim Löschen');
|
|
}
|
|
} catch {
|
|
alert('Netzwerkfehler');
|
|
}
|
|
}
|
|
|
|
document.getElementById('refreshBtn').addEventListener('click', loadList);
|
|
loadList(); |