no message

This commit is contained in:
belevo\mh
2025-12-17 14:17:23 +01:00
parent 26ec00c63e
commit 499eddccb9

View File

@@ -22,17 +22,36 @@
</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="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>
<!-- Debug/Status -->
<div id="dbg" style="margin-top:10px;font-size:12px;opacity:0.75;"></div>
<!-- Fehler -->
<div id="err" style="margin-top:6px;font-size:12px;opacity:0.9;color:#ffb4b4;"></div>
</div>
<!-- 1) Puffer-Handler: fängt Symcon-Messages ab, selbst wenn UI noch nicht ready ist -->
<script>
window.__EnergyPieQueue = window.__EnergyPieQueue || [];
window.__EnergyPieReady = false;
// Symcon ruft handleMessage evtl. sehr früh auf -> wir puffern.
window.handleMessage = function (data) {
window.__EnergyPieQueue.push(data);
if (window.__EnergyPieReady && typeof window.__EnergyPieConsume === "function") {
const q = window.__EnergyPieQueue.splice(0);
q.forEach(d => window.__EnergyPieConsume(d));
}
};
</script>
<script>
(function () {
const elRange = document.getElementById('range');
@@ -43,8 +62,12 @@
const elPeriod = document.getElementById('period');
const elHint = document.getElementById('hint');
const elGrid = document.getElementById('grid');
const elDbg = document.getElementById('dbg');
const elErr = document.getElementById('err');
// Start nicht "grau/leer"
elGrid.innerHTML = '<div style="opacity:.75;padding:12px;">Lade Daten…</div>';
// --- helpers ---
function toNum(x) {
const n = Number(x);
@@ -73,11 +96,27 @@
return `${dd}.${mm}.${yyyy} ${hh}:${mi}`;
}
// --- make requestAction safe (wait until available) ---
function showErr(e) {
const msg = (e && e.message) ? e.message : String(e);
elErr.textContent = 'JS-Fehler: ' + msg;
}
// Global error trap
window.addEventListener('error', (ev) => showErr(ev.error || ev.message));
window.addEventListener('unhandledrejection', (ev) => showErr(ev.reason));
// --- requestAction robust finden (Popup hat das oft am parent) ---
function getRequestAction() {
if (typeof window.requestAction === 'function') return window.requestAction;
if (typeof window.parent?.requestAction === 'function') return window.parent.requestAction;
return null;
}
function safeRequestAction(ident, value) {
const tryCall = () => {
if (typeof requestAction === 'function') {
try { requestAction(ident, value); } catch (e) { showErr(e); }
const ra = getRequestAction();
if (ra) {
try { ra(ident, value); } catch (e) { showErr(e); }
return true;
}
return false;
@@ -85,11 +124,11 @@
if (tryCall()) return;
// retry a few times (fullscreen reload timing)
// Popup/Fullscreen: manchmal kommt requestAction deutlich später -> länger retry
let tries = 0;
const timer = setInterval(() => {
tries++;
if (tryCall() || tries >= 25) clearInterval(timer);
if (tryCall() || tries >= 150) clearInterval(timer); // ~18s
}, 120);
}
@@ -97,15 +136,6 @@
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, wenns 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);
@@ -148,13 +178,13 @@
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 cons = toNum(values?.Hausverbrauch);
const grid = toNum(values?.Netz);
const eigen = cons - grid; // Eigenverbrauch
const eigen = cons - grid;
const eigenClamped = eigen < 0 ? 0 : eigen;
const evq = (prod > 0) ? (eigenClamped / prod * 100) : 0;
const evq = (prod > 0) ? (eigenClamped / prod * 100) : 0;
const autark = (cons > 0) ? (eigenClamped / cons * 100) : 0;
// Controls sync
@@ -198,10 +228,11 @@
})
].join('');
elErr.textContent = ''; // clear errors if render succeeded
elDbg.textContent = `Letzte Daten: ${new Date().toLocaleTimeString()} · range=${data?.range ?? '-'} · date=${data?.date ?? '-'}`;
elErr.textContent = '';
}
// --- cache last payload so fullscreen reload still shows something immediately ---
// --- cache ---
function saveLast(data) {
try { sessionStorage.setItem('EnergyPie_last', JSON.stringify(data)); } catch(e) {}
}
@@ -214,8 +245,8 @@
}
}
// Symcon -> Tile
window.handleMessage = function (data) {
// 2) Consumer für gepufferte Symcon-Messages
window.__EnergyPieConsume = function (data) {
if (typeof data === 'string') {
try { data = JSON.parse(data); } catch(e) {}
}
@@ -224,7 +255,7 @@
render(data);
};
// UI events (safe)
// UI events
elRange.addEventListener('change', () => safeRequestAction('SetRange', elRange.value));
let dateTimer = null;
@@ -239,10 +270,17 @@
elToday.addEventListener('click', () => safeRequestAction('Today', 1));
elNext.addEventListener('click', () => safeRequestAction('Next', 1));
// On load: render cached immediately, then refresh when ready
// On load: cached sofort rendern
const cached = loadLast();
if (cached) render(cached);
// UI ready -> Queue flushen
window.__EnergyPieReady = true;
if (window.__EnergyPieQueue && window.__EnergyPieQueue.length) {
const q = window.__EnergyPieQueue.splice(0);
q.forEach(d => window.__EnergyPieConsume(d));
}
// Refresh on start + on fullscreen/resize/visibility changes
safeRefreshSoon();
@@ -250,15 +288,8 @@
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();
});
window.addEventListener('resize', () => safeRefreshSoon());
window.addEventListener('pageshow', () => safeRefreshSoon());
})();
</script>