Sorry to bunch all of this into one commit! Lots of changes trying to debug, better logging, etc. Will revert some of it.

This commit is contained in:
Theodore Murphy
2024-03-28 12:10:16 +11:00
parent 5a028d0d35
commit 51a797ad2f

View File

@@ -118,11 +118,12 @@ function log() {
}
function validate_percentage() {
if ! [[ "$1" =~ ^[0-9]+$ ]] || [[ "$1" -lt 0 ]] || [[ "$1" -gt 100 ]]; then
echo false
else
echo true
fi
log "Validating $1"
if ! [[ "$1" =~ ^[0-9]+$ ]] || [[ "$1" -lt 0 ]] || [[ "$1" -gt 100 ]]; then
echo false
else
echo true
fi
}
## #################
@@ -132,6 +133,7 @@ function validate_percentage() {
# Change magsafe color
# see community sleuthing: https://github.com/actuallymentor/battery/issues/71
function change_magsafe_led_color() {
log "MagSafe LED function invoked"
color=$1
# Check whether user can run color changes without password (required for backwards compatibility)
@@ -143,11 +145,14 @@ function change_magsafe_led_color() {
fi
if [[ "$color" == "green" ]]; then
log "setting LED to green"
sudo smc -k ACLC -w 03
elif [[ "$color" == "orange" ]]; then
log "setting LED to orange"
sudo smc -k ACLC -w 04
else
# Default action: reset. Value 00 is a guess and needs confirmation
log "resetting LED"
sudo smc -k ACLC -w 00
fi
}
@@ -166,24 +171,26 @@ function disable_discharging() {
# Keep track of status
is_charging=$(get_smc_charging_status)
if ! $(validate_percentage "$setting"); then
if ! validate_percentage "$setting"; then
log "No valid maintain percentage set, charging freely"
# use direct commands since enable_charging also calls disable_discharging, and causing an eternal loop
log "Disabling discharging: No valid maintain percentage set, enabling charging"
# use direct commands since enable_charging also calls disable_discharging, and causes an eternal loop
sudo smc -k CH0B -w 00
sudo smc -k CH0C -w 00
change_magsafe_led_color "orange"
elif [[ "$battery_percentage" -ge "$setting" && "$is_charging" == "enabled" ]]; then
log "Charge above $setting"
log "Disabling discharging: Charge above $setting, disabling charging"
disable_charging
change_magsafe_led_color "green"
elif [[ "$battery_percentage" -lt "$setting" && "$is_charging" == "disabled" ]]; then
log "Charge below $setting"
enable_charging
log "Disabling discharging: Charge below $setting, enabling charging"
# use direct commands since enable_charging also calls disable_discharging, and causes an eternal loop
sudo smc -k CH0B -w 00
sudo smc -k CH0C -w 00
change_magsafe_led_color "orange"
fi
@@ -362,6 +369,9 @@ if [[ "$action" == "charging" ]]; then
enable_charging
elif [[ "$setting" == "off" ]]; then
disable_charging
else
log "Error: $setting is not \"on\" or \"off\"."
exit 1
fi
exit 0
@@ -381,6 +391,9 @@ if [[ "$action" == "adapter" ]]; then
disable_discharging
elif [[ "$setting" == "off" ]]; then
enable_discharging
else
log "Error: $setting is not \"on\" or \"off\"."
exit 1
fi
exit 0
@@ -390,17 +403,11 @@ fi
# Charging on/off controller
if [[ "$action" == "charge" ]]; then
if ! $(validate_percentage "$setting"); then
log "Error: $setting is not a valid setting for battery maintain. Please use a number between 0 and 100"
if ! validate_percentage "$setting"; then
log "Error: $setting is not a valid setting for battery charge. Please use a number between 0 and 100"
exit 1
fi
# Check if percentage is an integer [1-100]
if ! [[ $setting =~ ^[1-9][0-9]?$|^100$ ]]; then
log "Specified percentage ($setting) is not valid. Please specify an integer [1-100]."
exit 1
fi
# Disable running daemon
battery maintain stop
@@ -410,14 +417,16 @@ if [[ "$action" == "charge" ]]; then
# Start charging
battery_percentage=$(get_battery_percentage)
log "Charging to $setting% from $battery_percentage%"
enable_charging
enable_charging # also disables discharging
# Loop until battery percent is exceeded
while [[ "$battery_percentage" -lt "$setting" ]]; do
log "Battery at $battery_percentage%"
caffeinate -is sleep 60
battery_percentage=$(get_battery_percentage)
if [[ "$battery_percentage" -ge "$((setting-3))" ]]; then
sleep 20
else
caffeinate -is sleep 60
fi
done
@@ -431,8 +440,8 @@ fi
# Discharging on/off controller
if [[ "$action" == "discharge" ]]; then
if ! $(validate_percentage "$setting"); then
log "Error: $setting is not a valid setting for battery maintain. Please use a number between 0 and 100"
if ! validate_percentage "$setting"; then
log "Error: $setting is not a valid setting for battery discharge. Please use a number between 0 and 100"
exit 1
fi
@@ -458,7 +467,7 @@ fi
# Maintain at level
if [[ "$action" == "maintain_synchronous" ]]; then
if ! $(validate_percentage "$setting"); then
if ! validate_percentage "$setting"; then
log "Error: $setting is not a valid setting for battery maintain. Please use a number between 0 and 100"
exit 1
fi
@@ -538,13 +547,12 @@ if [[ "$action" == "maintain" ]]; then
rm $pidfile 2>/dev/null
battery disable_daemon
enable_charging
change_magsafe_led_color
battery status
exit 0
fi
# Check if setting is value between 0 and 100
if ! $(validate_percentage "$setting"); then
if ! validate_percentage "$setting"; then
log "Called with $setting $action"
# If non 0-100 setting is not a special keyword, exit with an error.
@@ -688,4 +696,4 @@ if [[ "$action" == "logs" ]]; then
exit 0
fi
fi