feat/ ver0.1
This commit is contained in:
@@ -29,22 +29,39 @@ def _fmt_body(resp: httpx.Response) -> Any:
|
||||
return resp.text
|
||||
|
||||
|
||||
_last_camera: dict | None = None
|
||||
_last_health: dict | None = None
|
||||
|
||||
|
||||
def _has_data(body: Any) -> bool:
|
||||
"""非空业务数据:code==200 且 data 里至少有一个非空字段。"""
|
||||
if not isinstance(body, dict):
|
||||
return False
|
||||
if body.get("code") != 200:
|
||||
return True # 错误也算"新信息"
|
||||
data = body.get("data", {})
|
||||
if not isinstance(data, dict):
|
||||
return bool(data)
|
||||
return any(bool(v) for v in data.values())
|
||||
|
||||
|
||||
async def poll_once(client: httpx.AsyncClient, base: str) -> None:
|
||||
global _last_camera, _last_health
|
||||
base = base.rstrip("/")
|
||||
camera_url = f"{base}/api/v1/biomass/real/camera/"
|
||||
health_url = f"{base}/api/v1/biomass/health/result/"
|
||||
r1 = await client.get(camera_url)
|
||||
r2 = await client.get(health_url)
|
||||
logger.info(
|
||||
"[real/camera/] HTTP {} | {}",
|
||||
r1.status_code,
|
||||
json.dumps(_fmt_body(r1), ensure_ascii=False),
|
||||
)
|
||||
logger.info(
|
||||
"[health/result/] HTTP {} | {}",
|
||||
r2.status_code,
|
||||
json.dumps(_fmt_body(r2), ensure_ascii=False),
|
||||
)
|
||||
b1 = _fmt_body(r1)
|
||||
b2 = _fmt_body(r2)
|
||||
changed1 = b1 != _last_camera
|
||||
changed2 = b2 != _last_health
|
||||
if changed1 and _has_data(b1):
|
||||
logger.info("[real/camera/] HTTP {} | {}", r1.status_code, json.dumps(b1, ensure_ascii=False))
|
||||
if changed2 and _has_data(b2):
|
||||
logger.info("[health/result/] HTTP {} | {}", r2.status_code, json.dumps(b2, ensure_ascii=False))
|
||||
_last_camera = b1
|
||||
_last_health = b2
|
||||
|
||||
|
||||
async def poll_loop(base: str, interval: float) -> None:
|
||||
|
||||
Reference in New Issue
Block a user