diff --git a/src/endpoints/bettwaage.py b/src/endpoints/bettwaage.py index 0c8e577..51a1b60 100644 --- a/src/endpoints/bettwaage.py +++ b/src/endpoints/bettwaage.py @@ -1,9 +1,10 @@ +from fastapi.responses import HTMLResponse, JSONResponse from fastapi import APIRouter from datetime import datetime import os +import csv -from fastapi.responses import HTMLResponse, JSONResponse router = APIRouter() @@ -29,6 +30,32 @@ async def get_file(): return HTMLResponse("\n".join(fp.readlines())) +@router.get("/file", tags=["file"]) +async def get_history(count: int = None) -> []: + points = [] + with open(file_path, "r", encoding="UTF-8") as fp: + reader = csv.DictReader(fp, delimiter=";") + for row in reader: + if not row: + continue + + points.append( + { + "timestamp": row["timestamp"], + "total": float(row["total"]), + "tl": float(row["tl"]), + "tr": float(row["tr"]), + "bl": float(row["bl"]), + "br": float(row["br"]), + } + ) + + if count: + return points[-count] + else: + return points + + @router.post("/add") async def add_weight(tl: int, tr: int, bl: int, br: int): global latest_values @@ -48,10 +75,10 @@ async def get_latest(): total = sum(latest_values) return JSONResponse( { - "top-left": latest_values[0], - "top-right": latest_values[1], - "bottom-left": latest_values[2], - "bottom-right": latest_values[3], + "tl": latest_values[0], + "tr": latest_values[1], + "bl": latest_values[2], + "br": latest_values[3], "total": total, } )