backend rework- full test 1

This commit is contained in:
derlole
2025-05-07 11:08:43 +00:00
parent f7d9a640ee
commit 76bfa50995
19 changed files with 397 additions and 127 deletions

View File

@@ -1,19 +1,26 @@
from flask import Flask
from flask_socketio import SocketIO
from routes.unsecure_routes import unsecure
from routes.esp_routes import esp
import threading
import time
import os
import paho.mqtt.client as mqtt
from datetime import datetime, timedelta
import sqlite3
from modules.persistence import load_dict, save_dict, esp_conn_infos
from modules.socketio import socketio, resend_static_data
MQTT_BROKER = "localhost"
MQTT_PORT = 1883
MQTT_TOPIC_SUB = "coffee/status"
MQTT_TOPIC_SEND = "coffee/command"
app = Flask(__name__, static_url_path='/unsecure/static')
app.config['SECRET_KEY'] = 'super-secret-key'
socketio = SocketIO(app, cors_allowed_origins="*", async_mode='threading')
socketio.init_app(app)
# Blueprints registrieren
app.register_blueprint(unsecure)
@@ -33,7 +40,7 @@ def on_message(client, userdata, msg):
'message': msg.payload.decode()
})
# MQTT-Thread starten
# MQTT-Thread
def mqtt_thread():
client = mqtt.Client()
client.on_connect = on_connect
@@ -41,24 +48,69 @@ def mqtt_thread():
client.connect(MQTT_BROKER, MQTT_PORT, 60)
client.loop_forever()
# Dummy-Daten-Thread
# def send_data():
# counter = 0
# while True:
# data = {
# 'test': 'Live-Daten',
# 'status': 'OK',
# 'counter': counter
# }
# socketio.emit('update_data', data)
# counter += 1
# time.sleep(2)
# DB-Cleanup-Thread
def cleanup_old_commands():
# Beide Threads starten
#threading.Thread(target=send_data, daemon=True).start()
db_path = os.path.join(os.path.dirname(__file__), "db", "commands.db")
while True:
try:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
five_minutes_ago = (datetime.utcnow() - timedelta(minutes=5)).strftime('%Y-%m-%d %H:%M:%S')
cursor.execute("""
UPDATE commands
SET status = 'failed'
WHERE status = 'pending' AND tstamp <= ?
""", (five_minutes_ago,))
updated_rows = cursor.rowcount
conn.commit()
conn.close()
if updated_rows > 0:
print(f"[Cleanup] {updated_rows} Einträge als 'failed' markiert.")
except Exception as e:
print(f"[Cleanup-Fehler] {e}")
time.sleep(60) # jede Minute prüfen ob es pending Einträge gibt, die älter als 5 Minuten sind
# Clear commands DB
def clear_commands_db():
import os
import sqlite3
db_path = os.path.join(os.path.dirname(__file__), "db", "commands.db")
if os.path.exists(db_path):
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute("DELETE FROM commands")
conn.commit()
conn.close()
print("[DB] commands-Tabelle geleert.")
else:
print("[DB] Keine Datenbank gefunden nichts geleert.")
# Motitior ESP-Connection
def monitor_esp_connection():
while True:
if esp_conn_infos["last_seen"]:
time_diff = datetime.now() - esp_conn_infos["last_seen"]
if time_diff > timedelta(minutes=30):
esp_conn_infos["connection_valid"] = False
resend_static_data()
time.sleep(60) # einmal pro Minute die Verbindung zum ESP prüfen
### THREADS START ###
threading.Thread(target=cleanup_old_commands, daemon=True).start()
threading.Thread(target=monitor_esp_connection, daemon=True).start()
#threading.Thread(target=mqtt_thread, daemon=True).start()
if __name__ == '__main__':
#clear_commands_db()
socketio.run(app, host='0.0.0.0', port=3060, allow_unsafe_werkzeug=True)