bugfix and refactoring

This commit is contained in:
Rene
2024-06-30 10:02:57 +02:00
parent affb7f029e
commit 3ad9113bdd

View File

@@ -79,7 +79,7 @@ Usage:
manually set the adapter to (not) charge even when plugged in manually set the adapter to (not) charge even when plugged in
eg: battery adapter off eg: battery adapter off
battery calibrate battery calibrate
calibrate the battery by discharging it to 15%, then recharging it to 100%, and keeping it there for 1 hour calibrate the battery by discharging it to 15%, then recharging it to 100%, and keeping it there for 1 hour
battery charge LEVEL[1-100] battery charge LEVEL[1-100]
@@ -143,6 +143,13 @@ function valid_percentage() {
fi fi
} }
function valid_voltage() {
if [[ "$1" =~ ^[0-9]+(\.[0-9]+)?V$ ]]; then
return 0
fi
return 1
}
## ################# ## #################
## SMC Manipulation ## SMC Manipulation
## ################# ## #################
@@ -462,7 +469,7 @@ if [[ "$action" == "charge" ]]; then
if [[ "$battery_percentage" -ge "$((setting - 3))" ]]; then if [[ "$battery_percentage" -ge "$((setting - 3))" ]]; then
sleep 20 sleep 20
else else
caffeinate -is sleep 60 caffeinate -is sleep 60
fi fi
done done
@@ -558,7 +565,7 @@ if [[ "$action" == "maintain_synchronous" ]]; then
log "Charge above $setting" log "Charge above $setting"
if [[ "$is_charging" == "enabled" ]]; then if [[ "$is_charging" == "enabled" ]]; then
disable_charging disable_charging
fi fi
change_magsafe_led_color "green" change_magsafe_led_color "green"
@@ -583,22 +590,22 @@ fi
# Maintain at voltage # Maintain at voltage
if [[ "$action" == "maintain_voltage_synchronous" ]]; then if [[ "$action" == "maintain_voltage_synchronous" ]]; then
# Recover old maintain status if old setting is found # Recover old maintain status if old setting is found
if [[ "$setting" == "recover" ]]; then if [[ "$setting" == "recover" ]]; then
# Before doing anything, log out environment details as a debugging trail # Before doing anything, log out environment details as a debugging trail
log "Debug trail. User: $USER, config folder: $configfolder, logfile: $logfile, file called with 1: $1, 2: $2" log "Debug trail. User: $USER, config folder: $configfolder, logfile: $logfile, file called with 1: $1, 2: $2"
maintain_voltage=$(cat $maintain_voltage_tracker_file 2>/dev/null) maintain_voltage=$(cat $maintain_voltage_tracker_file 2>/dev/null)
if [[ $maintain_voltage ]]; then if [[ $maintain_voltage ]]; then
log "Recovering maintenance voltage $maintain_voltage" log "Recovering maintenance voltage $maintain_voltage"
setting=$(echo $maintain_voltage | awk '{print $1}') setting=$(echo $maintain_voltage | awk '{print $1}')
subsetting=$(echo $maintain_voltage | awk '{print $2}') subsetting=$(echo $maintain_voltage | awk '{print $2}')
else else
log "No setting to recover, exiting" log "No setting to recover, exiting"
exit 0 exit 0
fi fi
fi fi
voltage=$(get_voltage) voltage=$(get_voltage)
lower_voltage=$(echo "$setting - $subsetting" | bc -l) lower_voltage=$(echo "$setting - $subsetting" | bc -l)
@@ -610,11 +617,11 @@ if [[ "$action" == "maintain_voltage_synchronous" ]]; then
is_charging=$(get_smc_charging_status) is_charging=$(get_smc_charging_status)
if (($(echo "$voltage < $lower_voltage" | bc -l))) && [[ "$is_charging" == "disabled" ]]; then if (($(echo "$voltage < $lower_voltage" | bc -l))) && [[ "$is_charging" == "disabled" ]]; then
log "Battery at ${voltage}V" log "Battery at ${voltage}V"
enable_charging enable_charging
fi fi
if (($(echo "$voltage >= $upper_voltage" | bc -l))) && [[ "$is_charging" == "enabled" ]]; then if (($(echo "$voltage >= $upper_voltage" | bc -l))) && [[ "$is_charging" == "enabled" ]]; then
log "Battery at ${voltage}V" log "Battery at ${voltage}V"
disable_charging disable_charging
fi fi
@@ -651,31 +658,30 @@ if [[ "$action" == "maintain" ]]; then
exit 0 exit 0
fi fi
# Check if setting is a voltage # Check if setting is a voltage
is_voltage=false is_voltage=false
if [[ "$setting" =~ ^[0-9]+(\.[0-9]+)?V$ ]]; then if valid_voltage "$setting"; then
setting="${setting//V/}" setting="${setting//V/}"
if [[ "$subsetting" =~ ^[0-9]+(\.[0-9]+)?V$ ]]; then if valid_voltage "$subsetting"; then
subsetting="${subsetting//V/}" subsetting="${subsetting//V/}"
else else
subsetting="0.1" subsetting="0.1"
fi fi
if (($(echo "$setting < $voltage_min" | bc -l) || $(echo "$setting > $voltage_max" | bc -l))); then if (($(echo "$setting < $voltage_min" | bc -l) || $(echo "$setting > $voltage_max" | bc -l))); then
log "Error: ${setting}V is not a valid setting. Please use a value between ${voltage_min}V and ${voltage_max}V" log "Error: ${setting}V is not a valid setting. Please use a value between ${voltage_min}V and ${voltage_max}V"
exit 1 exit 1
fi fi
if (($(echo "$subsetting < $voltage_hyst_min" | bc -l) || $(echo "$subsetting > $voltage_max" | bc -l))); then if (($(echo "$subsetting < $voltage_hyst_min" | bc -l) || $(echo "$subsetting > $voltage_max" | bc -l))); then
log "Error: ${subsetting}V is not a valid setting. Please use a value between ${voltage_hyst_min}V and ${voltage_hyst_max}V" log "Error: ${subsetting}V is not a valid setting. Please use a value between ${voltage_hyst_min}V and ${voltage_hyst_max}V"
exit 1 exit 1
fi fi
is_voltage=true is_voltage=true
fi
# Check if setting is value between 0 and 100 # Check if setting is value between 0 and 100
if ! valid_percentage "$setting"; then elif ! valid_percentage "$setting"; then
log "Called with $setting $action" log "Called with $setting $action"
# If non 0-100 setting is not a special keyword, exit with an error. # If non 0-100 setting is not a special keyword, exit with an error.
if ! { [[ "$setting" == "stop" ]] || [[ "$setting" == "recover" ]]; }; then if ! { [[ "$setting" == "stop" ]] || [[ "$setting" == "recover" ]]; }; then
@@ -686,14 +692,12 @@ if [[ "$action" == "maintain" ]]; then
fi fi
# Start maintenance script # Start maintenance script
log "Starting battery maintenance at $setting% $subsetting"
nohup $battery_binary maintain_synchronous $setting $subsetting >>$logfile &
if [ "$is_voltage" = true ]; then if [ "$is_voltage" = true ]; then
log "Starting battery maintenance at ${setting}V ±${subsetting}V" log "Starting battery maintenance at ${setting}V ±${subsetting}V"
nohup battery maintain_voltage_synchronous $setting $subsetting >>$logfile & nohup $battery_binary maintain_voltage_synchronous $setting $subsetting >>$logfile &
else else
log "Starting battery maintenance at $setting% $subsetting" log "Starting battery maintenance at $setting% $subsetting"
nohup battery maintain_synchronous $setting $subsetting >>$logfile & nohup $battery_binary maintain_synchronous $setting $subsetting >>$logfile &
fi fi
# Store pid of maintenance process and setting # Store pid of maintenance process and setting
@@ -704,16 +708,16 @@ if [[ "$action" == "maintain" ]]; then
rm "$maintain_percentage_tracker_file" "$maintain_voltage_tracker_file" 2>/dev/null rm "$maintain_percentage_tracker_file" "$maintain_voltage_tracker_file" 2>/dev/null
if [[ "$is_voltage" = true ]]; then if [[ "$is_voltage" = true ]]; then
log "Writing new setting $setting $subsetting to $maintain_voltage_tracker_file" log "Writing new setting $setting $subsetting to $maintain_voltage_tracker_file"
echo "$setting $subsetting" >$maintain_voltage_tracker_file echo "$setting $subsetting" >$maintain_voltage_tracker_file
log "Maintaining battery at ${setting}V ±${subsetting}V" log "Maintaining battery at ${setting}V ±${subsetting}V"
else else
log "Writing new setting $setting to $maintain_percentage_tracker_file" log "Writing new setting $setting to $maintain_percentage_tracker_file"
echo $setting >$maintain_percentage_tracker_file echo $setting >$maintain_percentage_tracker_file
log "Maintaining battery at $setting%" log "Maintaining battery at $setting%"
fi fi
fi fi
@@ -789,10 +793,10 @@ if [[ "$action" == "status" ]]; then
if test -f $pidfile; then if test -f $pidfile; then
maintain_percentage=$(cat $maintain_percentage_tracker_file 2>/dev/null) maintain_percentage=$(cat $maintain_percentage_tracker_file 2>/dev/null)
if [[ $maintain_percentage ]]; then if [[ $maintain_percentage ]]; then
maintain_level="$maintain_percentage%" maintain_level="$maintain_percentage%"
else else
maintain_level=$(cat $maintain_voltage_tracker_file 2>/dev/null) maintain_level=$(cat $maintain_voltage_tracker_file 2>/dev/null)
maintain_level=$(echo "$maintain_level" | awk '{print $1 "V ±" $2 "V"}') maintain_level=$(echo "$maintain_level" | awk '{print $1 "V ±" $2 "V"}')
fi fi
log "Your battery is currently being maintained at $maintain_level" log "Your battery is currently being maintained at $maintain_level"
fi fi
@@ -810,10 +814,10 @@ fi
# launchd daemon creator, inspiration: https://www.launchd.info/ # launchd daemon creator, inspiration: https://www.launchd.info/
if [[ "$action" == "create_daemon" ]]; then if [[ "$action" == "create_daemon" ]]; then
call_action="maintain_synchronous" call_action="maintain_synchronous"
if test -f "$maintain_voltage_tracker_file"; then if test -f "$maintain_voltage_tracker_file"; then
call_action="maintain_voltage_synchronous" call_action="maintain_voltage_synchronous"
fi fi
daemon_definition=" daemon_definition="
<?xml version=\"1.0\" encoding=\"UTF-8\"?> <?xml version=\"1.0\" encoding=\"UTF-8\"?>