feat(right_arm): endpoint /snapshot/{front,wrist}
This commit is contained in:
1 parent
dd2053cc47
commit
22e13435b1
2 files changed
+52
No files matched your search
@@ -4,6 +4,7 @@ from __future__ import annotations
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from fastapi.responses import Response
|
||||
|
||||
from ..clients import G1Unavailable
|
||||
from ..config import update_config
|
||||
@@ -54,3 +55,21 @@ def post_activate() -> dict[str, Any]:
|
||||
return _state_payload(sw)
|
||||
except G1Unavailable as e:
|
||||
raise HTTPException(status_code=502, detail=str(e))
|
||||
|
||||
|
||||
@router.get("/snapshot/front")
|
||||
def snapshot_front() -> Response:
|
||||
try:
|
||||
jpeg = _deps.front_camera().rgb_jpeg()
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=502, detail=f"front_camera unreachable: {e}")
|
||||
return Response(content=jpeg, media_type="image/jpeg")
|
||||
|
||||
|
||||
@router.get("/snapshot/wrist")
|
||||
def snapshot_wrist() -> Response:
|
||||
try:
|
||||
jpeg = _deps.nuwa().rgb_jpeg()
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=502, detail=f"wrist nuwa unreachable: {e}")
|
||||
return Response(content=jpeg, media_type="image/jpeg")
|
||||
@@ -60,3 +60,36 @@ def test_activate_sets_robot_active_g1_and_side_right():
|
||||
body = r.json()
|
||||
assert body["robot_active"] == "g1"
|
||||
assert body["side"] == "right"
|
||||
|
||||
|
||||
def test_snapshot_front_returns_jpeg():
|
||||
fake_jpeg = b"\xff\xd8\xff" + b"\x00" * 100 # magic JPEG bytes
|
||||
with patch("presa_mano.api.right_arm_routes._deps.front_camera") as factory:
|
||||
cam = MagicMock()
|
||||
cam.rgb_jpeg.return_value = fake_jpeg
|
||||
factory.return_value = cam
|
||||
r = client.get("/api/right_arm/snapshot/front")
|
||||
assert r.status_code == 200
|
||||
assert r.headers["content-type"].startswith("image/jpeg")
|
||||
assert r.content == fake_jpeg
|
||||
|
||||
|
||||
def test_snapshot_wrist_returns_jpeg():
|
||||
fake_jpeg = b"\xff\xd8\xff" + b"\x00" * 50
|
||||
with patch("presa_mano.api.right_arm_routes._deps.nuwa") as factory:
|
||||
cam = MagicMock()
|
||||
cam.rgb_jpeg.return_value = fake_jpeg
|
||||
factory.return_value = cam
|
||||
r = client.get("/api/right_arm/snapshot/wrist")
|
||||
assert r.status_code == 200
|
||||
assert r.headers["content-type"].startswith("image/jpeg")
|
||||
assert r.content == fake_jpeg
|
||||
|
||||
|
||||
def test_snapshot_front_502_on_failure():
|
||||
with patch("presa_mano.api.right_arm_routes._deps.front_camera") as factory:
|
||||
cam = MagicMock()
|
||||
cam.rgb_jpeg.side_effect = RuntimeError("connection refused")
|
||||
factory.return_value = cam
|
||||
r = client.get("/api/right_arm/snapshot/front")
|
||||
assert r.status_code == 502
|
||||
Reference in new issue
Block a user