PythonController/main.py

52 lines
1.4 KiB
Python

from flask import request, Flask, render_template
# Flask server boilerplate
app = Flask(__name__)
app.debug=True
app.config['SECRET_KEY'] = 'Thisisasecret!' # Feel free to change this TODO: Make this change itself each boot
# Values used to track the train's throttle and direction.
throttle: float = 0.0
direction: int = 0
@app.route('/')
def index():
"""
Renders the webpage if you view the IP address and port
"""
return render_template('index.html')
@app.route("/data", methods=["POST"])
def data() -> str:
"""
Used by the webpage to set the throttle and/or travel direction
:return: Always returns "Success" (200)
"""
global throttle, direction
print(request.json)
if "throttle" in request.json:
throttle = int(request.json["throttle"]) / 100.0
print(throttle)
if "direction" in request.json:
direction = int(request.json["direction"])
print(direction)
return "Success"
@app.route("/control", methods=["GET"])
def control() -> dict[str, str]:
"""
Used by the train controller to determine what the throttle and direction currently are
:return: A dictionary containing the throttle and direction values, will be jsonified
"""
return {
"throttle": throttle,
"direction": direction
}
if __name__=="__main__":
# Listen on all interfaces on port 2093
app.run(host="0.0.0.0", port=2093)