fixxed status check

This commit is contained in:
oberon 2026-02-12 19:06:39 +01:00
parent 33c2515044
commit da40eb5599
2 changed files with 56 additions and 15 deletions

33
app.py
View File

@ -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

View File

@ -317,24 +317,32 @@ document.addEventListener('DOMContentLoaded', () => {
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
});
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}`);
}
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);
});