# 1차 센서 목록(한 곳) → frontend/spec-src-v1/capture/sensors.json
#   capture(그림 렌더) 와 labels.py(라벨) 가 같이 읽음
import json
Z = {1: dict(temp=25.8, hum=68, co2=620, soil=42), 2: dict(temp=30.6, hum=61, co2=540, soil=38), 3: dict(temp=26.4, hum=70, co2=590, soil=45)}
RANGE = dict(temp=((18, 27), (10, 32)), hum=((60, 85), (40, 90)), co2=((400, 1000), (350, 1500)), soil=((30, 60), (20, 70)), ec=((1.5, 3.0), (1.0, 3.5)))
def status(m, v):
    ok, ca = RANGE[m]
    return 'ok' if ok[0] <= v <= ok[1] else 'caution' if ca[0] <= v <= ca[1] else 'warn'
RANK = ['ok', 'caution', 'warn', 'fault']
worst = lambda *s: max(s, key=RANK.index)
T_OFF = {1: [-0.4, 0, 0.3, 0.2, 0.5, 1.1], 2: [-0.8, 0, 0.8, 0.4, 1.0, 1.4], 3: [-0.2, 0, 0.2, 0.1, 0.3, 0.5]}
H_OFF = {1: [1, 0, -1, 0, -1], 2: [2, 0, -2, 1, -1], 3: [1, 0, -1, 0, -1]}
EC = {1: [2.2, 2.0, 2.4], 2: [2.3, 2.1, 2.5], 3: [2.4, 2.2, 2.6]}
LUX = {1: 630, 2: 655, 3: 620}
out = []
for b in (1, 2, 3):
    cx = (b - 1) * 8 + 4; z = Z[b]
    th = [('앞', cx, 10), ('가운데', cx, 25), ('뒤', cx, 40), ('왼쪽 앞', cx - 2.6, 17), ('오른쪽 뒤', cx + 2.6, 33)]
    for i, (where, x, zz) in enumerate(th):
        sid = f'T{b}-{i + 1}'
        if sid == 'T3-2':
            out.append(dict(id=sid, k='th', bay=b, x=x, z=zz, h=1.5, where=where, v=None, st='fault')); continue
        t = round(z['temp'] + T_OFF[b][i], 1); h = z['hum'] + H_OFF[b][i]
        out.append(dict(id=sid, k='th', bay=b, x=x, z=zz, h=1.5, where=where, v=dict(temp=t, hum=h), st=worst(status('temp', t), status('hum', h))))
    t = round(z['temp'] + T_OFF[b][5], 1)
    out.append(dict(id=f'T{b}-U', k='u', bay=b, x=cx, z=25, h=3.8, where='상부', v=dict(temp=t), st=status('temp', t)))
    for sid, x, zz, dv in ((f'C{b}', cx + 1.3, 31, 0), (f'C{b}-2', cx - 1.3, 12, 30)):
        out.append(dict(id=sid, k='co2', bay=b, x=x, z=zz, h=1.2, where='', v=dict(co2=z['co2'] + dv), st=status('co2', z['co2'] + dv)))
    for i, (x, zz, dv) in enumerate(((cx - 1.75, 15, 1), (cx - 1.75, 35, -1), (cx + 1.75, 25, 0))):
        s = z['soil'] + dv; e = EC[b][i]
        out.append(dict(id=f'S{b}-{i + 1}', k='soil', bay=b, x=x, z=zz, h=0.12, where='', v=dict(soil=s, ec=e), st=worst(status('soil', s), status('ec', e))))
    out.append(dict(id=f'L{b}', k='light', bay=b, x=cx + 0.6, z=20, h=2.4, where='작물 위', v=dict(light=LUX[b]), st='ok'))
json.dump(out, open('/smartfarm/frontend/spec-src-v1/capture/sensors.json', 'w', encoding='utf-8'), ensure_ascii=False, indent=0)
from collections import Counter
print(len(out), Counter(s['k'] for s in out), Counter(s['st'] for s in out))
