main
John Lauer Add measured-power and arm-timeout protection; make fan feedback validity explicit in control panel 97b70ea
#!/usr/bin/env python3
"""Small CLI using the dashboard's own endpoints and per-thread registry."""
import argparse
import hashlib
import json
import os
from pathlib import Path
import subprocess
import urllib.request

p=argparse.ArgumentParser()
p.add_argument('--ai-thread',required=True)
p.add_argument('--target',help='Verified Adom Bridge desktop target; required only for show')
p.add_argument('action',choices=['ls','show','state','cmd','settings'])
p.add_argument('json',nargs='?',default='{}')
a=p.parse_args()
root=Path(__file__).resolve().parent
if a.action=='ls':
    raise SystemExit(subprocess.call(['python3',str(root/'server.py'),'--ai-thread',a.ai_thread,'ls']))
key=hashlib.sha256(a.ai_thread.encode()).hexdigest()[:16]
reg=Path.home()/'.local/state/adom-custom-load'/(key+'.json')
if not reg.exists():p.error('No instance; run server.py --ai-thread "'+a.ai_thread+'" first')
r=json.loads(reg.read_text());base='http://127.0.0.1:'+str(r['port'])
if a.action=='show':
    if not a.target:p.error('--target is required for showing the dashboard on a verified desktop')
    proxy=os.environ.get('VSCODE_PROXY_URI')
    if not proxy:p.error('No Hydrogen proxy; supply a verified host-visible URL through the Adom display tools')
    url=proxy.replace('{{port}}',str(r['port']))
    result=subprocess.call(['adom-bridge','--target',a.target,'--ai-thread',a.ai_thread,'pup_open_tab',json.dumps({'url':url})])
    raise SystemExit(result)
path={'state':'/state','cmd':'/api/cmd','settings':'/settings'}[a.action]
if a.action=='state' or (a.action=='settings' and a.json=='{}'):
    print(urllib.request.urlopen(base+path,timeout=2).read().decode());raise SystemExit()
token=json.load(urllib.request.urlopen(base+'/bootstrap',timeout=2))['token']
body=dict(json.loads(a.json),aiThread=a.ai_thread)
q=urllib.request.Request(base+path,data=json.dumps(body,allow_nan=False).encode(),headers={'Content-Type':'application/json','X-Load-Token':token})
try:print(urllib.request.urlopen(q,timeout=3).read().decode())
except urllib.error.HTTPError as e:print(e.read().decode());raise SystemExit(1)