fix: harden P0 health monitoring RC2

This commit is contained in:
way
2026-09-26 17:10:53 +08:00
parent 83ec3f4379
commit 3a302b9c78
4 changed files with 245 additions and 177 deletions
+34 -38
View File
@@ -2,10 +2,17 @@
set -euo pipefail
cd "$(dirname "$0")"
echo "[1/7] 检查 Raspberry Pi I2C..."
for required in app/pigway_pi_control.py app/oled_font_5x7.bin config/pigway-pi-control.conf systemd/pigway-pi-control.service; do
if [ ! -f "$required" ]; then
echo "ERROR: required file missing: $required" >&2
exit 1
fi
done
echo "[1/8] 检查 Raspberry Pi I2C..."
[ -e /dev/i2c-1 ] || { echo "错误:/dev/i2c-1 不存在。先 sudo raspi-config -> Interface Options -> I2C -> Enable,然后重启。"; exit 1; }
echo "[2/7] 等待 APT/DPKG..."
echo "[2/8] 等待 APT/DPKG..."
for i in $(seq 1 60); do
if ! fuser /var/lib/dpkg/lock-frontend /var/lib/apt/lists/lock /var/cache/apt/archives/lock >/dev/null 2>&1; then break; fi
[ "$i" = 60 ] && { echo "APT 锁等待超时,请稍后重试。"; exit 1; }
@@ -13,17 +20,17 @@ for i in $(seq 1 60); do
sleep 5
done
echo "[3/7] 安装依赖..."
apt update
apt install -y python3 python3-pil python3-smbus2 i2c-tools
echo "[3/8] 安装依赖..."
apt-get update
apt-get install -y python3 python3-pil python3-smbus2 i2c-tools
echo "[4/7] 检查硬件地址..."
echo "[4/8] 检查硬件地址..."
SCAN="$(i2cdetect -y 1)"; echo "$SCAN"
echo "$SCAN" | grep -qi '3c' || echo "警告:未扫描到 OLED 0x3C"
echo "$SCAN" | grep -qi '0d' || echo "警告:未扫描到控制 MCU 0x0D"
echo "[5/7] 安装程序与配置..."
if systemctl list-unit-files 2>/dev/null | grep -q '^temp-control.service'; then
echo "[5/8] 安装程序与配置..."
if systemctl list-unit-files 2>/dev/null | grep '^temp-control.service' >/dev/null; then
echo "检测到旧 temp-control.service,正在停止并禁用..."
systemctl disable --now temp-control.service 2>/dev/null || true
fi
@@ -33,47 +40,36 @@ install -m 0644 app/oled_font_5x7.bin /usr/local/share/pigway-pi-control/oled_fo
install -m 0755 app/pigway_pi_control.py /usr/local/sbin/pigway-pi-control
NEW=/tmp/pigway-pi-control.conf.new
cp config/pigway-pi-control.conf "$NEW"
# Keep the complete existing configuration, including comments and custom sections.
# New options use runtime defaults until the user chooses to add them.
if [ -f /etc/pigway-pi-control.conf ]; then
cp /etc/pigway-pi-control.conf "/etc/pigway-pi-control.conf.bak.$(date +%Y%m%d-%H%M%S)"
# Preserve user's [services] and [processes] sections; adopt new defaults elsewhere.
python3 - "$NEW" /etc/pigway-pi-control.conf <<'PY'
import configparser,sys
newf,oldf=sys.argv[1:]
n=configparser.ConfigParser(); n.optionxform=str; n.read(newf)
o=configparser.ConfigParser(); o.optionxform=str; o.read(oldf)
for sec in ("services","processes"):
if o.has_section(sec):
if n.has_section(sec): n.remove_section(sec)
n.add_section(sec)
for k,v in o.items(sec): n.set(sec,k,v)
with open(newf,"w") as f:n.write(f)
PY
else
install -m 0644 config/pigway-pi-control.conf /etc/pigway-pi-control.conf
fi
install -m 0644 "$NEW" /etc/pigway-pi-control.conf
rm -f "$NEW"
install -m 0644 systemd/pigway-pi-control.service /etc/systemd/system/pigway-pi-control.service
echo "[6/7] 启用/重启服务..."
echo "[6/8] 启用/重启服务..."
systemctl daemon-reload
systemctl enable pigway-pi-control.service >/dev/null
systemctl restart pigway-pi-control.service
echo "[7/8] 安装后自检..."
FAIL=0
check() { if eval "$2"; then printf " %-30s PASS\n" "$1"; else printf " %-30s FAIL\n" "$1"; FAIL=1; fi; }
check "Version 3.3.0" "grep -q 'APP_VERSION=\"3.3.0\"' /usr/local/sbin/pigway-pi-control"
check "OLED physical 128x32" "grep -q 'W,H=128,32' /usr/local/sbin/pigway-pi-control"
check "OLED MUX 0x1F" "grep -q '0xA8,0x1F' /usr/local/sbin/pigway-pi-control"
check "OLED pages 0..3" "grep -q 'cmd(0x22);cmd(0);cmd(3)' /usr/local/sbin/pigway-pi-control"
check "UI CPU/TMP/DSK" "grep -q 'f\"TMP {temp:.1f}C\"' /usr/local/sbin/pigway-pi-control"
check "UI IP4/IP6" "grep -q 'ip_label+" "+ip' /usr/local/sbin/pigway-pi-control"
check "UI right column x=72" "grep -q \"draw_text_5x7(pix,72,y,right\\[row\\])\" /usr/local/sbin/pigway-pi-control"
check "Legacy service stopped" "! systemctl is-active --quiet temp-control.service"
check "Service running" "systemctl is-active --quiet pigway-pi-control.service"
check "P0 health monitor" "grep -q 'throttled_state' /usr/local/sbin/pigway-pi-control"
check "Diagnostic command" "grep -q -- '--diagnose' /usr/local/sbin/pigway-pi-control"
check() { local label="$1"; shift; if "$@"; then printf " %-30s PASS\n" "$label"; else printf " %-30s FAIL\n" "$label"; FAIL=1; fi; }
APP=/usr/local/sbin/pigway-pi-control
check "Version 3.3.0" grep -Fq 'APP_VERSION="3.3.0"' "$APP"
check "OLED physical 128x32" grep -Fq 'W,H=128,32' "$APP"
check "OLED MUX 0x1F" grep -Fq '0xA8,0x1F' "$APP"
check "OLED pages 0..3" grep -Fq 'cmd(0x22);cmd(0);cmd(3)' "$APP"
check "UI CPU/TMP/DSK" grep -Fq 'f"TMP {temp:.1f}C"' "$APP"
check "UI IP4/IP6" grep -Fq 'ip_label' "$APP"
check "UI right column x=72" grep -Fq 'draw_text_5x7(pix,72,y,right[row])' "$APP"
legacy_stopped() { ! systemctl is-active --quiet temp-control.service; }
check "Legacy service stopped" legacy_stopped
check "Service running" systemctl is-active --quiet pigway-pi-control.service
check "P0 health monitor" grep -Fq 'throttled_state' "$APP"
check "Diagnostic command" grep -Fq -- '--diagnose' "$APP"
if [ "$FAIL" -ne 0 ]; then echo "错误:安装后自检未全部通过。"; exit 1; fi
echo "[8/8] 状态..."