-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbatterycontrol
More file actions
634 lines (574 loc) · 22.2 KB
/
Copy pathbatterycontrol
File metadata and controls
634 lines (574 loc) · 22.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#!/bin/bash
RED=$'\033[31m'
GREEN=$'\033[32m'
YELLOW=$'\033[33m'
BLUE=$'\033[34m'
MAGENTA=$'\033[35m'
CYAN=$'\033[36m'
BOLD=$'\033[1m'
RESET=$'\033[0m'
INSTALL_DIR="@INSTALL_DIR@"
if [ -d "/home/chronos/user/MyFiles/Downloads" ]; then
CONFIG_FILE="/home/chronos/user/MyFiles/Downloads/ChromeOS_PowerControl_Config/config"
else
CONFIG_FILE="/usr/local/bin/ChromeOS_PowerControl_Config/config"
fi
BATTERY_PATH=""
STATUS_PATH=""
RUN_FLAG="$INSTALL_DIR/.batterycontrol_enabled"
PID_FILE="$INSTALL_DIR/.batterycontrol_pid"
LOG_FILE="/var/log/batterycontrol.log"
timestamp=$(printf '%(%Y-%m-%d %H:%M:%S)T\n' -1)
CONF_SOURCE="$INSTALL_DIR/batterycontrol.conf"
CONF_TARGET="/etc/init/batterycontrol.conf"
FLEX_MODE=0
CHARGE_THRESHOLD_PATH=""
detect_flex_mode() {
for d in /sys/class/power_supply/*; do
[[ -f "$d/charge_control_end_threshold" ]] || continue
[[ -f "$d/type" ]] || continue
read -r _type < "$d/type"
[[ "$_type" == "Battery" ]] || continue
FLEX_MODE=1
return
done
}
detect_flex_mode
#if [[ $FLEX_MODE -eq 1 ]]; then
# echo "${YELLOW}Linux detect, using sysfs mode.${RESET}"
#fi
wait_for_config() {
local max_wait=300
local elapsed=0
while [ $elapsed -lt $max_wait ]; do
if [ -f "$CONFIG_FILE" ]; then
return 0
fi
sleep 5
elapsed=$((elapsed + 5))
done
echo "${RED}Error: Config file not available after 5 minutes${RESET}" | tee -a "$LOG_FILE"
return 1
}
if ! ( [[ -z "$1" ]] || [[ "$1" == "--h" || "$1" == "-h" || "$1" == "h" || "$1" == "--help" || "$1" == "-help" || "$1" == "help" || "$1" == "monitor" || "$1" == "mon" || "$1" == "usage" || "$1" == "status" ]] ) && [[ "$(id -u)" -ne 0 ]]; then
echo "${RED}BatteryControl requires sudo to run.${RESET}"
echo " Try: sudo batterycontrol $* or sudo $0 $*"
exit 1
fi
DEFAULT_CHARGE_MAX=77
CHARGE_MAX=
detect_battery() {
BATTERY_PATH=""
STATUS_PATH=""
CHARGE_THRESHOLD_PATH=""
for d in /sys/class/power_supply/*; do
[[ -f "$d/capacity" ]] || continue
[[ -f "$d/status" ]] || continue
[[ -f "$d/voltage_min_design" ]] || continue
if [[ -f "$d/type" ]]; then
read -r type < "$d/type"
[[ "$type" == "Battery" ]] || continue
fi
case "$d" in
*hid*|*HID*|*stylus*|*pen*) continue ;;
esac
read -r status < "$d/status"
[[ "$status" != "Unknown" ]] || continue
BATTERY_PATH="$d/capacity"
STATUS_PATH="$d/status"
if [[ $FLEX_MODE -eq 1 && -f "$d/charge_control_end_threshold" ]]; then
CHARGE_THRESHOLD_PATH="$d/charge_control_end_threshold"
fi
return 0
done
echo "${RED}No internal battery detected!${RESET}" | tee -a "$LOG_FILE"
return 1
}
load_config() {
if [ -f "$CONFIG_FILE" ]; then
source "$CONFIG_FILE" 2>/dev/null
fi
validate_config
}
validate_config() {
if [[ -z "$CHARGE_MAX" || ! "$CHARGE_MAX" =~ ^[0-9]+$ ]]; then
CHARGE_MAX=$DEFAULT_CHARGE_MAX
fi
if (( CHARGE_MAX > 100 )); then
CHARGE_MAX=100
elif (( CHARGE_MAX < 14 )); then
CHARGE_MAX=14
fi
}
save_config() {
validate_config
if [[ -f "$CONFIG_FILE" ]]; then
sed -i "s/^CHARGE_MAX=.*/CHARGE_MAX=$CHARGE_MAX/" "$CONFIG_FILE" || echo "CHARGE_MAX=$CHARGE_MAX" >> "$CONFIG_FILE"
fi
}
update_status() {
local val="$1"
if [[ -f "$CONFIG_FILE" ]]; then
sed -i "s/^BATTERYCONTROL_STATUS=.*/BATTERYCONTROL_STATUS=$val/" "$CONFIG_FILE"
fi
}
wait_for_config
load_config
set_threshold() {
if ! [[ "$1" =~ ^[0-9]+$ ]]; then
echo "Error: Threshold must be an integer."
exit 1
fi
if (( $1 > 100 || $1 < 14 )); then
echo "Error: CHARGE_MAX must be between 14 and 100."
exit 1
fi
CHARGE_MAX=$1
save_config
if [[ $FLEX_MODE -eq 1 ]]; then
detect_battery
if [[ -n "$CHARGE_THRESHOLD_PATH" ]]; then
echo "$CHARGE_MAX" | sudo tee "$CHARGE_THRESHOLD_PATH" >/dev/null
echo "${GREEN}$(printf '%(%Y-%m-%d %H:%M:%S)T\n' -1) - Charge Limit: ${CHARGE_MAX}% (charge_control_end_threshold updated)${RESET}" | tee -a "$LOG_FILE"
else
echo "${RED}Warning: charge_control_end_threshold not found — config saved but kernel threshold not updated${RESET}"
echo "${GREEN}$(printf '%(%Y-%m-%d %H:%M:%S)T\n' -1) - Charge Limit: ${CHARGE_MAX}%${RESET}" | tee -a "$LOG_FILE"
fi
else
echo "${GREEN}$(printf '%(%Y-%m-%d %H:%M:%S)T\n' -1) - Charge Limit: ${CHARGE_MAX}%${RESET}" | tee -a "$LOG_FILE"
fi
}
load_config
show_help() {
echo "${GREEN}"
echo "╔════════════════════════════════════════════════════════════════════════════════════╗"
echo "║ ║"
echo "║ BatteryControl commands with examples: ║"
echo "║ ║"
echo "║ batterycontrol # Check BatteryControl status ║"
echo "║ batterycontrol help # Help menu ║"
echo "║ batterycontrol monitor # Real-time batterycontrol log ║"
echo "║ sudo batterycontrol start # Start BatteryControl ║"
echo "║ sudo batterycontrol stop # Stop BatteryControl ║"
echo "║ sudo batterycontrol 77 # Charge limit set to 77% - Minimum allowed is 14% ║"
echo "║ sudo batterycontrol startup # Copy or Remove batterycontrol.conf at: /etc/init/ ║"
echo "║ sudo batterycontrol usage # See power consumption in real-time! ║"
echo "║ ║"
echo "╚════════════════════════════════════════════════════════════════════════════════════╝"
echo "${RESET}"
}
apply_charge_control() {
local mode="$1"
if [[ $FLEX_MODE -eq 0 ]]; then
sudo ectool chargecontrol "$mode" >/dev/null 2>&1
else
if [[ -z "$CHARGE_THRESHOLD_PATH" ]]; then
detect_battery
fi
if [[ -n "$CHARGE_THRESHOLD_PATH" ]]; then
if [[ "$mode" == "idle" ]]; then
echo "$CHARGE_MAX" | sudo tee "$CHARGE_THRESHOLD_PATH" >/dev/null
else
echo "100" | sudo tee "$CHARGE_THRESHOLD_PATH" >/dev/null
fi
else
echo "${RED}$(printf '%(%Y-%m-%d %H:%M:%S)T\n' -1) - charge_control_end_threshold not found${RESET}" >&2
fi
fi
}
FLEX_AC_PATH=""
FLEX_AC_ONLINE_PATH=""
detect_active_usbpd_port() {
if [[ $FLEX_MODE -eq 0 ]]; then
local port_output line port
port_output=$(sudo ectool usbpdpower 2>/dev/null)
while IFS= read -r line; do
if [[ "$line" =~ ^Port[[:space:]]+([0-9]+):[[:space:]]+SNK[[:space:]]+Charger ]]; then
port="${BASH_REMATCH[1]}"
echo "$port"
return
fi
done <<< "$port_output"
echo "-1"
else
FLEX_AC_PATH=""
FLEX_AC_ONLINE_PATH=""
for d in /sys/class/power_supply/*; do
[[ -f "$d/type" ]] || continue
[[ -f "$d/online" ]] || continue
read -r ptype < "$d/type"
read -r online < "$d/online"
if [[ "$ptype" == "Mains" && "$online" == "1" ]]; then
FLEX_AC_PATH="$d"
FLEX_AC_ONLINE_PATH="$d/online"
echo "flex"
return
fi
done
echo "-1"
fi
}
sysfs_battery_info() {
local bat_dir
bat_dir=$(dirname "$BATTERY_PATH")
local cap status
read -r cap < "$BATTERY_PATH" 2>/dev/null || cap="?"
read -r status < "$STATUS_PATH" 2>/dev/null || status="?"
local voltage_str="?"
if [[ -f "$bat_dir/voltage_now" ]]; then
read -r v < "$bat_dir/voltage_now"
printf -v voltage_str "%d mV" "$(( v / 1000 ))"
fi
local current_str="?"
if [[ -f "$bat_dir/current_now" ]]; then
read -r c < "$bat_dir/current_now"
printf -v current_str "%d mA" "$(( c / 1000 ))"
fi
local energy_str="?"
if [[ -f "$bat_dir/energy_now" && -f "$bat_dir/energy_full" ]]; then
read -r en < "$bat_dir/energy_now"
read -r ef < "$bat_dir/energy_full"
printf -v energy_str "%d / %d mWh" "$(( en / 1000 ))" "$(( ef / 1000 ))"
fi
local threshold_str="?"
if [[ -n "$CHARGE_THRESHOLD_PATH" && -f "$CHARGE_THRESHOLD_PATH" ]]; then
read -r threshold_str < "$CHARGE_THRESHOLD_PATH"
threshold_str="${threshold_str}%"
fi
echo " Battery: ${cap}% (${status})"
echo " Voltage: ${voltage_str}"
echo " Current: ${current_str}"
echo " Energy: ${energy_str}"
echo " Kernel charge_control_end_threshold: ${threshold_str}"
}
battery_status() {
detect_battery
if [ -f "$RUN_FLAG" ]; then
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if ps -p "$PID" > /dev/null 2>&1; then
echo "${GREEN}"
echo "═════════════════════════════════════════════════════════════════════════════"
echo " BatteryControl is: RUNNING (PID $PID)"
else
echo "${RED}"
echo "═════════════════════════════════════════════════════════════════════════════"
echo " BatteryControl is: RUNNING (but process is NOT) "
sudo rm -f "$PID_FILE"
fi
else
echo "${RED}"
echo "═════════════════════════════════════════════════════════════════════════════"
echo " BatteryControl is: RUNNING (PID not found) "
fi
else
echo "${RED}"
echo "═════════════════════════════════════════════════════════════════════════════"
echo " BatteryControl is: STOPPED"
fi
echo ""
echo "Charge Limit: $CHARGE_MAX"
echo ""
if [[ $FLEX_MODE -eq 0 ]]; then
local active_port
active_port=$(detect_active_usbpd_port)
if [ "$active_port" != "-1" ]; then
echo "Active USB-PD Charger Port: $active_port (CROS_USBPD_CHARGER$active_port)"
else
echo "Active USB-PD Charger Port: None detected"
fi
echo
sudo ectool usbpdpower 2>/dev/null || echo "Failed to run ectool usbpdpower"
echo
sudo ectool battery 2>/dev/null || echo "Failed to run ectool battery"
echo " Capacity: $BATTERY_PATH"
echo " State: $STATUS_PATH"
else
local active_port
active_port=$(detect_active_usbpd_port)
if [ "$active_port" != "-1" ]; then
echo "Active AC Adapter: ${FLEX_AC_PATH:-detected}"
else
echo "Active AC Adapter: None detected"
fi
echo
sysfs_battery_info
fi
echo ""
echo "═════════════════════════════════════════════════════════════════════════════"
echo "${RESET}"
}
start_monitoring_loop() {
trap 'echo "$(date) - SIGTERM received" >> "$LOG_FILE"' TERM
trap 'echo "$(date) - SIGINT received" >> "$LOG_FILE"' INT
trap 'echo "$(date) - EXIT trap fired ($?)" >> "$LOG_FILE"' EXIT
trap 'echo "$(date) - EXIT trap fired ($?)" >> "$LOG_FILE"; update_status 0' EXIT
wait_for_config
detect_battery
touch "$RUN_FLAG"
echo "$BASHPID" > "$PID_FILE"
update_status 1
echo "${GREEN}$(printf '%(%Y-%m-%d %H:%M:%S)T\n' -1) - BatteryControl started. (PID $$).${RESET}"
local last_reload_time=0
while true; do
if [ ! -f "$RUN_FLAG" ]; then
echo "${RED}$(printf '%(%Y-%m-%d %H:%M:%S)T\n' -1) - BatteryControl Stopped${RESET}." | tee -a "$LOG_FILE"
sudo rm -f "$PID_FILE"
exit 0
fi
now=$(date +%s)
if (( now - last_reload_time >= 10 )); then
load_config
last_reload_time=$now
if read -r BATTERY_CHARGE < "$BATTERY_PATH"; then
read -r STATE < "$STATUS_PATH" || STATE="Unknown"
echo "${GREEN}$(printf '%(%Y-%m-%d %H:%M:%S)T\n' -1) - Battery: ${BATTERY_CHARGE}% - Charge Limit: ${CHARGE_MAX}% - State: $STATE${RESET}" >> "$LOG_FILE"
else
echo "Failed to read $BATTERY_PATH" >&2
fi
if [ -f "$LOG_FILE" ] && [ "$(stat -c%s "$LOG_FILE")" -gt 1048576 ]; then
echo "${GREEN}$(printf '%(%Y-%m-%d %H:%M:%S)T\n' -1) - Truncated log file (exceeded 1MB)${RESET}" > "$LOG_FILE"
fi
startup_enabled=0
if [[ -f "$CONFIG_FILE" ]]; then
while IFS='=' read -r k v; do
[[ $k == STARTUP_BATTERYCONTROL ]] && { [[ $v == 1 ]] && startup_enabled=1; break; }
done < "$CONFIG_FILE"
if (( startup_enabled )); then
if [[ ! -f "$CONF_TARGET" ]]; then
sudo cp "$CONF_SOURCE" "$CONF_TARGET"
printf '%s%(%F %T)T - BatteryControl Startup enabled%s\n' "$GREEN" -1 "$RESET" >> "$LOG_FILE"
fi
else
sudo rm -f "$CONF_TARGET"
printf '%s%(%F %T)T - BatteryControl Startup disabled%s\n' "$RED" -1 "$RESET" >> "$LOG_FILE"
fi
fi
fi
if ! read -r CHARGE < "$BATTERY_PATH"; then
echo "${RED}Failed to read $BATTERY_PATH ${RESET}" >&2
sleep 5
continue
fi
if ! read -r STATUS < "$STATUS_PATH"; then
STATUS="Unknown"
fi
ACTIVE_PORT=$(detect_active_usbpd_port)
if [ "$ACTIVE_PORT" != "-1" ]; then
if [[ $FLEX_MODE -eq 0 ]]; then
CHARGER_PATH="/sys/class/power_supply/CROS_USBPD_CHARGER${ACTIVE_PORT}/online"
if [ -f "$CHARGER_PATH" ]; then
read -r AC_ON < "$CHARGER_PATH" || AC_ON=0
else
AC_ON=0
fi
else
AC_ON=1
fi
else
AC_ON=0
fi
CHARGE_MIN=$((CHARGE_MAX - 4))
if [ "$AC_ON" -eq 1 ]; then
if (( CHARGE >= CHARGE_MAX )); then
apply_charge_control idle
CHARGE_STATE="idle"
elif (( CHARGE <= CHARGE_MIN )); then
apply_charge_control normal
CHARGE_STATE="normal"
else
apply_charge_control "${CHARGE_STATE:-idle}"
fi
else
if [[ "${CHARGE_STATE}" == "idle" ]]; then
apply_charge_control normal
CHARGE_STATE="normal"
fi
fi
sleep 7
done
}
stop_monitoring() {
local killed=0
if [[ -f "$PID_FILE" ]]; then
PID=$(<"$PID_FILE")
if [[ -n "$PID" ]] && ps -p "$PID" > /dev/null 2>&1; then
kill "$PID"
for i in {1..30}; do
if ! ps -p "$PID" > /dev/null 2>&1; then
break
fi
sleep 0.1
done
if ps -p "$PID" > /dev/null 2>&1; then
kill -9 "$PID"
fi
while ps -p "$PID" > /dev/null 2>&1; do
sleep 0.1
done
echo "${RED}$(date '+%F %T') - BatteryControl Stopped (PID $PID)${RESET}" | tee -a "$LOG_FILE"
killed=1
fi
rm -f "$PID_FILE"
fi
local orphan_pids
orphan_pids=$(pgrep -f '__batterycontrol_monitor__' 2>/dev/null)
if [[ -n "$orphan_pids" ]]; then
echo "${RED}$(date '+%F %T') - Ending BatteryControl processes: $orphan_pids${RESET}" | tee -a "$LOG_FILE"
kill $orphan_pids 2>/dev/null || true
sleep 1
kill -9 $orphan_pids 2>/dev/null || true
killed=1
fi
rm -f "$RUN_FLAG"
update_status 0
if (( killed == 0 )); then
echo "${YELLOW}$(date '+%F %T') - BatteryControl is not running." | tee -a "$LOG_FILE"
fi
}
usage() {
echo "${GREEN}CTRL-C to exit${RESET}"
detect_battery
local bat_dir
bat_dir=$(dirname "$BATTERY_PATH")
if [[ $FLEX_MODE -eq 0 ]]; then
while true; do
local data voltage_mV current_mA line
data=$(sudo ectool battery 2>/dev/null)
voltage_mV=""
current_mA=""
while IFS= read -r line; do
case "$line" in
*"Present voltage"*)
set -- $line
voltage_mV=${3//[!0-9-]}
;;
*"Present current"*)
set -- $line
current_mA=${3//[!0-9-]}
;;
esac
done <<< "$data"
printf -v now '%(%H:%M:%S)T' -1
if [[ -z "$voltage_mV" || -z "$current_mA" ]]; then
echo "${RED}${now} - Error reading battery${RESET}"
sleep 1
continue
fi
local power_uW abs_uW watts_int watts_frac power_fmt
power_uW=$(( voltage_mV * current_mA ))
abs_uW=${power_uW#-}
watts_int=$(( abs_uW / 1000000 ))
watts_frac=$(( (abs_uW % 1000000) / 1000 ))
printf -v power_fmt "%d.%03d" "$watts_int" "$watts_frac"
if (( power_uW > 0 )); then
COLOR="$GREEN"; SIGN="+"
else
COLOR="$YELLOW"; SIGN="-"
fi
printf "%b%s: %s%s W%b\n" "$COLOR" "$now" "$SIGN" "$power_fmt" "$RESET"
sleep 1
done
else
while true; do
printf -v now '%(%H:%M:%S)T' -1
local power_uW=""
if [[ -f "$bat_dir/power_now" ]]; then
read -r power_uW < "$bat_dir/power_now" 2>/dev/null
fi
if [[ -z "$power_uW" || "$power_uW" == "0" ]]; then
local voltage_uV current_uA
if [[ -f "$bat_dir/voltage_now" && -f "$bat_dir/current_now" ]]; then
read -r voltage_uV < "$bat_dir/voltage_now" 2>/dev/null
read -r current_uA < "$bat_dir/current_now" 2>/dev/null
if [[ -n "$voltage_uV" && -n "$current_uA" ]]; then
power_uW=$(( voltage_uV * current_uA / 1000000 ))
fi
fi
fi
if [[ -z "$power_uW" ]]; then
echo "${RED}${now} - Error reading power${RESET}"
sleep 1
continue
fi
local status_now=""
read -r status_now < "$STATUS_PATH" 2>/dev/null
if [[ "$status_now" == "Discharging" && "$power_uW" -gt 0 ]]; then
power_uW=$(( -power_uW ))
elif [[ "$status_now" == "Charging" && "$power_uW" -lt 0 ]]; then
power_uW=$(( -power_uW ))
fi
local abs_uW watts_int watts_frac power_fmt
abs_uW=${power_uW#-}
watts_int=$(( abs_uW / 1000000 ))
watts_frac=$(( (abs_uW % 1000000) / 1000 ))
printf -v power_fmt "%d.%03d" "$watts_int" "$watts_frac"
if (( power_uW >= 0 )); then
COLOR="$GREEN"; SIGN="+"
else
COLOR="$YELLOW"; SIGN="-"
fi
printf "%b%s: %s%s W%b\n" "$COLOR" "$now" "$SIGN" "$power_fmt" "$RESET"
sleep 1
done
fi
}
case "$1" in
set)
set_threshold "$2"
;;
start)
stop_monitoring >/dev/null 2>&1
nohup "$0" __batterycontrol_monitor__ >> "$LOG_FILE" 2>/dev/null &
echo "${GREEN}$(printf '%(%Y-%m-%d %H:%M:%S)T\n' -1) - Starting BatteryControl - Output is logged to $LOG_FILE${RESET}" | tee -a "$LOG_FILE"
;;
stop)
stop_monitoring
;;
""|status)
battery_status
;;
startup)
CONF_SOURCE="$INSTALL_DIR/batterycontrol.conf"
CONF_TARGET="/etc/init/batterycontrol.conf"
read -p "Do you want BatteryControl to startup automatically? (y/n): " choice
if [[ "$choice" =~ ^[Yy]$ ]]; then
if [ -f "$CONF_SOURCE" ]; then
echo "Copying batterycontrol.conf to /etc/init/..."
sudo cp "$CONF_SOURCE" "$CONF_TARGET"
echo "${GREEN}batterycontrol.conf copied.${RESET}"
else
echo "${RED}batterycontrol.conf not found at $CONF_SOURCE${RESET}"
fi
else
echo "BatteryControl will not startup automatically."
sudo rm -r /etc/init/batterycontrol.conf 2>/dev/null
fi
;;
usage)
usage
;;
--h|-h|h|--help|-help|help)
show_help
;;
__batterycontrol_monitor__)
start_monitoring_loop
;;
monitor|mon)
tail -f /var/log/batterycontrol.log
;;
''|*[!0-9]*)
echo "Unknown command or value is too low: $1"
echo "Use '$0 help' to see available commands."
exit 1
;;
*)
if [[ "$1" =~ ^[0-9]+$ ]] && (( $1 >= 14 && $1 <= 100 )); then
set_threshold "$1"
else
echo "${RED}Unknown command or value is too low: $1 ${RESET}"
show_help
exit 1
fi
;;
esac