mirror of
https://gitlab.fdmci.hva.nl/technische-informatica-sm3/ti-projectten/rooziinuubii79.git
synced 2025-08-03 20:04:58 +00:00
fix: update sensor_data function to process and store JSON message from kobuki
This commit is contained in:
@@ -14,8 +14,8 @@ def on_message(client,userdata, message):
|
|||||||
global kobuki_message, latest_image
|
global kobuki_message, latest_image
|
||||||
if message.topic == "kobuki/data":
|
if message.topic == "kobuki/data":
|
||||||
kobuki_message = str(message.payload.decode("utf-8"))
|
kobuki_message = str(message.payload.decode("utf-8"))
|
||||||
# with app.app_context():
|
with app.app_context():
|
||||||
# sensor_data() # Sla de data op in de database
|
sensor_data(kobuki_message) # Sla de data op in de database
|
||||||
elif message.topic == "kobuki/cam":
|
elif message.topic == "kobuki/cam":
|
||||||
latest_image = message.payload
|
latest_image = message.payload
|
||||||
|
|
||||||
@@ -82,7 +82,6 @@ def move():
|
|||||||
db_connection.commit()
|
db_connection.commit()
|
||||||
cursor.close()
|
cursor.close()
|
||||||
db_connection.close()
|
db_connection.close()
|
||||||
print(f"check: {data}")
|
|
||||||
return jsonify({"status": "success", "direction": direction})
|
return jsonify({"status": "success", "direction": direction})
|
||||||
|
|
||||||
@app.route("/database")
|
@app.route("/database")
|
||||||
@@ -94,16 +93,39 @@ def database():
|
|||||||
cursor.close()
|
cursor.close()
|
||||||
return str(rows)
|
return str(rows)
|
||||||
|
|
||||||
# def sensor_data():
|
def sensor_data(kobuki_message):
|
||||||
# data = request.get_json()
|
try:
|
||||||
# db = get_db()
|
# Parse de JSON-string naar een Python-dictionary
|
||||||
# cursor = db.cursor()
|
data = json.loads(kobuki_message)
|
||||||
# sql_sensor = "INSERT INTO kobuki_data (name, value) VALUES (%s)"
|
|
||||||
# sensor_data_tuples = [(name, float(value))for name, value in data.items()]
|
|
||||||
# cursor.executemany(sql_sensor, sensor_data_tuples)
|
|
||||||
|
|
||||||
# db.commit()
|
# Maak een lijst van tuples met de naam en waarde van elk veld
|
||||||
# cursor.close()
|
sensor_data_tuples = [(name, float(value)) for name, value in data.items() if isinstance(value, (int, float))]
|
||||||
|
|
||||||
|
# Extra informatie of nested data (zoals "extraInfo" of "gyroData") kun je apart verwerken
|
||||||
|
if "extraInfo" in data:
|
||||||
|
for key, value in data["extraInfo"].items():
|
||||||
|
sensor_data_tuples.append((f"extraInfo_{key}", float(value)))
|
||||||
|
|
||||||
|
if "gyroData" in data:
|
||||||
|
for i, gyro in enumerate(data["gyroData"]):
|
||||||
|
for axis, value in gyro.items():
|
||||||
|
sensor_data_tuples.append((f"gyroData_{i}_{axis}", float(value)))
|
||||||
|
|
||||||
|
# Database-insert
|
||||||
|
db = get_db()
|
||||||
|
cursor = db.cursor()
|
||||||
|
|
||||||
|
# Zorg dat je tabel `kobuki_data` kolommen heeft: `name` en `value`
|
||||||
|
sql_sensor = "INSERT INTO kobuki_data (name, value) VALUES (%s, %s)"
|
||||||
|
cursor.executemany(sql_sensor, sensor_data_tuples)
|
||||||
|
|
||||||
|
# Commit en sluit de cursor
|
||||||
|
db.commit()
|
||||||
|
cursor.close()
|
||||||
|
except json.JSONDecodeError as e:
|
||||||
|
print(f"JSON decode error: {e}")
|
||||||
|
except mysql.connector.Error as err:
|
||||||
|
print(f"Database error: {err}")
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(debug=True, port=5000)
|
app.run(debug=True, port=5000)
|
||||||
|
Reference in New Issue
Block a user