Skip to content

Turn a ~$5 AliExpress clock into a physical AI usage dashboard (Claude, Codex, multi-account). No firmware flashing.

1.9 KB
Raw Blame
"""Transfer normalized usage only; credentials stay on the source computer."""
import argparse
import json
import shlex
import subprocess
import time

from token_tv.sources import load_config
from token_tv.state import MIN_FETCH_SECONDS, UsageStore

REMOTE_WRITE = """import json,os,pathlib,sys
p=pathlib.Path(sys.argv[1]).expanduser()
data=json.load(sys.stdin)
p.parent.mkdir(parents=True,exist_ok=True)
t=p.with_suffix('.tmp')
fd=os.open(t,os.O_WRONLY|os.O_CREAT|os.O_TRUNC,0o600)
os.fchmod(fd,0o600)
with os.fdopen(fd,'w',encoding='utf-8') as f:
    f.write(json.dumps(data,ensure_ascii=False,separators=(',',':')))
t.replace(p)
"""


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--config", required=True)
    parser.add_argument("--ssh-host", required=True)
    parser.add_argument("--remote-path", required=True)
    args = parser.parse_args()
    if args.ssh_host.startswith("-"):
        parser.error("A host name is required")
    config = load_config(args.config)
    store = UsageStore(config["accounts"], min_fetch_seconds=MIN_FETCH_SECONDS)
    while True:
        try:
            store.refresh()
            data = json.dumps(store.snapshot(), ensure_ascii=False, separators=(",", ":"))
            command = "python3 -c " + shlex.quote(REMOTE_WRITE) + " " + shlex.quote(args.remote_path)
            subprocess.run(["ssh", "-o", "BatchMode=yes", "-o", "ConnectTimeout=10",
                            args.ssh_host, command], input=data, text=True, check=True,
                           stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=30)
            print("TokenTV normalized account snapshot delivered.", flush=True)
        except (OSError, subprocess.SubprocessError):
            print("TokenTV bridge unavailable; display will mark stale.", flush=True)
        time.sleep(max(60, int(config.get("poll_seconds", 60))))


if __name__ == "__main__":
    main()