Testing new uevent-based attribute reading

This commit is contained in:
Yessiest 2022-09-04 21:03:37 +04:00
parent e8dfae3013
commit f8f500d306
1 changed files with 30 additions and 26 deletions

View File

@ -38,6 +38,7 @@ syscontrol.backlight.read_attribs = function(dev_name)
writable = false writable = false
} }
fhandler:close() fhandler:close()
bhandler:close()
local wfhandler = io.open("/sys/class/backlight/"..dev_name.."/brightness","w") local wfhandler = io.open("/sys/class/backlight/"..dev_name.."/brightness","w")
if wfhandler then if wfhandler then
wfhandler:close() wfhandler:close()
@ -83,36 +84,39 @@ end
syscontrol.power_supply.read_attribs = function(dev_name) syscontrol.power_supply.read_attribs = function(dev_name)
local fhandler,err = io.open("/sys/class/power_supply/"..dev_name.."/type","r") local fhandler,err = io.open("/sys/class/power_supply/"..dev_name.."/type","r")
if fhandler then if fhandler then
local dtype = fhandler:read("*a"):match("%S*") local uevent_f = io.open("/sys/class/power_supply/"..dev_name.."/uevent","r")
fhandler:close() if not uevent_f then
local full_path = "/sys/class/power_supply/"..dev_name return nil, "Unable to open uevent file"
end
local uevent_data = {}
local device = { local device = {
type = dtype, type = fhandler:read("*a"),
full_path = full_path, full_path = "/sys/class/power_supply/"..dev_name,
name = dev_name name = dev_name
} }
if dtype == "Mains" then fhandler:close()
local online_f = io.open(full_path.."/online","r") uevent:read("*a"):gsub("[^\n]+",function(line)
device.online = (online_f:read("*a"):match("%S*") == "1") local key,value = line:match("^([^=]+)=([^=]*)")
online_f:close() if value:match("^%d+$") then
elseif dtype == "Battery" then value = tonumber(value)
local capacity_f = io.open(full_path.."/capacity","r") end
local model_f = io.open(full_path.."/model_name","r") uevent_data[key] = value
local energy_full_design_f = io.open(full_path.."/energy_full_design","r") end)
local energy_full_f = io.open(full_path.."/energy_full","r") uevent:close()
local charging_f = io.open(full_path.."/status","r") if device.type == "Battery" then
device.model = model_f:read("*a"):match("[^\n]*") device.model = uevent_data.POWER_SUPPLY_MODEL_NAME or "N/A"
device.energy_full_design = tonumber(energy_full_design_f:read("*a")) device.energy_full_design = uevent_data.POWER_SUPPLY_ENERGY_FULL_DESIGN
device.energy_full = tonumber(energy_full_f:read("*a")) device.energy_full = uevent_data.POWER_SUPPLY_ENERGY_FULL
device.quality = (device.energy_full/device.energy_full_design)*100 if not device.energy_full_design then
device.capacity = tonumber(capacity_f:read("*a")) device.energy_full_design = uevent_data.POWER_SUPPLY_CHARGE_FULL_DESIGN or -1
device.charging = (charging_f:read("*a"):match("%S*") == "Charging") device.energy_full = uevent_data.POWER_SUPPLY_CHARGE_FULL or -1
capacity_f:close() end
model_f:close() device.quality = (device.energy_full/device.energy_full_design)*100
energy_full_design_f:close() device.capacity = uevent_data.POWER_SUPPLY_CAPACITY or 0
energy_full_f:close() device.charging = (uevent_data.POWER_SUPPLY_STATUS == "Charging")
elseif device.type == "Mains" then
device.online = (uevent_data.POWER_SUPPLY_ONLINE == 1)
end end
return device
else else
return nil, err return nil, err
end end