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
|
#!/usr/bin/python
from __future__ import print_function
import atexit
import sys
import subprocess
import time
from gi.repository import Gio
CAPACITY_FILE = '/sys/class/power_supply/battery/capacity'
# NOTE: 1hr (3600) is the largest allowable activity-timeout.
ACTIVITY_TIMEOUT = 3600
_settings = Gio.Settings('com.canonical.powerd')
def _restart_powerd(timeout):
_settings.set_uint('activity-timeout', timeout)
subprocess.check_call(['/sbin/restart', 'powerd'])
def _setup():
print('updating the activity-timeout to help burn battery...')
_setup.original_timeout = _settings.get_uint('activity-timeout')
_restart_powerd(ACTIVITY_TIMEOUT)
def _teardown():
if _setup.original_timeout:
print('restoring activity-timeout to original value')
_restart_powerd(_setup.original_timeout)
def main():
_setup.original_timeout = None
atexit.register(_teardown)
_setup()
# powerd allows 1 hour max to keep screen on, so we have to restart it
# once every TIMEOUT seconds to keep the battery draining as we need
last_powerd = time.time()
while True:
cap = int(open(CAPACITY_FILE).read())
if cap < 20:
print("ERROR: Battery charge too low")
sys.exit(1)
elif cap < 90:
print("capacity at: {}".format(cap))
break
if time.time() - last_powerd > ACTIVITY_TIMEOUT:
print('restarting powerd because of TIMEOUT timeout')
_restart_powerd(ACTIVITY_TIMEOUT)
# we need to burn down the cpu a bit
fmt = "capacity {}%, burning cpu for 240 seconds and trying again..."
print(fmt.format(cap))
subprocess.call(['timeout', '240', 'burnCortexA9'])
if __name__ == '__main__':
main()
|