fix: sync root repository files with updated fiveparaminte-main code

This commit is contained in:
2026-07-15 16:13:43 +05:30
parent 4a0aae6003
commit 2011544f53
6 changed files with 977 additions and 1581 deletions
+57 -5
View File
@@ -133,7 +133,7 @@ def parse_contec_hl7_text(raw_text: str, client_ip: str) -> Optional[NormalizedV
found_any = False
# Parse PID and OBX segments
# Parse PID, PV1 and OBX segments
for seg in segments[1:]:
fields = seg.split("|")
if not fields:
@@ -142,10 +142,37 @@ def parse_contec_hl7_text(raw_text: str, client_ip: str) -> Optional[NormalizedV
seg_name = fields[0]
if seg_name == "PID":
# PID-3: MRN / Patient ID
if len(fields) > 3 and fields[3]:
patient_id = fields[3].split("^")[0]
patient_id = fields[3].split("^")[0].strip()
vitals_dict["patient_id"] = patient_id
# PID-5: Patient Name (format: FamilyName^GivenName^...)
if len(fields) > 5 and fields[5]:
name_parts = fields[5].split("^")
family = name_parts[0].strip() if len(name_parts) > 0 else ""
given = name_parts[1].strip() if len(name_parts) > 1 else ""
full_name = f"{given} {family}".strip() or family or given
if full_name:
vitals_dict["patient_name"] = full_name
# PID-7: Date of Birth (YYYYMMDD or YYYY-MM-DD)
if len(fields) > 7 and fields[7]:
vitals_dict["patient_dob"] = fields[7].strip()
# PID-8: Administrative Sex (M / F / U / O)
if len(fields) > 8 and fields[8]:
vitals_dict["patient_gender"] = fields[8].strip().upper()
elif seg_name == "PV1":
# PV1-3: Assigned Patient Location (Room^Bed^...)
if len(fields) > 3 and fields[3]:
location_parts = fields[3].split("^")
# Use second component (Bed) if present, else first (Room)
bed = location_parts[1].strip() if len(location_parts) > 1 else location_parts[0].strip()
if bed:
vitals_dict["bed_id"] = bed
elif seg_name == "OBX":
if len(fields) > 5:
obs_val = fields[5].strip()
@@ -322,7 +349,8 @@ def _parse_286_byte_packet(data: bytes, vitals_dict: dict) -> bool:
"""
Parse 286-byte waveform packet (Subtype 21).
Verified layout at tail:
Verified layout:
[8..263] Pleth waveform samples (8-bit unsigned, 256 samples)
[264-265] SpO2% (LE u16) — live oxygen saturation percentage
255 / 65535 = sensor disconnected
[266-267] ECG HR via SpO2 PR (LE u16) — 9999 / 65535 = not available
@@ -334,10 +362,20 @@ def _parse_286_byte_packet(data: bytes, vitals_dict: dict) -> bool:
vitals_dict["present_fields"] = []
if "spo2" not in vitals_dict["present_fields"]:
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)
found = False
# Extract Pleth waveform samples (bytes 8..263)
raw_pleth = list(data[8:264])
# Only store if not all-zeroes or all-sentinel (flatline = disconnected)
if any(v not in (0x00, 0x7F, 0x3A, 0x92, 0xFF) for v in raw_pleth[:10]):
vitals_dict["pleth_wave"] = raw_pleth
else:
vitals_dict["pleth_wave"] = raw_pleth # Always send so frontend can show flatline too
# Offset 264: SpO2 percentage
spo2_val = read_u16_le(data, 264)
if spo2_val is not None and spo2_val != 65535 and spo2_val != 255 and 50 <= spo2_val <= 100:
@@ -360,6 +398,7 @@ def _parse_288_byte_packet(data: bytes, vitals_dict: dict) -> bool:
Parse 288-byte waveform packet (Subtype 21 for CMS8500).
Layout:
[8..263] Pleth waveform samples (8-bit unsigned, 256 samples)
[264-265] SpO2% (LE u16) — live oxygen saturation percentage
[266-267] PR (LE u16) — pulse rate
"""
@@ -370,10 +409,15 @@ def _parse_288_byte_packet(data: bytes, vitals_dict: dict) -> bool:
vitals_dict["present_fields"] = []
if "spo2" not in vitals_dict["present_fields"]:
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)
found = False
# Extract Pleth waveform samples (bytes 8..263)
vitals_dict["pleth_wave"] = list(data[8:264])
# Offset 264: SpO2 percentage
spo2_val = read_u16_le(data, 264)
if spo2_val is not None and spo2_val != 65535 and spo2_val != 255 and 50 <= spo2_val <= 100:
@@ -423,9 +467,10 @@ def _parse_341_byte_packet(data: bytes, vitals_dict: dict) -> bool:
def _parse_989_byte_packet(data: bytes, vitals_dict: dict) -> bool:
"""
Parse 989-byte waveform packet (Subtype 20).
Parse 989-byte waveform packet (Subtype 20 / 14h).
Verified layout:
[8..903] ECG waveform samples (8-bit unsigned, 896 samples @ ~250Hz)
[904-905] ECG Heart Rate (LE u16)
65535 or 9999 = invalid/disconnected
"""
@@ -436,10 +481,17 @@ def _parse_989_byte_packet(data: bytes, vitals_dict: dict) -> bool:
vitals_dict["present_fields"] = []
if "heart_rate" not in vitals_dict["present_fields"]:
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)
found = False
# Extract ECG waveform samples (bytes 8..903), downsample to 200 points for efficiency
raw_ecg = list(data[8:904])
# Downsample by factor of 4 -> ~224 points (still high-resolution enough)
vitals_dict["ecg_wave"] = raw_ecg[::4]
hr_val = read_u16_le(data, 904)
if hr_val is not None and hr_val != 65535 and hr_val != 9999 and 20 <= hr_val <= 300:
vitals_dict["heart_rate"] = float(hr_val)