Added history endpoints

This commit is contained in:
Maximilian Giller 2024-05-02 18:00:27 +02:00
parent 5e3261d1f7
commit 368052f713

View file

@ -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,
}
)