From 4307d0a8d5d3048ff1a1503b04f3ed62317f7faf Mon Sep 17 00:00:00 2001 From: "ishak jmilou.ishak" Date: Tue, 17 Dec 2024 14:57:01 +0100 Subject: [PATCH] test if i still get error --- src/Python/flask/web/app.py | 42 ++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/Python/flask/web/app.py b/src/Python/flask/web/app.py index 4a5ef76..365ff8f 100644 --- a/src/Python/flask/web/app.py +++ b/src/Python/flask/web/app.py @@ -50,21 +50,39 @@ def control(): @app.route('/move', methods=['POST']) def move(): - data = request.get_json() - direction = data.get("direction") - - db_connection = get_db() - cursor = db_connection.cursor() - cursor.execute("INSERT INTO command (direction) VALUES (%s)", (direction,)) - db_connection.commit() - cursor.close() - db_connection.close() + try: + # Haal de richting op van de request + data = request.get_json() + direction = data.get("direction") - # Verstuur de richting via MQTT - if direction: + if not direction: + return jsonify({"status": "error", "message": "Direction is required"}), 400 + + # Maak verbinding met de database + conn = get_db() + cursor = conn.cursor() + + # Sla de richting op in de database + cursor.execute("INSERT INTO command (direction) VALUES (%s)", (direction,)) + conn.commit() + + # Sluit de cursor en de verbinding + cursor.close() + conn.close() + + # Verstuur de richting via MQTT mqtt_client.publish("home/commands", direction) - return jsonify({"status": "success", "direction": direction}) + return jsonify({"status": "success", "direction": direction}) + + except mysql.connector.Error as e: + # Foutafhandeling voor databasefouten + return jsonify({"status": "error", "message": str(e)}), 500 + + except Exception as e: + # Algemene foutafhandeling + return jsonify({"status": "error", "message": "An error occurred", "details": str(e)}), 500 + @app.route('/data', methods=['GET']) def data():