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 paho.mqtt.client as mqtt
|
||||||
import mysql.connector
|
import mysql.connector
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
# Globale MQTT setup
|
||||||
kobuki_message = "empty"
|
kobuki_message = "empty"
|
||||||
def on_message(client, userdata, message):
|
def on_message(client, userdata, message):
|
||||||
global kobuki_message
|
global kobuki_message
|
||||||
kobuki_message = str(message.payload.decode("utf-8"))
|
kobuki_message = str(message.payload.decode("utf-8"))
|
||||||
print(kobuki_message)
|
print(kobuki_message)
|
||||||
|
|
||||||
# Create an MQTT client instance
|
|
||||||
mqtt_client = mqtt.Client()
|
mqtt_client = mqtt.Client()
|
||||||
mqtt_client.username_pw_set("server", "serverwachtwoordofzo")
|
mqtt_client.username_pw_set("server", "serverwachtwoordofzo")
|
||||||
mqtt_client.connect("localhost", 80, 60)
|
mqtt_client.connect("localhost", 80, 60)
|
||||||
@@ -18,13 +18,24 @@ mqtt_client.loop_start()
|
|||||||
mqtt_client.subscribe("kobuki/data")
|
mqtt_client.subscribe("kobuki/data")
|
||||||
mqtt_client.on_message = on_message
|
mqtt_client.on_message = on_message
|
||||||
|
|
||||||
cnx = mysql.connector.connect(
|
# Database connectie-functie
|
||||||
host="127.0.0.1",
|
def get_db():
|
||||||
port=3306,
|
if 'db' not in g: # 'g' is specifiek voor een request en leeft zolang een request duurt
|
||||||
user="admin",
|
g.db = mysql.connector.connect(
|
||||||
password="kobuki",
|
host="127.0.0.1",
|
||||||
database="kobuki"
|
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('/')
|
@app.route('/')
|
||||||
def index():
|
def index():
|
||||||
@@ -42,11 +53,12 @@ def move():
|
|||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
direction = data.get("direction")
|
direction = data.get("direction")
|
||||||
|
|
||||||
cursor = cnx.cursor()
|
# Haal databaseverbinding op
|
||||||
cursor.execute("INSERT INTO kobuki_data (command) VALUES (%s)", (direction,))
|
db = get_db()
|
||||||
cnx.commit()
|
cursor = db.cursor()
|
||||||
|
cursor.execute("INSERT INTO command VALUES (%s)", (direction,))
|
||||||
|
db.commit()
|
||||||
cursor.close()
|
cursor.close()
|
||||||
cnx.close()
|
|
||||||
|
|
||||||
# Verstuur de richting via MQTT
|
# Verstuur de richting via MQTT
|
||||||
if direction:
|
if direction:
|
||||||
@@ -54,18 +66,13 @@ def move():
|
|||||||
|
|
||||||
return jsonify({"status": "success", "direction": direction})
|
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")
|
@app.route("/database")
|
||||||
def database():
|
def database():
|
||||||
cursor = cnx.cursor()
|
db = get_db()
|
||||||
|
cursor = db.cursor()
|
||||||
cursor.execute("SELECT * FROM kobuki_data")
|
cursor.execute("SELECT * FROM kobuki_data")
|
||||||
rows = cursor.fetchall()
|
rows = cursor.fetchall()
|
||||||
cursor.close()
|
cursor.close()
|
||||||
cnx.close()
|
|
||||||
return str(rows)
|
return str(rows)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Reference in New Issue
Block a user