mirror of
https://gitlab.fdmci.hva.nl/technische-informatica-sm3/ti-projectten/rooziinuubii79.git
synced 2025-08-04 04:14:58 +00:00
105 lines
2.8 KiB
Python
105 lines
2.8 KiB
Python
from flask import Flask, request, render_template, jsonify, g
|
|
import paho.mqtt.client as mqtt
|
|
import mysql.connector
|
|
|
|
app = Flask(__name__)
|
|
|
|
# Globale variabelen
|
|
kobuki_message = ""
|
|
latest_image = None
|
|
|
|
# Globale MQTT setup
|
|
def on_message(client,userdata, message):
|
|
global kobuki_message, latest_image
|
|
if message.topic == "kobuki/data":
|
|
kobuki_message = str(message.payload.decode("utf-8"))
|
|
save_sensor_data(kobuki_message)
|
|
elif message.topic == "kobuki/cam":
|
|
latest_image = message.payload
|
|
|
|
|
|
# Create an MQTT client instance
|
|
mqtt_client = mqtt.Client()
|
|
mqtt_client.username_pw_set("server", "serverwachtwoordofzo")
|
|
mqtt_client.connect("localhost", 1884, 60)
|
|
mqtt_client.loop_start()
|
|
mqtt_client.subscribe("kobuki/data")
|
|
|
|
mqtt_client.on_message = on_message # this line needs to be under the function definition otherwise it can't find which function it needs to use
|
|
|
|
|
|
# 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():
|
|
return render_template('index.html')
|
|
|
|
@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')
|
|
else:
|
|
return ('Unauthorized', 401, {'WWW-Authenticate': 'Basic realm="Login Required"'})
|
|
|
|
@app.route('/data', methods=['GET'])
|
|
def data():
|
|
return kobuki_message
|
|
|
|
@app.route('/move', methods=['POST'])
|
|
def move():
|
|
data = request.get_json()
|
|
direction = data.get("direction")
|
|
|
|
# Verstuur de richting via MQTT
|
|
if direction:
|
|
mqtt_client.publish("home/commands", direction)
|
|
|
|
db_connection = get_db()
|
|
cursor = db_connection.cursor()
|
|
sql = "INSERT INTO command (command) VALUES (%s)"
|
|
value = direction
|
|
cursor.execute(sql, (value,))
|
|
db_connection.commit()
|
|
cursor.close()
|
|
db_connection.close()
|
|
|
|
return jsonify({"status": "success", "direction": direction})
|
|
|
|
@app.route("/database")
|
|
def database():
|
|
db = get_db()
|
|
cursor = db.cursor()
|
|
cursor.execute("SELECT * FROM kobuki_data")
|
|
rows = cursor.fetchall()
|
|
cursor.close()
|
|
return str(rows)
|
|
|
|
def save_sensor_data(data):
|
|
db = get_db()
|
|
cursor = db.cursor()
|
|
sql = "INSERT INTO kobuki_data (data) VALUES (%s)"
|
|
value = data
|
|
cursor.execute(sql, (value,))
|
|
db.commit()
|
|
cursor.close()
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True, port=5000)
|