updated app.js

This commit is contained in:
oberon 2026-02-12 21:25:43 +01:00
parent 7adda970fb
commit 60eec88332
2 changed files with 39 additions and 26 deletions

27
app.py
View File

@ -254,37 +254,42 @@ def api_connect():
@app.route('/api/status', methods=['GET']) @app.route('/api/status', methods=['GET'])
def api_status(): 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: if current_device is None:
logger.debug("Status-Check: Kein Device aktiv") logger.debug("Status-Check: Kein Device aktiv")
return jsonify({ return jsonify({"connected": False, "message": "Keine aktive Verbindung"}), 200
"connected": False,
"message": "Keine aktive Verbindung"
}), 200
try: 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) 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({ return jsonify({
"connected": True, "connected": True,
"message": "Verbunden" "message": "Verbunden"
}) })
except Exception as e: except Exception as e:
logger.warning(f"Status-Check: SetChannel-Test fehlgeschlagen → Hub wahrscheinlich weg: {e}") logger.warning(f"Status-Check: Harter Test fehlgeschlagen → Hub wahrscheinlich weg: {str(e)}")
# Verbindung sauber beenden
# Sauber aufräumen
try: try:
current_device.Disconnect() current_device.Disconnect()
except: except:
pass pass
current_device = None current_device = None
current_module = None current_module = None
current_hub = 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']) @app.route('/api/control', methods=['POST'])

View File

@ -275,9 +275,11 @@ document.addEventListener('DOMContentLoaded', () => {
throw new Error(errData.message || 'Steuerbefehl fehlgeschlagen'); throw new Error(errData.message || 'Steuerbefehl fehlgeschlagen');
} }
} catch (err) { } catch (err) {
console.error('sendControl Fehler:', err); console.error('sendControl Fehler:', err);
// Bei Fehler: Verbindungsprüfung triggern failedChecks++;
updateStatus(false, 'Fehler beim Senden'); if (failedChecks >= MAX_FAILED_CHECKS) {
updateStatus(false, 'Mehrere Steuerbefehle fehlgeschlagen');
}
} }
} }
@ -311,38 +313,44 @@ document.addEventListener('DOMContentLoaded', () => {
// ── Automatische Verbindungsprüfung ──────────────────────────────────────── // ── Automatische Verbindungsprüfung ────────────────────────────────────────
let connectionCheckInterval = null; let connectionCheckInterval = null;
let failedChecks = 0;
const MAX_FAILED_CHECKS = 3; // erst nach 3 Fehlschlägen in Folge rot
function startConnectionCheck() { function startConnectionCheck() {
if (connectionCheckInterval) clearInterval(connectionCheckInterval); if (connectionCheckInterval) clearInterval(connectionCheckInterval);
connectionCheckInterval = setInterval(async () => { connectionCheckInterval = setInterval(async () => {
try { try {
console.log("→ Status-Check: Sende /api/status ..."); console.log("→ Status-Check ...");
const res = await fetch('/api/status'); const res = await fetch('/api/status');
if (!res.ok) {
throw new Error(`Status-Check HTTP ${res.status}`);
}
const data = await res.json(); const data = await res.json();
console.log("→ Status-Check Ergebnis:", data);
if (data.connected) { if (data.connected) {
failedChecks = 0;
updateStatus(true); updateStatus(true);
} else { } 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) { } catch (err) {
console.warn('Verbindungs-Check fehlgeschlagen:', err); failedChecks++;
updateStatus(false, 'Verbindung verloren'); 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(); startConnectionCheck();
updateStatus(true); updateStatus(true);
window.addEventListener('beforeunload', () => { window.addEventListener('beforeunload', () => {
if (connectionCheckInterval) clearInterval(connectionCheckInterval); if (connectionCheckInterval) clearInterval(connectionCheckInterval);
}); });