modules/freifunk: handle v6 conditionally (as in network / routes)
[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
141 local routes = {}
142 for i, route in ipairs(luci.sys.net.routes()) do
143         if route.dest:prefix() == 0 then
144                 routes[#routes+1] = route
145         end
146 end
147
148 v = r:section(Table, routes)
149
150 net = v:option(DummyValue, "iface", translate("network"))
151 function net.cfgvalue(self, section)
152         return luci.tools.webadmin.iface_get_network(routes[section].device)
153         or routes[section].device
154 end
155
156 target  = v:option(DummyValue, "target", translate("target"))
157 function target.cfgvalue(self, section)
158         return routes[section].dest:network():string()
159 end
160
161 netmask = v:option(DummyValue, "netmask", translate("netmask"))
162 function netmask.cfgvalue(self, section)
163         return routes[section].dest:mask():string()
164 end
165
166 gateway = v:option(DummyValue, "gateway", translate("gateway"))
167 function gateway.cfgvalue(self, section)
168         return routes[section].gateway:string()
169 end
170
171 metric = v:option(DummyValue, "metric", translate("metric"))
172 function metric.cfgvalue(self, section)
173         return routes[section].metric
174 end
175
176
177 local routes6 = {}
178 for i, route in ipairs(luci.sys.net.routes6() or {}) do
179         if route.dest:prefix() == 0 then
180                 routes6[#routes6+1] = route
181         end
182 end
183
184 if #routes6 > 0 then
185         v6 = r:section(Table, routes6)
186
187         net = v6:option(DummyValue, "iface", translate("network"))
188         function net.cfgvalue(self, section)
189                 return luci.tools.webadmin.iface_get_network(routes[section].device)
190                 or routes6[section].device
191         end
192
193         target  = v6:option(DummyValue, "target", translate("target"))
194         function target.cfgvalue(self, section)
195                 return routes6[section].dest:string()
196         end
197
198         gateway = v6:option(DummyValue, "gateway6", translate("gateway6"))
199         function gateway.cfgvalue(self, section)
200                 return routes6[section].source:string()
201         end
202
203         metric = v6:option(DummyValue, "metric", translate("metric"))
204         function metric.cfgvalue(self, section)
205                 return string.format("%X", routes6[section].metric)
206         end
207 end
208
209 return f, m, r