feat: add Microduck UNO Q deployment and hotspot skill control

This commit is contained in:
2026-11-19 13:49:08 +08:00
parent 2b0c395508
commit 164a397726
99 changed files with 12488 additions and 4 deletions
+10
View File
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REQ_FILE="${SCRIPT_DIR}/../python/requirements.txt"
echo "[microduck-unoq] Installing Python dependencies from ${REQ_FILE}..."
python3 -m pip install --upgrade pip
python3 -m pip install -r "${REQ_FILE}"
echo "[microduck-unoq] Done."
+44
View File
@@ -0,0 +1,44 @@
#!/usr/bin/env bash
set -euo pipefail
# Configure an offline Wi-Fi Access Point on Arduino UNO Q (Debian + NetworkManager)
# Phone connects to this hotspot and opens http://192.168.4.1:8000
CON_NAME="${AP_CON_NAME:-microduck-ap}"
SSID="${AP_SSID:-Microduck-AP}"
PASS="${AP_PASS:-microduck123}"
IFACE="${AP_IFACE:-wlan0}"
IP_CIDR="${AP_IP_CIDR:-192.168.4.1/24}"
if ! command -v nmcli >/dev/null 2>&1; then
echo "Error: nmcli (NetworkManager) not found. Configure hostapd + dnsmasq manually for ${IFACE} -> ${IP_CIDR}." >&2
exit 1
fi
echo "[microduck-ap] Configuring Wi-Fi AP '${SSID}' on ${IFACE} (${IP_CIDR})..."
if nmcli connection show "${CON_NAME}" >/dev/null 2>&1; then
nmcli connection delete "${CON_NAME}"
fi
nmcli connection add \
type wifi \
ifname "${IFACE}" \
con-name "${CON_NAME}" \
autoconnect yes \
ssid "${SSID}"
nmcli connection modify "${CON_NAME}" \
802-11-wireless.mode ap \
802-11-wireless.band bg \
ipv4.method shared \
ipv4.addresses "${IP_CIDR}" \
wifi-sec.key-mgmt wpa-psk \
wifi-sec.psk "${PASS}"
nmcli connection up "${CON_NAME}"
echo "[microduck-ap] Hotspot active!"
echo " SSID: ${SSID}"
echo " Pass: ${PASS}"
echo " Control URL: http://${IP_CIDR%/*}:8000"
+74
View File
@@ -0,0 +1,74 @@
"""USB-only ONNX staging page; never loads policies or talks to actuators."""
import hashlib
import json
import os
from pathlib import Path
import secrets
import tempfile
from http.server import BaseHTTPRequestHandler, HTTPServer
ROOT = Path.home() / 'microduck-usb-staging' / 'models'
LIMIT = 16 * 1024 * 1024
TOKEN = secrets.token_hex(24)
PAGE = '''<!doctype html><html lang="zh"><meta charset="utf-8"><title>Microduck USB · ONNX 写入</title>
<h1>通过 USB 写入 ONNX</h1><p>文件存入板上的 ~/microduck-usb-staging/models/,不会替换或启动当前策略。</p>
<input id="file" type="file" accept=".onnx"><button id="send">写入板上测试目录</button><pre id="result"></pre>
<script>send.onclick=async()=>{const f=file.files[0];if(!f||!f.name.toLowerCase().endsWith('.onnx')||!f.size||f.size>16777216){result.textContent='请选择不超过 16 MiB 的 ONNX 文件';return}
send.disabled=true;try{result.textContent='正在写入并校验…';const hash=Array.from(new Uint8Array(await crypto.subtle.digest('SHA-256',await f.arrayBuffer())),x=>x.toString(16).padStart(2,'0')).join('');const r=await fetch('/stage',{method:'POST',headers:{'X-Stage-Token':'TOKEN_VALUE','X-SHA256':hash},body:f});if(!r.ok)throw new Error('HTTP '+r.status);const j=await r.json();if(j.sha256!==hash)throw new Error('校验失败');result.textContent='已写入,SHA-256 一致。未启用策略。\\n'+j.path+'\\n'+hash}catch(e){result.textContent=e.message}finally{send.disabled=false}}</script></html>'''
class Handler(BaseHTTPRequestHandler):
def allowed(self):
return self.headers.get('Host') == '127.0.0.1:18600'
def reply(self, data, kind):
self.send_response(200)
self.send_header('Content-Type', kind)
self.send_header('Content-Length', str(len(data)))
self.send_header('Cache-Control', 'no-store')
self.send_header('X-Frame-Options', 'DENY')
self.end_headers()
self.wfile.write(data)
def do_GET(self):
if not self.allowed() or self.path != '/':
return self.send_error(404)
self.reply(PAGE.replace('TOKEN_VALUE', TOKEN).encode(), 'text/html; charset=utf-8')
def do_POST(self):
if (not self.allowed() or self.path != '/stage' or self.headers.get('X-Stage-Token') != TOKEN
or self.headers.get('Origin') != 'http://127.0.0.1:18600'):
return self.send_error(403)
try:
size = int(self.headers.get('Content-Length', '0'))
except ValueError:
return self.send_error(400)
if not 0 < size <= LIMIT:
return self.send_error(413)
self.connection.settimeout(20)
data = self.rfile.read(size)
digest = hashlib.sha256(data).hexdigest()
if len(data) != size or digest != self.headers.get('X-SHA256'):
return self.send_error(400)
target = ROOT / (digest + '.onnx')
if not target.exists():
if sum(p.stat().st_size for p in ROOT.glob('*.onnx')) + size > 128 * 1024 * 1024:
return self.send_error(507)
with tempfile.NamedTemporaryFile(dir=ROOT, delete=False) as f:
tmp = Path(f.name)
try:
f.write(data)
f.flush()
os.fsync(f.fileno())
os.replace(tmp, target)
finally:
tmp.unlink(missing_ok=True)
saved = hashlib.sha256(target.read_bytes()).hexdigest()
if saved != digest:
return self.send_error(500)
self.reply(json.dumps(dict(path=str(target), sha256=saved)).encode(), 'application/json')
if __name__ == '__main__':
ROOT.mkdir(mode=0o700, parents=True, exist_ok=True)
HTTPServer(('127.0.0.1', 18600), Handler).serve_forever()