modules.db.py integrity

This commit is contained in:
derlole
2025-05-13 10:29:48 +00:00
parent bf3a025427
commit e8951786a7
6 changed files with 91 additions and 41 deletions

Binary file not shown.

View File

@@ -1,11 +1,13 @@
import sqlite3 import sqlite3
import os import os
from datetime import datetime, timedelta from datetime import datetime, timedelta
from modules.socketio import resend_static_data
import random
DB_PATH = os.path.join(os.path.dirname(__file__), '../db/commands.db') DB_PATH = os.path.join(os.path.dirname(__file__), '../db/commands.db')
DB_PATH_COFFEE = os.path.join(os.path.dirname(__file__), '../db/coffee.db') DB_PATH_COFFEE = os.path.join(os.path.dirname(__file__), '../db/coffee.db')
### THIS CODE IS NOT PROOFED BUT LOOKS RIGHT###
def update_command_status(command_id, status): def update_command_status(command_id, status):
conn = sqlite3.connect(DB_PATH) conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor() cursor = conn.cursor()
@@ -20,7 +22,6 @@ def update_command_status(command_id, status):
conn.close() conn.close()
print(f"[DB] Befehl {command_id} auf {status} aktualisiert.") print(f"[DB] Befehl {command_id} auf {status} aktualisiert.")
return status return status
### THIS CODE IS NOT PROOFED BUT LOOKS RIGHT###
def get_coffee_count(): def get_coffee_count():
conn = sqlite3.connect(DB_PATH_COFFEE) conn = sqlite3.connect(DB_PATH_COFFEE)
@@ -41,3 +42,44 @@ def get_coffees():
conn.close() conn.close()
return coffees return coffees
def create_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"]))
resend_static_data()
conn.commit()
conn.close()
return fullCommand
def create_make_coffee():
randID = random.randint(1000, 9999)
fullCommand = {'command': 'make_coffee', '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()
return fullCommand
def create_coffee_entry():
conn = sqlite3.connect(DB_PATH_COFFEE)
cursor = conn.cursor()
cursor.execute("""
INSERT INTO coffee (user, status)
VALUES (?, ?)
""", ("admin", "served"))
conn.commit()
conn.close()

View File

@@ -20,6 +20,7 @@
from modules.persistence import load_dict, save_dict from modules.persistence import load_dict, save_dict
from modules.socketio import resend_static_data from modules.socketio import resend_static_data
from modules.db import create_coffee_entry
import os import os
import sqlite3 import sqlite3
@@ -30,15 +31,18 @@ def track_coffee_made(data, flanksUp, flanksDown):
#logic for tracking coffee made #logic for tracking coffee made
if coffee_made: if coffee_made:
conn = sqlite3.connect(DB_PATH_COFFEE) create_coffee_entry()
cursor = conn.cursor() #change to insert coffee at modules.db
cursor.execute(""" # conn = sqlite3.connect(DB_PATH_COFFEE)
INSERT INTO coffee (user, status) # cursor = conn.cursor()
VALUES (?, ?) # cursor.execute("""
""", ("admin", "served")) # INSERT INTO coffee (user, status)
conn.commit() # VALUES (?, ?)
conn.close() # """, ("admin", "served"))
# conn.commit()
# conn.close()
print("Coffee made detected, data saved.") print("Coffee made detected, data saved.")
# update water fill and beans fill and coffeeOn water and beans
return return
def track_error_water(data, flanksUp, flanksDown): def track_error_water(data, flanksUp, flanksDown):

View File

@@ -1,5 +1,5 @@
{ {
"state": "OFF", "state": "PENDING",
"connected": false, "connected": false,
"ready": false, "ready": false,
"peding_command": false, "peding_command": false,

View File

@@ -9,6 +9,7 @@ from modules.persistence import esp_conn_infos
from datetime import datetime, timedelta from datetime import datetime, timedelta
from modules.socketio import resend_static_data from modules.socketio import resend_static_data
from modules.persistence import load_dict, save_dict from modules.persistence import load_dict, save_dict
from modules.db import create_toggle_machine, create_make_coffee
esp = Blueprint('eps', __name__, url_prefix='/unsecure/esp') esp = Blueprint('eps', __name__, url_prefix='/unsecure/esp')
@@ -34,25 +35,27 @@ def esp_online():
@esp.route('/toggle-machine', methods=['POST']) @esp.route('/toggle-machine', methods=['POST'])
def toggle_machine(): def toggle_machine():
randID = random.randint(1000, 9999) fullCommand = create_toggle_machine()
fullCommand = {'command': 'toggle_machine', 'status': 'pending', 'command_id': randID} # randID = random.randint(1000, 9999)
conn = sqlite3.connect(DB_PATH) # fullCommand = {'command': 'toggle_machine', 'status': 'pending', 'command_id': randID}
cursor = conn.cursor() # conn = sqlite3.connect(DB_PATH)
# cursor = conn.cursor()
cursor.execute(""" # cursor.execute("""
INSERT INTO commands (command, status, command_id) # INSERT INTO commands (command, status, command_id)
VALUES (?, ?, ?) # VALUES (?, ?, ?)
""", (fullCommand["command"], fullCommand["status"], fullCommand["command_id"])) # """, (fullCommand["command"], fullCommand["status"], fullCommand["command_id"]))
new_status = load_dict("machine") new_status = load_dict("machine")
new_status["state"] = "PENDING" new_status["state"] = "PENDING"
save_dict("machine", new_status) save_dict("machine", new_status)
resend_static_data() # resend_static_data()
# conn.commit()
# conn.close()
conn.commit()
conn.close()
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(fullCommand)) client.publish(MQTT_TOPIC, json.dumps(fullCommand))
@@ -62,18 +65,19 @@ def toggle_machine():
@esp.route('/make_coffee', methods=['POST']) @esp.route('/make_coffee', methods=['POST'])
def make_coffee(): def make_coffee():
randID = random.randint(1000, 9999) fullCommand = create_make_coffee()
fullCommand = {'command': 'make_coffee', 'status': 'pending', 'command_id': randID} # randID = random.randint(1000, 9999)
conn = sqlite3.connect(DB_PATH) # fullCommand = {'command': 'make_coffee', 'status': 'pending', 'command_id': randID}
cursor = conn.cursor() # conn = sqlite3.connect(DB_PATH)
# cursor = conn.cursor()
cursor.execute(""" # cursor.execute("""
INSERT INTO commands (command, status, command_id) # INSERT INTO commands (command, status, command_id)
VALUES (?, ?, ?) # VALUES (?, ?, ?)
""", (fullCommand["command"], fullCommand["status"], fullCommand["command_id"])) # """, (fullCommand["command"], fullCommand["status"], fullCommand["command_id"]))
conn.commit() # conn.commit()
conn.close() # conn.close()
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(fullCommand)) client.publish(MQTT_TOPIC, json.dumps(fullCommand))

View File

@@ -80,14 +80,14 @@ switch (machine.state) {
} }
// All there // All there
function toggleMachine() { function toggleMachine() {
if (gebId("machine-status-butt").classList.contains("deniePress")){ // if (gebId("machine-status-butt").classList.contains("deniePress")){
return; // return;
} // }
// console.log("toggleMachine"); // // console.log("toggleMachine");
const result = confirm("Möchtest du den Vorgang wirklich ausführen?"); // const result = confirm("Möchtest du den Vorgang wirklich ausführen?");
if (!result) { // if (!result) {
return; // return;
} // }
// console.log("toggleMachine"); // console.log("toggleMachine");
fetch('/unsecure/esp/toggle-machine', { method: 'POST' }) fetch('/unsecure/esp/toggle-machine', { method: 'POST' })
.then(res => res.json()) .then(res => res.json())