File: wlan.lua

package info (click to toggle)
awesome-extra 2012061101
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 976 kB
  • ctags: 562
  • sloc: sh: 79; awk: 18; makefile: 11
file content (44 lines) | stat: -rw-r--r-- 1,029 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
--------------------------------
-- Author: Gregor Best        --
-- Copyright 2009 Gregor Best --
--------------------------------

local tonumber = tonumber
local setmetatable = setmetatable
local io = {
    open = io.open,
    popen = io.popen
}
local math = {
    floor = math.floor
}

module("obvious.lib.wlan")

local function get_data(device)
    local link = 0
    local fd = io.open("/proc/net/wireless")
    if not fd then return end

    for line in fd:lines() do
        if line:match("^ "..device) then
            link = tonumber(line:match("   (%d?%d?%d)"))
            break
        end
    end
    fd:close()

    fd = io.popen("iwconfig " .. device)
    if fd then
        local scale = 100
        for line in fd:lines() do
            if line:match("Link Quality=") then
                scale = tonumber(line:match("=%d+/(%d+)"))
            end
        end
        link = math.floor((link / scale) * 100)
    end
    return link
end

setmetatable(_M, { __call = function (_, ...) return get_data(...) end })