feat: simulator bridge, rig seed and web UI (in progress)

Work in progress from the same tree as the controller's sequences-v2 changes.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Haiku 5.5 committed 2026-11-11 14:10:55 +13:00
1 parent 4fc3345fc9
commit 4d8aeb5df8
26 files changed
+3907 -288

No files matched your search

+85
View File
@@ -0,0 +1,85 @@
"""Give led-controller the simulator's rig layout once its strips have registered.
The layout is the simulator's (``layout`` in sim.json, see ``layout.rig_placements``). Only strips the
controller has no placement for are sent, so moving bars in the controller's rig-layout page sticks:
the simulator page then draws them where the controller put them (each driver's ``place``).
"""
from __future__ import annotations
import json
import threading
import time
import urllib.request
from layout import rig_placements
def _get(url: str, timeout: float = 3):
with urllib.request.urlopen(url, timeout=timeout) as r:
return json.loads(r.read())
def _post(url: str, body: dict, timeout: float = 15):
req = urllib.request.Request(
url, data=json.dumps(body).encode(), headers={"Content-Type": "application/json"}, method="POST"
)
with urllib.request.urlopen(req, timeout=timeout) as r:
return json.loads(r.read())
def controller_url(cfg: dict, peer_ip: str) -> str | None:
"""The controller's web API: ``controller.url`` in sim.json, else the bridge peer on 8080 or 80."""
url = (cfg.get("controller") or {}).get("url")
candidates = [url] if url else ["http://%s:8080" % peer_ip, "http://%s" % peer_ip]
for base in candidates:
try:
_get(base.rstrip("/") + "/status", timeout=2)
return base.rstrip("/")
except (OSError, ValueError):
continue
return None
def seed(cfg: dict, macs: dict[int, str], peer_ip: str, log=print, wait_s: float = 180) -> dict | None:
"""Wait for the strips to register, then send placements for the unplaced ones. Returns the reply."""
if (cfg.get("controller") or {}).get("seed_layout", True) is False:
return None
base = controller_url(cfg, peer_ip)
if base is None:
log("[layout] controller web API not found at %s (set controller.url in sim.json)" % peer_ip)
return None
want = {i: m.replace(":", "").lower() for i, m in macs.items()}
end = time.monotonic() + wait_s
devs = {}
while time.monotonic() < end:
try:
devs = _get(base + "/devices")
except (OSError, ValueError):
devs = {}
if all(m in devs for m in want.values()):
break
time.sleep(1)
places, center = rig_placements(cfg["layout"])
todo = {
m: places[i]
for i, m in want.items()
if m in devs and not isinstance(devs[m].get("placement"), dict)
}
if not todo:
log("[layout] controller already has every strip placed")
return None
body = {"placements": todo}
if not any(isinstance(d, dict) and isinstance(d.get("placement"), dict) for d in devs.values()):
body["center"] = center # a fresh rig: the simulator's centre too
try:
reply = _post(base + "/devices/layout", body)
except (OSError, ValueError) as e:
log("[layout] sending the rig layout failed: %s" % e)
return None
log("[layout] sent %d strip placements to %s: %s" % (len(todo), base, reply))
return reply
def seed_soon(cfg: dict, macs: dict[int, str], peer_ip: str, log=print) -> None:
threading.Thread(target=seed, args=(cfg, macs, peer_ip, log), name="rig-seed", daemon=True).start()