refactor: implement packet parsing heuristics and introduce device-level state caching to throttle database writes and vitals updates
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add parent directory to path
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from contec_parser import parse_contec_binary_packet
|
||||
|
||||
# Read captured stream
|
||||
filepath = "/home/prathiyuman/Prathiyuman/ContecMonitor/fiveparaminte-main/scratch/raw_contec_stream.bin"
|
||||
try:
|
||||
with open(filepath, "rb") as f:
|
||||
stream = f.read()
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
print(f"Loaded raw stream of {len(stream)} bytes")
|
||||
|
||||
# We will scan the stream and feed packets into the parser
|
||||
# Since we know the packets start with 0x46 at index + 3, let's scan.
|
||||
i = 0
|
||||
packet_count = 0
|
||||
parsed_count = 0
|
||||
last_vitals = None
|
||||
|
||||
while i < len(stream) - 4:
|
||||
b0, b1, b2, b3 = stream[i], stream[i+1], stream[i+2], stream[i+3]
|
||||
if b3 == 0x46:
|
||||
pkt_len = b0 | (b1 << 8)
|
||||
if 30 < pkt_len < 1050 and i + pkt_len <= len(stream):
|
||||
pkt_data = stream[i:i + pkt_len]
|
||||
packet_count += 1
|
||||
|
||||
# Feed packet to parser
|
||||
vitals = parse_contec_binary_packet(pkt_data, "192.168.100.139")
|
||||
if vitals:
|
||||
parsed_count += 1
|
||||
# Check if fields changed
|
||||
v_dict = {k: v for k, v in vitals.model_dump().items() if v is not None and k not in ['device_id', 'patient_id', 'timestamp', 'ip_address', 'present_fields']}
|
||||
if v_dict and v_dict != last_vitals:
|
||||
print(f"Index {i} (len={pkt_len}) -> Vitals: {v_dict}")
|
||||
last_vitals = v_dict
|
||||
i += max(pkt_len, 4)
|
||||
else:
|
||||
i += 1
|
||||
else:
|
||||
i += 1
|
||||
|
||||
print(f"\nDone. Scanned {packet_count} packets, successfully parsed {parsed_count} packets.")
|
||||
Reference in New Issue
Block a user