"""本主题专属离线算法，仅读取同目录合成JSON，不联网、不控制设备。"""
import csv, json, math, re, struct
from pathlib import Path
from fractions import Fraction
BASE=Path(__file__).resolve().parent
data=json.loads((BASE/'input.json').read_text(encoding='utf-8'))['data']
import uuid
slots=bytes(data['slots']);total=126+len(slots);p=bytearray(total)
struct.pack_into('>HH',p,0,16,0);p[4:16]=b'ASC-E1.17'+bytes(3)
struct.pack_into('>HI',p,16,0x7000|(total-16),4);p[22:38]=uuid.UUID('11111111-1111-4111-8111-111111111111').bytes
struct.pack_into('>HI',p,38,0x7000|(total-38),2);p[44:51]=b'fixture';p[108]=data['priority'];p[111]=1
struct.pack_into('>H',p,113,data['universe']);struct.pack_into('>H',p,115,0x7000|(total-115));p[117]=2;p[118]=0xa1
struct.pack_into('>HHH',p,119,0,1,len(slots)+1);p[125]=0;p[126:]=slots
bad=bytearray(p);struct.pack_into('>H',bad,123,len(slots))
rows=[]
for name,b in [('valid',p),('count_missing_start_code',bad),('truncated',p[:-1])]:
 count=struct.unpack_from('>H',b,123)[0];ok=1<=count<=513 and len(b)==125+count and (struct.unpack_from('>H',b,16)[0]&0xfff)==len(b)-16
 rows.append({'case':name,'hex':b.hex(),'property_value_count':count,'declared_slots':count-1,'actual_bytes':len(b),'strict_count_boundary_valid':ok})
assert [r['strict_count_boundary_valid'] for r in rows]==[True,False,False]
result={'packets':rows,'offset_origin':'完整UDP负载第0字节','complete_protocol_stack':False}
(BASE/'result.json').write_text(json.dumps(result,ensure_ascii=False,indent=2)+'\n',encoding='utf-8')
print(json.dumps(result,ensure_ascii=False))
