From 10a7a2b98c666425695635b8f0dbdf36c17f5f47 Mon Sep 17 00:00:00 2001 From: "ishak jmilou.ishak" Date: Tue, 17 Dec 2024 14:08:42 +0100 Subject: [PATCH] add direction to db when pressed button --- src/Python/flask/web/app.py | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/Python/flask/web/app.py b/src/Python/flask/web/app.py index 1904e3e..1c9ecc5 100644 --- a/src/Python/flask/web/app.py +++ b/src/Python/flask/web/app.py @@ -18,6 +18,14 @@ mqtt_client.loop_start() mqtt_client.subscribe("kobuki/data") mqtt_client.on_message = on_message +cnx = mysql.connector.connect( + host="127.0.0.1", + port=3306, + user="admin", + password="kobuki", + database="kobuki" +) + @app.route('/') def index(): return render_template('index.html') @@ -33,6 +41,12 @@ def control(): def move(): data = request.get_json() direction = data.get("direction") + + cursor = cnx.cursor() + cursor.execute("INSERT INTO kobuki_data (command) VALUES (%s)", (direction,)) + cnx.commit() + cursor.close() + cnx.close() # Verstuur de richting via MQTT if direction: @@ -47,23 +61,12 @@ def phpmyadmin_passthrough(path): @app.route("/database") def database(): - try: - cnx = mysql.connector.connect( - host="127.0.0.1", - port=3306, - user="admin", - password="kobuki", - database="kobuki" - ) - cursor = cnx.cursor() - cursor.execute("SELECT * FROM kobuki_data") - rows = cursor.fetchall() - cursor.close() - cnx.close() - return str(rows) - except mysql.connector.Error as err: - print(f"Database error: {err}") - return f"Database error: {err}", 500 + cursor = cnx.cursor() + cursor.execute("SELECT * FROM kobuki_data") + rows = cursor.fetchall() + cursor.close() + cnx.close() + return str(rows) if __name__ == '__main__': app.run(debug=True, port=5000) \ No newline at end of file