Implement automatic page scaling feature with viewport adjustments

This commit is contained in:
nearxos
2026-02-18 09:33:44 +02:00
parent a9b3726ace
commit d6b09cdd6f
35 changed files with 5722 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
# CM4 Provisioning Dashboard
Flask web UI to monitor the eMMC deployment process and show device connection steps.
- **Connection steps**: Numbered instructions for putting the reTerminal in boot mode and connecting it.
- **Live status**: Idle / Connecting (rpiboot) / Flashing / Backup / Done / Error, with optional progress.
- **Backup / Restore**: Toggle between **Flash** (deploy golden image) and **Backup** (save eMMC to a timestamped file when device is connected in boot mode). List and download saved backups.
- **Recent log**: Tail of the flash log (from the host, via the shared bind mount).
The dashboard reads `/var/lib/cm4-provisioning/status.json` and `flash.log`, which the flash script (running on the Proxmox host) updates. When the dashboard runs inside the LXC, that directory is bind-mounted from the host, so it sees the same files.
## Run locally (development)
```bash
cd dashboard
pip install flask # or use venv
python3 app.py
# Open http://localhost:5000
```
## Run in LXC (Proxmox)
1. Copy the dashboard into the container (e.g. to `/opt/cm4-provisioning/dashboard`).
2. Install Flask if needed: `apt install -y python3-flask` or `pip install flask`.
3. Install the systemd unit and enable it:
```bash
cp /opt/cm4-provisioning/dashboard/cm4-dashboard.service /etc/systemd/system/
systemctl daemon-reload
systemctl enable --now cm4-dashboard
```
4. Open `http://<LXC-IP>:5000` (or port-forward from the Proxmox host).
## Environment (optional)
- `CM4_STATUS_FILE` path to status JSON (default: `/var/lib/cm4-provisioning/status.json`).
- `CM4_LOG_FILE` path to flash log (default: `/var/lib/cm4-provisioning/flash.log`).

View File

