refactor: Streamline message processing and visualization updates in heart rate monitoring
This commit is contained in:
123
main.py
123
main.py
@ -270,11 +270,11 @@ def main():
|
|||||||
else:
|
else:
|
||||||
selected_devices = []
|
selected_devices = []
|
||||||
|
|
||||||
placeholder = st.empty()
|
# Process available messages (no infinite loop)
|
||||||
|
# Process UDP messages (treat as device_id = 0)
|
||||||
while True:
|
message_processed = False
|
||||||
# Process UDP messages (treat as device_id = 0)
|
try:
|
||||||
try:
|
while True: # Process all available UDP messages
|
||||||
message = state["message_queue"].receive_nowait()
|
message = state["message_queue"].receive_nowait()
|
||||||
hr_value = parse_ble_hr_measurement(message)
|
hr_value = parse_ble_hr_measurement(message)
|
||||||
if hr_value is not None:
|
if hr_value is not None:
|
||||||
@ -288,10 +288,13 @@ def main():
|
|||||||
dev_hist["hr_data"].append(float(hr_value))
|
dev_hist["hr_data"].append(float(hr_value))
|
||||||
|
|
||||||
logger.debug("UDP Device: HR={}", hr_value)
|
logger.debug("UDP Device: HR={}", hr_value)
|
||||||
except anyio.WouldBlock:
|
message_processed = True
|
||||||
pass
|
except anyio.WouldBlock:
|
||||||
|
pass
|
||||||
|
|
||||||
try:
|
# Process MQTT messages
|
||||||
|
try:
|
||||||
|
while True: # Process all available MQTT messages
|
||||||
mqtt_message = state["mqtt_message_queue"].receive_nowait()
|
mqtt_message = state["mqtt_message_queue"].receive_nowait()
|
||||||
if mqtt_message.payload:
|
if mqtt_message.payload:
|
||||||
try:
|
try:
|
||||||
@ -330,61 +333,71 @@ def main():
|
|||||||
dev_hist["hr_data"].append(float(hr_value))
|
dev_hist["hr_data"].append(float(hr_value))
|
||||||
|
|
||||||
logger.debug("Device {}: HR={}", device_id, hr_value)
|
logger.debug("Device {}: HR={}", device_id, hr_value)
|
||||||
|
message_processed = True
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error("Failed to parse MQTT protobuf message: {}", e)
|
logger.error("Failed to parse MQTT protobuf message: {}", e)
|
||||||
except anyio.WouldBlock:
|
except anyio.WouldBlock:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Update visualization - HR Graphs
|
# Auto-refresh the page if new data was processed
|
||||||
with placeholder.container():
|
if message_processed:
|
||||||
if device_histories:
|
sleep(0.1) # Small delay to batch multiple messages
|
||||||
st.subheader("Heart Rate Data")
|
st.rerun()
|
||||||
|
|
||||||
# Create plots for selected devices
|
# Update visualization - HR Graphs
|
||||||
traces = []
|
if device_histories:
|
||||||
colors = [
|
st.subheader("Heart Rate Data")
|
||||||
"red",
|
|
||||||
"green",
|
|
||||||
"blue",
|
|
||||||
"orange",
|
|
||||||
"purple",
|
|
||||||
"brown",
|
|
||||||
"pink",
|
|
||||||
"gray",
|
|
||||||
"olive",
|
|
||||||
"cyan",
|
|
||||||
]
|
|
||||||
|
|
||||||
for i, device_id in enumerate(selected_devices):
|
# Create plots for selected devices
|
||||||
if device_id in device_histories:
|
traces = []
|
||||||
dev_hist = device_histories[device_id]
|
colors = [
|
||||||
if dev_hist["hr_data"] and dev_hist["timescape"]:
|
"red",
|
||||||
color = colors[i % len(colors)]
|
"green",
|
||||||
traces.append(
|
"blue",
|
||||||
go.Scatter(
|
"orange",
|
||||||
x=list(dev_hist["timescape"]),
|
"purple",
|
||||||
y=list(dev_hist["hr_data"]),
|
"brown",
|
||||||
mode="lines+markers",
|
"pink",
|
||||||
name=get_device_name(device_id),
|
"gray",
|
||||||
line=dict(color=color),
|
"olive",
|
||||||
marker=dict(size=4),
|
"cyan",
|
||||||
)
|
]
|
||||||
)
|
|
||||||
|
|
||||||
if traces:
|
for i, device_id in enumerate(selected_devices):
|
||||||
fig = go.Figure(data=traces)
|
if device_id in device_histories:
|
||||||
fig.update_layout(
|
dev_hist = device_histories[device_id]
|
||||||
title="Heart Rate Monitor",
|
if dev_hist["hr_data"] and dev_hist["timescape"]:
|
||||||
xaxis_title="Time",
|
color = colors[i % len(colors)]
|
||||||
yaxis_title="Heart Rate (BPM)",
|
traces.append(
|
||||||
hovermode="x unified",
|
go.Scatter(
|
||||||
showlegend=True,
|
x=list(dev_hist["timescape"]),
|
||||||
height=500,
|
y=list(dev_hist["hr_data"]),
|
||||||
|
mode="lines+markers",
|
||||||
|
name=get_device_name(device_id),
|
||||||
|
line=dict(color=color),
|
||||||
|
marker=dict(size=4),
|
||||||
|
)
|
||||||
)
|
)
|
||||||
st.plotly_chart(fig, use_container_width=True)
|
|
||||||
else:
|
if traces:
|
||||||
st.info("No heart rate data available for selected devices")
|
fig = go.Figure(data=traces)
|
||||||
|
fig.update_layout(
|
||||||
|
title="Heart Rate Monitor",
|
||||||
|
xaxis_title="Time",
|
||||||
|
yaxis_title="Heart Rate (BPM)",
|
||||||
|
hovermode="x unified",
|
||||||
|
showlegend=True,
|
||||||
|
height=500,
|
||||||
|
)
|
||||||
|
st.plotly_chart(fig, use_container_width=True)
|
||||||
|
else:
|
||||||
|
st.info("No heart rate data available for selected devices")
|
||||||
|
else:
|
||||||
|
st.info("No devices connected yet. Waiting for data...")
|
||||||
|
|
||||||
|
sleep(1)
|
||||||
|
st.rerun()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user