From f6bfbe281bcdef49b8bb1e9efe7328b2901d6bff Mon Sep 17 00:00:00 2001 From: Siraaj Khandkar Date: Wed, 14 Nov 2018 19:34:39 -0500 Subject: [PATCH] Handle multiple batteries --- README.md | 4 ---- src/awk/exe/parse_upower.awk | 43 ++++++++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index db52037..35f8d30 100644 --- a/README.md +++ b/README.md @@ -190,10 +190,6 @@ controller, which are in turn actualized as desktop notifications by the ![screenshot-self-error-propagation](screenshot-self-error-propagation.jpg) -FIXME ------ -- handle multiple batteries - TODO ---- - track energy usage rate diff --git a/src/awk/exe/parse_upower.awk b/src/awk/exe/parse_upower.awk index 07af1cb..09150c9 100644 --- a/src/awk/exe/parse_upower.awk +++ b/src/awk/exe/parse_upower.awk @@ -10,6 +10,11 @@ next } +/ native-path:/ && device["path"] { + device["native_path"] = $2 + next +} + # BEGIN battery / battery/ && device["path"] { device["is_battery"] = 1 @@ -21,6 +26,16 @@ next } +/ energy:/ && device["is_battery"] { + device["energy"] = $2 + next +} + +/ energy-full:/ && device["is_battery"] { + device["energy_full"] = $2 + next +} + / percentage:/ && device["is_battery"] { device["battery_percentage"] = $2 sub("%$", "", device["battery_percentage"]) @@ -28,8 +43,8 @@ } /^$/ && device["is_battery"] { - print("battery_state" , device["battery_state"]) - print("battery_percentage", device["battery_percentage"]) + print("battery_state" , aggregate_battery_state()) + print("battery_percentage", aggregate_battery_percentage()) } # END battery @@ -53,3 +68,27 @@ delete device next } + +function aggregate_battery_percentage( bat, curr, full) { + _battery_energy[device["native_path"]] = device["energy"] + _battery_energy_full[device["native_path"]] = device["energy_full"] + for (bat in _battery_energy) { + curr = curr + _battery_energy[bat] + full = full + _battery_energy_full[bat] + } + return ((curr / full) * 100) +} + +function aggregate_battery_state( curr, bat, new) { + _battery_state[device["native_path"]] = device["battery_state"] + curr = device["battery_state"] + for (bat in _battery_state) { + new = _battery_state[bat] + if (new == "discharging") { + curr = new + } else if (curr != "discharging" && new == "charging") { + curr = new + } + } + return curr +} -- 2.20.1