265 lines
8.6 KiB
HTML
265 lines
8.6 KiB
HTML
<div style="padding:12px;font-family:system-ui,-apple-system,Segoe UI,Roboto,sans-serif;">
|
||
<!-- Controls -->
|
||
<div style="display:flex;gap:10px;flex-wrap:wrap;align-items:center;">
|
||
<label style="display:flex;gap:6px;align-items:center;">
|
||
<span>Zeitraum</span>
|
||
<select id="range">
|
||
<option value="day">Tag</option>
|
||
<option value="week">Woche</option>
|
||
<option value="month">Monat</option>
|
||
<option value="total">Gesamt</option>
|
||
</select>
|
||
</label>
|
||
|
||
<label style="display:flex;gap:6px;align-items:center;">
|
||
<span>Datum</span>
|
||
<input id="date" type="date" />
|
||
</label>
|
||
|
||
<button id="prev" type="button">◀</button>
|
||
<button id="today" type="button">Letzter Zeit Punkt</button>
|
||
<button id="next" type="button">▶</button>
|
||
</div>
|
||
|
||
<div id="period" style="margin-top:10px;font-size:12px;opacity:0.75;"></div>
|
||
<div id="hint" style="margin-top:6px;font-size:12px;opacity:0.8;"></div>
|
||
|
||
<div id="grid"
|
||
style="margin-top:14px;display:grid;grid-template-columns:repeat(2,minmax(0,1fr));
|
||
gap:14px;max-width:880px;">
|
||
</div>
|
||
|
||
<!-- Fehler/Status sichtbar machen (damit es nicht einfach "leer" ist) -->
|
||
<div id="err" style="margin-top:10px;font-size:12px;opacity:0.8;color:#ffb4b4;"></div>
|
||
</div>
|
||
|
||
<script>
|
||
(function () {
|
||
const elRange = document.getElementById('range');
|
||
const elDate = document.getElementById('date');
|
||
const elPrev = document.getElementById('prev');
|
||
const elToday = document.getElementById('today');
|
||
const elNext = document.getElementById('next');
|
||
const elPeriod = document.getElementById('period');
|
||
const elHint = document.getElementById('hint');
|
||
const elGrid = document.getElementById('grid');
|
||
const elErr = document.getElementById('err');
|
||
|
||
// --- helpers ---
|
||
function toNum(x) {
|
||
const n = Number(x);
|
||
return Number.isFinite(n) ? n : 0;
|
||
}
|
||
function clamp01(x) {
|
||
if (!Number.isFinite(x)) return 0;
|
||
if (x < 0) return 0;
|
||
if (x > 1) return 1;
|
||
return x;
|
||
}
|
||
function escapeHtml(s) {
|
||
return String(s)
|
||
.replaceAll('&','&')
|
||
.replaceAll('<','<')
|
||
.replaceAll('>','>')
|
||
.replaceAll('"','"')
|
||
.replaceAll("'",''');
|
||
}
|
||
function fmtDateTime(d) {
|
||
const yyyy = d.getFullYear();
|
||
const mm = String(d.getMonth() + 1).padStart(2,'0');
|
||
const dd = String(d.getDate()).padStart(2,'0');
|
||
const hh = String(d.getHours()).padStart(2,'0');
|
||
const mi = String(d.getMinutes()).padStart(2,'0');
|
||
return `${dd}.${mm}.${yyyy} ${hh}:${mi}`;
|
||
}
|
||
|
||
// --- make requestAction safe (wait until available) ---
|
||
function safeRequestAction(ident, value) {
|
||
const tryCall = () => {
|
||
if (typeof requestAction === 'function') {
|
||
try { requestAction(ident, value); } catch (e) { showErr(e); }
|
||
return true;
|
||
}
|
||
return false;
|
||
};
|
||
|
||
if (tryCall()) return;
|
||
|
||
// retry a few times (fullscreen reload timing)
|
||
let tries = 0;
|
||
const timer = setInterval(() => {
|
||
tries++;
|
||
if (tryCall() || tries >= 25) clearInterval(timer);
|
||
}, 120);
|
||
}
|
||
|
||
function safeRefreshSoon() {
|
||
setTimeout(() => safeRequestAction('Refresh', 1), 150);
|
||
}
|
||
|
||
function showErr(e) {
|
||
const msg = (e && e.message) ? e.message : String(e);
|
||
elErr.textContent = 'JS-Fehler: ' + msg;
|
||
}
|
||
|
||
// Global error trap (damit wir sehen, wenn’s crasht)
|
||
window.addEventListener('error', (ev) => showErr(ev.error || ev.message));
|
||
window.addEventListener('unhandledrejection', (ev) => showErr(ev.reason));
|
||
|
||
// --- donut card ---
|
||
function donutCard({ title, percent, subtitle, color }) {
|
||
const share = clamp01(percent / 100);
|
||
const r = 56;
|
||
const c = 2 * Math.PI * r;
|
||
const dash = share * c;
|
||
const gap = c - dash;
|
||
|
||
return `
|
||
<div style="border:1px solid rgba(255,255,255,0.12);border-radius:16px;padding:14px;
|
||
display:flex;align-items:center;justify-content:center;min-height:210px;">
|
||
<div style="position:relative;width:170px;height:170px;">
|
||
<svg width="170" height="170" viewBox="0 0 170 170" style="display:block;">
|
||
<circle cx="85" cy="85" r="${r}"
|
||
fill="none" stroke="rgba(255,255,255,0.10)" stroke-width="18" />
|
||
<circle cx="85" cy="85" r="${r}"
|
||
fill="none" stroke="${color}" stroke-width="18"
|
||
stroke-linecap="butt"
|
||
stroke-dasharray="${dash} ${gap}"
|
||
transform="rotate(-90 85 85)" />
|
||
</svg>
|
||
|
||
<div style="position:absolute;inset:0;display:flex;flex-direction:column;
|
||
align-items:center;justify-content:center;text-align:center;">
|
||
<div style="font-weight:800;font-size:18px;line-height:1;">
|
||
${escapeHtml(percent.toFixed(1))}%
|
||
</div>
|
||
<div style="margin-top:4px;font-size:13px;opacity:0.80;">
|
||
${escapeHtml(title)}
|
||
</div>
|
||
<div style="margin-top:10px;font-size:12px;opacity:0.65;max-width:140px;">
|
||
${escapeHtml(subtitle)}
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
`;
|
||
}
|
||
|
||
function render(data) {
|
||
const values = data?.values || {};
|
||
const prod = toNum(values?.Produktion);
|
||
const cons = toNum(values?.Hausverbrauch); // Verbrauch
|
||
const grid = toNum(values?.Netz); // Netzbezug
|
||
|
||
const eigen = cons - grid; // Eigenverbrauch
|
||
const eigenClamped = eigen < 0 ? 0 : eigen;
|
||
|
||
const evq = (prod > 0) ? (eigenClamped / prod * 100) : 0;
|
||
const autark = (cons > 0) ? (eigenClamped / cons * 100) : 0;
|
||
|
||
// Controls sync
|
||
if (data?.range) elRange.value = data.range;
|
||
if (data?.date) elDate.value = data.date;
|
||
|
||
const isTotal = (data?.range === 'total');
|
||
elDate.disabled = isTotal;
|
||
|
||
// Zeitraum-Text
|
||
if (data?.tStart && data?.tEnd) {
|
||
const s = new Date(data.tStart * 1000);
|
||
const e = new Date(data.tEnd * 1000);
|
||
elPeriod.textContent = isTotal
|
||
? 'Zeitraum: Gesamt'
|
||
: `Zeitraum: ${fmtDateTime(s)} – ${fmtDateTime(e)}`;
|
||
} else {
|
||
elPeriod.textContent = '';
|
||
}
|
||
|
||
// Hint
|
||
if (data?.hasData === false) {
|
||
elHint.textContent = 'Keine Logdaten für den gewählten Zeitraum.';
|
||
} else {
|
||
elHint.textContent =
|
||
`Produktion: ${prod.toFixed(2)} kWh · Verbrauch: ${cons.toFixed(2)} kWh · Netzbezug: ${grid.toFixed(2)} kWh · Eigenverbrauch: ${eigenClamped.toFixed(2)} kWh`;
|
||
}
|
||
|
||
elGrid.innerHTML = [
|
||
donutCard({
|
||
title: 'EVQ',
|
||
percent: clamp01(evq / 100) * 100,
|
||
subtitle: 'Eigenverb. / Produktion',
|
||
color: '#63B3FF'
|
||
}),
|
||
donutCard({
|
||
title: 'Autarkiegrad',
|
||
percent: clamp01(autark / 100) * 100,
|
||
subtitle: 'Eigenverb. / Verbrauch',
|
||
color: '#A855F7'
|
||
})
|
||
].join('');
|
||
|
||
elErr.textContent = ''; // clear errors if render succeeded
|
||
}
|
||
|
||
// --- cache last payload so fullscreen reload still shows something immediately ---
|
||
function saveLast(data) {
|
||
try { sessionStorage.setItem('EnergyPie_last', JSON.stringify(data)); } catch(e) {}
|
||
}
|
||
function loadLast() {
|
||
try {
|
||
const raw = sessionStorage.getItem('EnergyPie_last');
|
||
return raw ? JSON.parse(raw) : null;
|
||
} catch(e) {
|
||
return null;
|
||
}
|
||
}
|
||
|
||
// Symcon -> Tile
|
||
window.handleMessage = function (data) {
|
||
if (typeof data === 'string') {
|
||
try { data = JSON.parse(data); } catch(e) {}
|
||
}
|
||
if (!data) return;
|
||
saveLast(data);
|
||
render(data);
|
||
};
|
||
|
||
// UI events (safe)
|
||
elRange.addEventListener('change', () => safeRequestAction('SetRange', elRange.value));
|
||
|
||
let dateTimer = null;
|
||
function pushDateNow() {
|
||
if (dateTimer) clearTimeout(dateTimer);
|
||
dateTimer = setTimeout(() => safeRequestAction('SetDate', elDate.value), 80);
|
||
}
|
||
elDate.addEventListener('input', pushDateNow);
|
||
elDate.addEventListener('change', pushDateNow);
|
||
|
||
elPrev.addEventListener('click', () => safeRequestAction('Prev', 1));
|
||
elToday.addEventListener('click', () => safeRequestAction('Today', 1));
|
||
elNext.addEventListener('click', () => safeRequestAction('Next', 1));
|
||
|
||
// On load: render cached immediately, then refresh when ready
|
||
const cached = loadLast();
|
||
if (cached) render(cached);
|
||
|
||
// Refresh on start + on fullscreen/resize/visibility changes
|
||
safeRefreshSoon();
|
||
|
||
document.addEventListener('visibilitychange', () => {
|
||
if (!document.hidden) safeRefreshSoon();
|
||
});
|
||
|
||
window.addEventListener('resize', () => {
|
||
// some Symcon fullscreen transitions trigger resize + reload timing
|
||
safeRefreshSoon();
|
||
});
|
||
|
||
// pageshow covers some "recreated webview" cases
|
||
window.addEventListener('pageshow', () => {
|
||
safeRefreshSoon();
|
||
});
|
||
|
||
})();
|
||
</script>
|