data_source.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334
  1. """陕西局 JSON 适配入口;真实接口路径由同事提供,不猜测路径。"""
  2. import asyncio
  3. import config
  4. from algorithms.main_fan.models import Device, MonitorPoint, OperatingSeries, SwitchRecord, FanStateRecord
  5. def items_of(value):
  6. """显式支持 items 或 data.items/list;其他响应要求补充接口适配。"""
  7. if isinstance(value, dict) and "data" in value:
  8. value = value["data"]
  9. if isinstance(value, dict):
  10. value = value.get("items")
  11. if not isinstance(value, list):
  12. raise ValueError("业务接口未返回 items 数组,请按对方实际响应补充字段适配")
  13. return value
  14. async def fetch(gateway, agent_code, mine_code):
  15. paths = {kind: config.get_str(key) for kind, key in {
  16. "devices": "MAIN_FAN_DEVICES_PATH", "points": "MAIN_FAN_POINTS_PATH", "files": "MAIN_FAN_REPORTS_PATH",
  17. "operating_series":"MAIN_FAN_SERIES_PATH", "switch_records":"MAIN_FAN_SWITCHES_PATH",
  18. "fan_state_records":"MAIN_FAN_OPERATION_LOG_PATH"}.items()}
  19. selected = [(kind, path) for kind, path in paths.items() if path]
  20. if not selected:
  21. raise ValueError("尚未配置主通风机设备、测点或报告目录接口路径")
  22. values = await asyncio.gather(*(gateway.proxy(agent_code, path, params={"mineCode": mine_code}) for _, path in selected))
  23. result = {kind: [] for kind in paths}
  24. for (kind, _), value in zip(selected, values):
  25. rows = items_of(value)
  26. schema = {"devices": Device, "points": MonitorPoint,"operating_series":OperatingSeries,
  27. "switch_records":SwitchRecord,"fan_state_records":FanStateRecord}.get(kind)
  28. result[kind] = [schema.model_validate(r).model_dump(mode="json") for r in rows] if schema else rows
  29. return result