Pick the simulator's stage map from a drop-down, and make the QLC+ show optional.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
df05c8ca49
commit
a295fefac0
8 files changed
+154
-20
No files matched your search
+43
-2
@@ -9,7 +9,7 @@ from urllib.parse import urlparse
|
||||
|
||||
from simulator.artnet import ArtNetNode
|
||||
from simulator.decode import view_stage
|
||||
from simulator.stage import Stage, set_heading
|
||||
from simulator.stage import Stage, apply_show, load_stage, set_heading
|
||||
|
||||
STATIC = Path(__file__).resolve().parent / "static"
|
||||
|
||||
@@ -21,15 +21,37 @@ CONTENT_TYPES = {
|
||||
|
||||
|
||||
class Simulator:
|
||||
def __init__(self, stage: Stage, node: ArtNetNode):
|
||||
def __init__(self, stage: Stage, node: ArtNetNode, show: Path | None = None):
|
||||
self.stage = stage
|
||||
self.node = node
|
||||
self.show = show
|
||||
|
||||
def state(self) -> dict:
|
||||
stats = self.node.stats()
|
||||
stats["fixtures"] = view_stage(self.stage, self.node.snapshot())
|
||||
return stats
|
||||
|
||||
def stage_dir(self) -> Path | None:
|
||||
return self.stage.path.parent if self.stage.path else None
|
||||
|
||||
def stages(self) -> dict:
|
||||
"""The stage maps next to the current one, for the page's drop-down."""
|
||||
folder = self.stage_dir()
|
||||
files = sorted(path.name for path in folder.glob("*.json")) if folder else []
|
||||
current = self.stage.path.name if self.stage.path else None
|
||||
return {"current": current, "stages": files}
|
||||
|
||||
def switch(self, name: str) -> Stage:
|
||||
"""Load another stage map from the same folder, with the same show."""
|
||||
folder = self.stage_dir()
|
||||
if folder is None or name not in self.stages()["stages"]:
|
||||
raise KeyError(name)
|
||||
stage = load_stage(folder / name)
|
||||
if self.show is not None:
|
||||
apply_show(stage, self.show)
|
||||
self.stage = stage
|
||||
return stage
|
||||
|
||||
|
||||
def make_handler(sim: Simulator):
|
||||
class Handler(BaseHTTPRequestHandler):
|
||||
@@ -40,6 +62,9 @@ def make_handler(sim: Simulator):
|
||||
|
||||
def do_POST(self) -> None:
|
||||
path = urlparse(self.path).path
|
||||
if path == "/api/stage":
|
||||
self._switch_stage()
|
||||
return
|
||||
if path != "/api/heading":
|
||||
self.send_error(404)
|
||||
return
|
||||
@@ -68,6 +93,9 @@ def make_handler(sim: Simulator):
|
||||
if path == "/api/stage":
|
||||
self._json(self.simulator.stage.to_json())
|
||||
return
|
||||
if path == "/api/stages":
|
||||
self._json(self.simulator.stages())
|
||||
return
|
||||
if path in ("/", "/index.html"):
|
||||
self._file(STATIC / "index.html")
|
||||
return
|
||||
@@ -78,6 +106,19 @@ def make_handler(sim: Simulator):
|
||||
return
|
||||
self.send_error(404)
|
||||
|
||||
def _switch_stage(self) -> None:
|
||||
length = int(self.headers.get("Content-Length") or 0)
|
||||
if length <= 0 or length > 4096:
|
||||
self._json({"error": "bad stage"}, 400)
|
||||
return
|
||||
try:
|
||||
name = str(json.loads(self.rfile.read(length))["file"])
|
||||
stage = self.simulator.switch(name)
|
||||
except (KeyError, TypeError, ValueError, json.JSONDecodeError):
|
||||
self._json({"error": "bad stage"}, 400)
|
||||
return
|
||||
self._json(stage.to_json())
|
||||
|
||||
def _json(self, payload: dict, status: int = 200) -> None:
|
||||
body = json.dumps(payload).encode()
|
||||
self.send_response(status)
|
||||
|
||||
Reference in new issue
Block a user