1
by Andy Doan
first attempt at new poweridle testing |
1 |
#!/usr/bin/python
|
2 |
||
3 |
from __future__ import print_function |
|
4 |
||
5 |
import atexit |
|
6 |
import sys |
|
7 |
import subprocess |
|
8 |
import time |
|
9 |
||
10 |
from gi.repository import Gio |
|
11 |
||
12 |
CAPACITY_FILE = '/sys/class/power_supply/battery/capacity' |
|
13 |
||
14 |
# NOTE: 1hr (3600) is the largest allowable activity-timeout.
|
|
15 |
ACTIVITY_TIMEOUT = 3600 |
|
16 |
||
17 |
_settings = Gio.Settings('com.canonical.powerd') |
|
18 |
||
19 |
||
20 |
def _restart_powerd(timeout): |
|
21 |
_settings.set_uint('activity-timeout', timeout) |
|
22 |
subprocess.check_call(['/sbin/restart', 'powerd']) |
|
23 |
||
24 |
||
25 |
def _setup(): |
|
26 |
print('updating the activity-timeout to help burn battery...') |
|
27 |
_setup.original_timeout = _settings.get_uint('activity-timeout') |
|
28 |
_restart_powerd(ACTIVITY_TIMEOUT) |
|
29 |
||
30 |
||
31 |
def _teardown(): |
|
32 |
if _setup.original_timeout: |
|
33 |
print('restoring activity-timeout to original value') |
|
34 |
_restart_powerd(_setup.original_timeout) |
|
35 |
||
36 |
||
37 |
def main(): |
|
38 |
_setup.original_timeout = None |
|
39 |
atexit.register(_teardown) |
|
40 |
_setup() |
|
41 |
||
42 |
# powerd allows 1 hour max to keep screen on, so we have to restart it
|
|
43 |
# once every TIMEOUT seconds to keep the battery draining as we need
|
|
44 |
last_powerd = time.time() |
|
45 |
||
46 |
while True: |
|
47 |
cap = int(open(CAPACITY_FILE).read()) |
|
48 |
if cap < 20: |
|
49 |
print("ERROR: Battery charge too low") |
|
50 |
sys.exit(1) |
|
51 |
elif cap < 90: |
|
52 |
print("capacity at: {}".format(cap)) |
|
53 |
break
|
|
54 |
||
55 |
if time.time() - last_powerd > ACTIVITY_TIMEOUT: |
|
56 |
print('restarting powerd because of TIMEOUT timeout') |
|
57 |
_restart_powerd(ACTIVITY_TIMEOUT) |
|
58 |
||
59 |
# we need to burn down the cpu a bit
|
|
60 |
fmt = "capacity {}%, burning cpu for 240 seconds and trying again..." |
|
61 |
print(fmt.format(cap)) |
|
62 |
subprocess.call(['timeout', '240', 'burnCortexA9']) |
|
63 |
||
64 |
if __name__ == '__main__': |
|
65 |
main() |