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