#!/data/data/com.termux/files/usr/bin/env bash # =============================================== # Termux Battery Daemon + On-demand TUI # Features: Background daemon, trend graph, degradation alerts, # Smart charging advice, and notifications. # =============================================== PID_FILE="$PREFIX/tmp/battery.pid" LOG_FILE="$PREFIX/tmp/battery.log" HISTORY_FILE="$PREFIX/tmp/battery_history.csv" CHECK_INTERVAL=60 MAX_TEMP=45 LOW_BATTERY=20 HIGH_DRAIN=800000 mkdir -p "$PREFIX/tmp" # ANSI colors RED="\033[1;31m" GREEN="\033[1;32m" CYAN="\033[1;36m" MAGENTA="\033[1;35m" YELLOW="\033[1;33m" RESET="\033[0m" notify() { termux-notification --title "$1" --content "$2" } get_battery() { termux-battery-status } log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE" } # =============================================== # Analyze battery and create alerts # =============================================== analyze() { local health="$1" local temp="$2" local percent="$3" local status="$4" local current="$5" local alerts=() # 1. Health alert [[ "$health" != "GOOD" ]] && alerts+=("Health=$health") # 2. Temperature alert (( $(echo "$temp >= $MAX_TEMP" | bc -l) )) && alerts+=("Temp=${temp}C") # 3. Low battery alert (( percent <= LOW_BATTERY )) && alerts+=("Low=${percent}%") # 4. High current / drain alert if [[ "$status" == "DISCHARGING" ]] && (( current >= HIGH_DRAIN )); then alerts+=("High Drain=$(echo "$current/1000000" | bc)A") fi # 5. Degradation alert if [ -f "$HISTORY_FILE" ]; then last_percent=$(tail -n1 "$HISTORY_FILE" | awk -F, '{print $2}') if [ -n "$last_percent" ]; then degradation=$(echo "$last_percent - $percent" | bc) if (( $(echo "$degradation > 10" | bc -l) )); then alerts+=("Battery degrading") fi fi fi echo "${alerts[@]}" } # =============================================== # Battery trend graph # =============================================== battery_graph() { local history=("$@") local bar="" for p in "${history[@]}"; do if (( p > 75 )); then bar+="${GREEN}#" ; elif (( p > 50 )); then bar+="${CYAN}#" ; elif (( p > 25 )); then bar+="${YELLOW}-" ; else bar+="${RED}-" ; fi done echo -e "$bar${RESET}" } # =============================================== # Smart advice # =============================================== battery_advice() { local percent="$1" local health="$2" if [[ "$health" != "GOOD" ]]; then echo "Check battery health!" elif (( percent < 40 )); then echo "Charge now" elif (( percent > 80 )); then echo "Unplug" else echo "Optimal" fi } # =============================================== # Draw TUI dashboard # =============================================== draw_status() { local status="$1" local health="$2" local percent="$3" local temp="$4" local volt="$5" local current="$6" local uptime="$7" local trend="$8" local alerts="$9" tput civis tput clear echo -e "${CYAN}============== Termux Battery Dashboard ==============${RESET}" echo -e "${MAGENTA}Status :${RESET} $status" echo -e "${MAGENTA}Health :${RESET} $health" echo -e "${MAGENTA}Level :${RESET} $percent%" echo -e "${MAGENTA}Temperature :${RESET} ${temp}C" echo -e "${MAGENTA}Voltage :${RESET} ${volt} mV" echo -e "${MAGENTA}Current :${RESET} $(echo "$current/1000000" | bc) A" echo -e "${MAGENTA}Device Uptime:${RESET} $uptime" echo -e "${MAGENTA}Battery Trend:${RESET} $trend" echo -e "${MAGENTA}Recommended :${RESET} $(battery_advice $percent $health)" [[ -n "$alerts" ]] && echo -e "${RED}ALERTS: $alerts${RESET}" || echo -e "${GREEN}Battery OK${RESET}" echo -e "${CYAN}=======================================================${RESET}" tput cnorm } # =============================================== # Daemon: Background collection # =============================================== daemon() { notify "Battery Daemon" "Started" log "Daemon started" battery_history=() [ ! -f "$HISTORY_FILE" ] && echo "timestamp,percent,voltage,temperature,current" > "$HISTORY_FILE" while true; do data=$(get_battery) health=$(echo "$data" | jq -r '.health') temp=$(echo "$data" | jq -r '.temperature') percent=$(echo "$data" | jq -r '.percentage') status_b=$(echo "$data" | jq -r '.status') volt=$(echo "$data" | jq -r '.voltage') current=$(echo "$data" | jq -r '.current') echo "$(date +%s),$percent,$volt,$temp,$current" >> "$HISTORY_FILE" battery_history+=("$percent") [ "${#battery_history[@]}" -gt 20 ] && battery_history=("${battery_history[@]: -20}") alerts=$(analyze "$health" "$temp" "$percent" "$status_b" "$current") uptime_sec=0 [ -f "$PID_FILE" ] && uptime_sec=$(( $(date +%s) - $(stat -c %Y "$PID_FILE") )) uptime_str=$(printf '%dh %dm %ds' $((uptime_sec/3600)) $((uptime_sec%3600/60)) $((uptime_sec%60))) trend=$(battery_graph "${battery_history[@]}") [[ -n "$alerts" ]] && log "$alerts" && notify "Battery Alert" "$alerts" || log "OK $percent% $temp C" sleep "$CHECK_INTERVAL" done } # =============================================== # CLI Commands # =============================================== start() { if [ -f "$PID_FILE" ]; then echo "Battery daemon already running" exit 1 fi daemon & # run in background echo $! > "$PID_FILE" echo "Battery daemon started in background" } stop() { if [ ! -f "$PID_FILE" ]; then echo "Battery daemon not running" exit 1 fi kill $(cat "$PID_FILE") 2>/dev/null rm "$PID_FILE" echo "Battery daemon stopped" } status() { if [ ! -f "$HISTORY_FILE" ]; then echo "No battery data collected yet. Start the daemon first." exit 1 fi mapfile -t history < <(tail -n20 "$HISTORY_FILE" | awk -F, '{print $2}') last_entry=$(tail -n1 "$HISTORY_FILE") percent=$(echo "$last_entry" | awk -F, '{print $2}') volt=$(echo "$last_entry" | awk -F, '{print $3}') temp=$(echo "$last_entry" | awk -F, '{print $4}') current=$(echo "$last_entry" | awk -F, '{print $5}') status_b="UNKNOWN" health="GOOD" uptime_sec=0 [ -f "$PID_FILE" ] && uptime_sec=$(( $(date +%s) - $(stat -c %Y "$PID_FILE") )) uptime_str=$(printf '%dh %dm %ds' $((uptime_sec/3600)) $((uptime_sec%3600/60)) $((uptime_sec%60))) alerts=$(analyze "$health" "$temp" "$percent" "$status_b" "$current") trend=$(battery_graph "${history[@]}") draw_status "$status_b" "$health" "$percent" "$temp" "$volt" "$current" "$uptime_str" "$trend" "$alerts" } logs() { tail -f "$LOG_FILE" } case "$1" in start) start ;; stop) stop ;; status) status ;; logs) logs ;; *) echo "Usage: battery start|stop|status|logs" ;; esac