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,63 @@
import struct
def search_value_in_packet(p: bytes, val: int, label: str):
# Search as uint8
for offset in range(len(p)):
if p[offset] == val:
print(f" {label} found as uint8 at offset {offset}")
# Search as uint16 LE
for offset in range(len(p) - 1):
v = struct.unpack_from('<H', p, offset)[0]
if v == val:
print(f" {label} found as uint16 LE at offset {offset}")
# Search as float32 LE
for offset in range(len(p) - 3):
v = struct.unpack_from('<f', p, offset)[0]
if abs(v - float(val)) < 0.01:
print(f" {label} found as float32 LE at offset {offset}")
with open("/home/prathiyuman/Prathiyuman/ContecMonitor/fiveparaminte-main/scratch/raw_contec_stream.bin", "rb") as f:
stream = f.read()
# Extract packets
packets = []
consumed = 0
while consumed < len(stream) - 4:
b0 = stream[consumed]
b1 = stream[consumed + 1]
b2 = stream[consumed + 2]
b3 = stream[consumed + 3]
if (b2 == 0x04 or b2 == 0x01) and b3 == 0x46:
pkt_len = b0 | (b1 << 8)
if pkt_len < 5 or pkt_len > 8192:
consumed += 1
continue
if consumed + pkt_len <= len(stream):
packets.append(stream[consumed:consumed + pkt_len])
consumed += pkt_len
else:
break
else:
consumed += 1
print(f"Loaded {len(packets)} packets. Scanning for HR=88, Systolic=108, MAP=75...")
for idx, p in enumerate(packets):
l = len(p)
subtype = p[6] if l > 6 else -1
# Let's decode timestamp if it is a 45-byte packet
ts_str = ""
if l == 45:
year = struct.unpack_from('<H', p, 8)[0]
month, day, hour, minute, second = p[10], p[11], p[12], p[13], p[14]
ts_str = f" @ {year}-{month:02d}-{day:02d} {hour:02d}:{minute:02d}:{second:02d}"
print(f"\nPacket #{idx} (Len={l}, Subtype={subtype}){ts_str}:")
search_value_in_packet(p, 88, "HR (88)")
search_value_in_packet(p, 108, "Systolic (108)")
search_value_in_packet(p, 75, "MAP (75)")