Handle multiple batteries
[khatus.git] / src / awk / exe / parse_upower.awk
1 # When parsing 'upower --dump'
2 /^Device:[ \t]+/ {
3 device["path"] = $2
4 next
5 }
6
7 # When parsing 'upower --monitor-detail'
8 /^\[[0-9]+:[0-9]+:[0-9]+\.[0-9]+\][ \t]+device changed:[ \t]+/ {
9 device["path"] = $4
10 next
11 }
12
13 / native-path:/ && device["path"] {
14 device["native_path"] = $2
15 next
16 }
17
18 # BEGIN battery
19 / battery/ && device["path"] {
20 device["is_battery"] = 1
21 next
22 }
23
24 / state:/ && device["is_battery"] {
25 device["battery_state"] = $2
26 next
27 }
28
29 / energy:/ && device["is_battery"] {
30 device["energy"] = $2
31 next
32 }
33
34 / energy-full:/ && device["is_battery"] {
35 device["energy_full"] = $2
36 next
37 }
38
39 / percentage:/ && device["is_battery"] {
40 device["battery_percentage"] = $2
41 sub("%$", "", device["battery_percentage"])
42 next
43 }
44
45 /^$/ && device["is_battery"] {
46 print("battery_state" , aggregate_battery_state())
47 print("battery_percentage", aggregate_battery_percentage())
48 }
49 # END battery
50
51 # BEGIN line-power
52 / line-power/ && device["path"] {
53 device["is_line_power"] = 1
54 next
55 }
56
57 / online:/ && device["is_line_power"] {
58 device["line_power_online"] = $2
59 next
60 }
61
62 /^$/ && device["is_line_power"] {
63 print("line_power", device["line_power_online"])
64 }
65 # END line-power
66
67 /^$/ {
68 delete device
69 next
70 }
71
72 function aggregate_battery_percentage( bat, curr, full) {
73 _battery_energy[device["native_path"]] = device["energy"]
74 _battery_energy_full[device["native_path"]] = device["energy_full"]
75 for (bat in _battery_energy) {
76 curr = curr + _battery_energy[bat]
77 full = full + _battery_energy_full[bat]
78 }
79 return ((curr / full) * 100)
80 }
81
82 function aggregate_battery_state( curr, bat, new) {
83 _battery_state[device["native_path"]] = device["battery_state"]
84 curr = device["battery_state"]
85 for (bat in _battery_state) {
86 new = _battery_state[bat]
87 if (new == "discharging") {
88 curr = new
89 } else if (curr != "discharging" && new == "charging") {
90 curr = new
91 }
92 }
93 return curr
94 }
This page took 0.076166 seconds and 4 git commands to generate.