Add device lock support and demo mode
Device lock (hardware lock of the DSP, independent of the app lock): - detect a locked DSP from the 0x2C answer during connect and wait for the code - unlock with 0x2D, optionally remember the code and unlock automatically - set a new code (0x2F) from the device settings - simulator: --lock CODE Demo mode: - switch in the settings starts a simulated DSP 204/408 inside the server - real DSPs are disconnected and greyed out meanwhile and resume afterwards - demo devices are never saved or exported; sim.py ships with the Pi image and EXEs Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fe13f261f7
commit
61400df667
13 files changed
+426
-38
No files matched your search
+67
-7
@@ -20,7 +20,8 @@ LINK_RESYNC_DELAY = 1.5 # re-read the config this long after the last change on
|
||||
|
||||
class Device:
|
||||
def __init__(self, dev_id: str, name: str, host: str, port: int = P.PORT, model: str = "auto",
|
||||
addr: int = 1, autoconnect: bool = True, on_event=None, transport: str = "tcp", usb: str = ""):
|
||||
addr: int = 1, autoconnect: bool = True, on_event=None, transport: str = "tcp", usb: str = "",
|
||||
lock_code: str = "", auto_unlock: bool = False):
|
||||
self.id = dev_id
|
||||
self.name = name
|
||||
self.host = host
|
||||
@@ -31,8 +32,12 @@ class Device:
|
||||
self.transport = "usb" if transport == "usb" else "tcp"
|
||||
self.usb = usb # HID device path, "" = first DSP found
|
||||
self.on_event = on_event or (lambda ev: None)
|
||||
# hardware lock of the DSP itself (not the app lock): remembered code for automatic unlocking
|
||||
self.lock_code = lock_code if P.LOCK_CODE.fullmatch(lock_code or "") else ""
|
||||
self.auto_unlock = auto_unlock
|
||||
self.demo = False # in-process simulator of the demo mode, never saved
|
||||
|
||||
self.status = "disconnected" # disconnected | connecting | loading | online | error
|
||||
self.status = "disconnected" # disconnected | connecting | loading | locked | online | error
|
||||
self.error = ""
|
||||
self.info = ""
|
||||
self.model: P.Model | None = P.MODELS.get(model)
|
||||
@@ -53,16 +58,20 @@ class Device:
|
||||
self._want_connected = False
|
||||
self._resync: asyncio.TimerHandle | None = None
|
||||
self._changes = 0 # bumped on every local change, so a resync never overwrites newer edits
|
||||
self._codes: asyncio.Queue | None = None # (code, future) entered while the DSP is locked
|
||||
|
||||
# ------------------------------------------------------------ public API
|
||||
|
||||
def config(self) -> dict:
|
||||
return {"id": self.id, "name": self.name, "host": self.host, "port": self.port,
|
||||
"model": self.model_key, "addr": self.addr, "autoconnect": self.autoconnect,
|
||||
"transport": self.transport, "usb": self.usb}
|
||||
"transport": self.transport, "usb": self.usb,
|
||||
"lock_code": self.lock_code, "auto_unlock": self.auto_unlock}
|
||||
|
||||
def snapshot(self) -> dict:
|
||||
return {**self.config(), "status": self.status, "error": self.error, "info": self.info,
|
||||
cfg = self.config()
|
||||
cfg["lock_code"] = bool(self.lock_code) # clients only learn whether a code is remembered
|
||||
return {**cfg, "demo": self.demo, "status": self.status, "error": self.error, "info": self.info,
|
||||
"detected_model": self.model_name or None,
|
||||
"n_in": self.model.n_in if self.model else 0,
|
||||
"n_out": self.model.n_out if self.model else 0,
|
||||
@@ -75,8 +84,6 @@ class Device:
|
||||
|
||||
async def stop(self):
|
||||
self._want_connected = False
|
||||
self._resync: asyncio.TimerHandle | None = None
|
||||
self._changes = 0 # bumped on every local change, so a resync never overwrites newer edits
|
||||
if self._supervisor:
|
||||
self._supervisor.cancel()
|
||||
try:
|
||||
@@ -121,6 +128,29 @@ class Device:
|
||||
await asyncio.sleep(0.3)
|
||||
await self.refresh()
|
||||
|
||||
async def unlock(self, code: str) -> bool:
|
||||
"""Try a lock code while the DSP waits for it. True if the DSP accepted it."""
|
||||
code = str(code or "").strip().upper()
|
||||
if not P.LOCK_CODE.fullmatch(code):
|
||||
raise ValueError("The code has 4 characters: digits and letters A–Z")
|
||||
if self.status != "locked" or not self._codes:
|
||||
raise RuntimeError("The DSP is not locked")
|
||||
fut = asyncio.get_running_loop().create_future()
|
||||
await self._codes.put((code, fut))
|
||||
return await fut
|
||||
|
||||
async def set_lock_code(self, code: str):
|
||||
"""Store a new lock code in the DSP; it asks for it from the next connection on."""
|
||||
code = str(code or "").strip().upper()
|
||||
if not P.LOCK_CODE.fullmatch(code):
|
||||
raise ValueError("The code has 4 characters: digits and letters A–Z")
|
||||
if self.status != "online":
|
||||
raise RuntimeError("DSP is not connected")
|
||||
if self.lock_code: # keep auto-unlock working with the new code
|
||||
self.lock_code = code
|
||||
await self._request(P.CMD_SET_LOCK_CODE, code.encode("ascii"))
|
||||
await self._request(P.CMD_LOGOUT) # as the original software does after setting the code
|
||||
|
||||
def _schedule_resync(self):
|
||||
"""Re-read the config shortly after edits on linked channels, so the app shows exactly
|
||||
what the DSP applied to the group (e.g. mute, which we don't mirror for certain)."""
|
||||
@@ -293,8 +323,11 @@ class Device:
|
||||
if detected and detected != self.model_key:
|
||||
log.warning("%s: configured as %s but device reports %s", self.name, self.model_key, self.info)
|
||||
count_frame = await self._request(P.CMD_PRESET_COUNT, expect=P.CMD_PRESET_COUNT)
|
||||
count = count_frame.payload[-1] if count_frame and count_frame.payload else 20
|
||||
payload = count_frame.payload if count_frame else b""
|
||||
count = payload[-1] if 0 < len(payload) < 6 else 20 # the long answer carries the lock state instead
|
||||
count = count if 0 < count <= 60 else 20
|
||||
if P.device_locked(payload):
|
||||
await self._wait_for_unlock()
|
||||
await self._request(P.CMD_INIT_22)
|
||||
await self._request(P.CMD_INIT_14)
|
||||
presets = []
|
||||
@@ -304,6 +337,33 @@ class Device:
|
||||
self.presets = presets
|
||||
await self._load_config()
|
||||
|
||||
async def _try_code(self, code: str) -> bool:
|
||||
f = await self._request(P.CMD_UNLOCK, b"\x00" + code.encode("ascii"), timeout=2.0, expect=P.CMD_UNLOCK)
|
||||
return bool(f and len(f.payload) > 1 and f.payload[1] == 1)
|
||||
|
||||
async def _wait_for_unlock(self):
|
||||
"""The DSP is locked with its code: unlock automatically or wait until a code is entered."""
|
||||
if self.auto_unlock and self.lock_code:
|
||||
if await self._try_code(self.lock_code):
|
||||
log.info("%s: unlocked automatically", self.name)
|
||||
return
|
||||
log.warning("%s: remembered lock code was rejected", self.name)
|
||||
self.error = "The remembered lock code was rejected"
|
||||
self._codes = asyncio.Queue()
|
||||
self._set_status("locked")
|
||||
try:
|
||||
while True:
|
||||
code, fut = await self._codes.get()
|
||||
ok = await self._try_code(code)
|
||||
if not fut.done():
|
||||
fut.set_result(ok)
|
||||
if ok:
|
||||
self.error = ""
|
||||
self._set_status("loading")
|
||||
return
|
||||
finally:
|
||||
self._codes = None
|
||||
|
||||
async def _load_config(self):
|
||||
self.state = await self._read_config()
|
||||
|
||||
|
||||
Reference in new issue
Block a user