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

25
app.py
View File

@ -254,36 +254,41 @@ 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

View File

@ -276,8 +276,10 @@ document.addEventListener('DOMContentLoaded', () => {
}
} catch (err) {
console.error('sendControl Fehler:', err);
// Bei Fehler: Verbindungsprüfung triggern
updateStatus(false, 'Fehler beim Senden');
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);
});