| 1 |
|
| 2 |
|
| 3 |
m> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
local type = type |
| 22 |
local tonumber = tonumber |
| 23 |
local io = { open = io.open } |
| 24 |
|
| 25 |
local helpers = require"vicious.helpers" |
| 26 |
local spawn = require"vicious.spawn" |
| 27 |
|
| 28 |
local pathcache = {} |
| 29 |
|
| 30 |
local function read_sensor(path, callback) |
| 31 |
local f = io.open(path, "r") |
| 32 |
callback{ tonumber(f:read"*line") / 1000 } |
| 33 |
f:close() |
| 34 |
end |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
return helpers.setasyncall{ |
| 39 |
async = function (format, warg, callback) |
| 40 |
if type(warg) ~= "table" or type(warg[1]) ~= "string" then |
| 41 |
return callback{} |
| 42 |
end |
| 43 |
|
| 44 |
local input = warg[2] |
| 45 |
if type(input) == "number" then |
| 46 |
input = ("temp%d_input"):format(input) |
| 47 |
else |
| 48 |
input = "temp1_input" |
| 49 |
end |
| 50 |
|
| 51 |
local sensor = warg[1] |
| 52 |
if pathcache[sensor] then |
| 53 |
read_sensor(pathcache[sensor] .. input, callback) |
| 54 |
else |
| 55 |
spawn.easy_async_with_shell( |
| 56 |
"grep " .. sensor .. " -wl /sys/class/hwmon/*/name", |
| 57 |
function (stdout, stderr, exitreason, exitcode) |
| 58 |
if exitreason == "exit" and exitcode == 0 then |
| 59 |
pathcache[sensor] = stdout:gsub("name%s+", "") |
| 60 |
read_sensor(pathcache[sensor] .. input, callback) |
| 61 |
else |
| 62 |
callback{} |
| 63 |
end |
| 64 |
end) |
| 65 |
end |
| 66 |
end } |
| 67 |
|