Skip to content

Commit

Permalink
Rewrite script scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
VR-25 committed Jul 17, 2022
1 parent e6e069f commit ded8b86
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 132 deletions.
51 changes: 10 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ In interactive mode, it also asks the user whether they want to download and ins
```
#DC#
configVerCode=202207160
configVerCode=202207170
capacity=(-1 60 70 75 false false)
Expand Down Expand Up @@ -270,8 +270,6 @@ prioritizeBattIdleMode=false
currentWorkaround=false
battStatusWorkaround=true
schedule=''
battStatusOverride=''
rebootResume=false
Expand All @@ -284,8 +282,6 @@ forceOff=
tempLevel=0
: one-line script sample; echo nothing >/dev/null
# WARNINGS
Expand Down Expand Up @@ -351,10 +347,6 @@ tempLevel=0
# battStatusWorkaround=batt_status_workaround=BOOLEAN
# schedule=sched='HHMM COMMAND...
# HHMM COMMAND...
# ...' NULLABLE
# battStatusOverride=batt_status_override=Idle|Discharging|'code to PRINT value for _status' NULLABLE
# rebootResume=reboot_resume=BOOLEAN
Expand Down Expand Up @@ -411,8 +403,6 @@ tempLevel=0
# cw current_workaround
# bsw batt_status_workaround
# sd sched
# bso batt_status_override
# rr reboot_resume
# dp discharge_polarity
Expand Down Expand Up @@ -554,29 +544,6 @@ tempLevel=0
# On the other hand, the user may observe charging control inconsistencies on devices that report wrong current values or major current fluctuations.
# Oftentimes, charging control issues are related to the power adapter.
# sched (sd) #
# Command/script schedules, in the following format:
#
# sched="HHMM command...
# HHMM command...
# ..."
#
# e.g., 3900 mV at 22:00, and 4100 mV at 6:00, daily:
# sched="2200 acc -s mcv=3900
# 0600 acc -s mcv=4100"
#
# 12 hour format is not supported.
# Each schedule must be on its own line.
# Each line is daemonized.
# This is not limited to acc commands. It can run anything.
#
# Commands:
# -s|--set [sd|sched]="[+-]schedule to add or pattern to delete"
# e.g.,
# acc -s sd=-2050 (delete schedules that match 2050)
# acc -s sd="+2200 acc -s mcv=3900 mcc=500; acc -n "Switched to \"sleep\" profile" (append schedule)
# Note: "acc -s sd=" behaves just like similar commands (restores default value; for schedules, it's null)
# batt_status_override (bso) #
# Overrides the battery status determined by the not_charging function.
# It can be Idle, Discharging (both case sensitive), or logic to PRINT the desired value for the _status variable.
Expand All @@ -602,6 +569,8 @@ tempLevel=0
# All script lines are executed whenever the config is loaded/sourced.
# This happens regularly while the daemon is running, and at least once per command run.
# Warning: all files used in one-line scripts must reside somewhere in /data/adb/, just like acc's own data files.
# One can schedule tasks with the following construct:
# : sleep profile; at 22:00 "acc -s pc=50 mcc=500 mcv=3900; acc -n 'Switched to night profile'"
# off_mid (om) #
# Whether to turn off charging after rebooting or restarting accd, if capacity is within resume_capacity and pause_capacity (default: true).
Expand Down Expand Up @@ -682,6 +651,11 @@ Options
acc -c less
acc -c cat
-c|--config a|d string|regex Append (a) or delete (d) string/pattern to/from config
e.g.,
acc -c a ": sleep profile; at 22:00 acc -s pc=50 mcc=500 mcv=3900" (append a schedule)
acc -c d sleep (remove all lines matching "sleep")
-d|--disable [#%, #s, #m, #h or #mv (optional)] Disable charging
e.g.,
acc -d 70% (do not recharge until capacity <= 70%)
Expand Down Expand Up @@ -762,12 +736,6 @@ Options
acc -s "charging_switch=battery/charging_enabled 1 0" resume_capacity=55 pause_capacity=60
Note: all properties have short aliases for faster typing; run "acc -c cat" to see them
-s|--set [sd|sched]="[+-]schedule to add or pattern to delete"
e.g.,
acc -s sd=-2050 (delete schedules that match 2050)
acc -s sd="+2200 acc -s mcv=3900 mcc=500; acc -n "Switched to \"sleep\" profile" (append schedule)
Note: "acc -s sd=" behaves just like similar commands (restores default value; for schedules, it's null)
-s|--set c|--current [milliamps|-] Set/print/restore_default max charging current (range: 0-9999 Milliamps)
e.g.,
acc -s c (print current limit)
Expand Down Expand Up @@ -886,9 +854,10 @@ Tips
Commands can be chained for extended functionality.
e.g., charge for 30 minutes, pause charging for 6 hours, charge to 85% and restart the daemon
acc -e 30m && acc -d 6h && acc -e 85 && accd
One can take advantage of one-line scripts and the built-in "at" function to schedule profiles (refer back to -c|--config).
Sample profile
acc -s pc=60 rc=55 mcc=500 mcv=3900
acc -s pc=60 mcc=500 mcv=3900
This keeps battery capacity between 55-60%, limits charging current to 500 mA and voltage to 3900 millivolts.
It's great for nighttime and "forever-plugged".
Expand Down
10 changes: 9 additions & 1 deletion install/acc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,15 @@ edit() {
local file="$1"
shift
if [ -n "${1-}" ]; then
IFS="$(printf ' \t\n')" eval "$* $file"
if [ "$1" = a ]; then
shift
echo >> $file
echo "$@" >> $file
elif [ "$1" = d ]; then
sed -Ei "\#$2#d" $file
else
IFS="$(printf ' \t\n')" eval "$* $file"
fi
else
! ${verbose:-true} || {
case $file in
Expand Down
5 changes: 4 additions & 1 deletion install/acca.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#!/system/bin/sh
# acca: acc for front-ends (faster and more efficient than acc)
# Copyright 2020, VR25
# Copyright 2020-2022, VR25
# License: GPLv3+


at() { :; }


daemon_ctrl() {
case "${1-}" in
start|restart)
Expand Down
15 changes: 1 addition & 14 deletions install/accd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,7 @@ if ! $init; then
capacitySync=false
fi

# schedules and custom loops
(set +eu

schedTime=$(date +%H%M)
schedScript=$TMPDIR/.${schedTime}_schedScript.sh
if [ ! -f $schedScript ] && echo "${schedules-}" | grep "$schedTime " >/dev/null; then
echo "$schedules" | sed -n "s|^$schedTime |#!/system/bin/sh\n|p" > $schedScript
echo "sleep 60; rm $schedScript; exit" >> $schedScript
chmod +x $schedScript
start-stop-daemon -bx $schedScript -S --
fi

set +x
eval '${loopCmd-}') || :
(set +eu; eval '${loopCmd-}') || :

# shutdown if battery temp >= shutdown_temp
[ $(cat $temp) -lt $(( ${temperature[3]} * 10 )) ] || shutdown
Expand Down
37 changes: 3 additions & 34 deletions install/default-config.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
configVerCode=202207160
configVerCode=202207170

capacity=(-1 60 70 75 false false)

Expand Down Expand Up @@ -31,8 +31,6 @@ prioritizeBattIdleMode=false
currentWorkaround=false
battStatusWorkaround=true

schedule=''

battStatusOverride=''

rebootResume=false
Expand All @@ -45,8 +43,6 @@ forceOff=

tempLevel=0

: one-line script sample; echo nothing >/dev/null


# WARNINGS

Expand Down Expand Up @@ -112,10 +108,6 @@ tempLevel=0

# battStatusWorkaround=batt_status_workaround=BOOLEAN

# schedule=sched='HHMM COMMAND...
# HHMM COMMAND...
# ...' NULLABLE

# battStatusOverride=batt_status_override=Idle|Discharging|'code to PRINT value for _status' NULLABLE

# rebootResume=reboot_resume=BOOLEAN
Expand Down Expand Up @@ -172,8 +164,6 @@ tempLevel=0
# cw current_workaround
# bsw batt_status_workaround

# sd sched

# bso batt_status_override
# rr reboot_resume
# dp discharge_polarity
Expand Down Expand Up @@ -315,29 +305,6 @@ tempLevel=0
# On the other hand, the user may observe charging control inconsistencies on devices that report wrong current values or major current fluctuations.
# Oftentimes, charging control issues are related to the power adapter.

# sched (sd) #
# Command/script schedules, in the following format:
#
# sched="HHMM command...
# HHMM command...
# ..."
#
# e.g., 3900 mV at 22:00, and 4100 mV at 6:00, daily:
# sched="2200 acc -s mcv=3900
# 0600 acc -s mcv=4100"
#
# 12 hour format is not supported.
# Each schedule must be on its own line.
# Each line is daemonized.
# This is not limited to acc commands. It can run anything.
#
# Commands:
# -s|--set [sd|sched]="[+-]schedule to add or pattern to delete"
# e.g.,
# acc -s sd=-2050 (delete schedules that match 2050)
# acc -s sd="+2200 acc -s mcv=3900 mcc=500; acc -n "Switched to \"sleep\" profile" (append schedule)
# Note: "acc -s sd=" behaves just like similar commands (restores default value; for schedules, it's null)

# batt_status_override (bso) #
# Overrides the battery status determined by the not_charging function.
# It can be Idle, Discharging (both case sensitive), or logic to PRINT the desired value for the _status variable.
Expand All @@ -363,6 +330,8 @@ tempLevel=0
# All script lines are executed whenever the config is loaded/sourced.
# This happens regularly while the daemon is running, and at least once per command run.
# Warning: all files used in one-line scripts must reside somewhere in /data/adb/, just like acc's own data files.
# One can schedule tasks with the following construct:
# : sleep profile; at 22:00 "acc -s pc=50 mcc=500 mcv=3900; acc -n 'Switched to night profile'"

# off_mid (om) #
# Whether to turn off charging after rebooting or restarting accd, if capacity is within resume_capacity and pause_capacity (default: true).
Expand Down
18 changes: 18 additions & 0 deletions install/misc-functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ apply_on_plug() {
}


at() {
local one=$1
local time=$(date +%H:%M)
local file=$TMPDIR/schedules/$one
shift
if [ ! -f $file ] && [ ${one#0} = ${time#0} ]; then
mkdir -p ${file%/*}
echo "#!/system/bin/sh
sleep 60
rm $file
exit" > $file
chmod u+x $file
start-stop-daemon -bx $file -S --
eval "$@"
fi
}


calc() {
awk "BEGIN {print $*}" | tr , .
}
Expand Down
3 changes: 2 additions & 1 deletion install/oem-custom.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ if (set +x; . $config) > /dev/null 2>&1; then
configVer=0$(_get_prop configVerCode)
defaultConfVer=0$(cat $TMPDIR/.config-ver)
[ $configVer -eq $defaultConfVer ] || {
if [ $configVer -lt 202207160 ]; then
if [ $configVer -lt 202207170 ]; then
rm $dataDir/logs/write.log 2>/dev/null || :
sed -i '/^: one-line script sample/d' $config
$TMPDIR/acca --set force_off=false
else
$TMPDIR/acca --set dummy=
Expand Down
7 changes: 2 additions & 5 deletions install/print-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ prioritize_batt_idle_mode=$prioritizeBattIdleMode
current_workaround=$currentWorkaround
batt_status_workaround=$battStatusWorkaround
sched='$schedule'
batt_status_override='$battStatusOverride'
reboot_resume=$rebootResume
Expand All @@ -57,7 +55,6 @@ off_mid=$offMid
force_off=$forceOff
temp_level=$tempLevel
"
temp_level=$tempLevel"

sed -n '/^: /p' $config
sed -n 's/^: /\n: /p' $config
19 changes: 10 additions & 9 deletions install/strings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ Options
acc -c less
acc -c cat
-c|--config a|d string|regex Append (a) or delete (d) string/pattern to/from config
e.g.,
acc -c a ": sleep profile; at 22:00 acc -s pc=50 mcc=500 mcv=3900" (append a schedule)
acc -c d sleep (remove all lines matching "sleep")
-d|--disable [#%, #s, #m, #h or #mv (optional)] Disable charging
e.g.,
acc -d 70% (do not recharge until capacity <= 70%)
Expand Down Expand Up @@ -203,12 +208,6 @@ Options
acc -s "charging_switch=battery/charging_enabled 1 0" resume_capacity=55 pause_capacity=60
Note: all properties have short aliases for faster typing; run "acc -c cat" to see them
-s|--set [sd|sched]="[+-]schedule to add or pattern to delete"
e.g.,
acc -s sd=-2050 (delete schedules that match 2050)
acc -s sd="+2200 acc -s mcv=3900 mcc=500; acc -n "Switched to \"sleep\" profile" (append schedule)
Note: "acc -s sd=" behaves just like similar commands (restores default value; for schedules, it's null)
-s|--set c|--current [milliamps|-] Set/print/restore_default max charging current (range: 0-9999$(print_mA))
e.g.,
acc -s c (print current limit)
Expand Down Expand Up @@ -327,9 +326,10 @@ Tips
Commands can be chained for extended functionality.
e.g., charge for 30 minutes, pause charging for 6 hours, charge to 85% and restart the daemon
acc -e 30m && acc -d 6h && acc -e 85 && accd
One can take advantage of one-line scripts and the built-in "at" function to schedule profiles (refer back to -c|--config).
Sample profile
acc -s pc=60 rc=55 mcc=500 mcv=3900
acc -s pc=60 mcc=500 mcv=3900
This keeps battery capacity between 55-60%, limits charging current to 500 mA and voltage to 3900 millivolts.
It's great for nighttime and "forever-plugged".
Expand Down Expand Up @@ -523,7 +523,8 @@ print_acct_info() {

print_panic() {
printf "\nWARNING: experimental feature, dragons ahead!
Some problematic control files are blacklisted automatically, based on known patterns.
Some problematic control files are excluded, based on known patterns.
Control files that trigger a reboot are automatically blacklisted.
Do you want to see/edit the list of potential switches before testing?
a: abort operation | n: no | y: yes (default) "
}
Expand All @@ -534,6 +535,6 @@ print_resume() {
Waiting for charging to resume...
If it doesn't happen after a few seconds, try re-plugging the charger.
If it's taking too long, unplug the charger, stop the test with CTRL-C, run accd -i, wait a few seconds, and retest.
In extreme cases, one shall comment out (blacklist) this switch in $dataDir/logs/write.log, reboot, and re-run the test.
In extreme cases, one shall comment out (blacklist) this switch in $dataDir/logs/write.log, reboot (to enable charging), and re-run the test.
##########"
}
Loading

0 comments on commit ded8b86

Please sign in to comment.