fix: resolve stale waveform duplicates and throttle bypass for real-time streaming
This commit is contained in:
@@ -362,6 +362,8 @@ def _parse_286_byte_packet(data: bytes, vitals_dict: dict) -> bool:
|
|||||||
vitals_dict["present_fields"] = []
|
vitals_dict["present_fields"] = []
|
||||||
if "spo2" not in vitals_dict["present_fields"]:
|
if "spo2" not in vitals_dict["present_fields"]:
|
||||||
vitals_dict["present_fields"].append("spo2")
|
vitals_dict["present_fields"].append("spo2")
|
||||||
|
if "pleth_wave" not in vitals_dict["present_fields"]:
|
||||||
|
vitals_dict["present_fields"].append("pleth_wave")
|
||||||
vitals_dict.setdefault("spo2", None)
|
vitals_dict.setdefault("spo2", None)
|
||||||
|
|
||||||
found = False
|
found = False
|
||||||
@@ -407,6 +409,8 @@ def _parse_288_byte_packet(data: bytes, vitals_dict: dict) -> bool:
|
|||||||
vitals_dict["present_fields"] = []
|
vitals_dict["present_fields"] = []
|
||||||
if "spo2" not in vitals_dict["present_fields"]:
|
if "spo2" not in vitals_dict["present_fields"]:
|
||||||
vitals_dict["present_fields"].append("spo2")
|
vitals_dict["present_fields"].append("spo2")
|
||||||
|
if "pleth_wave" not in vitals_dict["present_fields"]:
|
||||||
|
vitals_dict["present_fields"].append("pleth_wave")
|
||||||
vitals_dict.setdefault("spo2", None)
|
vitals_dict.setdefault("spo2", None)
|
||||||
|
|
||||||
found = False
|
found = False
|
||||||
@@ -477,6 +481,8 @@ def _parse_989_byte_packet(data: bytes, vitals_dict: dict) -> bool:
|
|||||||
vitals_dict["present_fields"] = []
|
vitals_dict["present_fields"] = []
|
||||||
if "heart_rate" not in vitals_dict["present_fields"]:
|
if "heart_rate" not in vitals_dict["present_fields"]:
|
||||||
vitals_dict["present_fields"].append("heart_rate")
|
vitals_dict["present_fields"].append("heart_rate")
|
||||||
|
if "ecg_wave" not in vitals_dict["present_fields"]:
|
||||||
|
vitals_dict["present_fields"].append("ecg_wave")
|
||||||
vitals_dict.setdefault("heart_rate", None)
|
vitals_dict.setdefault("heart_rate", None)
|
||||||
|
|
||||||
found = False
|
found = False
|
||||||
|
|||||||
@@ -177,6 +177,9 @@ async def process_vitals(vitals):
|
|||||||
if device_id not in device_field_timestamps:
|
if device_id not in device_field_timestamps:
|
||||||
device_field_timestamps[device_id] = {}
|
device_field_timestamps[device_id] = {}
|
||||||
|
|
||||||
|
# Check if incoming packet has fresh waveforms
|
||||||
|
has_fresh_waveforms = (vitals.ecg_wave is not None) or (vitals.pleth_wave is not None)
|
||||||
|
|
||||||
# 1. Merge new vitals into cached vitals to prevent fragmented entries
|
# 1. Merge new vitals into cached vitals to prevent fragmented entries
|
||||||
if device_id not in device_cache:
|
if device_id not in device_cache:
|
||||||
device_cache[device_id] = vitals
|
device_cache[device_id] = vitals
|
||||||
@@ -236,6 +239,7 @@ async def process_vitals(vitals):
|
|||||||
logger.debug(f"WebSocket broadcast failed: {e}")
|
logger.debug(f"WebSocket broadcast failed: {e}")
|
||||||
|
|
||||||
# 4. Throttled Database & REST forwarding (at most once every 5 seconds)
|
# 4. Throttled Database & REST forwarding (at most once every 5 seconds)
|
||||||
|
# Note: If we have fresh waveforms, we bypass throttle for REST forwarding to keep cloud graphs real-time
|
||||||
now = datetime.now(timezone.utc)
|
now = datetime.now(timezone.utc)
|
||||||
should_write_db = False
|
should_write_db = False
|
||||||
if device_id not in last_db_write or (now - last_db_write[device_id]).total_seconds() >= 5.0:
|
if device_id not in last_db_write or (now - last_db_write[device_id]).total_seconds() >= 5.0:
|
||||||
@@ -339,3 +343,14 @@ async def process_vitals(vitals):
|
|||||||
db.rollback()
|
db.rollback()
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
db.close()
|
||||||
|
elif has_fresh_waveforms:
|
||||||
|
# Bypass throttle for REST forwarding to keep cloud graphs real-time
|
||||||
|
try:
|
||||||
|
await forward_vitals_to_api(vitals)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Real-time REST forwarding failed: {e}")
|
||||||
|
|
||||||
|
# Clear waveforms from cache to prevent repetition in subsequent non-waveform sub-packets
|
||||||
|
if device_id in device_cache:
|
||||||
|
device_cache[device_id].ecg_wave = None
|
||||||
|
device_cache[device_id].pleth_wave = None
|
||||||
|
|||||||
Reference in New Issue
Block a user