47 lines
1.5 KiB
Python
47 lines
1.5 KiB
Python
"""Register identification CSV results and poll the server for 0/1 review."""
|
|
|
|
|
|
def _post(payload, timeout=10):
|
|
from api import device_post
|
|
|
|
try:
|
|
result = device_post(payload, timeout=timeout)
|
|
except Exception as exc:
|
|
raise ValueError(f"辨识反馈服务请求失败: {exc}") from exc
|
|
if not result.get("success"):
|
|
raise ValueError(result.get("errMsg", "辨识反馈服务拒绝请求"))
|
|
return result
|
|
|
|
|
|
def register_identification_result(run_id: str, timeout=10) -> None:
|
|
"""Register one uploaded CSV as the customer's current review target."""
|
|
if not run_id:
|
|
raise ValueError("辨识结果缺少 run_id")
|
|
_post({
|
|
"type": "registerIdentificationResult",
|
|
"runId": run_id,
|
|
"fileName": run_id,
|
|
}, timeout=timeout)
|
|
|
|
|
|
def get_identification_feedback(run_id: str, timeout=10):
|
|
"""Return None while pending, otherwise return the integer 0 or 1."""
|
|
result = _post({
|
|
"type": "getIdentificationFeedback",
|
|
"runId": run_id,
|
|
}, timeout=timeout)
|
|
if not result.get("ready"):
|
|
return None
|
|
feedback = result.get("result")
|
|
if isinstance(feedback, bool) or feedback not in (0, 1):
|
|
raise ValueError("云端辨识反馈必须是数字 0 或 1")
|
|
return int(feedback)
|
|
|
|
|
|
def acknowledge_identification_feedback(run_id: str, timeout=10) -> None:
|
|
"""Delete the consumed review record so stale feedback cannot be reused."""
|
|
_post({
|
|
"type": "ackIdentificationFeedback",
|
|
"runId": run_id,
|
|
}, timeout=timeout)
|