Released version 3-2014010502
[notion.git] / contrib / statusd / legacy / statusd_linuxbatt.lua
blobfbb2bd50d1243b4c2b545b87c15118daa4a21fb0
1 -- Authors: Unknown
2 -- License: Public domain
3 -- Last Changed: Unknown
4 --
5 -- statusd_linuxbatt.lua
6 --
7 -- Public domain
8 --
9 -- Uses the /proc/acpi interface to get battery percentage.
11 -- Use the key "linuxbatt" to get the battery percentage; use
12 -- "linuxbatt_state" to get a symbol indicating charging "+",
13 -- discharging "-", or charged " ".
15 -- Now uses lua functions instead of bash, awk, dc. MUCH faster!
17 -- The "bat" option to the statusd settings for linuxbatt modifies which
18 -- battery we look at.
20 local defaults={
21 update_interval=15*1000,
22 bat=0,
23 important_threshold=30,
24 critical_threshold=10,
26 local settings=table.join(statusd.get_config("linuxbatt"), defaults)
28 function linuxbatt_do_find_capacity()
29 local f=io.open('/proc/acpi/battery/BAT'.. settings.bat ..'/info')
30 local infofile=f:read('*a')
31 f:close()
32 local i, j, capacity = string.find(infofile, 'last full capacity:%s*(%d+) .*')
33 return capacity
34 end
36 local capacity = linuxbatt_do_find_capacity()
38 function get_linuxbatt()
40 local f=io.open('/proc/acpi/battery/BAT'.. settings.bat ..'/state')
41 local statefile=f:read('*a')
42 f:close()
43 local i, j, remaining = string.find(statefile, 'remaining capacity:%s*(%d+) .*')
44 local percent = math.floor( remaining * 100 / capacity )
46 local i, j, statename = string.find(statefile, 'charging state:%s*(%a+).*')
47 if statename == "charging" then
48 return percent, "+"
49 elseif statename == "discharging" then
50 return percent, "-"
51 else
52 return percent, " "
53 end
54 end
56 function update_linuxbatt()
57 local perc, state = get_linuxbatt()
58 statusd.inform("linuxbatt", tostring(perc))
59 statusd.inform("linuxbatt_state", state)
60 if perc < settings.critical_threshold
61 then statusd.inform("linuxbatt_hint", "critical")
62 elseif perc < settings.important_threshold
63 then statusd.inform("linuxbatt_hint", "important")
64 else statusd.inform("linuxbatt_hint", "normal")
65 end
66 linuxbatt_timer:set(settings.update_interval, update_linuxbatt)
67 end
69 linuxbatt_timer = statusd.create_timer()
70 update_linuxbatt()