From 72f0f05096cea0695a6b65c8c911c43b89b791c0 Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Mon, 16 Nov 2026 17:54:32 +0100 Subject: fix(xbox): follow a power change quickly --- deckit/modules/xbox.py | 8 +++++++- static/modules/xbox.js | 20 ++++++++++++++++++-- tests/test_xbox.py | 7 +++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/deckit/modules/xbox.py b/deckit/modules/xbox.py index bd2fd6e..60f402e 100644 --- a/deckit/modules/xbox.py +++ b/deckit/modules/xbox.py @@ -72,6 +72,7 @@ COMMANDS = { FAST = 30 # seconds between presence and console reads SLOW = 600 # seconds between profile, games, achievements and friends reads +QUICK, QUICK_FOR = 3, 30 # after a command: read every 3s for 30s, until the power state follows # ponytail: renewed by age, not by the XSTS NotAfter (about 16h); a 401 renews early too. XSTS_FOR = 8 * 3600 TITLES = 12 # recent games shown @@ -245,6 +246,7 @@ class Xbox: self.status_lock = threading.Lock() self.slow_at, self.slow_title = None, None self.wake, self.stopping = threading.Event(), False + self.quick_until = 0.0 # read every QUICK seconds until then # --- the refresh token file ---------------------------------------- def _load(self): @@ -512,16 +514,20 @@ class Xbox: raise ModuleError("The console is not reachable from Xbox Live: turn it on by hand once, then retry.") if status.get("errorCode", "OK") != "OK": raise ModuleError(status.get("errorMessage") or status["errorCode"]) + self.quick_until = time.monotonic() + QUICK_FOR self.wake.set() # --- the reader thread ---------------------------------------------- + def next_read(self) -> float: + return QUICK if time.monotonic() < self.quick_until else FAST + def run(self): while not self.stopping: try: self.tick() except Exception as e: self.offline, self.reason = True, str(e) - self.wake.wait(FAST) + self.wake.wait(self.next_read()) self.wake.clear() def start(self): diff --git a/static/modules/xbox.js b/static/modules/xbox.js index b6b1c45..ddfb252 100644 --- a/static/modules/xbox.js +++ b/static/modules/xbox.js @@ -80,11 +80,22 @@ async function post(action, body) { if (!r.ok) throw new Error((await r.text()) || `${action} failed`); } +// Xbox Live reports a power change some seconds late: until it does (30s at +// most), the pill says what was asked. console id -> {label, from, until} +const pending = {}; +const PENDING = { wake: "Waking…", off: "Turning off…" }; + async function send(action, id, card) { const err = card.querySelector(".xb-err"); err.textContent = ""; try { await post(action, { console: id }); + if (PENDING[action]) { + const pill = card.querySelector(".xb-pill"); + pending[id] = { label: PENDING[action], from: pill.dataset.power, until: Date.now() + 30000 }; + pill.textContent = PENDING[action]; + pill.className = "xb-pill warn"; + } } catch (e) { err.textContent = e.message; } @@ -104,10 +115,15 @@ function consoleCard(c, apps) { const on = c.power === "On"; const card = h("div", "xb-card xb-console"); const head = h("div", "xb-chead"); - const [label, cls] = POWER[c.power] ?? [c.power ?? "?", ""]; + const p = pending[c.id]; + const waiting = p && p.from === c.power && Date.now() < p.until; + if (!waiting) delete pending[c.id]; + const [label, cls] = waiting ? [p.label, "warn"] : POWER[c.power] ?? [c.power ?? "?", ""]; const names = h("div", "xb-cnames"); const line = h("div", "xb-cline"); - line.append(h("span", "xb-cname", c.name), h("span", `xb-pill ${cls}`, label)); + const pill = h("span", `xb-pill ${cls}`, label); + pill.dataset.power = c.power ?? ""; + line.append(h("span", "xb-cname", c.name), pill); names.append(line, h("div", "xb-cmodel", [model(c.type), c.region].filter(Boolean).join(" · "))); head.append(h("i", "nf xb-glyph", G.xbox), names); // Tap wakes, hold turns off: a stray touch must not switch a game off. diff --git a/tests/test_xbox.py b/tests/test_xbox.py index d37423e..0bcc883 100644 --- a/tests/test_xbox.py +++ b/tests/test_xbox.py @@ -410,6 +410,13 @@ class Commands(Base): with self.assertRaises(xb.ModuleError): x.command("wake", {"console": "OTHER"}) + def test_a_command_reads_quickly_for_a_while(self): + x = self.xbox() + x.tick() + self.assertEqual(x.next_read(), xb.FAST) + x.command("off", {"console": "CON1"}) + self.assertEqual(x.next_read(), xb.QUICK) + def test_an_unreachable_console_says_what_to_do(self): self.fake.answers["/commands"] = {"status": {"errorCode": "ErrorCallingWNS", "errorMessage": "Send command failed"}} x = self.xbox() -- cgit v1.2.3