mirror of
https://gitlab.fdmci.hva.nl/technische-informatica-sm3/ti-projectten/rooziinuubii79.git
synced 2025-08-03 20:04:58 +00:00
made function for db connection
This commit is contained in:
@@ -1,16 +1,16 @@
|
||||
from flask import Flask, request, render_template, jsonify
|
||||
from flask import Flask, request, render_template, jsonify, g
|
||||
import paho.mqtt.client as mqtt
|
||||
import mysql.connector
|
||||
import mysql.connector
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# Globale MQTT setup
|
||||
kobuki_message = "empty"
|
||||
def on_message(client, userdata, message):
|
||||
global kobuki_message
|
||||
kobuki_message = str(message.payload.decode("utf-8"))
|
||||
print(kobuki_message)
|
||||
|
||||
# Create an MQTT client instance
|
||||
mqtt_client = mqtt.Client()
|
||||
mqtt_client.username_pw_set("server", "serverwachtwoordofzo")
|
||||
mqtt_client.connect("localhost", 80, 60)
|
||||
@@ -18,13 +18,24 @@ 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"
|
||||
)
|
||||
# Database connectie-functie
|
||||
def get_db():
|
||||
if 'db' not in g: # 'g' is specifiek voor een request en leeft zolang een request duurt
|
||||
g.db = mysql.connector.connect(
|
||||
host="127.0.0.1",
|
||||
port=3306,
|
||||
user="admin",
|
||||
password="kobuki",
|
||||
database="kobuki"
|
||||
)
|
||||
return g.db
|
||||
|
||||
# Sluit de database na elke request
|
||||
@app.teardown_appcontext
|
||||
def close_db(error):
|
||||
db = g.pop('db', None)
|
||||
if db is not None:
|
||||
db.close()
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
@@ -42,11 +53,12 @@ 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()
|
||||
# Haal databaseverbinding op
|
||||
db = get_db()
|
||||
cursor = db.cursor()
|
||||
cursor.execute("INSERT INTO command VALUES (%s)", (direction,))
|
||||
db.commit()
|
||||
cursor.close()
|
||||
cnx.close()
|
||||
|
||||
# Verstuur de richting via MQTT
|
||||
if direction:
|
||||
@@ -54,19 +66,14 @@ def move():
|
||||
|
||||
return jsonify({"status": "success", "direction": direction})
|
||||
|
||||
@app.route('/phpmyadmin/<path:path>')
|
||||
def phpmyadmin_passthrough(path):
|
||||
# Laat Apache deze route direct afhandelen
|
||||
return "", 404
|
||||
|
||||
@app.route("/database")
|
||||
def database():
|
||||
cursor = cnx.cursor()
|
||||
db = get_db()
|
||||
cursor = db.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)
|
||||
app.run(debug=True, port=5000)
|
||||
|
Reference in New Issue
Block a user