From 20fa37b0d9991dcdc4ded1c4f01f0e377929c19a Mon Sep 17 00:00:00 2001 From: derlole <122916573+derlole@users.noreply.github.com> Date: Thu, 8 May 2025 13:26:43 +0000 Subject: [PATCH] feat: implement command status update and enhance MQTT message handling --- db/init_scripts/initCom.py | 2 +- modules/db.py | 22 ++++++++++++++++++++++ modules/other.py | 4 ++++ routes/esp_routes.py | 7 +------ server.py | 32 +++++++++++++++++++++++++------- static/style.css | 1 + 6 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 modules/db.py create mode 100644 modules/other.py diff --git a/db/init_scripts/initCom.py b/db/init_scripts/initCom.py index 9d5b31c..e8b8c78 100644 --- a/db/init_scripts/initCom.py +++ b/db/init_scripts/initCom.py @@ -30,4 +30,4 @@ CREATE TABLE IF NOT EXISTS commands ( conn.commit() conn.close() -print(f"Datenbank erstellt unter: {db_path}") +print(f"[DB]Datenbank erstellt unter: {db_path}") diff --git a/modules/db.py b/modules/db.py new file mode 100644 index 0000000..001a5d0 --- /dev/null +++ b/modules/db.py @@ -0,0 +1,22 @@ +import sqlite3 +import os +from datetime import datetime, timedelta + +DB_PATH = os.path.join(os.path.dirname(__file__), '../db/commands.db') + +### THIS CODE IS NOT PROOFED BUT LOOKS RIGHT### +def update_command_status(command_id, status): + conn = sqlite3.connect(DB_PATH) + cursor = conn.cursor() + + cursor.execute(""" + UPDATE commands + SET status = ? + WHERE command_id = ? + """, (status, command_id)) + + conn.commit() + conn.close() + print(f"[DB] Befehl {command_id} auf {status} aktualisiert.") + return status +### THIS CODE IS NOT PROOFED BUT LOOKS RIGHT### \ No newline at end of file diff --git a/modules/other.py b/modules/other.py new file mode 100644 index 0000000..796eb95 --- /dev/null +++ b/modules/other.py @@ -0,0 +1,4 @@ + + +def refactor_and_use_esp_data(data): + return diff --git a/routes/esp_routes.py b/routes/esp_routes.py index 77daa06..756faa0 100644 --- a/routes/esp_routes.py +++ b/routes/esp_routes.py @@ -29,16 +29,12 @@ def esp_online(): esp_conn_infos["last_seen"] = datetime.now() esp_conn_infos["connection_valid"] = True resend_static_data() - - print(f"ESP ONLINE von IP: {esp_ip}, roher IP: {sender_ip}") return jsonify({"status": "ok"}) @esp.route('/toggle-machine', methods=['POST']) def toggle_machine(): - print("ESP: toggle-machine") randID = random.randint(1000, 9999) fullCommand = {'command': 'toggle_machine', 'status': 'pending', 'command_id': randID} - print("ESP: toggle-machine 1") conn = sqlite3.connect(DB_PATH) cursor = conn.cursor() @@ -49,10 +45,9 @@ def toggle_machine(): conn.commit() conn.close() - print("ESP: toggle-machine 2") client = mqtt.Client() client.connect(MQTT_BROKER, MQTT_PORT, 60) client.publish(MQTT_TOPIC, json.dumps(fullCommand)) client.disconnect() - print("ESP: toggle-machine 3") + return jsonify({"status": json.dumps(fullCommand)}) \ No newline at end of file diff --git a/server.py b/server.py index 81008f8..127467f 100644 --- a/server.py +++ b/server.py @@ -9,11 +9,15 @@ 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 +from modules.db import update_command_status +import json +from modules.other import refactor_and_use_esp_data MQTT_BROKER = "localhost" MQTT_PORT = 1883 MQTT_TOPIC_SUB = "coffee/status" MQTT_TOPIC_SEND = "coffee/command" +MQTT_TOPIC_RETURN = "coffee/return" app = Flask(__name__, static_url_path='/unsecure/static') @@ -30,15 +34,29 @@ app.register_blueprint(esp) def on_connect(client, userdata, flags, rc): print(f"[MQTT] Verbunden mit Code {rc}") client.subscribe(MQTT_TOPIC_SUB) + client.subscribe(MQTT_TOPIC_RETURN) + print(f"[MQTT] Subscribed to topic: {MQTT_TOPIC_RETURN}") print(f"[MQTT] Subscribed to topic: {MQTT_TOPIC_SUB}") def on_message(client, userdata, msg): - print(f"[MQTT] Nachricht empfangen: {msg.topic} -> {msg.payload.decode()}") - # Optional an Clients senden - socketio.emit('mqtt_message', { - 'topic': msg.topic, - 'message': msg.payload.decode() - }) + if msg.topic == MQTT_TOPIC_SUB: + print(f"[MQTT] Nachricht empfangen: {msg.topic} -> {msg.payload.decode()}") + refactor_and_use_esp_data(msg.payload.decode()) + elif msg.topic == MQTT_TOPIC_RETURN: + print(f"[MQTT] Nachricht empfangen: {msg.topic} -> {msg.payload.decode()}") + try: + payload = json.loads(msg.payload.decode()) + command_id = payload.get("command_id") + if command_id: + update_command_status(command_id, "served") # form modules + else: + print("[MQTT] Keine command_id im Payload gefunden.") + except json.JSONDecodeError as e: + print(f"[MQTT] Fehler beim Dekodieren der Nachricht: {e}") + else: + print(f"[MQTT] Unbekanntes Topic: {msg.topic}") + return + # MQTT-Thread def mqtt_thread(): @@ -108,7 +126,7 @@ def monitor_esp_connection(): 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() +#threading.Thread(target=mqtt_thread, daemon=True).start() if __name__ == '__main__': #clear_commands_db() diff --git a/static/style.css b/static/style.css index 1fdb997..8615162 100644 --- a/static/style.css +++ b/static/style.css @@ -174,6 +174,7 @@ header { } .not:hover { background: #5f5f5f; + cursor: default; } .clickable .top-left-text { position: absolute;