59 lines
1.8 KiB
Python
59 lines
1.8 KiB
Python
"""Register identification CSV results and poll the server for 0/1 review."""
|
|
|
|
|
|
def _post(payload, timeout=10):
|
|
import requests
|
|
from api import data_record_url
|
|
|
|
try:
|
|
response = requests.post(data_record_url, json=payload, timeout=timeout)
|
|
response.raise_for_status()
|
|
result = response.json()
|
|
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."""
|
|
from api import the_folder
|
|
|
|
if not run_id:
|
|
raise ValueError("辨识结果缺少 run_id")
|
|
_post({
|
|
"type": "registerIdentificationResult",
|
|
"deviceId": the_folder,
|
|
"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."""
|
|
from api import the_folder
|
|
|
|
result = _post({
|
|
"type": "getIdentificationFeedback",
|
|
"deviceId": the_folder,
|
|
"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."""
|
|
from api import the_folder
|
|
|
|
_post({
|
|
"type": "ackIdentificationFeedback",
|
|
"deviceId": the_folder,
|
|
"runId": run_id,
|
|
}, timeout=timeout)
|