From a5c03d9e414a679ef7a2bc746107d72fe39bee45 Mon Sep 17 00:00:00 2001 From: derlole <122916573+derlole@users.noreply.github.com> Date: Tue, 6 May 2025 15:29:53 +0000 Subject: [PATCH] db command storage --- db/commands.db | Bin 0 -> 16384 bytes db/init_scripts/initCom.py | 30 ++++++++++++++++++++++++++++++ routes/esp_routes.py | 24 +++++++++++++++++++++--- 3 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 db/commands.db create mode 100644 db/init_scripts/initCom.py diff --git a/db/commands.db b/db/commands.db new file mode 100644 index 0000000000000000000000000000000000000000..f079480abc8c57bed8359772d0dc1286c14c546d GIT binary patch literal 16384 zcmeI%&rZTH90%|g2pSUp+&Gc^ju42(_y9Vgn#FCv=n^?C6J|nWL)=Kb8V^32&%m2_ zw3}O^1YZEZP1>$&f8F|7FU=-nV-frGuzC)>n9f;)an3G@7-L-JlFE77*UtO?syREk z*jDSGtXO+zwQc=FRY(wk00bZa0SG_<0uX=z1R(H#1xCekv)kp<*t=i)(R!)Qq;BM| zH-0$VZI=4B(4-)#T^WLQ#!fqeRzcu}^C+(izh=qrQoBMb zE#{UbxQ@B!jipoWEMzL+)1*ADm#Yp-4(@NyovVpWI^ta z$2gWgcErvMv6>=yqJdhi)J;JHajiLqq`qU@>J8lF=0s}dsM0wtl$-q)XNz#|zhoxf zCSIj&H(#E6mR}XsRy$6%TQw;fNr40b2tWV=5P$##AOHafKmY;|fWV;&RFZ|^{C}vQ z7wdum1Rwwb2tWV=5P$##AOHafqyqT=kBtBU2tWV=5P$##AOHafKmY;|IDCOGC(?Zk literal 0 HcmV?d00001 diff --git a/db/init_scripts/initCom.py b/db/init_scripts/initCom.py new file mode 100644 index 0000000..f563877 --- /dev/null +++ b/db/init_scripts/initCom.py @@ -0,0 +1,30 @@ +import os +import sqlite3 + +# Ordner und Pfad definieren +db_folder = "db" +db_filename = "commands.db" +db_path = os.path.join(db_folder, db_filename) + +# Ordner erstellen, falls nicht vorhanden +os.makedirs(db_folder, exist_ok=True) + +# Verbindung zur SQLite-Datenbank +conn = sqlite3.connect(db_path) +cursor = conn.cursor() + +# Tabelle erstellen +cursor.execute(""" +CREATE TABLE IF NOT EXISTS commands ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + command TEXT NOT NULL, + status TEXT NOT NULL, + command_id INTEGER UNIQUE NOT NULL, + tstamp DATETIME DEFAULT CURRENT_TIMESTAMP +) +""") + +conn.commit() +conn.close() + +print(f"Datenbank erstellt unter: {db_path}") diff --git a/routes/esp_routes.py b/routes/esp_routes.py index 5bb1900..2454a69 100644 --- a/routes/esp_routes.py +++ b/routes/esp_routes.py @@ -3,8 +3,14 @@ import routes.shared as shared from flask import Flask, jsonify, request import paho.mqtt.client as mqtt import json +import random +import sqlite3 +import os + esp = Blueprint('eps', __name__, url_prefix='/unsecure/esp') +DB_PATH = os.path.join(os.path.dirname(__file__), '../db/commands.db') + MQTT_BROKER = "localhost" # oder IP/Domain MQTT_PORT = 1883 MQTT_TOPIC = "coffee/command" @@ -26,11 +32,23 @@ def esp_online(): @esp.route('/toggle-machine', methods=['GET']) def toggle_machine(): + randID = random.randint(1000, 9999) + fullCommand = {'command': 'toggle_machine', 'status': 'pending', 'command_id': randID} + + conn = sqlite3.connect(DB_PATH) + cursor = conn.cursor() + + cursor.execute(""" + INSERT INTO commands (command, status, command_id) + VALUES (?, ?, ?) + """, (fullCommand["command"], fullCommand["status"], fullCommand["command_id"])) + + conn.commit() + conn.close() - testData = {'test': 'Live-Daten', 'status': 'OK', 'counter': 0} client = mqtt.Client() client.connect(MQTT_BROKER, MQTT_PORT, 60) - client.publish(MQTT_TOPIC, json.dumps(testData)) + client.publish(MQTT_TOPIC, json.dumps(fullCommand)) client.disconnect() - return jsonify({"status": json.dumps(testData)}) \ No newline at end of file + return jsonify({"status": json.dumps(fullCommand)}) \ No newline at end of file