From f8f500d30660760db4c9bc86d9dd53697ce9efd0 Mon Sep 17 00:00:00 2001 From: Yessiest Date: Sun, 4 Sep 2022 21:03:37 +0400 Subject: [PATCH] Testing new uevent-based attribute reading --- libs/syscontrol.lua | 56 ++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/libs/syscontrol.lua b/libs/syscontrol.lua index cb0bc9f..9e89b5d 100644 --- a/libs/syscontrol.lua +++ b/libs/syscontrol.lua @@ -38,6 +38,7 @@ syscontrol.backlight.read_attribs = function(dev_name) writable = false } fhandler:close() + bhandler:close() local wfhandler = io.open("/sys/class/backlight/"..dev_name.."/brightness","w") if wfhandler then wfhandler:close() @@ -83,36 +84,39 @@ end syscontrol.power_supply.read_attribs = function(dev_name) local fhandler,err = io.open("/sys/class/power_supply/"..dev_name.."/type","r") if fhandler then - local dtype = fhandler:read("*a"):match("%S*") - fhandler:close() - local full_path = "/sys/class/power_supply/"..dev_name + local uevent_f = io.open("/sys/class/power_supply/"..dev_name.."/uevent","r") + if not uevent_f then + return nil, "Unable to open uevent file" + end + local uevent_data = {} local device = { - type = dtype, - full_path = full_path, + type = fhandler:read("*a"), + full_path = "/sys/class/power_supply/"..dev_name, name = dev_name } - if dtype == "Mains" then - local online_f = io.open(full_path.."/online","r") - device.online = (online_f:read("*a"):match("%S*") == "1") - online_f:close() - elseif dtype == "Battery" then - local capacity_f = io.open(full_path.."/capacity","r") - local model_f = io.open(full_path.."/model_name","r") - local energy_full_design_f = io.open(full_path.."/energy_full_design","r") - local energy_full_f = io.open(full_path.."/energy_full","r") - local charging_f = io.open(full_path.."/status","r") - device.model = model_f:read("*a"):match("[^\n]*") - device.energy_full_design = tonumber(energy_full_design_f:read("*a")) - device.energy_full = tonumber(energy_full_f:read("*a")) + fhandler:close() + uevent:read("*a"):gsub("[^\n]+",function(line) + local key,value = line:match("^([^=]+)=([^=]*)") + if value:match("^%d+$") then + value = tonumber(value) + end + uevent_data[key] = value + end) + uevent:close() + if device.type == "Battery" then + device.model = uevent_data.POWER_SUPPLY_MODEL_NAME or "N/A" + device.energy_full_design = uevent_data.POWER_SUPPLY_ENERGY_FULL_DESIGN + device.energy_full = uevent_data.POWER_SUPPLY_ENERGY_FULL + if not device.energy_full_design then + device.energy_full_design = uevent_data.POWER_SUPPLY_CHARGE_FULL_DESIGN or -1 + device.energy_full = uevent_data.POWER_SUPPLY_CHARGE_FULL or -1 + end device.quality = (device.energy_full/device.energy_full_design)*100 - device.capacity = tonumber(capacity_f:read("*a")) - device.charging = (charging_f:read("*a"):match("%S*") == "Charging") - capacity_f:close() - model_f:close() - energy_full_design_f:close() - energy_full_f:close() - end - return device + device.capacity = uevent_data.POWER_SUPPLY_CAPACITY or 0 + device.charging = (uevent_data.POWER_SUPPLY_STATUS == "Charging") + elseif device.type == "Mains" then + device.online = (uevent_data.POWER_SUPPLY_ONLINE == 1) + end else return nil, err end