mash-server/bettwaage-plotter/main.py

137 lines
3.2 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
2024-05-06 01:03:34 +02:00
# 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
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]
2024-05-06 01:03:34 +02:00
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)]
# 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,
)
)
# 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")
2024-05-02 20:06:47 +02:00
2024-05-06 01:03:34 +02:00
ax0.legend(
["Total", "Top Left", "Top Right", "Bottom Left", "Bottom Right", "Top", "Bottom"]
)
2024-05-02 20:06:47 +02:00
2024-05-06 01:03:34 +02:00
# Experiment: Plot position
import math
2024-05-02 20:06:47 +02:00
2024-05-06 01:03:34 +02:00
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()
2024-05-02 20:06:47 +02:00
plt.show()