#!/usr/bin/env python3 """Сбор «правил взаимодействия» Yealink в compat/rules.yaml. Правило = что с чем соединяется и на каких условиях. Источники: 1. матрица совместимости (graph.yaml) — официальные связки с условиями; 2. интерфейсы (interfaces_verified.yaml) — порты и питание; 3. вычисляемые признаки: «хаб» (устройство, принимающее микрофоны/спикеры). Зачем: автогенерация схем должна опираться не только на «есть порт», но и на практику вендора: камера — микрофонный хаб, RoomCast — только HDMI-выход, ПК приходит через WPP30, питание отдельным ярусом. """ import json import re import yaml BASE = "/opt/work-hub/compat" OUT = BASE + "/rules.yaml" def norm(s): return re.sub(r"[^a-z0-9а-я]+", "", str(s or "").lower()) def main(): graph = yaml.safe_load(open(BASE + "/graph.yaml", encoding="utf-8")) or {} iface = yaml.safe_load(open(BASE + "/interfaces_verified.yaml", encoding="utf-8")) or {} cat = yaml.safe_load(open(BASE + "/catalog.yaml", encoding="utf-8")) or {} roles = {norm(x.get("name")): x.get("role") for x in (cat.get("devices") or [])} ports = {norm(x.get("device")): x for x in (iface.get("devices") or [])} edges = graph.get("edges") or [] rules = [] for e in edges: rules.append({ "from": e.get("from"), "to": e.get("to"), "ok": bool(e.get("supported")), "max_qty": e.get("max_qty"), "condition": (e.get("condition") or "").strip()[:160], "source": "matrix", }) # хабы: кто принимает микрофоны/спикеры hubs = {} for e in edges: if not e.get("supported"): continue to = norm(e.get("to")) if roles.get(to) in ("audio", "speaker"): h = hubs.setdefault(norm(e.get("from")), {"name": e.get("from"), "max": 0, "devs": []}) h["max"] = max(h["max"], int(e.get("max_qty") or 0)) if e.get("to") not in h["devs"]: h["devs"].append(e.get("to")) hub_list = [{"device": v["name"], "max_qty": v["max"], "supports": v["devs"][:12], "count": len(v["devs"])} for v in hubs.values() if v["max"]] layout = [] for k, role in roles.items(): if not role: continue tier = {"infrastructure": 0, "camera": 1, "sharing_or_hub": 1, "speaker": 1, "audio": 1, "codec": 3, "display": 2, "pc": 2, "power": 4, "charger": 4}.get(role) if tier is not None: layout.append({"role": role, "tier": tier}) data = { "updated": __import__("time").strftime("%Y-%m-%dT%H:%M"), "source": "graph.yaml + interfaces_verified.yaml + catalog.yaml", "rules": rules, "hubs": sorted(hub_list, key=lambda x: -x["count"])[:30], "layout_tiers": layout, "ports": {k: {"in": v.get("in") or [], "out": v.get("out") or [], "poe_budget_w": v.get("poe_budget_w"), "battery_hours": v.get("battery_hours")} for k, v in ports.items()}, } open(OUT, "w", encoding="utf-8").write( yaml.safe_dump(data, allow_unicode=True, sort_keys=False)) print("записано:", OUT) print(" правил (связок из матрицы):", len(rules)) print(" поддерживаемых:", sum(1 for r in rules if r["ok"])) print(" запрещённых:", sum(1 for r in rules if not r["ok"])) print(" хабов (принимают микрофоны/спикеры):", len(hub_list)) for h in data["hubs"][:6]: print(" %-24s до %d × %d видов" % (h["device"], h["max_qty"], h["count"])) print(" ярусов раскладки:", len(set(x["tier"] for x in layout))) if __name__ == "__main__": main()