From 651dcbc6a5e10ac5ea7d8c6a9d6123f2cd7885b6 Mon Sep 17 00:00:00 2001 From: "ishak jmilou.ishak" Date: Tue, 17 Dec 2024 14:01:15 +0100 Subject: [PATCH] see if connection is actually made --- src/Python/flask/web/app.py | 50 +++++++++++++++---------------------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/src/Python/flask/web/app.py b/src/Python/flask/web/app.py index 18c12b1..1904e3e 100644 --- a/src/Python/flask/web/app.py +++ b/src/Python/flask/web/app.py @@ -6,7 +6,7 @@ app = Flask(__name__) kobuki_message = "empty" def on_message(client, userdata, message): - global kobuki_message #set scope for this variable + global kobuki_message kobuki_message = str(message.payload.decode("utf-8")) print(kobuki_message) @@ -16,24 +16,13 @@ mqtt_client.username_pw_set("server", "serverwachtwoordofzo") mqtt_client.connect("localhost", 80, 60) mqtt_client.loop_start() mqtt_client.subscribe("kobuki/data") -mqtt_client.on_message = on_message # this lines needs to be under the function definition otherwise it cant find which function it needs to use - - -cnx = mysql.connector.connect( - host="127.0.0.1", - port=3306, - user="admin", - password="kobuki", - database="kobuki") - -cnx.close() +mqtt_client.on_message = on_message @app.route('/') def index(): return render_template('index.html') - -@app.route('/control', methods=["GET","POST"]) +@app.route('/control', methods=["GET", "POST"]) def control(): if request.authorization and request.authorization.username == 'ishak' and request.authorization.password == 'kobuki': return render_template('control.html') @@ -47,15 +36,10 @@ def move(): # Verstuur de richting via MQTT if direction: - mqtt_client.publish("home/commands", direction) # Het topic kan aangepast worden + mqtt_client.publish("home/commands", direction) return jsonify({"status": "success", "direction": direction}) - -@app.route('/data', methods=['GET']) -def data(): - return kobuki_message - @app.route('/phpmyadmin/') def phpmyadmin_passthrough(path): # Laat Apache deze route direct afhandelen @@ -64,16 +48,22 @@ def phpmyadmin_passthrough(path): @app.route("/database") def database(): try: - with cnx.cursor() as cur: - cur.execute("SELECT DATABASE()") - rows = cur.fetchall() # Haal alle rijen op uit het resultaat + 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 Exception as e: - print(f"Database error: {e}") - return "je hebt iets fout gedaan.", 500 - -def add_command(command): - command_query = "INSERT INTO kobuki (command) VALUES ();" + except mysql.connector.Error as err: + print(f"Database error: {err}") + return f"Database error: {err}", 500 if __name__ == '__main__': - app.run(debug=True, port=5000) + app.run(debug=True, port=5000) \ No newline at end of file