2026-04-27 06:01:00 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
2026-05-02 20:54:53 +02:00
|
|
|
/* ── Config from Flask ── */
|
|
|
|
|
const ROOT = document.getElementById('mirror-root');
|
|
|
|
|
const POLL_MS = parseInt(ROOT.dataset.pollMs || '2500', 10);
|
|
|
|
|
const SENSORS = JSON.parse(ROOT.dataset.sensors || '[]');
|
2026-04-27 06:01:00 +02:00
|
|
|
|
2026-05-02 20:54:53 +02:00
|
|
|
/* ── State ── */
|
|
|
|
|
let lastLogCount = 0;
|
|
|
|
|
let alertsSeen = 0;
|
|
|
|
|
let chartData = []; // {ts, v} for temp channel (field_index 0)
|
|
|
|
|
let firstPoll = true;
|
2026-04-27 06:01:00 +02:00
|
|
|
|
2026-05-02 20:54:53 +02:00
|
|
|
/* ══════════════════════════════
|
2026-04-27 06:01:00 +02:00
|
|
|
CLOCK
|
2026-05-02 20:54:53 +02:00
|
|
|
══════════════════════════════ */
|
|
|
|
|
const DAYS = ['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'];
|
|
|
|
|
const MONTHS = ['Januar','Februar','März','April','Mai','Juni',
|
|
|
|
|
'Juli','August','September','Oktober','November','Dezember'];
|
2026-04-27 06:01:00 +02:00
|
|
|
|
2026-05-02 20:54:53 +02:00
|
|
|
function tickClock() {
|
|
|
|
|
const n = new Date();
|
|
|
|
|
const pad = x => String(x).padStart(2, '0');
|
|
|
|
|
document.getElementById('clock-time').textContent =
|
|
|
|
|
`${pad(n.getHours())}:${pad(n.getMinutes())}:${pad(n.getSeconds())}`;
|
|
|
|
|
document.getElementById('clock-date').textContent =
|
|
|
|
|
`${DAYS[n.getDay()]}, ${n.getDate()}. ${MONTHS[n.getMonth()]} ${n.getFullYear()}`;
|
2026-04-27 06:01:00 +02:00
|
|
|
}
|
|
|
|
|
setInterval(tickClock, 1000);
|
|
|
|
|
tickClock();
|
|
|
|
|
|
2026-05-02 20:54:53 +02:00
|
|
|
/* ══════════════════════════════
|
2026-04-27 06:01:00 +02:00
|
|
|
CANVAS CHART
|
2026-05-02 20:54:53 +02:00
|
|
|
══════════════════════════════ */
|
2026-04-27 06:01:00 +02:00
|
|
|
const canvas = document.getElementById('chart');
|
|
|
|
|
const ctx = canvas.getContext('2d');
|
|
|
|
|
|
|
|
|
|
function resizeCanvas() {
|
2026-05-02 20:54:53 +02:00
|
|
|
canvas.width = canvas.parentElement.clientWidth - 36;
|
|
|
|
|
canvas.height = 130;
|
2026-04-27 06:01:00 +02:00
|
|
|
}
|
|
|
|
|
window.addEventListener('resize', () => { resizeCanvas(); drawChart(); });
|
|
|
|
|
resizeCanvas();
|
|
|
|
|
|
|
|
|
|
function drawChart() {
|
|
|
|
|
const W = canvas.width, H = canvas.height;
|
|
|
|
|
ctx.clearRect(0, 0, W, H);
|
|
|
|
|
|
2026-05-02 20:54:53 +02:00
|
|
|
const pts = chartData.slice(-80).map(d => d.v);
|
|
|
|
|
|
|
|
|
|
if (pts.length < 2) {
|
|
|
|
|
ctx.fillStyle = 'rgba(228,233,245,.15)';
|
|
|
|
|
ctx.font = `11px 'JetBrains Mono', monospace`;
|
|
|
|
|
ctx.fillText('Warte auf Messdaten …', 10, H / 2 + 4);
|
2026-04-27 06:01:00 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-02 20:54:53 +02:00
|
|
|
const mn = Math.min(...pts);
|
|
|
|
|
const mx = Math.max(...pts);
|
|
|
|
|
const rng = (mx - mn) || 1;
|
|
|
|
|
const sx = W / (pts.length - 1);
|
|
|
|
|
const yp = v => H - ((v - mn) / rng) * H * 0.80 - H * 0.10;
|
2026-04-27 06:01:00 +02:00
|
|
|
|
2026-05-02 20:54:53 +02:00
|
|
|
/* grid */
|
2026-04-27 06:01:00 +02:00
|
|
|
ctx.strokeStyle = 'rgba(255,255,255,.04)';
|
|
|
|
|
ctx.lineWidth = 1;
|
2026-05-02 20:54:53 +02:00
|
|
|
ctx.font = `9px 'JetBrains Mono', monospace`;
|
2026-04-27 06:01:00 +02:00
|
|
|
for (let i = 0; i <= 4; i++) {
|
2026-05-02 20:54:53 +02:00
|
|
|
const y = H - (i / 4) * H * 0.80 - H * 0.10;
|
2026-04-27 06:01:00 +02:00
|
|
|
ctx.beginPath(); ctx.moveTo(0, y); ctx.lineTo(W, y); ctx.stroke();
|
2026-05-02 20:54:53 +02:00
|
|
|
ctx.fillStyle = 'rgba(228,233,245,.18)';
|
|
|
|
|
ctx.fillText((mn + (i / 4) * rng).toFixed(1) + '°', 4, y - 3);
|
2026-04-27 06:01:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* gradient fill */
|
2026-05-02 20:54:53 +02:00
|
|
|
const grd = ctx.createLinearGradient(0, 0, 0, H);
|
|
|
|
|
grd.addColorStop(0, 'rgba(59,130,246,.2)');
|
|
|
|
|
grd.addColorStop(1, 'rgba(59,130,246,.0)');
|
2026-04-27 06:01:00 +02:00
|
|
|
|
|
|
|
|
ctx.beginPath();
|
|
|
|
|
pts.forEach((v, i) => {
|
2026-05-02 20:54:53 +02:00
|
|
|
const x = i * sx, y = yp(v);
|
2026-04-27 06:01:00 +02:00
|
|
|
i === 0 ? ctx.moveTo(x, y) : ctx.lineTo(x, y);
|
|
|
|
|
});
|
2026-05-02 20:54:53 +02:00
|
|
|
ctx.lineTo((pts.length - 1) * sx, H);
|
2026-04-27 06:01:00 +02:00
|
|
|
ctx.lineTo(0, H);
|
|
|
|
|
ctx.closePath();
|
2026-05-02 20:54:53 +02:00
|
|
|
ctx.fillStyle = grd;
|
2026-04-27 06:01:00 +02:00
|
|
|
ctx.fill();
|
|
|
|
|
|
|
|
|
|
/* line */
|
|
|
|
|
ctx.beginPath();
|
|
|
|
|
pts.forEach((v, i) => {
|
2026-05-02 20:54:53 +02:00
|
|
|
const x = i * sx, y = yp(v);
|
2026-04-27 06:01:00 +02:00
|
|
|
i === 0 ? ctx.moveTo(x, y) : ctx.lineTo(x, y);
|
|
|
|
|
});
|
2026-05-02 20:54:53 +02:00
|
|
|
ctx.strokeStyle = '#3b82f6';
|
|
|
|
|
ctx.lineWidth = 2;
|
|
|
|
|
ctx.shadowColor = 'rgba(59,130,246,.5)';
|
|
|
|
|
ctx.shadowBlur = 10;
|
2026-04-27 06:01:00 +02:00
|
|
|
ctx.stroke();
|
|
|
|
|
ctx.shadowBlur = 0;
|
|
|
|
|
|
2026-05-02 20:54:53 +02:00
|
|
|
/* last dot */
|
2026-04-27 06:01:00 +02:00
|
|
|
const lv = pts[pts.length - 1];
|
2026-05-02 20:54:53 +02:00
|
|
|
const lx = (pts.length - 1) * sx;
|
|
|
|
|
const ly = yp(lv);
|
2026-04-27 06:01:00 +02:00
|
|
|
ctx.beginPath();
|
2026-05-02 20:54:53 +02:00
|
|
|
ctx.arc(lx, ly, 4, 0, Math.PI * 2);
|
|
|
|
|
ctx.fillStyle = '#3b82f6';
|
|
|
|
|
ctx.shadowColor = '#3b82f6';
|
|
|
|
|
ctx.shadowBlur = 14;
|
2026-04-27 06:01:00 +02:00
|
|
|
ctx.fill();
|
|
|
|
|
ctx.shadowBlur = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-02 20:54:53 +02:00
|
|
|
/* ══════════════════════════════
|
2026-04-27 06:01:00 +02:00
|
|
|
SENSOR TILES
|
2026-05-02 20:54:53 +02:00
|
|
|
══════════════════════════════ */
|
2026-04-27 06:01:00 +02:00
|
|
|
function updateSensorTiles(values) {
|
|
|
|
|
SENSORS.forEach((s, i) => {
|
2026-05-02 20:54:53 +02:00
|
|
|
const idx = s.field_index;
|
2026-04-27 06:01:00 +02:00
|
|
|
if (idx >= values.length) return;
|
|
|
|
|
|
|
|
|
|
const v = values[idx];
|
|
|
|
|
const tile = document.getElementById(`sc-${i}`);
|
|
|
|
|
const valEl= document.getElementById(`sv-${i}`);
|
|
|
|
|
if (!tile || !valEl) return;
|
|
|
|
|
|
2026-05-02 20:54:53 +02:00
|
|
|
const fmt = Number.isInteger(v) ? String(v) : v.toFixed(1);
|
|
|
|
|
valEl.textContent = `${fmt}${s.unit ? ' ' + s.unit : ''}`;
|
2026-04-27 06:01:00 +02:00
|
|
|
|
|
|
|
|
tile.querySelectorAll('.sensor-alert-badge').forEach(b => b.remove());
|
|
|
|
|
tile.classList.remove('breach-high', 'breach-low');
|
|
|
|
|
|
|
|
|
|
const isHigh = s.notify_on_high && s.threshold_high != null && v > s.threshold_high;
|
|
|
|
|
const isLow = s.notify_on_low && s.threshold_low != null && v < s.threshold_low;
|
|
|
|
|
|
|
|
|
|
if (isHigh) {
|
|
|
|
|
tile.classList.add('breach-high');
|
|
|
|
|
const b = document.createElement('span');
|
|
|
|
|
b.className = 'sensor-alert-badge';
|
2026-05-02 20:54:53 +02:00
|
|
|
b.textContent = '↑ Überschritten';
|
|
|
|
|
tile.querySelector('.sensor-meta').appendChild(b);
|
2026-04-27 06:01:00 +02:00
|
|
|
} else if (isLow) {
|
|
|
|
|
tile.classList.add('breach-low');
|
|
|
|
|
const b = document.createElement('span');
|
|
|
|
|
b.className = 'sensor-alert-badge low';
|
2026-05-02 20:54:53 +02:00
|
|
|
b.textContent = '↓ Unterschritten';
|
|
|
|
|
tile.querySelector('.sensor-meta').appendChild(b);
|
2026-04-27 06:01:00 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-02 20:54:53 +02:00
|
|
|
/* ══════════════════════════════
|
2026-04-27 06:01:00 +02:00
|
|
|
TABS
|
2026-05-02 20:54:53 +02:00
|
|
|
══════════════════════════════ */
|
2026-04-27 06:01:00 +02:00
|
|
|
function switchTab(name) {
|
2026-05-02 20:54:53 +02:00
|
|
|
document.querySelectorAll('.tab').forEach(t =>
|
|
|
|
|
t.classList.toggle('active', t.dataset.tab === name));
|
|
|
|
|
document.querySelectorAll('.tab-pane').forEach(p =>
|
|
|
|
|
p.classList.toggle('active', p.id === `pane-${name}`));
|
2026-04-27 06:01:00 +02:00
|
|
|
if (name === 'alerts') {
|
2026-05-02 20:54:53 +02:00
|
|
|
const b = document.getElementById('alert-badge');
|
|
|
|
|
b.classList.remove('visible');
|
|
|
|
|
b.textContent = '';
|
2026-04-27 06:01:00 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-02 20:54:53 +02:00
|
|
|
/* ══════════════════════════════
|
|
|
|
|
POLL
|
|
|
|
|
══════════════════════════════ */
|
2026-04-27 06:01:00 +02:00
|
|
|
async function poll() {
|
|
|
|
|
try {
|
|
|
|
|
const [dataRes, alertRes] = await Promise.all([
|
|
|
|
|
fetch('/api/data'),
|
|
|
|
|
fetch('/api/alerts'),
|
|
|
|
|
]);
|
|
|
|
|
const data = await dataRes.json();
|
|
|
|
|
const alerts = await alertRes.json();
|
|
|
|
|
|
2026-05-02 20:54:53 +02:00
|
|
|
/* Connection badge */
|
2026-04-27 06:01:00 +02:00
|
|
|
const badge = document.getElementById('conn-badge');
|
2026-05-02 20:54:53 +02:00
|
|
|
const ok = data.status.connected;
|
|
|
|
|
badge.className = `conn-badge card ${ok ? 'ok' : 'err'}`;
|
|
|
|
|
document.getElementById('conn-text').textContent = ok
|
|
|
|
|
? `${data.status.port} · ${data.status.baud} Bd`
|
|
|
|
|
: 'Getrennt — warte …';
|
|
|
|
|
|
|
|
|
|
/* Stat cards */
|
2026-04-27 06:01:00 +02:00
|
|
|
document.getElementById('stat-total').textContent = data.status.total_lines;
|
|
|
|
|
document.getElementById('stat-errors').textContent = data.status.errors;
|
2026-05-02 20:54:53 +02:00
|
|
|
document.getElementById('stat-port').textContent = data.status.port || '—';
|
|
|
|
|
document.getElementById('stat-baud').textContent = data.status.baud
|
|
|
|
|
? `${data.status.baud} baud` : '—';
|
2026-04-27 06:01:00 +02:00
|
|
|
|
|
|
|
|
if (data.entries.length) {
|
|
|
|
|
const latest = data.entries[data.entries.length - 1];
|
|
|
|
|
const disp = latest.values?.length
|
2026-05-02 20:54:53 +02:00
|
|
|
? latest.values[0].toFixed(2)
|
2026-04-27 06:01:00 +02:00
|
|
|
: latest.raw.slice(0, 14);
|
|
|
|
|
document.getElementById('stat-last').textContent = disp;
|
2026-05-02 20:54:53 +02:00
|
|
|
document.getElementById('stat-ts').textContent = latest.ts.split('T')[1].slice(0,8);
|
|
|
|
|
|
|
|
|
|
if (latest.values?.length) {
|
|
|
|
|
updateSensorTiles(latest.values);
|
|
|
|
|
chartData.push({ ts: latest.ts, v: latest.values[0] });
|
|
|
|
|
if (chartData.length > 200) chartData = chartData.slice(-200);
|
|
|
|
|
}
|
2026-04-27 06:01:00 +02:00
|
|
|
}
|
|
|
|
|
|
2026-05-02 20:54:53 +02:00
|
|
|
/* Log — only new entries */
|
|
|
|
|
const newEntries = data.entries.slice(lastLogCount);
|
2026-04-27 06:01:00 +02:00
|
|
|
lastLogCount = data.entries.length;
|
|
|
|
|
const logPane = document.getElementById('pane-log');
|
|
|
|
|
|
2026-05-02 20:54:53 +02:00
|
|
|
if (firstPoll && newEntries.length === 0) {
|
|
|
|
|
/* keep placeholder */
|
|
|
|
|
} else if (firstPoll) {
|
|
|
|
|
logPane.innerHTML = '';
|
|
|
|
|
firstPoll = false;
|
|
|
|
|
}
|
2026-04-27 06:01:00 +02:00
|
|
|
|
2026-05-02 20:54:53 +02:00
|
|
|
newEntries.forEach(e => {
|
|
|
|
|
const row = document.createElement('div');
|
2026-04-27 06:01:00 +02:00
|
|
|
row.className = 'log-entry';
|
2026-05-02 20:54:53 +02:00
|
|
|
const numStr = e.values?.map(v => v.toFixed(2)).join(' ') || '';
|
2026-04-27 06:01:00 +02:00
|
|
|
row.innerHTML =
|
2026-05-02 20:54:53 +02:00
|
|
|
`<span class="log-ts">${e.ts.split('T')[1].slice(0,8)}</span>` +
|
2026-04-27 06:01:00 +02:00
|
|
|
`<span class="log-raw">${e.raw}</span>` +
|
2026-05-02 20:54:53 +02:00
|
|
|
(numStr ? `<span class="log-num">${numStr}</span>` : '<span></span>');
|
2026-04-27 06:01:00 +02:00
|
|
|
logPane.prepend(row);
|
2026-05-02 20:54:53 +02:00
|
|
|
|
|
|
|
|
/* trim log DOM to 100 rows */
|
|
|
|
|
while (logPane.children.length > 100) {
|
|
|
|
|
logPane.removeChild(logPane.lastChild);
|
|
|
|
|
}
|
2026-04-27 06:01:00 +02:00
|
|
|
});
|
|
|
|
|
|
2026-05-02 20:54:53 +02:00
|
|
|
/* Alerts */
|
2026-04-27 06:01:00 +02:00
|
|
|
if (alerts.length > alertsSeen) {
|
|
|
|
|
const newAlerts = alerts.slice(alertsSeen);
|
2026-05-02 20:54:53 +02:00
|
|
|
alertsSeen = alerts.length;
|
2026-04-27 06:01:00 +02:00
|
|
|
|
|
|
|
|
const alertPane = document.getElementById('pane-alerts');
|
|
|
|
|
if (alertsSeen === newAlerts.length) alertPane.innerHTML = '';
|
|
|
|
|
|
|
|
|
|
newAlerts.forEach(a => {
|
|
|
|
|
const row = document.createElement('div');
|
|
|
|
|
row.className = 'alert-entry';
|
|
|
|
|
row.innerHTML =
|
|
|
|
|
`<span class="alert-icon">⚠</span>` +
|
2026-05-02 20:54:53 +02:00
|
|
|
`<span class="alert-ts">${a.ts.split('T')[1].slice(0,8)}</span>` +
|
2026-04-27 06:01:00 +02:00
|
|
|
`<span class="alert-msg">${a.subject}</span>`;
|
|
|
|
|
alertPane.prepend(row);
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-02 20:54:53 +02:00
|
|
|
const ab = document.getElementById('alert-badge');
|
2026-04-27 06:01:00 +02:00
|
|
|
if (!document.getElementById('pane-alerts').classList.contains('active')) {
|
2026-05-02 20:54:53 +02:00
|
|
|
ab.textContent = newAlerts.length > 9 ? '9+' : String(newAlerts.length);
|
|
|
|
|
ab.classList.add('visible');
|
2026-04-27 06:01:00 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
drawChart();
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.warn('[Mirror] Poll error:', err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setTimeout(poll, POLL_MS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
poll();
|