Files
smart-home-control-system/scripts/run_web.sh
T
sambu a96cad867e fix(web/cli): 让 --bind 真正生效 + 新增设备表 CSV 导出命令 export
- app_run_web 之前写死 127.0.0.1,--help 却宣传了 --bind;现改为取配置的 bind_addr,
  并在 main.c 把命令行覆盖移到 system_load 之后(原先在 load 前覆盖会被配置文件覆盖掉);
  覆盖只对本次运行生效,退出保存前还原,避免把一次性参数写进 smart_home.ini
- 新增控制台命令 export [路径]:设备表导出 CSV(此前 storage_export_csv 只有接口没有入口)
- 清理 data/ 下旧版本遗留的空目录(config/devices/logs)
- 测试与文档同步:integration.menu 增 export 正向用例与 CSV 落盘断言;README/docs/04 命令表补 export
- 实测:--bind 0.0.0.0 后 ss 显示监听 0.0.0.0:8091,内网 192.168.1.10 访问 200;
  export 生成 data/devices.csv(表头+8 行);ctest 19/19、acceptance 25/25
2026-09-15 00:05:09 +08:00

58 lines
1.8 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# ============================================================================
# run_web.sh —— 后台启动网页上位机(教材演示用)
#
# 用法:
# bash scripts/run_web.sh # 默认 127.0.0.1:8088
# bash scripts/run_web.sh 9000 # 指定端口
# bash scripts/run_web.sh 8088 data # 指定端口与数据目录
# 停止: bash scripts/stop_web.sh
# ============================================================================
set -uo pipefail
PORT="${1:-8088}"
DATA_DIR="${2:-data}"
cd "$(dirname "$0")/.." || exit 1
BIN="build/debug/smart_home"
if [ ! -x "$BIN" ]; then
echo "未找到 $BIN,先构建:cmake --preset debug && cmake --build build/debug -j"
exit 1
fi
# 端口占用检查:先礼后兵(避免把别人的服务顶掉)
if command -v ss >/dev/null 2>&1 && ss -ltn 2>/dev/null | grep -q ":${PORT} "; then
echo "端口 ${PORT} 已被占用,请换一个端口:bash scripts/run_web.sh 8090"
ss -ltnp 2>/dev/null | grep ":${PORT} " || true
exit 1
fi
mkdir -p "$DATA_DIR" logs
LOG="logs/web_${PORT}.log"
PIDFILE="logs/web_${PORT}.pid"
nohup "$BIN" --web --port "$PORT" --data "$(pwd)/$DATA_DIR" >"$LOG" 2>&1 &
PID=$!
echo "$PID" >"$PIDFILE"
# 就绪探测(最多 3 秒)
for _ in 1 2 3 4 5 6 7 8 9 10 11 12; do
if curl -s -m 1 -o /dev/null --noproxy '*' "http://127.0.0.1:${PORT}/"; then
break
fi
sleep 0.25
done
if curl -s -m 2 -o /dev/null --noproxy '*' "http://127.0.0.1:${PORT}/"; then
echo "网页上位机已启动:http://127.0.0.1:${PORT}/"
echo " PID :$PID(记录于 $PIDFILE)"
echo " 日志 :$LOG"
echo " 数据目录 :$(pwd)/$DATA_DIR"
echo " 停止 :bash scripts/stop_web.sh ${PORT}"
else
echo "启动失败,日志尾部:"
tail -n 20 "$LOG"
exit 1
fi