Update map picture and order

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 18:33:14 +09:00
co-authored by Claude Opus 5.5
parent 0a2d25e1d3
commit e740ea6970
3 changed files with 124 additions and 17 deletions
+32 -14
View File
@@ -4,6 +4,11 @@ import no_robot # noqa: F401 any connection
from fastapi import HTTPException
from api import server
import tempfile, pathlib
server.MARKER_TIMES = pathlib.Path(tempfile.mkdtemp()) / "marker_times.json" # not the real file
server._map_dates = lambda: {}
dated = []
server._mark_time = lambda name, when: dated.append((name, when is not None)) # the date book is test_map_info's
sent = []
server.agv.request = lambda cmd, timeout=3.0: sent.append(cmd) or {"status": "OK", "results": {}}
@@ -23,6 +28,7 @@ def refused(fn, *a, code=409):
# where Lumi stands, and at a tapped pose (an existing name is moved there)
server.marker_save(server.MarkerBody(name="door_1"))
assert sent == ["/api/markers/insert?name=door_1&type=0&num=1", "refresh"], sent
assert dated == [("door_1", True)], dated # dated, so the app lists it first
sent.clear()
server.marker_save(server.MarkerBody(name="p11", x=1.5, y=-2.25, theta=0.5))
assert sent == ["/api/markers/insert_by_pose?name=p11&x=1.5&y=-2.25&theta=0.5&type=0", "refresh"], sent
@@ -58,23 +64,35 @@ assert refused(server.map_position, server.PointBody(x=0, y=0))
assert sent == []
# the map picture in the app's dark look: unknown almost black, free space dark, walls light (same size)
# the map picture in the app's dark look: unknown almost black, free space dark, walls light (same size). The base's
# clean ui_map.png (three colours, BGR) when it has one; else its raw grid map.png (205 unknown, 255 free, 0 walls)
import cv2 # noqa: E402
import numpy as np # noqa: E402
pic = np.full((4, 6), 205, np.uint8) # unknown
pic[1, 1:5] = 255 # free
pic[2, 2] = 0 # a wall
png = cv2.imencode(".png", pic)[1].tobytes()
ui = np.full((4, 6, 3), 250, np.uint8) # unknown
ui[1, 1:5] = (253, 241, 234) # free
ui[2, 2] = (194, 177, 161) # outline
grid = np.full((4, 6), 205, np.uint8)
grid[1, 1:5] = 255
grid[2, 2] = 0
pngs = {"ui_map.png": cv2.imencode(".png", ui)[1].tobytes(), "map.png": cv2.imencode(".png", grid)[1].tobytes()}
def fake_open(url, timeout=10):
name = url.rsplit("/", 1)[1]
if name not in pngs:
raise OSError("404")
return type("R", (), {"read": lambda s: pngs[name], "__enter__": lambda s: s, "__exit__": lambda s, *a: False})()
server.agv.request = lambda cmd, timeout=3.0: {"results": {"map_name": "m", "floor": 1}}
server.urllib.request.urlopen = lambda url, timeout=10: type("R", (), {"read": lambda s: png, "__enter__": lambda s: s,
"__exit__": lambda s, *a: False})()
out = cv2.imdecode(np.frombuffer(server.map_image(dark=True).body, np.uint8), cv2.IMREAD_COLOR)
assert out.shape == (4, 6, 3), out.shape
assert tuple(out[0, 0]) == server.DARK_UNKNOWN[::-1] and tuple(out[1, 1]) == server.DARK_FREE[::-1], (out[0, 0], out[1, 1])
assert tuple(out[2, 2]) == server.DARK_WALL[::-1], out[2, 2]
assert server.map_image().body == png # as the base draws it, unless asked
server.urllib.request.urlopen = fake_open
for _ in range(2): # with ui_map.png, then without it
out = cv2.imdecode(np.frombuffer(server.map_image(dark=True).body, np.uint8), cv2.IMREAD_COLOR)
assert out.shape == (4, 6, 3), out.shape
assert tuple(out[0, 0]) == server.DARK_UNKNOWN[::-1] and tuple(out[1, 1]) == server.DARK_FREE[::-1], (out[0, 0], out[1, 1])
assert tuple(out[2, 2]) == server.DARK_WALL[::-1], out[2, 2]
pngs.pop("ui_map.png", None)
assert server.map_image().body == pngs["map.png"] # as the base draws it, unless asked
# Lumi stands at a marker: the base takes its pose from it (after a map switch, the quickest way)
sent.clear()
server.agv.request = lambda cmd, timeout=3.0: sent.append(cmd) or {"status": "OK", "results": {}}
+13
View File
@@ -6,6 +6,9 @@ import time
import no_robot # noqa: F401 any connection to the robot fails the test
from api import server
import tempfile, pathlib
server.MARKER_TIMES = pathlib.Path(tempfile.mkdtemp()) / "marker_times.json" # not the real file
server._map_dates = lambda: {}
from robot import agv
MARKS = {"home_dock": {"key": 11, "num": 1, "floor": 1, "pose": {"position": {"x": 0, "y": 0},
@@ -48,4 +51,14 @@ assert agv.known_markers(fresh=True) == ["home", "p13"] # a name not
agv._known["t"] = time.time() - agv.MARKERS_FRESH_S - 1
del MARKS["p13"]
assert agv.known_markers() == ["home"] # and within MARKERS_FRESH_S anyway
# newest first: markers by when this API saved them (undated ones after, in the base's order), maps by folder date
server.MARKER_TIMES.write_text(json.dumps({"office/1/p13": 200, "office/1/z9": 100}))
MARKS.update({n: {"key": 0, "floor": 1, "pose": {"position": {"x": 0, "y": 0}, "orientation": {"z": 0, "w": 1}}}
for n in ("p13", "z9", "a1")})
assert names(server.map_info()) == ["p13", "z9", "home_dock", "a1"], names(server.map_info())
server._map_dates = lambda: {"old": "2026-01-01 00:00", "office": "2026-11-17 09:00"}
real_request = agv.request
agv.request = server.agv.request = lambda cmd, timeout=3.0: ({"results": {"office": [1], "old": [1], "nodate": [1]}}
if cmd == "/api/map/list" else real_request(cmd, timeout))
assert [m["name"] for m in server.map_info()["maps"]] == ["office", "old", "nodate"], server.map_info()["maps"]
print("map info ok")