diff options
Diffstat (limited to 'deckit/modules/tablet.py')
| -rw-r--r-- | deckit/modules/tablet.py | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/deckit/modules/tablet.py b/deckit/modules/tablet.py new file mode 100644 index 0000000..3f4b0d9 --- /dev/null +++ b/deckit/modules/tablet.py @@ -0,0 +1,108 @@ +# deck-it - tablet module +# Copyright (C) 2026 Danilo M. <danix@danix.xyz> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +"""The tablet's own battery and Wi-Fi, read over adb. + +The tablet is the device whose adb reverse carries the dashboard socket, the +same test `deck-it status` uses, so a USB/Wi-Fi switch is followed. +""" + +import re +import subprocess +import threading +import time + +from ..module import Module + +STATUS = {"2": "charging", "3": "discharging", "4": "not charging", "5": "full"} +TTL = 10 # seconds; each read is two adb round trips + + +def _adb(*args, timeout=5): + return subprocess.run(["adb", *args], capture_output=True, text=True, + timeout=timeout, check=True).stdout + + +def _find_tablet(): + for line in _adb("devices").splitlines()[1:]: + f = line.split() + if len(f) == 2 and f[1] == "device": + try: + if "deck-it/http.sock" in _adb("-s", f[0], "reverse", "--list", timeout=3): + return f[0] + except (subprocess.SubprocessError, OSError): + continue + return None + + +def parse_battery(text): + kv = dict(re.findall(r"^\s*([\w ]+): (.+)$", text, re.M)) + plug = next((k.split()[0] for k in ("AC powered", "USB powered", "Wireless powered") + if kv.get(k) == "true"), None) + scale = int(kv.get("scale", 100)) or 100 + temp = kv.get("temperature") + return { + "level": round(100 * int(kv.get("level", 0)) / scale), + "status": STATUS.get(kv.get("status"), "unknown"), + "plug": plug, + "temp": int(temp) / 10 if temp else None, + } + + +def parse_wifi(text): + """`cmd wifi status` (Android 11+): a 'connected to "SSID"' line plus WifiInfo.""" + m = re.search(r'connected to "(.*?)"', text) + if not m: + return {"connected": False} + rssi = re.search(r"RSSI: (-?\d+)", text) + speed = re.search(r"Link speed: (\d+)", text) + freq = re.search(r"Frequency: (\d+)", text) + return { + "connected": True, + "ssid": m[1], + "rssi": int(rssi[1]) if rssi else None, + "speed": int(speed[1]) if speed else None, + "band": f"{int(freq[1]) / 1000:.1f} GHz" if freq else None, + } + + +def make(cfg: dict) -> Module: + lock = threading.Lock() + cache = {"t": 0, "v": None, "serial": None} + + def read(): + serial = cache["serial"] or _find_tablet() + if not serial: + return {"connected": False, "error": "no tablet with the dashboard tunnel"} + try: + batt = _adb("-s", serial, "shell", "dumpsys", "battery") + wifi = _adb("-s", serial, "shell", "cmd", "wifi", "status") + except (subprocess.SubprocessError, OSError) as e: + cache["serial"] = None # look again next time; it may have moved USB <-> Wi-Fi + return {"connected": False, "error": str(e) or type(e).__name__} + cache["serial"] = serial + return {"connected": True, "link": "Wi-Fi" if ":" in serial else "USB", + "battery": parse_battery(batt), "wifi": parse_wifi(wifi)} + + def state(): + # ponytail: one shared cache, so several open pages cost one adb read per TTL. + with lock: + if time.monotonic() - cache["t"] >= TTL: + cache["v"] = read() + cache["t"] = time.monotonic() + return cache["v"] + + return Module(name="tablet", title="Tablet", state=state) |
