Replace the obsolete host-daemon recover.sh with the current App-Lab-node flow: re-forward the adb tunnels (:8080 gateway, :9999 matrix relay), wait for the daemon (start the app if needed), re-register the node with the local API, and sync a fixed claim code onto the matrix so the board + API agree. Adds a `--watch` mode and a launchd agent (com.redclaw.apess-board-recover.plist) that runs it on every reconnect — so a re-plug heals itself, no manual step. Co-Authored-By: Claude Opus 4.8 <[email protected]>
105 lines
4.1 KiB
Bash
Executable File
105 lines
4.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# recover.sh — restore the Uno Q board's LOCAL DEV binding after a USB re-plug.
|
|
#
|
|
# On re-plug the Uno Q's Linux reboots and the laptop's adb tunnels vanish, so
|
|
# the local API (and the LED-matrix mirror) lose the board; the board also boots
|
|
# showing a fresh random claim code the API never received. This makes it all
|
|
# consistent again in one pass:
|
|
# 1. re-forward the adb tunnels (:8080 gateway, :9999 matrix relay)
|
|
# 2. wait for the node daemon (start the App Lab app if it isn't up)
|
|
# 3. re-register the node with the local API (restores its in-memory binding)
|
|
# 4. scroll a fixed claim code on the matrix so the board + API agree
|
|
#
|
|
# Usage:
|
|
# ./recover.sh # one recovery pass, then exit
|
|
# ./recover.sh --watch # run forever: recover on every (re)connect
|
|
#
|
|
# The App Lab node (apess-onboard) carries its own baked cloud token, so unlike
|
|
# the old host-daemon flow this needs NO secrets in the environment. Config via
|
|
# env (defaults suit the current dev board):
|
|
# SERIAL FLEET_SECRET KIT_ID CLAIM_CODE API_URL NODE_URL APP
|
|
set -uo pipefail
|
|
|
|
SERIAL="${SERIAL:-65301572}"
|
|
API="${API_URL:-http://127.0.0.1:3000}"
|
|
FLEET_SECRET="${FLEET_SECRET:-apess2026}"
|
|
KIT_ID="${KIT_ID:-crimson-node}"
|
|
CLAIM_CODE="${CLAIM_CODE:-7777}"
|
|
NODE_URL="${NODE_URL:-http://127.0.0.1:8080}"
|
|
APP="${APP:-/home/arduino/ArduinoApps/apess-onboard}"
|
|
PORTS=(8080 9999)
|
|
|
|
# Resolve adb even under launchd's minimal PATH.
|
|
ADB="$(command -v adb || true)"
|
|
for c in /opt/homebrew/bin/adb /usr/local/bin/adb "$HOME/Library/Android/sdk/platform-tools/adb"; do
|
|
[ -n "$ADB" ] && break
|
|
[ -x "$c" ] && ADB="$c"
|
|
done
|
|
[ -n "$ADB" ] || { echo "recover: adb not found in PATH"; exit 127; }
|
|
|
|
log() { printf '\033[36m[recover]\033[0m %s\n' "$*"; }
|
|
ok() { printf ' \033[32m✓\033[0m %s\n' "$*"; }
|
|
warn() { printf ' \033[33m!\033[0m %s\n' "$*"; }
|
|
|
|
adb_present() { "$ADB" devices | grep -q "^${SERIAL}[[:space:]].*device$"; }
|
|
|
|
matrix_code() {
|
|
python3 - "$CLAIM_CODE" <<'PY' 2>/dev/null
|
|
import socket, sys
|
|
s = socket.create_connection(('127.0.0.1', 9999), timeout=3)
|
|
s.sendall(f"text {sys.argv[1]}\n".encode()); s.recv(16); s.close()
|
|
PY
|
|
}
|
|
|
|
recover_once() {
|
|
adb_present || { warn "board $SERIAL not connected"; return 1; }
|
|
|
|
# 1 · re-forward tunnels (they vanish on re-plug)
|
|
for p in "${PORTS[@]}"; do
|
|
"$ADB" -s "$SERIAL" forward --list 2>/dev/null | grep -q "tcp:$p" \
|
|
|| "$ADB" -s "$SERIAL" forward "tcp:$p" "tcp:$p" >/dev/null
|
|
done
|
|
ok "tunnels forwarded (${PORTS[*]})"
|
|
|
|
# 2 · wait for the node daemon (auto-starts on boot; start it if not)
|
|
local n=0
|
|
until curl -s -m2 "$NODE_URL/health" -o /dev/null 2>/dev/null; do
|
|
n=$((n + 1))
|
|
if [ "$n" -eq 20 ]; then
|
|
warn "daemon not up after ~40s — starting the app"
|
|
"$ADB" -s "$SERIAL" shell "arduino-app-cli app start $APP" >/dev/null 2>&1 || true
|
|
fi
|
|
if [ "$n" -gt 90 ]; then warn "daemon never came up ($NODE_URL/health)"; return 1; fi
|
|
sleep 2
|
|
done
|
|
ok "node daemon healthy"
|
|
|
|
# 3 · re-register with the local API (restores the in-memory node binding)
|
|
local r
|
|
r="$(curl -s -m5 -X POST "$API/nodes/self-register" \
|
|
-H "x-fleet-secret: $FLEET_SECRET" -H 'content-type: application/json' \
|
|
-d "{\"kitId\":\"$KIT_ID\",\"claimCode\":\"$CLAIM_CODE\",\"url\":\"$NODE_URL\",\"token\":\"open-lan\"}" 2>/dev/null)"
|
|
if echo "$r" | grep -q '"url"'; then ok "re-registered with API (code $CLAIM_CODE)"
|
|
else warn "API self-register failed — is the API up at $API? ($r)"; return 1; fi
|
|
|
|
# 4 · sync the claim code onto the matrix so board + API agree
|
|
matrix_code && ok "matrix showing $CLAIM_CODE" || warn "could not set matrix code (relay :9999)"
|
|
log "recovered — bind in the UI with code $CLAIM_CODE"
|
|
}
|
|
|
|
watch_loop() {
|
|
log "watching board $SERIAL — recover on every (re)connect (Ctrl-C to stop)"
|
|
while true; do
|
|
"$ADB" -s "$SERIAL" wait-for-device
|
|
sleep 3 # let Linux + the App Lab app finish booting
|
|
recover_once || warn "recovery pass incomplete; will retry on next reconnect"
|
|
while adb_present; do sleep 2; done
|
|
log "board disconnected — waiting for re-plug"
|
|
done
|
|
}
|
|
|
|
case "${1:-}" in
|
|
--watch | -w) watch_loop ;;
|
|
*) recover_once ;;
|
|
esac
|