# 函数:发送 Telegram 通知 send_telegram_notification() { if [ "$TELEGRAM_ENABLED" = true ]; then local message="$1"; local proxy_option="" if [ "$PROXY_ENABLED" = true ]; then proxy_option="-x ${PROXY_URL}"; fi /usr/bin/curl -s -o /dev/null ${proxy_option} --data-urlencode "chat_id=${CHAT_ID}" --data-urlencode "text=${message}""https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendMessage" fi }
# 函数:清理旧日志 cleanup_log() { if [ -f "$LOG_FILE" ]; then tail -n "$MAX_LOG_LINES""$LOG_FILE" > "${LOG_FILE}.tmp" && mv"${LOG_FILE}.tmp""$LOG_FILE" fi }
# 函数:获取插座状态 ("on" / "off" / "error") get_plug_status() { local status_output; status_output=$("$MIIOCLI_PATH" genericmiot --ip "$PLUG_IP" --token "$PLUG_TOKEN" status 2>/dev/null) ifecho"$status_output" | grep -q "Switch Status (switch:on, access: RW): True"; thenecho"on"; elifecho"$status_output" | grep -q "Switch Status (switch:on, access: RW): False"; thenecho"off"; elseecho"error"; fi }
# 函数:控制插座开关 set_plug_state() { local action="$1"; local value="" if [ "$action" = "on" ]; then value="True"; else value="False"; fi "$MIIOCLI_PATH" genericmiot --ip "$PLUG_IP" --token "$PLUG_TOKEN" set_property_by 2 1 "$value" bool >/dev/null 2>&1 }
# 函数:发送周期性状态报告 send_periodic_status_update() { local health_path="/sys/class/power_supply/pmi8998-charger/health" local temp_path="/sys/class/power_supply/bq27411-0/temp" local voltage_path="/sys/class/power_supply/bq27411-0/voltage_now" local current_path="/sys/class/power_supply/bq27411-0/current_now" local capacity=$(cat"$CAPACITY_PATH"); local health=$(cat"$health_path"); local temp_raw=$(cat"$temp_path"); local temp_c=$(echo"$temp_raw" | awk '{printf "%.1f", $1/10}'); local voltage_uv=$(cat"$voltage_path"); local voltage_mv=$(($voltage_uv / 1000)); local current_ua=$(cat"$current_path"); local current_ma=$(($current_ua / 1000)); local current_status_text=""; if [ "$current_ma" -gt 0 ]; then current_status_text="⚡️ 正在充电"; elif [ "$current_ma" -lt 0 ]; then current_status_text=" discharging "; else current_status_text="~ 待机"; fi local plug_state_text=$(get_plug_status); if [ "$plug_state_text" = "on" ]; then plug_state_text="开启"; elif [ "$plug_state_text" = "off" ]; then plug_state_text="关闭"; else plug_state_text="状态未知"; fi local message="🕒 周期健康报告: - 电池电量: ${capacity}% - 电池健康: ${health} - 电池温度: ${temp_c}°C - 实时电压: ${voltage_mv}mV - 实时电流: ${current_ma}mA (${current_status_text}) - 智能插座: ${plug_state_text}" log_message "发送周期性健康报告。"; send_telegram_notification "$message" }
whiletrue; do capacity=$(cat"$CAPACITY_PATH"); current_plug_status=$(get_plug_status) if [ "$current_plug_status" = "error" ]; then log_message "错误:无法获取智能插座状态,1分钟后重试。"; sleep 60; continue; fi if [ "$capacity" -ge "$STOP_CHARGE_THRESHOLD" ]; then if [ "$current_plug_status" = "on" ]; then set_plug_state "off" if [ "$last_notified_action" = "charging" ]; then message="✅ 电量已达 ${capacity}%,已自动关闭智能插座。"; log_message "$message"; send_telegram_notification "$message"; last_notified_action="stopped" else message="🚨 守护警报:电量高于 ${STOP_CHARGE_THRESHOLD}%,但检测到插座意外开启,已强制关闭!"; log_message "$message"; send_telegram_notification "$message" fi fi elif [ "$capacity" -le "$START_CHARGE_THRESHOLD" ]; then if [ "$current_plug_status" = "off" ]; then set_plug_state "on" if [ "$last_notified_action" = "stopped" ]; then message="⚠️ 电量已低至 ${capacity}%,已自动打开智能插座。"; log_message "$message"; send_telegram_notification "$message"; last_notified_action="charging" else message="🚨 守护警报:电量低于 ${START_CHARGE_THRESHOLD}%,但检测到插座意外关闭,已强制开启!"; log_message "$message"; send_telegram_notification "$message" fi fi fi log_cleanup_count=$((log_cleanup_count+1)); periodic_report_count=$((periodic_report_count+1)) if [ "$log_cleanup_count" -ge 60 ]; then cleanup_log; log_cleanup_count=0; fi if [ "$periodic_report_count" -ge 120 ]; then send_periodic_status_update; periodic_report_count=0; fi sleep 60 done