mash-server/bettwaage-plotter/main.py

51 lines
1.1 KiB
Python
Raw Normal View History

2024-05-02 20:06:47 +02:00
import requests
import matplotlib.pyplot as plt
from datetime import datetime
2024-05-03 22:39:30 +02:00
import json
2024-05-02 20:06:47 +02:00
2024-05-03 22:39:30 +02:00
file_path = "history.json"
2024-05-02 20:06:47 +02:00
history_url = "http://192.168.178.84:9587/bettwaage/history"
2024-05-03 22:39:30 +02:00
convert_time_to_seconds = True
2024-05-02 20:06:47 +02:00
2024-05-03 22:39:30 +02:00
# Script
data = None
2024-05-02 20:06:47 +02:00
2024-05-03 22:39:30 +02:00
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)
2024-05-02 20:06:47 +02:00
# make data
2024-05-03 22:39:30 +02:00
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]
2024-05-02 20:06:47 +02:00
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()
2024-05-03 22:39:30 +02:00
ax1.set_xlabel("Time (s)")
ax1.set_ylabel("Weight (kg)")
2024-05-02 20:06:47 +02:00
2024-05-03 22:39:30 +02:00
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")
2024-05-02 20:06:47 +02:00
2024-05-03 22:39:30 +02:00
ax1.legend(["Total", "Top Left", "Top Right", "Bottom Left", "Bottom Right"])
2024-05-02 20:06:47 +02:00
plt.show()