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