bettwaage-sidequest #2

Merged
m.giller merged 38 commits from bettwaage-sidequest into master 2024-06-07 23:38:56 +02:00
3 changed files with 165 additions and 26 deletions
Showing only changes of commit 41073fce50 - Show all commits

File diff suppressed because one or more lines are too long

View file

@ -2,19 +2,27 @@ import requests
import matplotlib.pyplot as plt
from datetime import datetime
import json
import os
latest_history_path = "latest_history.json"
file_path = None
file_path = latest_history_path
history_url = "http://192.168.178.84:9587/bettwaage/history"
focus_on_latest_bed_data = False
convert_time_to_seconds = True
# Get data
data = None
if file_path is None:
if file_path is None or not os.path.exists(file_path):
print("Fetching data ...")
data = requests.get(history_url)
data = data.json()
print("Saving latest data ...")
with open(latest_history_path, "w", encoding="UTF-8") as fp:
json.dump(data, fp)
else:
print("Reading data ...")
with open(file_path, "r") as fp:
@ -34,36 +42,38 @@ for d in data:
"bl": d["bl"],
"br": d["br"],
}
total_bed_only_weight = sum(bed_only_weight.values())
break
# Collect all coherent sequences of someone being in bed
in_bed_datas: list[list[dict]] = []
is_in_bed_sequence = False
threshhold = 100.0
for d in data:
t = d["total"]
if t >= threshhold:
if not is_in_bed_sequence:
in_bed_datas.append([])
is_in_bed_sequence = True
in_bed_datas[-1].append(d)
elif is_in_bed_sequence:
is_in_bed_sequence = False
if focus_on_latest_bed_data:
# Collect all coherent sequences of someone being in bed
in_bed_datas: list[list[dict]] = []
is_in_bed_sequence = False
threshhold = 100.0
for d in data:
t = d["total"]
if t >= threshhold:
if not is_in_bed_sequence:
in_bed_datas.append([])
is_in_bed_sequence = True
in_bed_datas[-1].append(d)
elif is_in_bed_sequence:
is_in_bed_sequence = False
# Pick latest with minimum length/duration
min_length = 100
for sequence in in_bed_datas:
if len(sequence) >= min_length:
data = sequence
# Pick latest with minimum length/duration
min_length = 100
for sequence in in_bed_datas:
if len(sequence) >= min_length:
data = sequence
# Prepare data for plotting
x = [d["timestamp"] for d in data]
x = [datetime.strptime(d, "%Y-%m-%d %H:%M:%S.%f") for d in x]
# 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]
# 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]
@ -76,6 +86,18 @@ left = [t + b for t, b in zip(tl, bl)]
right = [t + b for t, b in zip(tr, br)]
fig, ax = plt.subplots()
person_weight = [t - total_bed_only_weight for t in total]
ax.set_xlabel("Time (s)")
ax.set_ylabel("Weight (kg)")
ax.plot(x, person_weight, color="tab:blue")
plt.show()
exit()
# Experiment: Calculate position over time
bed_size = (140, 200)
left_bed_only = bed_only_weight["tl"] + bed_only_weight["bl"]
@ -93,7 +115,6 @@ for t, b, l, r in zip(top, bottom, left, right):
)
)
# Plot data
fig, (ax0, ax1) = plt.subplots(nrows=2)
@ -112,7 +133,6 @@ ax0.legend(
["Total", "Top Left", "Top Right", "Bottom Left", "Bottom Right", "Top", "Bottom"]
)
# Experiment: Plot position
import math
import colorsys

View file

@ -1,3 +1,4 @@
matplotlib
requests
numpy
PyQt5