@@ -0,0 +1,245 @@
#!/usr/bin/env python3
"""
Flask dashboard for CM4 eMMC provisioning.
Monitors deployment status, shows device connection steps, backup/restore.
Supports USB boot mode (ask Backup/Deploy) and network-booted devices (register, then Backup/Deploy).
"""
import json
import os
import time
from pathlib import Path
from flask import Flask, render_template, jsonify, request, send_file, Response
app = Flask(__name__)
BASE_DIR = Path(os.environ.get("CM4_PROVISIONING_DIR", "/var/lib/cm4-provisioning"))
STATUS_FILE = os.environ.get("CM4_STATUS_FILE", str(BASE_DIR / "status.json"))
LOG_FILE = os.environ.get("CM4_LOG_FILE", str(BASE_DIR / "flash.log"))
ACTION_REQUEST_FILE = os.environ.get("CM4_ACTION_REQUEST_FILE", str(BASE_DIR / "action_request"))
DEVICE_SOURCE_FILE = os.environ.get("CM4_DEVICE_SOURCE_FILE", str(BASE_DIR / "device_source"))
BACKUPS_DIR = Path(os.environ.get("CM4_BACKUPS_DIR", str(BASE_DIR / "backups")))
GOLDEN_IMAGE = Path(os.environ.get("CM4_GOLDEN_IMAGE", str(BASE_DIR / "golden.img")))
NETWORK_DEVICES_FILE = Path(os.environ.get("CM4_NETWORK_DEVICES_FILE", str(BASE_DIR / "network_devices.json")))
DEFAULT_STATUS = {
"phase": "idle",
"message": "Waiting for reTerminal in boot mode or network.",
"progress": None,
"updated": None,
}
def read_status():
try:
with open(STATUS_FILE, "r") as f:
data = json.load(f)
out = {**DEFAULT_STATUS, **data}
if out.get("phase") == "waiting_choice":
try:
with open(DEVICE_SOURCE_FILE, "r") as sf:
out["device_source"] = (sf.read() or "").strip() or "usb"
except (FileNotFoundError, OSError):
out["device_source"] = "usb"
return out
except (FileNotFoundError, json.JSONDecodeError):
return DEFAULT_STATUS
def read_log_tail(lines=50):
try:
with open(LOG_FILE, "r") as f:
all_lines = f.readlines()
return "".join(all_lines[-lines:]).strip() if all_lines else ""
except (FileNotFoundError, PermissionError):
return ""
def _load_network_devices():
try:
if NETWORK_DEVICES_FILE.is_file():
with open(NETWORK_DEVICES_FILE, "r") as f:
return json.load(f)
except (json.JSONDecodeError, OSError):
pass
return {"devices": []}
def _save_network_devices(data):
try:
os.makedirs(NETWORK_DEVICES_FILE.parent, exist_ok=True)
with open(NETWORK_DEVICES_FILE, "w") as f:
json.dump(data, f, indent=2)
return True
except (PermissionError, OSError):
return False
def list_backups():
if not BACKUPS_DIR.is_dir():
return []
out = []
for p in sorted(BACKUPS_DIR.iterdir(), key=lambda x: x.stat().st_mtime, reverse=True):
if p.is_file() and p.suffix in (".img", ".img.gz"):
try:
st = p.stat()
out.append({"name": p.name, "size": st.st_size, "mtime": st.st_mtime})
except OSError:
pass
return out
@app.route("/")
def index():
return render_template("index.html")
@app.route("/api/status")
def api_status():
return jsonify(read_status())
@app.route("/api/log")
def api_log():
return jsonify({"log": read_log_tail()})
@app.route("/api/pending-devices")
def api_pending_devices():
"""Returns USB (if waiting_choice) and registered network devices so the UI can show Backup/Deploy."""
st = read_status()
usb = None
if st.get("phase") == "waiting_choice":
usb = {"source": "usb", "message": st.get("message", "Device connected (USB). Choose action.")}
data = _load_network_devices()
network = [d for d in data.get("devices", []) if d.get("action") in (None, "wait")]
return jsonify({"usb": usb, "network": network})
@app.route("/api/device-action", methods=["POST"])
def api_device_action():
"""User chose Backup or Deploy for a device. source=usb | network; for network pass mac=."""
body = request.get_json(force=True, silent=True) or {}
source = (body.get("source") or "").strip().lower()
action = (body.get("action") or "").strip().lower()
if action not in ("backup", "deploy"):
return jsonify({"ok": False, "error": "action must be 'backup' or 'deploy'"}), 400
if source == "usb":
try:
os.makedirs(os.path.dirname(ACTION_REQUEST_FILE) or ".", exist_ok=True)
with open(ACTION_REQUEST_FILE, "w") as f:
f.write(action)
return jsonify({"ok": True})
except (PermissionError, OSError):
return jsonify({"ok": False, "error": "Could not write action file"}), 500
if source == "network":
mac = (body.get("mac") or "").strip()
if not mac:
return jsonify({"ok": False, "error": "mac required for network device"}), 400
data = _load_network_devices()
for d in data.get("devices", []):
if (d.get("mac") or "").lower() == mac.lower():
d["action"] = action
d["action_at"] = time.time()
_save_network_devices(data)
return jsonify({"ok": True})
return jsonify({"ok": False, "error": "Device not found"}), 404
return jsonify({"ok": False, "error": "source must be 'usb' or 'network'"}), 400
@app.route("/api/register-device", methods=["POST"])
def api_register_device():
"""Called by a network-booted device to register (mac, ip)."""
body = request.get_json(force=True, silent=True) or request.form
mac = (body.get("mac") or "").strip()
ip = (body.get("ip") or request.remote_addr or "").strip()
if not mac:
return jsonify({"ok": False, "error": "mac required"}), 400
data = _load_network_devices()
devices = data.get("devices", [])
for d in devices:
if (d.get("mac") or "").lower() == mac.lower():
d["ip"] = ip
d["registered_at"] = time.time()
d["action"] = d.get("action") or "wait"
_save_network_devices(data)
return jsonify({"ok": True, "message": "registered"})
devices.append({"mac": mac, "ip": ip, "registered_at": time.time(), "action": "wait"})
data["devices"] = devices
_save_network_devices(data)
return jsonify({"ok": True, "message": "registered"})
@app.route("/api/device-action-poll")
def api_device_action_poll():
"""Network device polls this to get its assigned action (deploy/backup) and URL."""
mac = (request.args.get("mac") or "").strip()
if not mac:
return jsonify({"action": "wait"}), 200
data = _load_network_devices()
base = request.host_url.rstrip("/")
for d in data.get("devices", []):
if (d.get("mac") or "").lower() == mac.lower():
action = d.get("action") or "wait"
if action == "deploy":
return jsonify({"action": "deploy", "url": f"{base}/api/golden-image"})
if action == "backup":
return jsonify({"action": "backup", "upload_url": f"{base}/api/backup-upload?mac={mac}"})
return jsonify({"action": "wait"})
return jsonify({"action": "wait"})
@app.route("/api/golden-image")
def api_golden_image():
"""Stream the golden image for network deploy (device pulls and writes to eMMC)."""
if not GOLDEN_IMAGE.is_file():
return jsonify({"error": "Golden image not found"}), 404
return send_file(
GOLDEN_IMAGE,
mimetype="application/octet-stream",
as_attachment=True,
download_name="golden.img",
)
@app.route("/api/backup-upload", methods=["POST"])
def api_backup_upload():
"""Network device uploads its eMMC backup (raw body)."""
mac = (request.args.get("mac") or "").strip().replace(":", "-")[:20]
if not mac:
return jsonify({"error": "mac query param required"}), 400
BACKUPS_DIR.mkdir(parents=True, exist_ok=True)
name = f"backup-net-{mac}-{int(time.time())}.img"
path = BACKUPS_DIR / name
try:
with open(path, "wb") as f:
while True:
chunk = request.stream.read(1024 * 1024)
if not chunk:
break
f.write(chunk)
return jsonify({"ok": True, "file": name})
except (OSError, IOError) as e:
if path.exists():
path.unlink(missing_ok=True)
return jsonify({"error": str(e)}), 500
@app.route("/api/backups")
def api_backups():
return jsonify({"backups": list_backups()})
@app.route("/api/backups/<path:name>")
def api_backup_download(name):
if ".." in name or "/" in name or "\\" in name:
return jsonify({"error": "invalid name"}), 400
path = BACKUPS_DIR / name
if not path.is_file():
return jsonify({"error": "not found"}), 404
return send_file(path, as_attachment=True, download_name=name)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=False)

