"""本主题专属离线算法，仅读取同目录合成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 xml.etree.ElementTree as ET
node=ET.fromstring(data['gdtf_fragment']);offsets=[int(v) for v in node.attrib['Offset'].split(',')];assert offsets==[3,4]
rows=[]
for value in data['values']:
 assert 0<=value<=65535
 coarse,fine=divmod(value,256);decoded=coarse*256+fine;wrong=fine*256+coarse
 assert decoded==value
 rows.append({'value16':value,'coarse_offset':offsets[0],'fine_offset':offsets[1],'coarse':coarse,'fine':fine,'decoded':decoded,'swapped_decoded':wrong,'normalized':decoded/65535})
assert rows[0]['swapped_decoded']==65280 and rows[1]['swapped_decoded']==1
result={'attribute':'虚构调光Dimmer','rows':rows,'fragment_is_full_fixture':False,'device_write_performed':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))
