root/luci/trunk/modules/freifunk/luasrc/model/cbi/freifunk/public_status.lua @ 4128

Revision 4128, 6.1 KB (checked in by jow, 4 years ago)

modules/freifunk: display status of all non-encrypted wireless networks if no freifunk zone is defined

Line 
1require "luci.sys"
2require "luci.tools.webadmin"
3
4local bit = require "bit"
5local uci = luci.model.uci.cursor_state()
6
7local ffzone = luci.tools.webadmin.firewall_find_zone("freifunk")
8local ffznet = ffzone and uci:get("firewall", ffzone, "network")
9local ffwifs = ffznet and luci.util.split(ffznet, " ") or {}
10
11-- System --
12
13f = SimpleForm("system", "System")
14f.submit = false
15f.reset = false
16local system, model, memtotal, memcached, membuffers, memfree = luci.sys.sysinfo()
17local uptime = luci.sys.uptime()
18
19f:field(DummyValue, "_system", translate("system")).value = system
20f:field(DummyValue, "_cpu", translate("m_i_processor")).value = model
21
22local load1, load5, load15 = luci.sys.loadavg()
23f:field(DummyValue, "_la", translate("load")).value =
24string.format("%.2f, %.2f, %.2f", load1, load5, load15)
25
26f:field(DummyValue, "_memtotal", translate("m_i_memory")).value =
27string.format("%.2f MB (%.0f%% %s, %.0f%% %s, %.0f%% %s)",
28    tonumber(memtotal) / 1024,
29    100 * memcached / memtotal,
30    translate("mem_cached") or "",
31    100 * membuffers / memtotal,
32    translate("mem_buffered") or "",
33    100 * memfree / memtotal,
34    translate("mem_free") or "")
35
36f:field(DummyValue, "_systime", translate("m_i_systemtime")).value =
37os.date("%c")
38
39f:field(DummyValue, "_uptime", translate("m_i_uptime")).value =
40luci.tools.webadmin.date_format(tonumber(uptime))
41
42
43-- Wireless --
44
45local wireless = uci:get_all("wireless")
46local wifidata = luci.sys.wifi.getiwconfig()
47local ifaces = {}
48
49for k, v in pairs(wireless) do
50    if v[".type"] == "wifi-iface" and (
51        luci.util.contains(ffwifs, v.device) or
52        ( #ffwifs == 0 and (not v.encryption or v.encryption == "none") ) )
53    then
54        table.insert(ifaces, v)
55    end
56end
57
58
59m = SimpleForm("wireless", "Freifunk WLAN")
60m.submit = false
61m.reset = false
62
63s = m:section(Table, ifaces, translate("networks"))
64
65link = s:option(DummyValue, "_link", translate("link"))
66function link.cfgvalue(self, section)
67    local ifname = self.map:get(section, "ifname")
68    return wifidata[ifname] and wifidata[ifname]["Link Quality"] or "-"
69end
70
71essid = s:option(DummyValue, "ssid", "ESSID")
72
73bssid = s:option(DummyValue, "_bsiid", "BSSID")
74function bssid.cfgvalue(self, section)
75    local ifname = self.map:get(section, "ifname")
76    return (wifidata[ifname] and (wifidata[ifname].Cell
77        or wifidata[ifname]["Access Point"])) or "-"
78end
79
80channel = s:option(DummyValue, "channel", translate("channel"))
81    function channel.cfgvalue(self, section)
82    return wireless[self.map:get(section, "device")].channel
83end
84
85protocol = s:option(DummyValue, "_mode", translate("protocol"))
86function protocol.cfgvalue(self, section)
87    local mode = wireless[self.map:get(section, "device")].mode
88    return mode and "802." .. mode
89end
90
91mode = s:option(DummyValue, "mode", translate("mode"))
92encryption = s:option(DummyValue, "encryption", translate("iwscan_encr"))
93
94power = s:option(DummyValue, "_power", translate("power"))
95function power.cfgvalue(self, section)
96    local ifname = self.map:get(section, "ifname")
97    return wifidata[ifname] and wifidata[ifname]["Tx-Power"] or "-"
98end
99
100scan = s:option(Button, "_scan", translate("scan"))
101scan.inputstyle = "find"
102
103function scan.cfgvalue(self, section)
104    return self.map:get(section, "ifname") or false
105end
106
107t2 = m:section(Table, {}, translate("iwscan"), translate("iwscan1"))
108
109function scan.write(self, section)
110    t2.render = t2._render
111    local ifname = self.map:get(section, "ifname")
112    luci.util.update(t2.data, luci.sys.wifi.iwscan(ifname))
113end
114
115t2._render = t2.render
116t2.render = function() end
117
118t2:option(DummyValue, "Quality", translate("iwscan_link"))
119essid = t2:option(DummyValue, "ESSID", "ESSID")
120function essid.cfgvalue(self, section)
121    return luci.util.pcdata(self.map:get(section, "ESSID"))
122end
123
124t2:option(DummyValue, "Address", "BSSID")
125t2:option(DummyValue, "Mode", translate("mode"))
126chan = t2:option(DummyValue, "channel", translate("channel"))
127function chan.cfgvalue(self, section)
128    return self.map:get(section, "Channel")
129    or self.map:get(section, "Frequency")
130    or "-"
131end
132
133t2:option(DummyValue, "Encryption key", translate("iwscan_encr"))
134
135t2:option(DummyValue, "Signal level", translate("iwscan_signal"))
136
137t2:option(DummyValue, "Noise level", translate("iwscan_noise"))
138
139
140-- Routes --
141r = SimpleForm("routes", "Standardrouten")
142r.submit = false
143r.reset = false
144
145local routes = {}
146for i, route in ipairs(luci.sys.net.routes()) do
147    if route.dest:prefix() == 0 then
148        routes[#routes+1] = route
149    end
150end
151
152v = r:section(Table, routes)
153
154net = v:option(DummyValue, "iface", translate("network"))
155function net.cfgvalue(self, section)
156    return luci.tools.webadmin.iface_get_network(routes[section].device)
157    or routes[section].device
158end
159
160target  = v:option(DummyValue, "target", translate("target"))
161function target.cfgvalue(self, section)
162    return routes[section].dest:network():string()
163end
164
165netmask = v:option(DummyValue, "netmask", translate("netmask"))
166function netmask.cfgvalue(self, section)
167    return routes[section].dest:mask():string()
168end
169
170gateway = v:option(DummyValue, "gateway", translate("gateway"))
171function gateway.cfgvalue(self, section)
172    return routes[section].gateway:string()
173end
174
175metric = v:option(DummyValue, "metric", translate("metric"))
176function metric.cfgvalue(self, section)
177    return routes[section].metric
178end
179
180
181local routes6 = {}
182for i, route in ipairs(luci.sys.net.routes6() or {}) do
183    if route.dest:prefix() == 0 then
184        routes6[#routes6+1] = route
185    end
186end
187
188if #routes6 > 0 then
189    v6 = r:section(Table, routes6)
190
191    net = v6:option(DummyValue, "iface", translate("network"))
192    function net.cfgvalue(self, section)
193        return luci.tools.webadmin.iface_get_network(routes6[section].device)
194        or routes6[section].device
195    end
196
197    target  = v6:option(DummyValue, "target", translate("target"))
198    function target.cfgvalue(self, section)
199        return routes6[section].dest:string()
200    end
201
202    gateway = v6:option(DummyValue, "gateway6", translate("gateway6"))
203    function gateway.cfgvalue(self, section)
204        return routes6[section].source:string()
205    end
206
207    metric = v6:option(DummyValue, "metric", translate("metric"))
208    function metric.cfgvalue(self, section)
209        local metr = routes6[section].metric
210        local lower = bit.band(metr, 0xffff)
211        local higher = bit.rshift(bit.band(metr, 0xffff0000), 16)
212        return "%04X%04X" % {higher, lower}
213    end
214end
215
216return f, m, r
Note: See TracBrowser for help on using the browser.