From 47bd8c2fdd81dda2c51ccaefd61d066f0f7d8451 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Thu, 2 May 2024 17:31:38 +0200 Subject: [PATCH 01/25] Added basic bettwaage endpoints --- src/endpoints/bettwaage.py | 31 +++++++++++++++++++++++++++++++ src/main.py | 2 ++ 2 files changed, 33 insertions(+) create mode 100644 src/endpoints/bettwaage.py diff --git a/src/endpoints/bettwaage.py b/src/endpoints/bettwaage.py new file mode 100644 index 0000000..4975b53 --- /dev/null +++ b/src/endpoints/bettwaage.py @@ -0,0 +1,31 @@ +from fastapi import APIRouter + +from datetime import datetime +from os.path import exists + +from fastapi.responses import HTMLResponse + +router = APIRouter() + +file_path = "bettwaage.csv" + + +def add_line_to_history(line: str) -> None: + with open(file_path, "+a", encoding="UTF-8") as fp: + fp.writelines([line]) + + +@router.get("/file") +async def get_file(): + with open(file_path, "r", encoding="UTF-8") as fp: + return HTMLResponse("\n".join(fp.readlines())) + + +@router.post("/add") +async def add_weight(tl: int, tr: int, bl: int, br: int): + sum = tl + tr + bl + br + add_line_to_history(f"{str(datetime.now())};{";".join([tl, tr, bl, br, sum])};") + + +if not exists(file_path): + add_line_to_history("timestamp;tl;tr;bl;br;total;") diff --git a/src/main.py b/src/main.py index f88263b..39c4267 100644 --- a/src/main.py +++ b/src/main.py @@ -1,9 +1,11 @@ from fastapi import FastAPI from endpoints.hue import router as hue_router +from endpoints.bettwaage import router as bettwaage_router app = FastAPI() app.include_router(hue_router, prefix="/hue", tags=["hue"]) +app.include_router(bettwaage_router, prefix="/bettwaage") if __name__ == "__main__": app.run() From 07fe36570982d03cac7cf48a6905d7eefb9a008a Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Thu, 2 May 2024 17:36:34 +0200 Subject: [PATCH 02/25] Fixed format string --- src/endpoints/bettwaage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/endpoints/bettwaage.py b/src/endpoints/bettwaage.py index 4975b53..b0b7a39 100644 --- a/src/endpoints/bettwaage.py +++ b/src/endpoints/bettwaage.py @@ -24,7 +24,7 @@ async def get_file(): @router.post("/add") async def add_weight(tl: int, tr: int, bl: int, br: int): sum = tl + tr + bl + br - add_line_to_history(f"{str(datetime.now())};{";".join([tl, tr, bl, br, sum])};") + add_line_to_history(f"{str(datetime.now())};{';'.join([tl, tr, bl, br, sum])};") if not exists(file_path): From 30efc2b405316fe622ef40112e9b9fafc2ae3339 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Thu, 2 May 2024 17:38:10 +0200 Subject: [PATCH 03/25] Fixed format string --- src/endpoints/bettwaage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/endpoints/bettwaage.py b/src/endpoints/bettwaage.py index b0b7a39..42b1475 100644 --- a/src/endpoints/bettwaage.py +++ b/src/endpoints/bettwaage.py @@ -24,7 +24,7 @@ async def get_file(): @router.post("/add") async def add_weight(tl: int, tr: int, bl: int, br: int): sum = tl + tr + bl + br - add_line_to_history(f"{str(datetime.now())};{';'.join([tl, tr, bl, br, sum])};") + add_line_to_history(f"{str(datetime.now())};{tl};{tr};{bl};{br};{sum};") if not exists(file_path): From ee5de34647a1cdd4e3fe3138c0649f3ab465db78 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Thu, 2 May 2024 17:39:06 +0200 Subject: [PATCH 04/25] Fixed file insert --- src/endpoints/bettwaage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/endpoints/bettwaage.py b/src/endpoints/bettwaage.py index 42b1475..e79596c 100644 --- a/src/endpoints/bettwaage.py +++ b/src/endpoints/bettwaage.py @@ -12,7 +12,7 @@ file_path = "bettwaage.csv" def add_line_to_history(line: str) -> None: with open(file_path, "+a", encoding="UTF-8") as fp: - fp.writelines([line]) + fp.write(f"{line}\n") @router.get("/file") From 918a0fa04ceb1fe02a4a5bdfc8292e9d3d892431 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Thu, 2 May 2024 17:40:11 +0200 Subject: [PATCH 05/25] Fixed bettwaage csv insert --- src/endpoints/bettwaage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/endpoints/bettwaage.py b/src/endpoints/bettwaage.py index e79596c..02781f5 100644 --- a/src/endpoints/bettwaage.py +++ b/src/endpoints/bettwaage.py @@ -12,7 +12,7 @@ file_path = "bettwaage.csv" def add_line_to_history(line: str) -> None: with open(file_path, "+a", encoding="UTF-8") as fp: - fp.write(f"{line}\n") + fp.write(line) @router.get("/file") From 89168356544da486bdab7485caca724bacd14327 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Thu, 2 May 2024 17:48:37 +0200 Subject: [PATCH 06/25] Added calibration endpoints --- src/endpoints/bettwaage.py | 41 ++++++++++++++++++++++++++++++++++++-- src/main.py | 2 +- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/endpoints/bettwaage.py b/src/endpoints/bettwaage.py index 02781f5..9319eb1 100644 --- a/src/endpoints/bettwaage.py +++ b/src/endpoints/bettwaage.py @@ -1,7 +1,7 @@ from fastapi import APIRouter from datetime import datetime -from os.path import exists +import os from fastapi.responses import HTMLResponse @@ -9,12 +9,20 @@ router = APIRouter() file_path = "bettwaage.csv" +latest_values = None +zero_values = [0, 0, 0, 0] +scale_values = [1, 1, 1, 1] + def add_line_to_history(line: str) -> None: with open(file_path, "+a", encoding="UTF-8") as fp: fp.write(line) +def convert_to_weight(value: int, zero_value: int, scale: float) -> float: + return (value - zero_value) * scale + + @router.get("/file") async def get_file(): with open(file_path, "r", encoding="UTF-8") as fp: @@ -23,9 +31,38 @@ async def get_file(): @router.post("/add") async def add_weight(tl: int, tr: int, bl: int, br: int): + global latest_values + latest_values = [tl, tr, bl, br] + + tl = convert_to_weight(tl, zero_values[0], scale_values[0]) + tr = convert_to_weight(tl, zero_values[1], scale_values[1]) + bl = convert_to_weight(tl, zero_values[2], scale_values[2]) + br = convert_to_weight(tl, zero_values[3], scale_values[3]) + sum = tl + tr + bl + br add_line_to_history(f"{str(datetime.now())};{tl};{tr};{bl};{br};{sum};") -if not exists(file_path): +@router.post("/delete") +async def delete_file(): + os.remove(file_path) + + +@router.post("/zero") +async def set_zero(): + if latest_values is None: + return HTMLResponse( + status_code=400, content="Requiring data before setting zeros." + ) + global zero_values + zero_values = latest_values + + +@router.post("/scales") +async def set_scales(tl: float, tr: float, bl: float, br: float): + global scale_values + scale_values = [tl, tr, bl, br] + + +if not os.path.exists(file_path): add_line_to_history("timestamp;tl;tr;bl;br;total;") diff --git a/src/main.py b/src/main.py index 39c4267..28fcb83 100644 --- a/src/main.py +++ b/src/main.py @@ -5,7 +5,7 @@ from endpoints.bettwaage import router as bettwaage_router app = FastAPI() app.include_router(hue_router, prefix="/hue", tags=["hue"]) -app.include_router(bettwaage_router, prefix="/bettwaage") +app.include_router(bettwaage_router, prefix="/bettwaage", tags=["bett"]) if __name__ == "__main__": app.run() From 7b21fa130f375037668e26f48d34b097e5750da1 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Thu, 2 May 2024 17:49:06 +0200 Subject: [PATCH 07/25] Added tags --- src/endpoints/bettwaage.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/endpoints/bettwaage.py b/src/endpoints/bettwaage.py index 9319eb1..2efad60 100644 --- a/src/endpoints/bettwaage.py +++ b/src/endpoints/bettwaage.py @@ -23,7 +23,7 @@ def convert_to_weight(value: int, zero_value: int, scale: float) -> float: return (value - zero_value) * scale -@router.get("/file") +@router.get("/file", tags=["file"]) async def get_file(): with open(file_path, "r", encoding="UTF-8") as fp: return HTMLResponse("\n".join(fp.readlines())) @@ -43,12 +43,12 @@ async def add_weight(tl: int, tr: int, bl: int, br: int): add_line_to_history(f"{str(datetime.now())};{tl};{tr};{bl};{br};{sum};") -@router.post("/delete") +@router.post("/delete", tags=["file"]) async def delete_file(): os.remove(file_path) -@router.post("/zero") +@router.post("/zero", tags=["calibration"]) async def set_zero(): if latest_values is None: return HTMLResponse( @@ -58,7 +58,7 @@ async def set_zero(): zero_values = latest_values -@router.post("/scales") +@router.post("/scales", tags=["calibration"]) async def set_scales(tl: float, tr: float, bl: float, br: float): global scale_values scale_values = [tl, tr, bl, br] From 5e3261d1f741ac52e77cc7e3238432d26dbe1d63 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Thu, 2 May 2024 17:53:49 +0200 Subject: [PATCH 08/25] Added latest endpoint --- src/endpoints/bettwaage.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/endpoints/bettwaage.py b/src/endpoints/bettwaage.py index 2efad60..0c8e577 100644 --- a/src/endpoints/bettwaage.py +++ b/src/endpoints/bettwaage.py @@ -3,7 +3,7 @@ from fastapi import APIRouter from datetime import datetime import os -from fastapi.responses import HTMLResponse +from fastapi.responses import HTMLResponse, JSONResponse router = APIRouter() @@ -43,6 +43,20 @@ async def add_weight(tl: int, tr: int, bl: int, br: int): add_line_to_history(f"{str(datetime.now())};{tl};{tr};{bl};{br};{sum};") +@router.post("/latest") +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], + "total": total, + } + ) + + @router.post("/delete", tags=["file"]) async def delete_file(): os.remove(file_path) From 368052f713e39db0532441cc5cf92df7c3decaec Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Thu, 2 May 2024 18:00:27 +0200 Subject: [PATCH 09/25] Added history endpoints --- src/endpoints/bettwaage.py | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) 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, } ) From 10255b966b93852c5b62d0fbd3881415d7fc50ab Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Thu, 2 May 2024 18:02:03 +0200 Subject: [PATCH 10/25] Fixed data missing case --- src/endpoints/bettwaage.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/endpoints/bettwaage.py b/src/endpoints/bettwaage.py index 51a1b60..db61e12 100644 --- a/src/endpoints/bettwaage.py +++ b/src/endpoints/bettwaage.py @@ -10,7 +10,7 @@ router = APIRouter() file_path = "bettwaage.csv" -latest_values = None +latest_values = [] zero_values = [0, 0, 0, 0] scale_values = [1, 1, 1, 1] @@ -91,7 +91,7 @@ async def delete_file(): @router.post("/zero", tags=["calibration"]) async def set_zero(): - if latest_values is None: + if not latest_values: return HTMLResponse( status_code=400, content="Requiring data before setting zeros." ) From dd3264365c89f326022fdd7de40d8787738d969d Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Thu, 2 May 2024 18:03:44 +0200 Subject: [PATCH 11/25] Added return values for each endpoint --- src/endpoints/bettwaage.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/endpoints/bettwaage.py b/src/endpoints/bettwaage.py index db61e12..cc319c5 100644 --- a/src/endpoints/bettwaage.py +++ b/src/endpoints/bettwaage.py @@ -68,6 +68,7 @@ async def add_weight(tl: int, tr: int, bl: int, br: int): sum = tl + tr + bl + br add_line_to_history(f"{str(datetime.now())};{tl};{tr};{bl};{br};{sum};") + return "Added data" @router.post("/latest") @@ -87,6 +88,7 @@ async def get_latest(): @router.post("/delete", tags=["file"]) async def delete_file(): os.remove(file_path) + return "Deleted file" @router.post("/zero", tags=["calibration"]) @@ -97,12 +99,14 @@ async def set_zero(): ) global zero_values zero_values = latest_values + return "Set zeroes to: " + " | ".join(str(v) for v in zero_values) @router.post("/scales", tags=["calibration"]) async def set_scales(tl: float, tr: float, bl: float, br: float): global scale_values scale_values = [tl, tr, bl, br] + return "Set zeroes to: " + " | ".join(str(v) for v in scale_values) if not os.path.exists(file_path): From 63e1c29e52cdfe993de3597bbb2bb0661d31a680 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Thu, 2 May 2024 18:05:40 +0200 Subject: [PATCH 12/25] Fixed edge case response --- src/endpoints/bettwaage.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/endpoints/bettwaage.py b/src/endpoints/bettwaage.py index cc319c5..a243b28 100644 --- a/src/endpoints/bettwaage.py +++ b/src/endpoints/bettwaage.py @@ -73,6 +73,8 @@ async def add_weight(tl: int, tr: int, bl: int, br: int): @router.post("/latest") async def get_latest(): + if not latest_values: + return HTMLResponse(status_code=200, content="No data given yet") total = sum(latest_values) return JSONResponse( { From 7bb61ea90565fa9c608f53d6827219215cfaf485 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Thu, 2 May 2024 18:08:14 +0200 Subject: [PATCH 13/25] Fixed http methods --- src/endpoints/bettwaage.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/endpoints/bettwaage.py b/src/endpoints/bettwaage.py index a243b28..f6bf313 100644 --- a/src/endpoints/bettwaage.py +++ b/src/endpoints/bettwaage.py @@ -71,7 +71,7 @@ async def add_weight(tl: int, tr: int, bl: int, br: int): return "Added data" -@router.post("/latest") +@router.get("/latest") async def get_latest(): if not latest_values: return HTMLResponse(status_code=200, content="No data given yet") @@ -87,7 +87,7 @@ async def get_latest(): ) -@router.post("/delete", tags=["file"]) +@router.delete("/delete", tags=["file"]) async def delete_file(): os.remove(file_path) return "Deleted file" From 636b14f206fa2fcc35f17524b8bdf175c37ccedc Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Thu, 2 May 2024 18:10:12 +0200 Subject: [PATCH 14/25] Fixed history endpoint --- src/endpoints/bettwaage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/endpoints/bettwaage.py b/src/endpoints/bettwaage.py index f6bf313..42f577b 100644 --- a/src/endpoints/bettwaage.py +++ b/src/endpoints/bettwaage.py @@ -30,7 +30,7 @@ async def get_file(): return HTMLResponse("\n".join(fp.readlines())) -@router.get("/file", tags=["file"]) +@router.get("/history") async def get_history(count: int = None) -> []: points = [] with open(file_path, "r", encoding="UTF-8") as fp: From cc8eb0c2a3294df55b2b00bf5ea9ba2fa81a238a Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Thu, 2 May 2024 18:12:25 +0200 Subject: [PATCH 15/25] Fixed wrong word in response --- src/endpoints/bettwaage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/endpoints/bettwaage.py b/src/endpoints/bettwaage.py index 42f577b..1381ff2 100644 --- a/src/endpoints/bettwaage.py +++ b/src/endpoints/bettwaage.py @@ -108,7 +108,7 @@ async def set_zero(): async def set_scales(tl: float, tr: float, bl: float, br: float): global scale_values scale_values = [tl, tr, bl, br] - return "Set zeroes to: " + " | ".join(str(v) for v in scale_values) + return "Set scales to: " + " | ".join(str(v) for v in scale_values) if not os.path.exists(file_path): From 2ac70ddac62d29fbb51e160fefa3e71d52109a1d Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Thu, 2 May 2024 18:16:39 +0200 Subject: [PATCH 16/25] Hopefully fixed write endpoint --- src/endpoints/bettwaage.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/endpoints/bettwaage.py b/src/endpoints/bettwaage.py index 1381ff2..dfd5141 100644 --- a/src/endpoints/bettwaage.py +++ b/src/endpoints/bettwaage.py @@ -16,8 +16,8 @@ scale_values = [1, 1, 1, 1] def add_line_to_history(line: str) -> None: - with open(file_path, "+a", encoding="UTF-8") as fp: - fp.write(line) + with open(file_path, "a", encoding="UTF-8") as fp: + fp.write(line + "\n") def convert_to_weight(value: int, zero_value: int, scale: float) -> float: From ed1f6a747568f996bb5f2191948851fdbaff1c5a Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Thu, 2 May 2024 18:17:42 +0200 Subject: [PATCH 17/25] Fixed delete file endpoint --- src/endpoints/bettwaage.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/endpoints/bettwaage.py b/src/endpoints/bettwaage.py index dfd5141..6c2abf5 100644 --- a/src/endpoints/bettwaage.py +++ b/src/endpoints/bettwaage.py @@ -90,7 +90,8 @@ async def get_latest(): @router.delete("/delete", tags=["file"]) async def delete_file(): os.remove(file_path) - return "Deleted file" + add_line_to_history("timestamp;tl;tr;bl;br;total;") + return "Deleted file and created new file with headers" @router.post("/zero", tags=["calibration"]) From eac465e1a8ce58480cfb48c5e20c44bcce7b83f1 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Thu, 2 May 2024 18:27:38 +0200 Subject: [PATCH 18/25] Maybe fixed write endpoint? --- src/endpoints/bettwaage.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/endpoints/bettwaage.py b/src/endpoints/bettwaage.py index 6c2abf5..a3ee5c3 100644 --- a/src/endpoints/bettwaage.py +++ b/src/endpoints/bettwaage.py @@ -9,6 +9,7 @@ import csv router = APIRouter() file_path = "bettwaage.csv" +header = "timestamp;tl;tr;bl;br;total;" latest_values = [] zero_values = [0, 0, 0, 0] @@ -16,7 +17,7 @@ scale_values = [1, 1, 1, 1] def add_line_to_history(line: str) -> None: - with open(file_path, "a", encoding="UTF-8") as fp: + with open(file_path, "a") as fp: fp.write(line + "\n") @@ -90,7 +91,7 @@ async def get_latest(): @router.delete("/delete", tags=["file"]) async def delete_file(): os.remove(file_path) - add_line_to_history("timestamp;tl;tr;bl;br;total;") + add_line_to_history(header) return "Deleted file and created new file with headers" @@ -113,4 +114,4 @@ async def set_scales(tl: float, tr: float, bl: float, br: float): if not os.path.exists(file_path): - add_line_to_history("timestamp;tl;tr;bl;br;total;") + add_line_to_history(header) From 5bc507f38a1ce2173cdeb34f0b3a12487741181f Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Thu, 2 May 2024 18:29:44 +0200 Subject: [PATCH 19/25] Fixed add endpoint --- src/endpoints/bettwaage.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/endpoints/bettwaage.py b/src/endpoints/bettwaage.py index a3ee5c3..50cbbe0 100644 --- a/src/endpoints/bettwaage.py +++ b/src/endpoints/bettwaage.py @@ -63,9 +63,9 @@ async def add_weight(tl: int, tr: int, bl: int, br: int): latest_values = [tl, tr, bl, br] tl = convert_to_weight(tl, zero_values[0], scale_values[0]) - tr = convert_to_weight(tl, zero_values[1], scale_values[1]) - bl = convert_to_weight(tl, zero_values[2], scale_values[2]) - br = convert_to_weight(tl, zero_values[3], scale_values[3]) + tr = convert_to_weight(tr, zero_values[1], scale_values[1]) + bl = convert_to_weight(bl, zero_values[2], scale_values[2]) + br = convert_to_weight(br, zero_values[3], scale_values[3]) sum = tl + tr + bl + br add_line_to_history(f"{str(datetime.now())};{tl};{tr};{bl};{br};{sum};") From 21376509ffaceb9fc48c56eb28f935e111c99256 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Thu, 2 May 2024 20:06:47 +0200 Subject: [PATCH 20/25] Implemented simple plotter --- bettwaage-plotter/main.py | 44 ++++++++++++++++++++++++++++++ bettwaage-plotter/requirements.txt | 3 ++ 2 files changed, 47 insertions(+) create mode 100644 bettwaage-plotter/main.py create mode 100644 bettwaage-plotter/requirements.txt diff --git a/bettwaage-plotter/main.py b/bettwaage-plotter/main.py new file mode 100644 index 0000000..abff5be --- /dev/null +++ b/bettwaage-plotter/main.py @@ -0,0 +1,44 @@ +import requests +import matplotlib.pyplot as plt +import numpy as np +from datetime import datetime + +history_url = "http://192.168.178.84:9587/bettwaage/history" + +data = requests.get(history_url) + +data = data.json() + + +# make data +x = [datetime.strptime(d["timestamp"], "%Y-%m-%d %H:%M:%S.%f") for d in data] +max_time = max(x) +x = [(max_time - d).total_seconds() for d in x] + +total = [d["total"] for d in data] +tl = [d["tl"] for d in data] +tr = [d["tr"] for d in data] +bl = [d["bl"] for d in data] +br = [d["br"] for d in data] + + +fig, ax1 = plt.subplots() + +color = "tab:red" +ax1.set_xlabel("time (s)") +ax1.set_ylabel("Total", color=color) +ax1.plot(x, total, color=color) + +ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis + +color = "tab:blue" +ax2.set_ylabel("Legs", color=color) # we already handled the x-label with ax1 +ax2.plot(x, tl, color="tab:red") +ax2.plot(x, tr, color="tab:green") +ax2.plot(x, bl, color="tab:purple") +ax2.plot(x, br, color="tab:orange") + +fig.tight_layout() # otherwise the right y-label is slightly clipped +plt.show() + +plt.show() diff --git a/bettwaage-plotter/requirements.txt b/bettwaage-plotter/requirements.txt new file mode 100644 index 0000000..74599f5 --- /dev/null +++ b/bettwaage-plotter/requirements.txt @@ -0,0 +1,3 @@ +matplotlib +requests +numpy From 2fb05d641beff46ba1cef3a0f266637ae0a67a32 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Fri, 3 May 2024 00:29:06 +0200 Subject: [PATCH 21/25] Working versions --- bettwaage-plotter/bett.py | 27 +++++++++++++++++++++++++++ bettwaage-plotter/main.py | 4 +--- 2 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 bettwaage-plotter/bett.py diff --git a/bettwaage-plotter/bett.py b/bettwaage-plotter/bett.py new file mode 100644 index 0000000..2e40e9e --- /dev/null +++ b/bettwaage-plotter/bett.py @@ -0,0 +1,27 @@ +import requests as r +from time import sleep + +bett_ip = "http://192.168.178.110:80" +mash_ip = "http://192.168.178.84:9587" + +while True: + try: + tl = r.get(f"{bett_ip}/sensor/tl/").json()["value"] + tr = r.get(f"{bett_ip}/sensor/tr/").json()["value"] + br = r.get(f"{bett_ip}/sensor/br/").json()["value"] + + print(f"tl = {tl}") + print(f"tr = {tr}") + # print(f"tl = {tl}") + print(f"br = {br}") + print("==========") + print(f"total = {tl + tr + br * 2}") + print("==========") + + s = r.post(f"{mash_ip}/bettwaage/add?tl={int(tl * 1000)}&tr={int(tr * 1000)}&bl={int(br * 1000)}&br={int(br * 1000)}") + + sleep(1) + except KeyboardInterrupt: + exit() + except: + pass diff --git a/bettwaage-plotter/main.py b/bettwaage-plotter/main.py index abff5be..afc6dde 100644 --- a/bettwaage-plotter/main.py +++ b/bettwaage-plotter/main.py @@ -24,7 +24,7 @@ br = [d["br"] for d in data] fig, ax1 = plt.subplots() -color = "tab:red" +color = "tab:blue" ax1.set_xlabel("time (s)") ax1.set_ylabel("Total", color=color) ax1.plot(x, total, color=color) @@ -40,5 +40,3 @@ ax2.plot(x, br, color="tab:orange") fig.tight_layout() # otherwise the right y-label is slightly clipped plt.show() - -plt.show() From c84b53a374c2149f617e62cdf5ddc019b5f8a6a8 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Fri, 3 May 2024 22:39:30 +0200 Subject: [PATCH 22/25] Improved plotter script --- bettwaage-plotter/main.py | 44 +++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/bettwaage-plotter/main.py b/bettwaage-plotter/main.py index afc6dde..50a3a36 100644 --- a/bettwaage-plotter/main.py +++ b/bettwaage-plotter/main.py @@ -1,19 +1,31 @@ import requests import matplotlib.pyplot as plt -import numpy as np from datetime import datetime +import json + +file_path = "history.json" history_url = "http://192.168.178.84:9587/bettwaage/history" -data = requests.get(history_url) +convert_time_to_seconds = True -data = data.json() +# Script +data = None +if file_path is None: + data = requests.get(history_url) + data = data.json() +else: + with open(file_path, "r") as fp: + data = json.load(fp) # make data -x = [datetime.strptime(d["timestamp"], "%Y-%m-%d %H:%M:%S.%f") for d in data] -max_time = max(x) -x = [(max_time - d).total_seconds() for d in x] +x = [d["timestamp"] for d in data] +x = [datetime.strptime(d, "%Y-%m-%d %H:%M:%S.%f") for d in x] + +if convert_time_to_seconds: + max_time = max(x) + x = [(d - max_time).total_seconds() for d in x] total = [d["total"] for d in data] tl = [d["tl"] for d in data] @@ -24,19 +36,15 @@ br = [d["br"] for d in data] fig, ax1 = plt.subplots() -color = "tab:blue" -ax1.set_xlabel("time (s)") -ax1.set_ylabel("Total", color=color) -ax1.plot(x, total, color=color) +ax1.set_xlabel("Time (s)") +ax1.set_ylabel("Weight (kg)") -ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis +line_total = ax1.plot(x, total, color="tab:blue") +line_tl = ax1.plot(x, tl, color="tab:red") +line_tr = ax1.plot(x, tr, color="tab:green") +line_bl = ax1.plot(x, bl, color="tab:orange") +line_br = ax1.plot(x, br, color="tab:purple") -color = "tab:blue" -ax2.set_ylabel("Legs", color=color) # we already handled the x-label with ax1 -ax2.plot(x, tl, color="tab:red") -ax2.plot(x, tr, color="tab:green") -ax2.plot(x, bl, color="tab:purple") -ax2.plot(x, br, color="tab:orange") +ax1.legend(["Total", "Top Left", "Top Right", "Bottom Left", "Bottom Right"]) -fig.tight_layout() # otherwise the right y-label is slightly clipped plt.show() From 9004125dd5223b88213f183617fc68094ce2c6c3 Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 6 May 2024 01:03:34 +0200 Subject: [PATCH 23/25] Position experiments --- bettwaage-plotter/main.py | 106 ++++++++++++++++++++++++++++++++++---- 1 file changed, 96 insertions(+), 10 deletions(-) diff --git a/bettwaage-plotter/main.py b/bettwaage-plotter/main.py index 50a3a36..9ca68cb 100644 --- a/bettwaage-plotter/main.py +++ b/bettwaage-plotter/main.py @@ -19,7 +19,49 @@ else: with open(file_path, "r") as fp: data = json.load(fp) -# make data + +# Experiment: Solving for missing foot with known total weight +bed_weight = 78290 +person_weight = 63000 +known_total_weight = bed_weight + person_weight +bed_only_weight = {} +for d in data: + if d["total"] == bed_weight: + bed_only_weight = { + "tl": d["tl"], + "tr": d["tr"], + "bl": bed_weight - (d["tl"] + d["tr"] + d["br"]), + "br": d["br"], + } + break + +in_bed_data = None +threshhold = 100000 +min_length = 100 +for d in data: + t = d["total"] + if t >= threshhold: + if in_bed_data is None: + in_bed_data = [] + in_bed_data.append(d) + elif in_bed_data is not None: + if len(in_bed_data) < min_length: + in_bed_data = [] + else: + break + + +# Calculate bottom left +for d in data: + d["bl"] = known_total_weight - (d["br"] + d["tr"] + d["tl"]) + # Set known total weight + d["total"] = known_total_weight + + +data = in_bed_data + + +# Array data x = [d["timestamp"] for d in data] x = [datetime.strptime(d, "%Y-%m-%d %H:%M:%S.%f") for d in x] @@ -32,19 +74,63 @@ tl = [d["tl"] for d in data] tr = [d["tr"] for d in data] bl = [d["bl"] for d in data] br = [d["br"] for d in data] +top = [l + r for l, r in zip(tl, tr)] +bottom = [l + r for l, r in zip(bl, br)] +left = [t + b for t, b in zip(tl, bl)] +right = [t + b for t, b in zip(tr, br)] -fig, ax1 = plt.subplots() +# Experiment: Calculate position over time +bed_size = (140, 200) +left_bed_only = bed_only_weight["tl"] + bed_only_weight["bl"] +top_bed_only = bed_only_weight["tr"] + bed_only_weight["tl"] +position_over_time = [] +for t, l in zip(top, left): + position_over_time.append( + ( + bed_size[0] * (l - left_bed_only) / person_weight, + bed_size[1] * (t - top_bed_only) / person_weight, + ) + ) -ax1.set_xlabel("Time (s)") -ax1.set_ylabel("Weight (kg)") -line_total = ax1.plot(x, total, color="tab:blue") -line_tl = ax1.plot(x, tl, color="tab:red") -line_tr = ax1.plot(x, tr, color="tab:green") -line_bl = ax1.plot(x, bl, color="tab:orange") -line_br = ax1.plot(x, br, color="tab:purple") +# Plot data +fig, (ax0, ax1) = plt.subplots(nrows=2) + +ax0.set_xlabel("Time (s)") +ax0.set_ylabel("Weight (kg)") + +ax0.plot(x, total, color="tab:blue") +ax0.plot(x, tl, color="tab:red") +ax0.plot(x, tr, color="tab:green") +ax0.plot(x, bl, color="tab:orange") +ax0.plot(x, br, color="tab:purple") +ax0.plot(x, top, color="tab:pink") +ax0.plot(x, bottom, color="tab:grey") + +ax0.legend( + ["Total", "Top Left", "Top Right", "Bottom Left", "Bottom Right", "Top", "Bottom"] +) + + +# Experiment: Plot position +import math + +segments = 100 +seg_length = math.ceil(len(position_over_time) / segments) +horizontal, vertical = zip(*position_over_time) +for i in range(0, segments): + low = int(i * seg_length) + high = min(int((i + 1) * seg_length), len(position_over_time)) + ax1.plot( + horizontal[low:high], + vertical[low:high], + color=(0.0, 0.5, i / segments), + linewidth=0.3, + ) +ax1.set_xlim((0, bed_size[0])) +ax1.set_ylim((0, bed_size[1])) +ax1.invert_yaxis() -ax1.legend(["Total", "Top Left", "Top Right", "Bottom Left", "Bottom Right"]) plt.show() From 965dd3066505fdf7b568cd6ff86839a76c586b98 Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 6 May 2024 15:19:12 +0200 Subject: [PATCH 24/25] Added proper color gradient to position plot --- bettwaage-plotter/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bettwaage-plotter/main.py b/bettwaage-plotter/main.py index 9ca68cb..afc76e8 100644 --- a/bettwaage-plotter/main.py +++ b/bettwaage-plotter/main.py @@ -115,6 +115,7 @@ ax0.legend( # Experiment: Plot position import math +import colorsys segments = 100 seg_length = math.ceil(len(position_over_time) / segments) @@ -125,7 +126,7 @@ for i in range(0, segments): ax1.plot( horizontal[low:high], vertical[low:high], - color=(0.0, 0.5, i / segments), + color=colorsys.hsv_to_rgb(i / segments * 0.5, 1, 0.7), linewidth=0.3, ) ax1.set_xlim((0, bed_size[0])) From c823f2a40a050122b18bb24cb8fd4f27e547bb8c Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 6 May 2024 15:20:14 +0200 Subject: [PATCH 25/25] Preparing for merge with old smart home --- .gitignore | 3 +++ {src => src-new}/endpoints/bettwaage.py | 0 {src => src-new}/endpoints/handlers/hue.py | 0 {src => src-new}/endpoints/hue.py | 0 {src => src-new}/hue_bridge_registered.txt | 0 {src => src-new}/main.py | 0 {src => src-new}/old_philips_hue_examples.py | 0 7 files changed, 3 insertions(+) rename {src => src-new}/endpoints/bettwaage.py (100%) rename {src => src-new}/endpoints/handlers/hue.py (100%) rename {src => src-new}/endpoints/hue.py (100%) rename {src => src-new}/hue_bridge_registered.txt (100%) rename {src => src-new}/main.py (100%) rename {src => src-new}/old_philips_hue_examples.py (100%) diff --git a/.gitignore b/.gitignore index ef5bac5..1107673 100644 --- a/.gitignore +++ b/.gitignore @@ -160,3 +160,6 @@ cython_debug/ #.idea/ .vscode/settings.json .vscode/launch.json + +# Custom +history.json diff --git a/src/endpoints/bettwaage.py b/src-new/endpoints/bettwaage.py similarity index 100% rename from src/endpoints/bettwaage.py rename to src-new/endpoints/bettwaage.py diff --git a/src/endpoints/handlers/hue.py b/src-new/endpoints/handlers/hue.py similarity index 100% rename from src/endpoints/handlers/hue.py rename to src-new/endpoints/handlers/hue.py diff --git a/src/endpoints/hue.py b/src-new/endpoints/hue.py similarity index 100% rename from src/endpoints/hue.py rename to src-new/endpoints/hue.py diff --git a/src/hue_bridge_registered.txt b/src-new/hue_bridge_registered.txt similarity index 100% rename from src/hue_bridge_registered.txt rename to src-new/hue_bridge_registered.txt diff --git a/src/main.py b/src-new/main.py similarity index 100% rename from src/main.py rename to src-new/main.py diff --git a/src/old_philips_hue_examples.py b/src-new/old_philips_hue_examples.py similarity index 100% rename from src/old_philips_hue_examples.py rename to src-new/old_philips_hue_examples.py