Merge pull request #384 from shuber2/fix-bat-unknownstatus
Fix bat unknownstatus
This commit is contained in:
commit
5f5aa85bed
|
@ -125,20 +125,25 @@ local function worker(user_args)
|
||||||
local battery_info = {}
|
local battery_info = {}
|
||||||
local capacities = {}
|
local capacities = {}
|
||||||
for s in stdout:gmatch("[^\r\n]+") do
|
for s in stdout:gmatch("[^\r\n]+") do
|
||||||
|
-- Match a line with status and charge level
|
||||||
local status, charge_str, _ = string.match(s, '.+: ([%a%s]+), (%d?%d?%d)%%,?(.*)')
|
local status, charge_str, _ = string.match(s, '.+: ([%a%s]+), (%d?%d?%d)%%,?(.*)')
|
||||||
if status ~= nil then
|
if status ~= nil then
|
||||||
|
-- Enforce that for each entry in battery_info there is an
|
||||||
|
-- entry in capacities of zero. If a battery has status
|
||||||
|
-- "Unknown" then there is no capacity reported and we treat it
|
||||||
|
-- as zero capactiy for later calculations.
|
||||||
table.insert(battery_info, {status = status, charge = tonumber(charge_str)})
|
table.insert(battery_info, {status = status, charge = tonumber(charge_str)})
|
||||||
else
|
table.insert(capacities, 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Match a line where capacity is reported
|
||||||
local cap_str = string.match(s, '.+:.+last full capacity (%d+)')
|
local cap_str = string.match(s, '.+:.+last full capacity (%d+)')
|
||||||
table.insert(capacities, tonumber(cap_str))
|
if cap_str ~= nil then
|
||||||
|
capacities[#capacities] = tonumber(cap_str) or 0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local capacity = 0
|
local capacity = 0
|
||||||
for _, cap in ipairs(capacities) do
|
|
||||||
capacity = capacity + cap
|
|
||||||
end
|
|
||||||
|
|
||||||
local charge = 0
|
local charge = 0
|
||||||
local status
|
local status
|
||||||
for i, batt in ipairs(battery_info) do
|
for i, batt in ipairs(battery_info) do
|
||||||
|
@ -148,7 +153,11 @@ local function worker(user_args)
|
||||||
-- this is arbitrary, and maybe another metric should be used
|
-- this is arbitrary, and maybe another metric should be used
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Adds up total (capacity-weighted) charge and total capacity.
|
||||||
|
-- It effectively ignores batteries with status "Unknown" as we
|
||||||
|
-- treat them with capacity zero.
|
||||||
charge = charge + batt.charge * capacities[i]
|
charge = charge + batt.charge * capacities[i]
|
||||||
|
capacity = capacity + capacities[i]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
charge = charge / capacity
|
charge = charge / capacity
|
||||||
|
|
Loading…
Reference in New Issue