root/luci/trunk/modules/admin-core/luasrc/tools/status.lua @ 6954

Revision 6954, 3.0 KB (checked in by jow, 2 years ago)

modules/admin-core: add wifi_network() to luci.tools.status

  • Property svn:keywords set to Id
Line 
1--[[
2LuCI - Lua Configuration Interface
3
4Copyright 2011 Jo-Philipp Wich <xm@subsignal.org>
5
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12$Id$
13]]--
14
15module("luci.tools.status", package.seeall)
16
17local uci = require "luci.model.uci".cursor()
18
19function dhcp_leases()
20    local rv = { }
21    local nfs = require "nixio.fs"
22    local leasefile = "/var/dhcp.leases"
23
24    uci:foreach("dhcp", "dnsmasq",
25        function(s)
26            if s.leasefile and nfs.access(s.leasefile) then
27                leasefile = s.leasefile
28                return false
29            end
30        end)
31
32    local fd = io.open(leasefile, "r")
33    if fd then
34        while true do
35            local ln = fd:read("*l")
36            if not ln then
37                break
38            else
39                local ts, mac, ip, name = ln:match("^(%d+) (%S+) (%S+) (%S+)")
40                if ts and mac and ip and name then
41                    rv[#rv+1] = {
42                        expires  = os.difftime(tonumber(ts) or 0, os.time()),
43                        macaddr  = mac,
44                        ipaddr   = ip,
45                        hostname = (name ~= "*") and name
46                    }
47                end
48            end
49        end
50        fd:close()
51    end
52
53    return rv
54end
55
56function wifi_networks()
57    local rv = { }
58    local ntm = require "luci.model.network".init()
59
60    local dev
61    for _, dev in ipairs(ntm:get_wifidevs()) do
62        local rd = {
63            up       = dev:is_up(),
64            device   = dev:name(),
65            name     = dev:get_i18n(),
66            networks = { }
67        }
68
69        local net
70        for _, net in ipairs(dev:get_wifinets()) do
71            rd.networks[#rd.networks+1] = {
72                name       = net:shortname(),
73                link       = net:adminlink(),
74                up         = net:is_up(),
75                mode       = net:active_mode(),
76                ssid       = net:active_ssid(),
77                bssid      = net:active_bssid(),
78                encryption = net:active_encryption(),
79                frequency  = net:frequency(),
80                channel    = net:channel(),
81                signal     = net:signal(),
82                quality    = net:signal_percent(),
83                noise      = net:noise(),
84                bitrate    = net:bitrate(),
85                ifname     = net:ifname(),
86                assoclist  = net:assoclist(),
87                country    = net:country(),
88                txpower    = net:txpower()
89            }
90        end
91
92        rv[#rv+1] = rd
93    end
94
95    return rv
96end
97
98function wifi_network(id)
99    local ntm = require "luci.model.network".init()
100    local net = ntm:get_wifinet(id)
101    if net then
102        local dev = net:get_device()
103        if dev then
104            return {
105                id         = id,
106                name       = net:shortname(),
107                link       = net:adminlink(),
108                up         = net:is_up(),
109                mode       = net:active_mode(),
110                ssid       = net:active_ssid(),
111                bssid      = net:active_bssid(),
112                encryption = net:active_encryption(),
113                frequency  = net:frequency(),
114                channel    = net:channel(),
115                signal     = net:signal(),
116                quality    = net:signal_percent(),
117                noise      = net:noise(),
118                bitrate    = net:bitrate(),
119                ifname     = net:ifname(),
120                assoclist  = net:assoclist(),
121                country    = net:country(),
122                txpower    = net:txpower(),
123                device     = {
124                    up     = dev:is_up(),
125                    device = dev:name(),
126                    name   = dev:get_i18n()
127                }
128            }
129        end
130    end
131    return { }
132end
Note: See TracBrowser for help on using the browser.