fixxed status check
This commit is contained in:
parent
33c2515044
commit
da40eb5599
33
app.py
33
app.py
@ -252,6 +252,39 @@ def api_connect():
|
|||||||
return jsonify({"success": False, "message": f"Verbindungsfehler: {str(e)}"}), 500
|
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'])
|
@app.route('/api/control', methods=['POST'])
|
||||||
def api_control():
|
def api_control():
|
||||||
global current_device
|
global current_device
|
||||||
|
|||||||
@ -309,31 +309,39 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
// Initialer Status
|
// Initialer Status
|
||||||
updateStatus(false);
|
updateStatus(false);
|
||||||
|
|
||||||
// ── Automatische Verbindungsprüfung ────────────────────────────────────────
|
// ── Automatische Verbindungsprüfung ────────────────────────────────────────
|
||||||
let connectionCheckInterval = null;
|
let connectionCheckInterval = null;
|
||||||
|
|
||||||
function startConnectionCheck() {
|
function startConnectionCheck() {
|
||||||
if (connectionCheckInterval) clearInterval(connectionCheckInterval);
|
if (connectionCheckInterval) clearInterval(connectionCheckInterval);
|
||||||
|
|
||||||
connectionCheckInterval = setInterval(async () => {
|
connectionCheckInterval = setInterval(async () => {
|
||||||
try {
|
try {
|
||||||
const res = await fetch('/api/control', {
|
console.log("→ Status-Check: Sende /api/status ...");
|
||||||
method: 'POST',
|
const res = await fetch('/api/status');
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({ port: 'ping', value: 0 }) // Dummy
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
throw new Error('Check fehlgeschlagen');
|
throw new Error(`Status-Check HTTP ${res.status}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
updateStatus(true);
|
const data = await res.json();
|
||||||
} catch (err) {
|
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);
|
console.warn('Verbindungs-Check fehlgeschlagen:', err);
|
||||||
updateStatus(false, 'Verbindung verloren');
|
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', () => {
|
window.addEventListener('beforeunload', () => {
|
||||||
if (connectionCheckInterval) clearInterval(connectionCheckInterval);
|
if (connectionCheckInterval) clearInterval(connectionCheckInterval);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user