import json from edge.detectors.zone import Zone def zones_from_config(config: dict, camera_id: str | None = None) -> list[Zone]: """Parse cloud config zone rows into Zone objects. When camera_id is given, only rows for that camera are kept (multi-camera routing); None keeps all rows.""" out: list[Zone] = [] rows = config.get("zones", []) or [] if not isinstance(rows, list): return [] for row in rows: try: if camera_id is not None and str(row["camera_id"]) != str(camera_id): continue raw = row["polygon_json"] pts = json.loads(raw) if isinstance(raw, str) else raw polygon = [(float(p[0]), float(p[1])) for p in pts] if len(polygon) < 3: continue out.append(Zone(str(row["zone_id"]), str(row["level"]), polygon)) except (KeyError, ValueError, TypeError, IndexError, AttributeError, json.JSONDecodeError): continue return out def thresholds_from_config(config: dict) -> dict: t = config.get("thresholds") return t if isinstance(t, dict) else {}