DJ Brake page without login; routing level text follows the slider live

Optional page /djbrake (off by default) with a red light while a limiter works.
It needs no PIN and only ever gets {on, ready} from /djbrake/state.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
GivouandClaude Opus 5.5 committed 2026-11-02 18:38:36 +01:00
1 parent 3ce0334aea
commit e56524a8cd
7 files changed
+179 -6

No files matched your search

+49 -3
View File
@@ -212,6 +212,7 @@ def rew_send_curve(body: dict):
"magnitude": rew_floats(mag), "phase": rew_floats(phase)})
DJ_HOLD = 0.8 # s the light of /djbrake stays on after a limiter acted, so short hits are seen
DASH_TYPES = ("status", "meters", "mixer", "curve", "channel", "limiter", "routing", "delays", "fir", "notes")
@@ -292,6 +293,9 @@ class App:
self.limiter: dict = clean_limiter({})
self.soundfield: dict = clean_soundfield({})
self.dashboard: dict | None = None
self.djbrake = False # page /djbrake for the DJ: without PIN, shows only whether a limiter acts
self.dj_hit = 0.0 # when a limiter was last seen acting
self.dj_streams = 0
self.logo = ""
# desktop app: its own window logs in with a one-time token, other devices need the PIN
self.app_token: str | None = None
@@ -340,6 +344,7 @@ class App:
self.soundfield = clean_soundfield(cfg.get("soundfield"))
if "dashboard" in cfg:
self.dashboard = clean_dashboard(cfg.get("dashboard"))
self.djbrake = bool(cfg.get("djbrake"))
if "logo" in cfg:
self.logo = clean_logo(cfg.get("logo"))
r = cfg.get("remote") or {}
@@ -356,7 +361,7 @@ class App:
return {"format": CONFIG_FORMAT, "version": 1, "exported": time.strftime("%Y-%m-%dT%H:%M:%S"),
"devices": [d.config() for d in self.real_devices()], "auth": self.auth,
"limiter": self.limiter, "remote": self.remote, "soundfield": self.soundfield, "logo": self.logo,
"dashboard": self.dashboard}
"dashboard": self.dashboard, "djbrake": self.djbrake}
async def restore_config(self, doc: dict, session: str):
if not isinstance(doc, dict) or doc.get("format") != CONFIG_FORMAT:
@@ -414,7 +419,7 @@ class App:
sessions = {k: v for k, v in self.sessions.items() if k not in self.app_sessions}
cfg = {"devices": [d.config() for d in self.real_devices()], "auth": self.auth,
"sessions": sessions, "limiter": self.limiter, "remote": self.remote, "locked": self.locked, "lock_dash": self.lock_dash,
"soundfield": self.soundfield, "logo": self.logo, "dashboard": self.dashboard,
"soundfield": self.soundfield, "logo": self.logo, "dashboard": self.dashboard, "djbrake": self.djbrake,
"mute_all": {"active": self.mute_all["active"],
"restore": {k: v for k, v in self.mute_all["restore"].items() if k != "demo"}}}
if self.lang:
@@ -871,6 +876,8 @@ class App:
# ------------------------------------------------------------ events
def broadcast(self, ev: dict):
if ev.get("type") == "meters" and any(ev.get("limit") or ()):
self.dj_hit = time.monotonic()
msg = json.dumps(ev, separators=(",", ":"))
for q in list(self.clients):
if ev["type"] == "meters" and q.qsize() > 50:
@@ -893,6 +900,11 @@ class App:
return {"logo": self.logo}
if path == "/api/logo":
return {"logo": self.logo}
if path == "/api/djbrake" and method == "PUT":
self.djbrake = bool(body.get("enabled"))
self.save()
self.broadcast({"type": "djbrake", "enabled": self.djbrake})
return {"enabled": self.djbrake}
if path == "/api/dashboard" and method == "PUT":
self.dashboard = clean_dashboard(body)
self.save()
@@ -1096,6 +1108,11 @@ class App:
else:
await self.respond(writer, 401, b"invalid app token", "text/plain")
return
if path == "/djbrake/state":
await self.dj_stream(writer)
return
if path in ("/djbrake", "/djbrake/"):
path = "/djbrake.html"
if path.startswith("/api/"):
try:
data = json.loads(body) if body else {}
@@ -1216,6 +1233,9 @@ class App:
async def static(self, writer, path: str):
rel = "index.html" if path in ("/", "") else path.lstrip("/")
if rel == "djbrake.html" and not self.djbrake:
await self.respond(writer, 404, b"not found", "text/plain")
return
f = (STATIC / rel).resolve()
if STATIC not in f.parents or not f.is_file():
await self.respond(writer, 404, b"not found", "text/plain")
@@ -1225,6 +1245,32 @@ class App:
ctype += "; charset=utf-8"
await self.respond(writer, 200, f.read_bytes(), ctype)
async def dj_stream(self, writer):
"""The one thing /djbrake may know, without a login: is a limiter acting. Nothing else leaves here."""
if not self.djbrake or self.dj_streams >= 20:
await self.respond(writer, 404, b"not found", "text/plain")
return
self.dj_streams += 1
writer.write(b"HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nCache-Control: no-cache\r\n"
b"Connection: keep-alive\r\nX-Accel-Buffering: no\r\n\r\n")
last, idle = None, 0.0
try:
while self.djbrake:
now = time.monotonic()
state = {"on": now - self.dj_hit < DJ_HOLD,
"ready": any(d.status == "online" for d in self.devices.values())}
if state != last:
writer.write(f"data: {json.dumps(state)}\n\n".encode())
last, idle = state, 0.0
elif idle > 15:
writer.write(b": ping\n\n")
idle = 0.0
await writer.drain()
await asyncio.sleep(0.1)
idle += 0.1
finally:
self.dj_streams -= 1
async def sse(self, writer):
writer.write(b"HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nCache-Control: no-cache\r\n"
b"Connection: keep-alive\r\nX-Accel-Buffering: no\r\n\r\n")
@@ -1233,7 +1279,7 @@ class App:
try:
hello = {"type": "hello", "devices": [d.snapshot() for d in self.devices.values()], "limiter": self.limiter,
"demo": self.demo_info(), "muteall": self.mute_all_info(), "soundfield": self.soundfield, "logo": self.logo,
"dashboard": self.dashboard}
"dashboard": self.dashboard, "djbrake": self.djbrake}
writer.write(f"data: {json.dumps(hello)}\n\n".encode())
await writer.drain()
while True: