diff --git a/widgets/hwmontemp_linux.lua b/widgets/hwmontemp_linux.lua index 7631dc9..fe3e50d 100644 --- a/widgets/hwmontemp_linux.lua +++ b/widgets/hwmontemp_linux.lua @@ -1,5 +1,5 @@ -- widget type providing name-indexed temperatures from /sys/class/hwmon --- Copyright (C) 2019 Alexander Koch +-- Copyright (C) 2019, 2020 Alexander Koch -- Copyright (C) 2019 Nguyễn Gia Phong -- -- This file is part of Vicious. @@ -25,6 +25,14 @@ local io = { open = io.open } local helpers = require"vicious.helpers" local spawn = require"vicious.spawn" +local pathcache = {} + +local function read_sensor(path, callback) + local f = io.open(path, "r") + callback{ tonumber(f:read"*line") / 1000 } + f:close() +end + -- hwmontemp: provides name-indexed temps from /sys/class/hwmon -- vicious.widgets.hwmontemp return helpers.setasyncall{ @@ -32,6 +40,7 @@ return helpers.setasyncall{ if type(warg) ~= "table" or type(warg[1]) ~= "string" then return callback{} end + local input = warg[2] if type(input) == "number" then input = ("temp%d_input"):format(input) @@ -39,16 +48,20 @@ return helpers.setasyncall{ input = "temp1_input" end - spawn.easy_async_with_shell( - "grep " .. warg[1] .. " -wl /sys/class/hwmon/*/name", - function (stdout, stderr, exitreason, exitcode) - if exitreason == "exit" and exitcode == 0 then - local f = io.open(stdout:gsub("name%s+", input), "r") - callback{ tonumber(f:read"*line") / 1000 } - f:close() - else - callback{} - end - end) + local sensor = warg[1] + if pathcache[sensor] then + read_sensor(pathcache[sensor] .. input, callback) + else + spawn.easy_async_with_shell( + "grep " .. sensor .. " -wl /sys/class/hwmon/*/name", + function (stdout, stderr, exitreason, exitcode) + if exitreason == "exit" and exitcode == 0 then + pathcache[sensor] = stdout:gsub("name%s+", "") + read_sensor(pathcache[sensor] .. input, callback) + else + callback{} + end + end) + end end } -- vim: ts=4:sw=4:expandtab