refactor: implement packet parsing heuristics and introduce device-level state caching to throttle database writes and vitals updates

This commit is contained in:
2026-07-11 14:24:14 +05:30
parent a4972b3251
commit 84b8acc904
32 changed files with 3499 additions and 180 deletions
@@ -0,0 +1,58 @@
import struct
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}")
import sys
sys.exit(1)
packets_288 = []
packets_989 = []
i = 0
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 pkt_len == 288 and i + pkt_len <= len(stream):
packets_288.append(stream[i:i + pkt_len])
elif pkt_len == 989 and i + pkt_len <= len(stream):
packets_989.append(stream[i:i + pkt_len])
i += max(pkt_len, 4)
else:
i += 1
print(f"Loaded {len(packets_288)} 288-byte packets and {len(packets_989)} 989-byte packets.")
# Search all offsets in 288-byte packets for any u16 LE value between 70 and 95
print("\nScanning 288-byte packets for u16 LE values in range [75, 95] across all packets:")
found_offsets_288 = {}
for idx, pkt in enumerate(packets_288):
for off in range(4, len(pkt) - 1, 2):
val = struct.unpack_from('<H', pkt, off)[0]
if 75 <= val <= 95:
found_offsets_288.setdefault(off, []).append((idx, val))
for off, matches in found_offsets_288.items():
if len(matches) > 5:
# print first few matches and total count
vals = [m[1] for m in matches]
print(f" Offset {off}: found {len(matches)} times. Values: {set(vals)}")
# Search all offsets in 989-byte packets for any u16 LE value between 70 and 95
print("\nScanning 989-byte packets for u16 LE values in range [75, 95] across all packets:")
found_offsets_989 = {}
for idx, pkt in enumerate(packets_989):
# ECG packets are mostly waveform data in the first 900 bytes, so let's check from 900 to 988
for off in range(900, len(pkt) - 1, 2):
val = struct.unpack_from('<H', pkt, off)[0]
if 75 <= val <= 95:
found_offsets_989.setdefault(off, []).append((idx, val))
for off, matches in found_offsets_989.items():
if len(matches) > 5:
vals = [m[1] for m in matches]
print(f" Offset {off}: found {len(matches)} times. Values: {set(vals)}")