From 6d90b34929735ba9a5438335a02a607c23ad2f56 Mon Sep 17 00:00:00 2001 From: derlole <122916573+derlole@users.noreply.github.com> Date: Mon, 12 May 2025 18:30:20 +0000 Subject: [PATCH] jikes --- modules/other.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 2 deletions(-) diff --git a/modules/other.py b/modules/other.py index a5d6571..a808075 100644 --- a/modules/other.py +++ b/modules/other.py @@ -1,4 +1,3 @@ - # this function is processing the cyclic data from the esp format: # [off_value/on_value] # { @@ -19,5 +18,77 @@ # "kaffee_fertig": [0/1], # } +from modules.persistence import load_dict, save_dict +from modules.socketio import resend_static_data +import os +import sqlite3 + +DB_PATH_COFFEE = os.path.join(os.path.dirname(__file__), '../db/coffee.db') + +def track_coffee_made(data, flanksUp, flanksDown): + coffee_made = False + #logic for tracking coffee made + + if coffee_made: + conn = sqlite3.connect(DB_PATH_COFFEE) + cursor = conn.cursor() + cursor.execute(""" + INSERT INTO coffee (user, status) + VALUES (?, ?) + """, ("admin", "served")) + conn.commit() + conn.close() + print("Coffee made detected, data saved.") + return + +def track_error_water(data, flanksUp, flanksDown): + water = load_dict("water") + if water["fill"] <= 7: + return True + return False + +def track_error_beans(data, flanksUp, flanksDown): + beans = load_dict("beans") + if beans["fill"] <= 7: + return True + return False + +def track_error(data, flanksUp, flanksDown): + if track_error_water(data, flanksUp, flanksDown): + return "Wasser Leer" + elif track_error_beans(data, flanksUp, flanksDown): + return "Bohnen Leer" + return "Unbekannter Fehler" + def refactor_and_use_esp_data(data): - return + # global oldDataSet + if 'oldDataSet' not in globals() or oldDataSet is None: + oldDataSet = data # Initialize oldDataSet with default values + + flanksUp = {key: (oldDataSet[key] == 0 and data[key] == 1) for key in data} + flanksDown = {key: (oldDataSet[key] == 1 and data[key] == 0) for key in data} + + machine = load_dict("machine") + if data["an"] == 0: + machine["state"] = "ON" + elif data["an"] == 1: #elif die Sängerin xD + machine["state"] = "OFF" + + if data["bereit"] == 0: + machine["ready"] = True + elif data["bereit"] == 1: + machine["ready"] = False + + if data["fehler"] == 0: + machine["berror"] = True + machine["error"] = track_error(data, flanksUp, flanksDown) + elif data["fehler"] == 1: + machine["berror"] = False + machine["error"] = "Keine Fehler" + + save_dict("machine", machine) + resend_static_data() + + track_coffee_made(data, flanksUp, flanksDown) + oldDataSet = data + return \ No newline at end of file