feat: 修订了调试功能
这个提交包含在:
@ -1,7 +1,10 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Query
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import APIRouter, Header, HTTPException, Query
|
||||
|
||||
from app.core.dependencies import find_field, list_fields, reading_guard_manager, resolve_session_id
|
||||
from app.core.experiment_debug import VoiceSpeedDebugProvider, debug_registry
|
||||
from app.schemas.validation import DebugMenuItem, DebugMenuResponse
|
||||
|
||||
@ -121,3 +124,38 @@ async def debug_oscilloscope(action: str = Query(default="status")):
|
||||
return provider.capture_frequency()
|
||||
else:
|
||||
raise HTTPException(status_code=400, detail=f"unknown action '{action}', use: status, peak, frequency")
|
||||
|
||||
|
||||
@router.get("/guarded-fields")
|
||||
async def debug_guarded_fields(
|
||||
experiment: str = Query(default="cn.edu.muc.lxy.phyexp.voicespeed"),
|
||||
):
|
||||
fields = list_fields(experiment)
|
||||
guarded = [
|
||||
{
|
||||
"field_id": f.fieldId,
|
||||
"source": f.readingGuard.source.value if f.readingGuard else None,
|
||||
"tolerance": f.readingGuard.tolerance if f.readingGuard else None,
|
||||
"placeholder": f.placeholder,
|
||||
"unit": f.unit,
|
||||
}
|
||||
for f in fields
|
||||
if f.readingGuard and f.readingGuard.enabled
|
||||
]
|
||||
return {"experiment": experiment, "fields": guarded, "count": len(guarded)}
|
||||
|
||||
|
||||
@router.get("/answer")
|
||||
async def debug_answer(
|
||||
field_id: str = Query(...),
|
||||
experiment: str = Query(default="cn.edu.muc.lxy.phyexp.voicespeed"),
|
||||
x_session_id: str | None = Header(default=None),
|
||||
):
|
||||
session_id = resolve_session_id(x_session_id)
|
||||
all_fields = list_fields(experiment)
|
||||
result = reading_guard_manager.get_reference_answer(session_id, field_id, all_fields)
|
||||
if result is None:
|
||||
raise HTTPException(status_code=400, detail="no reading guard active for this experiment")
|
||||
if "error" in result:
|
||||
raise HTTPException(status_code=400, detail=result["error"])
|
||||
return {"experiment": experiment, "session_id": session_id, **result}
|
||||
|
||||
@ -22,6 +22,44 @@ _active_monitors: dict[str, "_PeakMonitor"] = {}
|
||||
_lock = threading.Lock()
|
||||
|
||||
|
||||
def _auto_start_default() -> None:
|
||||
with _lock:
|
||||
session_id = "default"
|
||||
existing = _active_monitors.pop(session_id, None)
|
||||
if existing is not None:
|
||||
existing.stop()
|
||||
detector = PeakDetector()
|
||||
caliper_ocr_service.reset()
|
||||
monitor = _PeakMonitor(session_id, detector)
|
||||
_active_monitors[session_id] = monitor
|
||||
monitor.start()
|
||||
_logger.info("峰值监控自动启动 (f5 已完成)")
|
||||
|
||||
|
||||
reading_guard_manager.set_peak_ocr_start_callback(_auto_start_default)
|
||||
|
||||
|
||||
def _peak_ocr_done() -> None:
|
||||
with _lock:
|
||||
monitor = _active_monitors.get("default")
|
||||
if monitor is not None:
|
||||
monitor.detector.acknowledge_peak()
|
||||
_logger.info("峰值已确认,进入冷却等待下一次上升")
|
||||
|
||||
|
||||
def _peak_ocr_failed() -> None:
|
||||
with _lock:
|
||||
monitor = _active_monitors.get("default")
|
||||
if monitor is not None:
|
||||
monitor.detector.acknowledge_peak()
|
||||
caliper_ocr_service._index = max(0, caliper_ocr_service._index - 1)
|
||||
_logger.info("峰值验证失败,回退 OCR 索引至 %d,请重新寻找峰值", caliper_ocr_service._index)
|
||||
|
||||
|
||||
reading_guard_manager.set_peak_ocr_done_callback(_peak_ocr_done)
|
||||
reading_guard_manager.set_peak_ocr_failed_callback(_peak_ocr_failed)
|
||||
|
||||
|
||||
class _PeakMonitor:
|
||||
def __init__(
|
||||
self,
|
||||
@ -57,6 +95,11 @@ class _PeakMonitor:
|
||||
self.detector.set_ocr_reading(ocr_result.reading_mm)
|
||||
_logger.info("峰值 %d 已检测 OCR读数=%.2f mm session=%s",
|
||||
result.peak_index, ocr_result.reading_mm, self.session_id)
|
||||
pending = reading_guard_manager.get_pending_field(self.session_id)
|
||||
if pending and ocr_result.reading_mm is not None:
|
||||
reading_guard_manager.submit_peak_ocr_answer(
|
||||
self.session_id, pending[0], ocr_result.reading_mm)
|
||||
_logger.info("OCR读数已提交到 reading_guard field=%s", pending[0])
|
||||
except Exception as exc:
|
||||
_logger.warning("峰值轮询异常: %s", exc)
|
||||
time.sleep(POLL_INTERVAL)
|
||||
@ -166,3 +209,23 @@ async def peak_monitor_acknowledge(
|
||||
"peak_index": result.peak_index,
|
||||
"state": result.state,
|
||||
}
|
||||
|
||||
|
||||
@router.post("/undo")
|
||||
async def peak_monitor_undo(
|
||||
x_session_id: str | None = Header(default=None),
|
||||
):
|
||||
session_id = resolve_session_id(x_session_id)
|
||||
monitor = _get_monitor(session_id)
|
||||
if monitor is None:
|
||||
raise HTTPException(status_code=400, detail="峰值监控未启动")
|
||||
|
||||
detector = monitor.detector
|
||||
if detector.state == PeakState.IDLE:
|
||||
return {"success": False, "message": "无峰值可回退", "state": detector.state}
|
||||
|
||||
detector._state = PeakState.PEAK_TRIGGERED
|
||||
caliper_ocr_service._index = max(0, caliper_ocr_service._index - 1)
|
||||
|
||||
_logger.info("峰值回退 session=%s index=%d", session_id, caliper_ocr_service._index)
|
||||
return {"success": True, "message": "已回退到上一个峰值", "state": detector.state}
|
||||
|
||||
@ -2,14 +2,33 @@ from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, Header, HTTPException, Query
|
||||
|
||||
from app.api.peak_monitor import _active_monitors, _get_monitor
|
||||
from app.core.dependencies import find_field, list_fields, reading_guard_manager, resolve_session_id, session_store
|
||||
from app.core.validation import FieldValidator
|
||||
from app.schemas.experiment import ReadingGuardSource
|
||||
from app.schemas.validation import FieldValidationRequest, FieldValidationResponse
|
||||
|
||||
|
||||
router = APIRouter(prefix="/value", tags=["value"])
|
||||
|
||||
|
||||
def _peak_monitor_status_text(session_id: str) -> str | None:
|
||||
monitor = _get_monitor(session_id) or _get_monitor("default")
|
||||
if monitor is None:
|
||||
return None
|
||||
detector = monitor.detector
|
||||
state = detector.state.value
|
||||
if state == "idle":
|
||||
return "空闲 — 等待数值上升"
|
||||
if state == "rising":
|
||||
return f"上升中 — 当前峰值 {detector._peak_max:.1f}"
|
||||
if state == "peak_triggered":
|
||||
return f"峰值已检测 (#{detector._peak_index}) — 请读取游标卡尺"
|
||||
if state == "cooldown":
|
||||
return "冷却中 — 等待下一次上升"
|
||||
return None
|
||||
|
||||
|
||||
@router.post("")
|
||||
async def validate_field_value(
|
||||
request: FieldValidationRequest,
|
||||
@ -30,21 +49,71 @@ async def validate_field_value(
|
||||
all_fields=list_fields(experiment),
|
||||
)
|
||||
if guard_result is not None:
|
||||
guard_accept, guard_message = guard_result
|
||||
if not guard_accept:
|
||||
return FieldValidationResponse(accept=False, message=guard_message)
|
||||
pending = reading_guard_manager.get_pending_field(session_id)
|
||||
ctrl = _peak_monitor_status_text(session_id)
|
||||
if not guard_result["accept"]:
|
||||
return FieldValidationResponse(
|
||||
accept=False, message=guard_result["message"],
|
||||
severity=guard_result.get("severity", "error"),
|
||||
reference=guard_result.get("reference"),
|
||||
pending_field=pending[0] if pending else None,
|
||||
control_status=ctrl,
|
||||
)
|
||||
session_store.set_value(session_id, request.field_id, request.value)
|
||||
return FieldValidationResponse(accept=True, message=guard_message)
|
||||
return FieldValidationResponse(
|
||||
accept=True, message=guard_result["message"],
|
||||
severity=guard_result.get("severity", "ok"),
|
||||
reference=guard_result.get("reference"),
|
||||
pending_field=pending[0] if pending else None,
|
||||
control_status=ctrl,
|
||||
)
|
||||
|
||||
session_store.set_value(session_id, request.field_id, request.value)
|
||||
return base_result
|
||||
|
||||
|
||||
@router.post("/undo")
|
||||
async def undo_last_reading(
|
||||
field_id: str,
|
||||
experiment: str = Query(default="cn.edu.muc.lxy.phyexp.voicespeed"),
|
||||
x_session_id: str | None = Header(default=None),
|
||||
):
|
||||
session_id = resolve_session_id(x_session_id)
|
||||
field = find_field(experiment, field_id)
|
||||
result = reading_guard_manager.undo(session_id, field, list_fields(experiment))
|
||||
if result is None:
|
||||
raise HTTPException(status_code=400, detail="no reading guard active for this session")
|
||||
|
||||
ok, msg = result
|
||||
if ok:
|
||||
session_store.delete_value(session_id, field_id)
|
||||
|
||||
pending = reading_guard_manager.get_pending_field(session_id)
|
||||
cursor_now, cursor_total = reading_guard_manager.get_cursor(session_id)
|
||||
return {
|
||||
"success": ok,
|
||||
"message": msg,
|
||||
"pending_field": pending[0] if pending else None,
|
||||
"cursor": cursor_now,
|
||||
"total": cursor_total,
|
||||
"session_id": session_id,
|
||||
}
|
||||
|
||||
|
||||
@router.get("")
|
||||
async def get_all_field_values(x_session_id: str | None = Header(default=None)):
|
||||
session_id = resolve_session_id(x_session_id)
|
||||
fields = session_store.get_all(session_id)
|
||||
return {"fields": fields, "count": len(fields), "session_id": session_id}
|
||||
pending = reading_guard_manager.get_pending_field(session_id)
|
||||
cursor_now, cursor_total = reading_guard_manager.get_cursor(session_id)
|
||||
return {
|
||||
"fields": fields,
|
||||
"count": len(fields),
|
||||
"session_id": session_id,
|
||||
"pending_field": pending[0] if pending else None,
|
||||
"cursor": cursor_now,
|
||||
"total": cursor_total,
|
||||
}
|
||||
|
||||
|
||||
@router.get("/{field_id}")
|
||||
|
||||
在新工单中引用
屏蔽一个用户