db command storage
This commit is contained in:
BIN
db/commands.db
Normal file
BIN
db/commands.db
Normal file
Binary file not shown.
30
db/init_scripts/initCom.py
Normal file
30
db/init_scripts/initCom.py
Normal file
@@ -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}")
|
||||||
@@ -3,8 +3,14 @@ import routes.shared as shared
|
|||||||
from flask import Flask, jsonify, request
|
from flask import Flask, jsonify, request
|
||||||
import paho.mqtt.client as mqtt
|
import paho.mqtt.client as mqtt
|
||||||
import json
|
import json
|
||||||
|
import random
|
||||||
|
import sqlite3
|
||||||
|
import os
|
||||||
|
|
||||||
esp = Blueprint('eps', __name__, url_prefix='/unsecure/esp')
|
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_BROKER = "localhost" # oder IP/Domain
|
||||||
MQTT_PORT = 1883
|
MQTT_PORT = 1883
|
||||||
MQTT_TOPIC = "coffee/command"
|
MQTT_TOPIC = "coffee/command"
|
||||||
@@ -26,11 +32,23 @@ def esp_online():
|
|||||||
|
|
||||||
@esp.route('/toggle-machine', methods=['GET'])
|
@esp.route('/toggle-machine', methods=['GET'])
|
||||||
def toggle_machine():
|
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 = mqtt.Client()
|
||||||
client.connect(MQTT_BROKER, MQTT_PORT, 60)
|
client.connect(MQTT_BROKER, MQTT_PORT, 60)
|
||||||
client.publish(MQTT_TOPIC, json.dumps(testData))
|
client.publish(MQTT_TOPIC, json.dumps(fullCommand))
|
||||||
client.disconnect()
|
client.disconnect()
|
||||||
|
|
||||||
return jsonify({"status": json.dumps(testData)})
|
return jsonify({"status": json.dumps(fullCommand)})
|
||||||
Reference in New Issue
Block a user