Refactor move endpoint to accept JSON input and update form submission method

This commit is contained in:
ishak jmilou.ishak
2024-11-04 11:54:21 +01:00
parent 17e1399643
commit ca9b81c03e
3 changed files with 28 additions and 34 deletions

View File

@@ -15,18 +15,14 @@ def index():
@app.route('/move', methods=['POST'])
def move():
# Get the direction from the form data
direction = request.form['direction']
# Publish the direction to the MQTT topic "home/commands"
result = mqtt_client.publish("home/commands", direction)
# Check if the publish was successful
if result.rc == mqtt.MQTT_ERR_SUCCESS:
return jsonify({"message": "Bericht succesvol gepubliceerd"}), 200
else:
return jsonify({"message": "Fout bij het publiceren van bericht"}), 500
data = request.get_json()
direction = data.get("direction")
# Verstuur de richting via MQTT
if direction:
mqtt_client.publish("home/commands", direction) # Het topic kan aangepast worden
return jsonify({"status": "success", "direction": direction})
# Run the Flask application in debug mode
if __name__ == '__main__':
app.run(debug=True)