From c88f0b15425b217ac7f732b09766e057c80fa94c Mon Sep 17 00:00:00 2001 From: Cutshion Date: Tue, 17 Nov 2026 16:41:06 +0900 Subject: [PATCH] Added line editing Co-Authored-By: Claude Opus 5.5 --- api/server.py | 44 ++++++++++++++++++++++++++++++++++++++-- tests/test_map_info.py | 6 ++++-- tests/test_map_lines.py | 45 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 tests/test_map_lines.py diff --git a/api/server.py b/api/server.py index bc0a149..fa38288 100644 --- a/api/server.py +++ b/api/server.py @@ -757,6 +757,12 @@ class MarkerName(BaseModel): name: str +class NoGoLineBody(BaseModel): + name: str = Field(examples=["door_1"], description="letters, digits, _ and - (an existing name is replaced)") + points: list[list[float]] = Field(min_length=2, examples=[[[0.0, 1.0], [2.0, 1.0]]], + description="[x, y] in m, map frame, in order") + + class OnBody(BaseModel): on: bool @@ -784,16 +790,50 @@ def jog_stop(): def _forbidden_lines(floor): """The map's no-go lines drawn in the base's web page (forbidden_manager): [{name, points [[x, y], ...]}] on - this floor, in map metres. Empty when the base does not answer: the map still shows.""" + this floor, in map metres, the newest first. Empty when the base does not answer: the map still shows.""" try: values = rosbridge.call_service("/forbidden_manager/action_service", {"cmd": "list_lines", "message": ""}) lines = json.loads(values.get("message") or "{}") if values.get("success") else {} except Exception: return [] - return [{"name": n, "points": l["points"]} for n, l in sorted(lines.items()) + newest = sorted(lines.items(), key=lambda nl: (nl[1].get("date") or "", nl[0]), reverse=True) # the base dates each + return [{"name": n, "points": l["points"]} for n, l in newest if l.get("floor", 1) == floor and len(l.get("points") or []) >= 2] +FORBIDDEN = "/forbidden_manager/action_service" # the base's no-go lines (its web page :9001 uses it) + + +def _lines_call(cmd, message): + """One forbidden_manager command; raises 502 when the base refuses or does not answer.""" + try: + v = rosbridge.call_service(FORBIDDEN, {"cmd": cmd, "message": json.dumps(message)}) + except Exception as e: + raise HTTPException(502, f"the base did not answer: {e}") + if not v.get("success"): + raise HTTPException(502, f"the base refused: {v.get('message')}") + return v + + +@app.post("/map/line", tags=["map"], summary="Save a no-go line on this floor (the base never drives across it)") +def line_save(body: NoGoLineBody): + name = _marker_name(body.name) + if any(len(pt) != 2 for pt in body.points): + raise HTTPException(422, "Each point is [x, y].") + floor = agv.request("/api/map/get_current_map")["results"]["floor"] + line = {"name": name, "type": "forbidden", "floor": floor, "points": [[round(x, 3), round(y, 3)] for x, y in body.points], + "comment": "", "properties": "{}"} + _lines_call("add_lines", {"data": [line]}) + return {"ok": True, "line": line} + + +@app.post("/map/line/delete", tags=["map"], summary="Delete a no-go line") +def line_delete(body: MarkerName): + name = _marker_name(body.name) + _lines_call("delete_lines", {"names": [name]}) + return {"ok": True} + + @app.get("/map/info", tags=["map"], summary="Current map, its geometry, the markers with poses and all maps") def map_info(): cur = agv.request("/api/map/get_current_map")["results"] diff --git a/tests/test_map_info.py b/tests/test_map_info.py index ff33029..21b6ea0 100644 --- a/tests/test_map_info.py +++ b/tests/test_map_info.py @@ -23,7 +23,8 @@ def fake(cmd, timeout=3.0): raise AssertionError(cmd) -LINES = {"door": {"floor": 1, "points": [[0, 1], [2, 1]], "type": "forbidden"}, +LINES = {"door": {"floor": 1, "points": [[0, 1], [2, 1]], "type": "forbidden", "date": "2026-10-08 16:35:09"}, + "new": {"floor": 1, "points": [[5, 5], [6, 5]], "date": "2026-11-17 15:00:00"}, "up": {"floor": 2, "points": [[0, 0], [1, 0]]}, "dot": {"floor": 1, "points": [[3, 3]]}} @@ -36,7 +37,8 @@ agv.request = server.agv.request = fake server.rosbridge.call_service = fake_service names = lambda d: [m["name"] for m in d["markers"]] assert names(server.map_info()) == ["home_dock"] -assert server.map_info()["lines"] == [{"name": "door", "points": [[0, 1], [2, 1]]}] # this floor, two points or more +assert server.map_info()["lines"] == [{"name": "new", "points": [[5, 5], [6, 5]]}, # newest first + {"name": "door", "points": [[0, 1], [2, 1]]}] # this floor, 2 points or more assert server.markers()["markers"] == ["home"] and agv.known_markers() == ["home"] MARKS["p13"] = {"key": 0, "floor": 1, "pose": {"position": {"x": 1.0, "y": 2.0}, "orientation": {"z": 0, "w": 1}}} diff --git a/tests/test_map_lines.py b/tests/test_map_lines.py new file mode 100644 index 0000000..b76e67f --- /dev/null +++ b/tests/test_map_lines.py @@ -0,0 +1,45 @@ +"""Self-check of the no-go lines the Lumi app draws (2026-11-17): save one on this floor, delete one, refuse bad input, +and a base that refuses. Fake base and rosbridge, no robot: python tests/test_map_lines.py""" +import json + +import no_robot # noqa: F401 any connection to the robot fails the test +from fastapi import HTTPException + +from api import server + +calls = [] +reply = {"success": True, "message": ""} +server.agv.request = lambda cmd, timeout=3.0: {"results": {"map_name": "office", "floor": 2}} +server.rosbridge.call_service = lambda service, args, timeout=3.0: (calls.append((service, args["cmd"], + json.loads(args["message"]))), reply)[1] + + +def refused(fn, *a, code): + try: + fn(*a) + except HTTPException as e: + return e.status_code == code + return False + + +r = server.line_save(server.NoGoLineBody(name="door_1", points=[[0, 1.23456], [2, 1]])) +assert calls == [("/forbidden_manager/action_service", "add_lines", {"data": [ + {"name": "door_1", "type": "forbidden", "floor": 2, "points": [[0, 1.235], [2, 1]], "comment": "", + "properties": "{}"}]})], calls +calls.clear() +server.line_delete(server.MarkerName(name="door_1")) +assert calls == [("/forbidden_manager/action_service", "delete_lines", {"names": ["door_1"]})], calls +calls.clear() + +assert refused(server.line_save, server.NoGoLineBody(name="a b", points=[[0, 0], [1, 1]]), code=422) +assert refused(server.line_save, server.NoGoLineBody(name="x", points=[[0, 0], [1]]), code=422) +try: + server.NoGoLineBody(name="x", points=[[0, 0]]) # one point is not a line + raise AssertionError("one point accepted") +except ValueError: + pass +assert calls == [], calls + +reply = {"success": False, "message": "busy"} +assert refused(server.line_save, server.NoGoLineBody(name="x", points=[[0, 0], [1, 1]]), code=502) +print("ok: map lines")