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