|
| 1 | +import csv |
| 2 | +import os |
| 3 | +import time |
| 4 | + |
| 5 | +from ament_index_python import get_package_share_directory |
| 6 | +from flask import Flask, render_template, request |
| 7 | + |
| 8 | +template_dir = os.path.join(get_package_share_directory("bitbots_education"), "templates") |
| 9 | +static_folder = os.path.join(get_package_share_directory("bitbots_education"), "static") |
| 10 | +csv_file_path = os.path.realpath("logs.csv") |
| 11 | + |
| 12 | +app = Flask(__name__, template_folder=template_dir, static_folder=static_folder) |
| 13 | + |
| 14 | + |
| 15 | +def write_csv(vp, page, button, status): |
| 16 | + file_exists = os.path.isfile(csv_file_path) |
| 17 | + with open(csv_file_path, "a+", newline="") as file: |
| 18 | + writer = csv.writer(file) |
| 19 | + # Kopfzeile nur schreiben, wenn die Datei neu ist |
| 20 | + if not file_exists or os.stat("logs.csv").st_size == 0: |
| 21 | + writer.writerow(["VP", "Page", "Button", "Status", "Timestamp"]) |
| 22 | + curr_time = time.time() |
| 23 | + writer.writerow([vp, page, button, status, curr_time]) |
| 24 | + |
| 25 | + |
| 26 | +def vp_track(page, button, status): |
| 27 | + username = request.cookies.get("vp_number") |
| 28 | + if username is not None: |
| 29 | + write_csv(username, page, button, status) |
| 30 | + |
| 31 | + |
| 32 | +@app.route("/") |
| 33 | +def index(): |
| 34 | + vp_track("dashboard", "", "") |
| 35 | + dashboard = "pages/dashboard.html" |
| 36 | + return render_template(dashboard, logging=bool(request.args.get("logging", False))) |
| 37 | + |
| 38 | + |
| 39 | +@app.route("/imu") |
| 40 | +def imu(): |
| 41 | + vp_track("imu", "", "") |
| 42 | + return render_template("pages/imu.html") |
| 43 | + |
| 44 | + |
| 45 | +@app.route("/vision") |
| 46 | +def vision(): |
| 47 | + vp_track("vision", "", "") |
| 48 | + return render_template("pages/vision.html") |
| 49 | + |
| 50 | + |
| 51 | +@app.route("/motors") |
| 52 | +def motors(): |
| 53 | + vp_track("motors", "", "") |
| 54 | + return render_template("pages/motors.html") |
| 55 | + |
| 56 | + |
| 57 | +@app.route("/behavior") |
| 58 | +def behavior(): |
| 59 | + vp_track("behavior", "", "") |
| 60 | + return render_template("pages/behavior.html") |
| 61 | + |
| 62 | + |
| 63 | +@app.route("/logs") |
| 64 | +def logs(): |
| 65 | + page = request.args.get("page") |
| 66 | + button = request.args.get("button") |
| 67 | + status = request.args.get("status") |
| 68 | + vp_track(page[1:], button, status) |
| 69 | + return ("", 204) |
| 70 | + |
| 71 | + |
| 72 | +def main(): |
| 73 | + app.run(host="0.0.0.0", port=8000) |
0 commit comments