diff --git a/routes/esp_routes.py b/routes/esp_routes.py index 756faa0..70e4d77 100644 --- a/routes/esp_routes.py +++ b/routes/esp_routes.py @@ -50,4 +50,25 @@ def toggle_machine(): client.publish(MQTT_TOPIC, json.dumps(fullCommand)) client.disconnect() + return jsonify({"status": json.dumps(fullCommand)}) + +@esp.route('/make_coffee', methods=['POST']) +def 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() + client = mqtt.Client() + client.connect(MQTT_BROKER, MQTT_PORT, 60) + client.publish(MQTT_TOPIC, json.dumps(fullCommand)) + client.disconnect() + return jsonify({"status": json.dumps(fullCommand)}) \ No newline at end of file diff --git a/server.py b/server.py index 746420a..977df4a 100644 --- a/server.py +++ b/server.py @@ -41,6 +41,7 @@ def on_connect(client, userdata, flags, rc): def on_message(client, userdata, msg): if msg.topic == MQTT_TOPIC_SUB: print(f"[MQTT] Nachricht empfangen: {msg.topic} -> {msg.payload.decode()}") + esp_conn_infos["last_seen"] = datetime.now() refactor_and_use_esp_data(msg.payload.decode()) elif msg.topic == MQTT_TOPIC_RETURN: print(f"[MQTT] Nachricht empfangen: {msg.topic} -> {msg.payload.decode()}") @@ -123,7 +124,7 @@ def monitor_esp_connection(): while True: if esp_conn_infos["last_seen"]: time_diff = datetime.now() - esp_conn_infos["last_seen"] - if time_diff > timedelta(minutes=30): + if time_diff > timedelta(minutes=3): esp_conn_infos["connection_valid"] = False resend_static_data() time.sleep(60) # einmal pro Minute die Verbindung zum ESP prüfen diff --git a/static/socketio.js b/static/socketio.js index 5a34657..798a1e7 100644 --- a/static/socketio.js +++ b/static/socketio.js @@ -15,24 +15,25 @@ socket.on('static_data', (data) => { gebId("ip_local").innerText = data.esp_conn_infos.ip_local gebId("valid_connection").innerText = data.esp_conn_infos.connection_valid gebId("last_seen").innerText = data.esp_conn_infos.last_seen + stBut = gebId("machine-status-butt") switch (data.machine.state) { case "ON": gebId("machine-status").innerText = "AN"; - gebId("machine-status-butt").classList.remove("blink-orange"); - gebId("machine-status-butt").classList.remove("initBackRed"); - gebId("machine-status-butt").classList.add("initBackGreen"); + stBut.classList.remove("blink-orange"); + stBut.classList.remove("initBackRed"); + stBut.classList.add("initBackGreen"); break; case "PENDING": gebId("machine-status").innerText = "WARTEN"; - gebId("machine-status-butt").classList.add("blink-orange"); - gebId("machine-status-butt").classList.remove("initBackRed"); - gebId("machine-status-butt").classList.remove("initBackGreen"); + stBut.classList.add("blink-orange"); + stBut.classList.remove("initBackRed"); + stBut.classList.remove("initBackGreen"); break; case "OFF": gebId("machine-status").innerText = "AUS"; - gebId("machine-status-butt").classList.remove("blink-orange"); - gebId("machine-status-butt").classList.add("initBackRed"); - gebId("machine-status-butt").classList.remove("initBackGreen"); + stBut.classList.remove("blink-orange"); + stBut.classList.add("initBackRed"); + stBut.classList.remove("initBackGreen"); break; default: @@ -60,11 +61,11 @@ socket.on('static_data', (data) => { } if(data.machine.berror){ gebId("machine-error-butt").classList.add("initBackRed"); - gebId("machiene-error-text").innerText = machine.error; + gebId("machiene-error-text").innerText = data.machine.error; } else { gebId("machine-error-butt").classList.remove("initBackRed"); - gebId("machiene-error-text").innerText = ""; + gebId("machiene-error-text").innerText = "Keiner"; } machienReady = gebId("machine-ready-butt") makeCoffee = gebId("make-coffee-butt") @@ -74,12 +75,12 @@ socket.on('static_data', (data) => { makeCoffee.classList.add("deniePress"); } if(data.machine.ready){ - machienReady.classList.add("initBackRed"); - machienReady.classList.remove("initBackGreen"); - gebId("machiene-ready-text").innerText = "Bereit"; - }else { machienReady.classList.remove("initBackRed"); machienReady.classList.add("initBackGreen"); + gebId("machiene-ready-text").innerText = "Bereit"; + }else { + machienReady.classList.add("initBackRed"); + machienReady.classList.remove("initBackGreen"); gebId("machiene-ready-text").innerText = "Nicht bereit"; } });