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
}
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()
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
return device
else
return nil, err
end