รับคำสั่งแบบไม่ไว้ใจใคร — 03 กรองทุกช่อง
def known_models():
return {m["name"]: m["index"] for m in edge_ai.models() if m["name"] in ALLOWED}
def parse_cmd(raw, known):
if len(raw) > MAX_CMD_BYTES:
return None, "too long"
try:
c = json.loads(raw.decode())
except Exception:
return None, "not JSON"
if not isinstance(c, dict):
return None, "not a JSON object"
cmd = c.get("cmd")
if cmd == "select":
m = c.get("model")
return ("model", m) if isinstance(m, str) and m in known else (None, "unknown model")
if cmd in ("stop", "run"):
return (cmd,)
return None, "unknown command"
ทำตามเฉพาะชื่อที่อยู่ใน ALLOWED และมีบนบอร์ดจริง นอกนั้นปฏิเสธพร้อมเหตุผลบนจอ