diff --git a/app.py b/app.py index 6624a0f..61f039a 100644 --- a/app.py +++ b/app.py @@ -254,37 +254,42 @@ def api_connect(): @app.route('/api/status', methods=['GET']) def api_status(): - global current_device, current_module, current_hub # ← Global am Anfang! + global current_device, current_module, current_hub if current_device is None: logger.debug("Status-Check: Kein Device aktiv") - return jsonify({ - "connected": False, - "message": "Keine aktive Verbindung" - }), 200 + return jsonify({"connected": False, "message": "Keine aktive Verbindung"}), 200 try: - # Echter Mini-Test: Setze Kanal 0 (A) auf 0.0 + # Harter Test: Setze Kanal 0 (A) kurz auf 0.1 und sofort zurück auf 0.0 + # Das ist fast unsichtbar (nur ein minimaler Impuls), aber zwingt den Hub zu reagieren + current_device.SetChannel(0, 0.1) current_device.SetChannel(0, 0.0) - logger.debug("Status-Check: SetChannel(0, 0.0) erfolgreich → Hub antwortet") + + logger.debug("Status-Check: Harter Test (SetChannel 0.1 → 0.0) erfolgreich → Hub lebt") return jsonify({ "connected": True, "message": "Verbunden" }) except Exception as e: - logger.warning(f"Status-Check: SetChannel-Test fehlgeschlagen → Hub wahrscheinlich weg: {e}") - # Verbindung sauber beenden + logger.warning(f"Status-Check: Harter Test fehlgeschlagen → Hub wahrscheinlich weg: {str(e)}") + + # Sauber aufräumen try: current_device.Disconnect() except: pass + current_device = None current_module = None current_hub = None - return jsonify({"connected": False, "message": f"Hub reagiert nicht: {str(e)}"}), 200 - + return jsonify({ + "connected": False, + "message": f"Hub reagiert nicht mehr: {str(e)}" + }), 200 + @app.route('/api/control', methods=['POST']) diff --git a/static/js/app.js b/static/js/app.js index 5f2899b..4dd86b0 100644 --- a/static/js/app.js +++ b/static/js/app.js @@ -275,9 +275,11 @@ document.addEventListener('DOMContentLoaded', () => { throw new Error(errData.message || 'Steuerbefehl fehlgeschlagen'); } } catch (err) { - console.error('sendControl Fehler:', err); - // Bei Fehler: Verbindungsprüfung triggern - updateStatus(false, 'Fehler beim Senden'); + console.error('sendControl Fehler:', err); + failedChecks++; + if (failedChecks >= MAX_FAILED_CHECKS) { + updateStatus(false, 'Mehrere Steuerbefehle fehlgeschlagen'); + } } } @@ -311,38 +313,44 @@ document.addEventListener('DOMContentLoaded', () => { // ── Automatische Verbindungsprüfung ──────────────────────────────────────── let connectionCheckInterval = null; + let failedChecks = 0; + const MAX_FAILED_CHECKS = 3; // erst nach 3 Fehlschlägen in Folge rot function startConnectionCheck() { if (connectionCheckInterval) clearInterval(connectionCheckInterval); connectionCheckInterval = setInterval(async () => { try { - console.log("→ Status-Check: Sende /api/status ..."); + console.log("→ Status-Check ..."); const res = await fetch('/api/status'); - - if (!res.ok) { - throw new Error(`Status-Check HTTP ${res.status}`); - } - const data = await res.json(); - console.log("→ Status-Check Ergebnis:", data); if (data.connected) { + failedChecks = 0; updateStatus(true); } else { - throw new Error(data.message || 'Nicht verbunden'); + failedChecks++; + console.warn(`Status-Check fehlgeschlagen (${failedChecks}/${MAX_FAILED_CHECKS}):`, data.message); + if (failedChecks >= MAX_FAILED_CHECKS) { + updateStatus(false, data.message || 'Mehrere Checks fehlgeschlagen'); + } } } catch (err) { - console.warn('Verbindungs-Check fehlgeschlagen:', err); - updateStatus(false, 'Verbindung verloren'); + failedChecks++; + console.warn(`Status-Check Netzwerkfehler (${failedChecks}/${MAX_FAILED_CHECKS}):`, err); + if (failedChecks >= MAX_FAILED_CHECKS) { + updateStatus(false, 'Keine Antwort vom Hub/Server'); } - }, 8000); // alle 8 Sekunden + } + }, 6000); // 6 Sekunden – aggressiver als 8 s } - // Im Connect- und Reconnect-Handler nach erfolgreichem Connect/Reconnect aufrufen: + // In Connect- und Reconnect-Handler nach erfolgreichem Connect/Reconnect: startConnectionCheck(); updateStatus(true); + + window.addEventListener('beforeunload', () => { if (connectionCheckInterval) clearInterval(connectionCheckInterval); });