From da40eb5599d0a4302b8e4ec67703e5685c7fc30c Mon Sep 17 00:00:00 2001 From: oberon Date: Thu, 12 Feb 2026 19:06:39 +0100 Subject: [PATCH] fixxed status check --- app.py | 33 +++++++++++++++++++++++++++++++++ static/js/app.js | 38 +++++++++++++++++++++++--------------- 2 files changed, 56 insertions(+), 15 deletions(-) diff --git a/app.py b/app.py index 3dc375b..5cbcecb 100644 --- a/app.py +++ b/app.py @@ -252,6 +252,39 @@ def api_connect(): return jsonify({"success": False, "message": f"Verbindungsfehler: {str(e)}"}), 500 +@app.route('/api/status', methods=['GET']) +def api_status(): + """ + Einfacher Status-Check: Ist ein Hub verbunden? + Wird von Frontend periodisch aufgerufen, um Verbindungsverlust zu erkennen. + """ + global current_device + + if current_device is None: + logger.debug("Status-Check: Kein Device aktiv") + return jsonify({ + "connected": False, + "message": "Keine aktive Verbindung" + }), 200 # 200 OK, damit der Check nicht als Fehler gilt + + try: + # Optional: Hier könnte man später einen echten Test-Befehl machen + # z. B. current_device.CreateTelegram() oder nur prüfen, ob Instanz lebt + logger.debug("Status-Check: Device vorhanden → verbunden") + return jsonify({ + "connected": True, + "message": "Verbunden", + "hub_id": current_config.get('hub_id') if current_config else None + }) + + except Exception as e: + logger.exception("Status-Check-Fehler") + return jsonify({ + "connected": False, + "message": f"Verbindungsfehler: {str(e)}" + }), 200 # immer 200, damit Frontend entscheidet + + @app.route('/api/control', methods=['POST']) def api_control(): global current_device diff --git a/static/js/app.js b/static/js/app.js index c27958f..5f2899b 100644 --- a/static/js/app.js +++ b/static/js/app.js @@ -309,31 +309,39 @@ document.addEventListener('DOMContentLoaded', () => { // Initialer Status updateStatus(false); - // ── Automatische Verbindungsprüfung ──────────────────────────────────────── - let connectionCheckInterval = null; + // ── Automatische Verbindungsprüfung ──────────────────────────────────────── + let connectionCheckInterval = null; - function startConnectionCheck() { + function startConnectionCheck() { if (connectionCheckInterval) clearInterval(connectionCheckInterval); connectionCheckInterval = setInterval(async () => { - try { - const res = await fetch('/api/control', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ port: 'ping', value: 0 }) // Dummy - }); + try { + console.log("→ Status-Check: Sende /api/status ..."); + const res = await fetch('/api/status'); if (!res.ok) { - throw new Error('Check fehlgeschlagen'); + throw new Error(`Status-Check HTTP ${res.status}`); } - updateStatus(true); - } catch (err) { + const data = await res.json(); + console.log("→ Status-Check Ergebnis:", data); + + if (data.connected) { + updateStatus(true); + } else { + throw new Error(data.message || 'Nicht verbunden'); + } + } catch (err) { console.warn('Verbindungs-Check fehlgeschlagen:', err); updateStatus(false, 'Verbindung verloren'); - } - }, 8000); // 8 Sekunden - } + } + }, 8000); // alle 8 Sekunden + } + + // Im Connect- und Reconnect-Handler nach erfolgreichem Connect/Reconnect aufrufen: + startConnectionCheck(); + updateStatus(true); window.addEventListener('beforeunload', () => { if (connectionCheckInterval) clearInterval(connectionCheckInterval);