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