View File

@@ -0,0 +1,16 @@
[Unit]
Description=CM4 eMMC Provisioning Dashboard
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=root
WorkingDirectory=/opt/cm4-provisioning/dashboard
ExecStart=/usr/bin/python3 -m flask --app app run --host=0.0.0.0 --port=5000
Restart=on-failure
RestartSec=5
Environment=FLASK_ENV=production
[Install]
WantedBy=multi-user.target

View File

@@ -0,0 +1,520 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CM4 eMMC Provisioning</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;500;600&family=Outfit:wght@300;400;500;600;700&display=swap" rel="stylesheet">
<style>
/* Portal Styling Guide tokens */
:root {
--bg-primary: #0a0e14;
--bg-secondary: #11151c;
--bg-tertiary: #1a1f2b;
--bg-card: #151a24;
--accent-primary: #00d4aa;
--accent-secondary: #00b894;
--accent-glow: rgba(0, 212, 170, 0.15);
--text-primary: #e6e8eb;
--text-secondary: #8b949e;
--text-muted: #5c6370;
--border-color: #2d333b;
--danger: #ff6b6b;
--danger-glow: rgba(255, 107, 107, 0.15);
--warning: #ffd93d;
--success: #00d4aa;
--gradient-accent: linear-gradient(135deg, #00d4aa 0%, #00b894 50%, #00cec9 100%);
}
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: 'Outfit', -apple-system, BlinkMacSystemFont, sans-serif;
background: var(--bg-primary);
color: var(--text-primary);
min-height: 100vh;
line-height: 1.6;
}
body::before {
content: '';
position: fixed;
inset: 0;
background:
radial-gradient(circle at 20% 20%, rgba(0, 212, 170, 0.03) 0%, transparent 50%),
radial-gradient(circle at 80% 80%, rgba(0, 184, 148, 0.03) 0%, transparent 50%),
linear-gradient(180deg, var(--bg-primary) 0%, var(--bg-secondary) 100%);
pointer-events: none;
z-index: -1;
}
.header {
position: fixed;
top: 0;
left: 0;
right: 0;
background: var(--bg-secondary);
border-bottom: 1px solid var(--border-color);
z-index: 1000;
padding: 1rem 2rem;
display: flex;
align-items: center;
justify-content: space-between;
}
.logo {
display: flex;
align-items: center;
gap: 0.75rem;
}
.logo-icon {
width: 40px;
height: 40px;
background: var(--gradient-accent);
border-radius: 10px;
box-shadow: 0 4px 20px var(--accent-glow);
display: flex;
align-items: center;
justify-content: center;
font-size: 1.25rem;
}
.logo-title {
font-size: 1.5rem;
font-weight: 600;
background: var(--gradient-accent);
-webkit-background-clip: text;
background-clip: text;
-webkit-text-fill-color: transparent;
}
.main {
max-width: 720px;
margin: 0 auto;
padding: 2rem;
padding-top: calc(2rem + 72px);
}
.card {
background: var(--bg-card);
border: 1px solid var(--border-color);
border-radius: 16px;
padding: 1.5rem;
margin-bottom: 1.5rem;
}
.card-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 1.5rem;
padding-bottom: 1rem;
border-bottom: 1px solid var(--border-color);
}
.card-title {
font-size: 1.1rem;
font-weight: 600;
display: flex;
align-items: center;
gap: 0.5rem;
}
.card-title .label {
font-size: 1.1rem;
font-weight: 600;
color: var(--text-primary);
}
.subtitle { color: var(--text-secondary); font-size: 0.9rem; margin-bottom: 1.5rem; }
.steps { list-style: none; }
.steps li {
display: flex;
align-items: flex-start;
gap: 0.75rem;
padding: 0.6rem 0;
border-bottom: 1px solid var(--border-color);
}
.steps li:last-child { border-bottom: none; }
.step-num {
flex-shrink: 0;
width: 1.75rem;
height: 1.75rem;
background: var(--bg-tertiary);
color: var(--text-primary);
border-radius: 50%;
display: inline-flex;
align-items: center;
justify-content: center;
font-weight: 600;
font-size: 0.8rem;
}
.step-num.done { background: var(--gradient-accent); color: var(--bg-primary); }
.status-box {
padding: 1rem 1.25rem;
border-radius: 12px;
margin-bottom: 0.75rem;
border: 1px solid var(--border-color);
}
.status-box.idle { background: var(--accent-glow); border-color: var(--accent-primary); }
.status-box.rpiboot,
.status-box.flashing,
.status-box.backup,
.status-box.waiting_choice { background: rgba(255, 217, 61, 0.1); border-color: var(--warning); }
.status-box.done { background: rgba(0, 212, 170, 0.12); border-color: var(--success); }
.status-box.error { background: var(--danger-glow); border-color: var(--danger); }
.status-phase {
font-weight: 600;
font-size: 0.85rem;
text-transform: uppercase;
letter-spacing: 0.5px;
margin-bottom: 0.35rem;
}
.status-box.idle .status-phase { color: var(--accent-primary); }
.status-box.rpiboot .status-phase,
.status-box.flashing .status-phase,
.status-box.backup .status-phase,
.status-box.waiting_choice .status-phase { color: var(--warning); }
.status-box.done .status-phase { color: var(--success); }
.status-box.error .status-phase { color: var(--danger); }
.status-message { color: var(--text-primary); font-size: 0.95rem; }
.status-error { color: var(--danger); font-size: 0.9rem; margin-top: 0.5rem; }
.status-updated { color: var(--text-muted); font-size: 0.75rem; margin-top: 0.5rem; }
.progress-wrap {
margin-top: 0.75rem;
height: 6px;
background: var(--bg-tertiary);
border-radius: 3px;
overflow: hidden;
}
.progress-bar {
height: 100%;
background: var(--gradient-accent);
border-radius: 3px;
transition: width 0.4s ease;
}
.progress-bar.indeterminate {
width: 40%;
animation: indeterminate 1.2s ease-in-out infinite;
}
@keyframes indeterminate {
0% { transform: translateX(-100%); }
100% { transform: translateX(350%); }
}
.log-box {
font-family: 'JetBrains Mono', monospace;
font-size: 0.8rem;
background: var(--bg-tertiary);
border: 1px solid var(--border-color);
border-radius: 8px;
padding: 0.75rem 1rem;
max-height: 180px;
overflow-y: auto;
white-space: pre-wrap;
word-break: break-all;
color: var(--text-muted);
}
.log-box:empty::before { content: 'No flash log yet.'; color: var(--text-muted); }
.backups-list { list-style: none; }
.backups-list li {
display: flex;
align-items: center;
justify-content: space-between;
padding: 0.6rem 0;
border-bottom: 1px solid var(--border-color);
gap: 0.75rem;
}
.backups-list li:last-child { border-bottom: none; }
.backups-list a {
color: var(--accent-primary);
text-decoration: none;
font-weight: 500;
}
.backups-list a:hover { text-decoration: underline; }
.backups-meta { color: var(--text-muted); font-size: 0.85rem; font-family: 'JetBrains Mono', monospace; }
.pending-device {
padding: 0.75rem 1rem;
background: var(--bg-tertiary);
border: 1px solid var(--border-color);
border-radius: 12px;
margin-bottom: 0.5rem;
display: flex;
align-items: center;
justify-content: space-between;
flex-wrap: wrap;
gap: 0.5rem;
}
.pending-device .label { font-weight: 500; color: var(--text-primary); }
.pending-device .actions { display: flex; gap: 0.5rem; }
.btn {
padding: 0.75rem 1.5rem;
border-radius: 8px;
font-size: 0.9rem;
font-weight: 500;
font-family: 'Outfit', sans-serif;
display: inline-flex;
align-items: center;
gap: 0.5rem;
cursor: pointer;
transition: all 0.2s ease;
border: none;
}
.btn-primary {
background: var(--gradient-accent);
color: var(--bg-primary);
}
.btn-primary:hover {
transform: translateY(-2px);
box-shadow: 0 4px 20px var(--accent-glow);
}
.btn-secondary {
background: var(--bg-tertiary);
border: 1px solid var(--border-color);
color: var(--text-primary);
}
.btn-secondary:hover {
border-color: var(--accent-primary);
color: var(--accent-primary);
}
.pending-device .btn { padding: 0.5rem 1rem; font-size: 0.85rem; }
code {
font-family: 'JetBrains Mono', monospace;
font-size: 0.85em;
background: var(--bg-tertiary);
padding: 0.15rem 0.4rem;
border-radius: 4px;
color: var(--accent-primary);
}
.empty-state {
text-align: center;
padding: 2rem;
color: var(--text-muted);
font-size: 0.9rem;
}
details summary {
cursor: pointer;
color: var(--text-secondary);
font-size: 0.85rem;
font-weight: 500;
}
@media (max-width: 768px) {
.header { padding: 1rem; flex-direction: column; gap: 0.5rem; text-align: center; }
.main { padding: 1rem; padding-top: calc(1rem + 100px); }
}
</style>
</head>
<body>
<header class="header">
<div class="logo">
<div class="logo-icon"></div>
<h1 class="logo-title">CM4 eMMC Provisioning</h1>
</div>
</header>
<main class="main">
<p class="subtitle">reTerminal DM4 — deploy or backup via USB boot mode or network boot</p>
<div class="card">
<div class="card-header">
<h2 class="card-title"><span class="label">Connect the device</span></h2>
</div>
<p style="color: var(--text-secondary); font-size: 0.85rem; margin-bottom: 0.75rem;">Choose one:</p>
<p style="font-size: 0.9rem; font-weight: 600; margin-bottom: 0.25rem;">USB boot mode</p>
<ol class="steps">
<li><span class="step-num">1</span> Set the reTerminal to <strong>boot mode</strong>: fit the <strong>eMMC disable</strong> jumper (e.g. J2 / nRPIBOOT).</li>
<li><span class="step-num">2</span> Connect the reTerminals <strong>USB slave</strong> port to the Proxmox host. Power on. The device will appear in “Device detected” below; choose <strong>Backup</strong> or <strong>Deploy</strong>.</li>
<li><span class="step-num">3</span> When done, remove the jumper and power cycle so it boots from eMMC.</li>
</ol>
<p style="font-size: 0.9rem; font-weight: 600; margin: 1rem 0 0.25rem 0;">Network boot</p>
<ol class="steps">
<li><span class="step-num">1</span> Enable network boot on the CM4 (e.g. <code>BOOT_ORDER=0x21</code>) and ensure it can reach this server.</li>
<li><span class="step-num">2</span> Boot the device over the network with an environment that runs the <strong>provisioning client</strong> (register + poll for action). It will show under “Device detected (Network)”. Choose <strong>Backup</strong> or <strong>Deploy</strong>.</li>
</ol>
</div>
<div class="card">
<div class="card-header">
<h2 class="card-title"><span class="label">Device detected — choose action</span></h2>
</div>
<p id="pendingHint" style="color: var(--text-secondary); font-size: 0.9rem; margin-bottom: 0.75rem;">When a device is detected (USB boot mode or network boot), it will appear below. Choose <strong>Backup</strong> to save its eMMC to a file, or <strong>Deploy</strong> to write the golden image to it.</p>
<div id="pendingDevices"></div>
<p id="noPending" class="empty-state" style="display: none;">No device waiting. Connect a reTerminal in USB boot mode, or ensure a network-booted device has registered.</p>
<h3 style="font-size: 0.85rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; color: var(--text-secondary); margin: 1rem 0 0.5rem 0;">Saved backups</h3>
<ul id="backupsList" class="backups-list"></ul>
<p id="backupsEmpty" class="empty-state" style="display: none;">No backups yet.</p>
</div>
<div class="card">
<div class="card-header">
<h2 class="card-title"><span class="label">Deployment status</span></h2>
</div>
<div id="status" class="status-box idle">
<div class="status-phase">Idle</div>
<div class="status-message">Waiting for reTerminal in boot mode.</div>
<div id="statusError" class="status-error" style="display:none;"></div>
<div id="statusUpdated" class="status-updated"></div>
<div id="progressWrap" class="progress-wrap" style="display:none;">
<div id="progressBar" class="progress-bar"></div>
</div>
</div>
<details style="margin-top: 0.75rem;">
<summary>Recent flash log</summary>
<div id="log" class="log-box" style="margin-top: 0.5rem;"></div>
</details>
</div>
</main>
<script>
const statusEl = document.getElementById('status');
const phaseEl = statusEl.querySelector('.status-phase');
const messageEl = statusEl.querySelector('.status-message');
const errorEl = document.getElementById('statusError');
const updatedEl = document.getElementById('statusUpdated');
const progressWrap = document.getElementById('progressWrap');
const progressBar = document.getElementById('progressBar');
const logEl = document.getElementById('log');
const phaseLabels = {
idle: 'Idle',
rpiboot: 'Connecting',
waiting_choice: 'Choose action',
flashing: 'Flashing',
backup: 'Backing up',
done: 'Done',
error: 'Error'
};
function renderStatus(data) {
const phase = data.phase || 'idle';
statusEl.className = 'status-box ' + phase;
phaseEl.textContent = phaseLabels[phase] || phase;
messageEl.textContent = data.message || '';
if (data.error) {
errorEl.textContent = data.error;
errorEl.style.display = 'block';
} else {
errorEl.style.display = 'none';
}
if (data.updated) {
updatedEl.textContent = 'Updated: ' + data.updated;
updatedEl.style.display = 'block';
} else {
updatedEl.style.display = 'none';
}
const progress = data.progress;
const inProgress = phase === 'rpiboot' || phase === 'flashing' || phase === 'backup';
const showProgress = progress != null && (inProgress || phase === 'done');
if (showProgress) {
progressWrap.style.display = 'block';
progressBar.classList.remove('indeterminate');
progressBar.style.width = (progress === null ? 0 : progress) + '%';
if (progress === null && inProgress) {
progressBar.classList.add('indeterminate');
progressBar.style.width = '40%';
}
} else if (inProgress) {
progressWrap.style.display = 'block';
progressBar.classList.add('indeterminate');
progressBar.style.width = '40%';
} else {
progressWrap.style.display = 'none';
}
}
function renderPendingDevices(usb, network) {
const container = document.getElementById('pendingDevices');
const noPending = document.getElementById('noPending');
container.innerHTML = '';
let hasAny = false;
if (usb) {
hasAny = true;
const div = document.createElement('div');
div.className = 'pending-device';
div.innerHTML = '<span class="label">Device connected (USB boot mode)</span><span class="actions"><button type="button" class="btn btn-secondary" data-source="usb" data-action="backup">Backup</button><button type="button" class="btn btn-primary" data-source="usb" data-action="deploy">Deploy</button></span>';
container.appendChild(div);
}
(network || []).forEach(function(d) {
hasAny = true;
const div = document.createElement('div');
div.className = 'pending-device';
div.innerHTML = '<span class="label">Device (Network): ' + escapeHtml(d.ip || '') + ' — ' + escapeHtml(d.mac || '') + '</span><span class="actions"><button type="button" class="btn btn-secondary" data-source="network" data-mac="' + escapeHtml(d.mac || '') + '" data-action="backup">Backup</button><button type="button" class="btn btn-primary" data-source="network" data-mac="' + escapeHtml(d.mac || '') + '" data-action="deploy">Deploy</button></span>';
container.appendChild(div);
});
noPending.style.display = hasAny ? 'none' : 'block';
container.querySelectorAll('button[data-action]').forEach(function(btn) {
btn.onclick = function() {
const source = btn.getAttribute('data-source');
const action = btn.getAttribute('data-action');
const mac = btn.getAttribute('data-mac');
const body = { source: source, action: action };
if (mac) body.mac = mac;
fetch('/api/device-action', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) })
.then(function(r) { return r.json(); })
.then(function(data) {
if (data.ok) { fetchPending(); fetchStatus(); }
else alert(data.error || 'Failed');
})
.catch(function() { alert('Request failed'); });
};
});
}
function fetchPending() {
fetch('/api/pending-devices').then(function(r) { return r.json(); }).then(function(data) {
renderPendingDevices(data.usb || null, data.network || []);
}).catch(function() { renderPendingDevices(null, []); });
}
function fetchStatus() {
fetch('/api/status')
.then(r => r.json())
.then(renderStatus)
.catch(() => renderStatus({ phase: 'error', message: 'Could not load status.' }));
}
function fetchLog() {
fetch('/api/log')
.then(r => r.json())
.then(data => { logEl.textContent = data.log || ''; })
.catch(() => { logEl.textContent = ''; });
}
function fmtSize(n) {
if (n >= 1e9) return (n / 1e9).toFixed(1) + ' GB';
if (n >= 1e6) return (n / 1e6).toFixed(1) + ' MB';
return (n / 1e3).toFixed(0) + ' KB';
}
function fmtDate(ts) { return new Date(ts * 1000).toLocaleString(); }
function fetchBackups() {
fetch('/api/backups')
.then(r => r.json())
.then(data => {
const list = document.getElementById('backupsList');
const empty = document.getElementById('backupsEmpty');
list.innerHTML = '';
const backups = data.backups || [];
if (backups.length === 0) {
empty.style.display = 'block';
return;
}
empty.style.display = 'none';
backups.forEach(b => {
const li = document.createElement('li');
li.innerHTML = '<a href="/api/backups/' + encodeURIComponent(b.name) + '" download>' + escapeHtml(b.name) + '</a>' +
'<span class="backups-meta">' + fmtSize(b.size) + ' · ' + fmtDate(b.mtime) + '</span>';
list.appendChild(li);
});
})
.catch(() => {});
}
function escapeHtml(s) {
const div = document.createElement('div');
div.textContent = s;
return div.innerHTML;
}
fetchStatus();
fetchLog();
fetchPending();
fetchBackups();
setInterval(fetchStatus, 2000);
setInterval(fetchLog, 4000);
setInterval(fetchPending, 2000);
setInterval(fetchBackups, 5000);
</script>
</body>
</html>