modules/freifunk: fix same problem
[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         tostring(translate("mem_cached"), "")),
31         100 * membuffers / memtotal,
32         tostring(translate("mem_buffered", "")),
33         100 * memfree / memtotal,
34         tostring(translate("mem_free", ""))
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 (
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
56 end
57
58
59 m = SimpleForm("wireless", "Freifunk WLAN")
60 m.submit = false
61 m.reset = false
62
63 s = m:section(Table, ifaces, translate("networks"))
64
65 link = s:option(DummyValue, "_link", translate("link"))
66 function link.cfgvalue(self, section)
67         local ifname = self.map:get(section, "ifname")
68         return wifidata[ifname] and wifidata[ifname]["Link Quality"] or "-"
69 end
70
71 essid = s:option(DummyValue, "ssid", "ESSID")
72
73 bssid = s:option(DummyValue, "_bsiid", "BSSID")
74 function 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 "-"
78 end
79
80 channel = s:option(DummyValue, "channel", translate("channel"))
81         function channel.cfgvalue(self, section)
82         return wireless[self.map:get(section, "device")].channel
83 end
84
85 protocol = s:option(DummyValue, "_mode", translate("protocol"))
86 function protocol.cfgvalue(self, section)
87         local mode = wireless[self.map:get(section, "device")].mode
88         return mode and "802." .. mode
89 end
90
91 mode = s:option(DummyValue, "mode", translate("mode"))
92 encryption = s:option(DummyValue, "encryption", translate("iwscan_encr"))
93
94 power = s:option(DummyValue, "_power", translate("power"))
95 function power.cfgvalue(self, section)
96         local ifname = self.map:get(section, "ifname")
97         return wifidata[ifname] and wifidata[ifname]["Tx-Power"] or "-"
98 end
99
100 scan = s:option(Button, "_scan", translate("scan"))
101 scan.inputstyle = "find"
102
103 function scan.cfgvalue(self, section)
104         return self.map:get(section, "ifname") or false
105 end
106
107 t2 = m:section(Table, {}, translate("iwscan"), translate("iwscan1"))
108
109 function 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))
113 end
114
115 t2._render = t2.render
116 t2.render = function() end
117
118 t2:option(DummyValue, "Quality", translate("iwscan_link"))
119 essid = t2:option(DummyValue, "ESSID", "ESSID")
120 function essid.cfgvalue(self, section)
121         return luci.util.pcdata(self.map:get(section, "ESSID"))
122 end
123
124 t2:option(DummyValue, "Address", "BSSID")
125 t2:option(DummyValue, "Mode", translate("mode"))
126 chan = t2:option(DummyValue, "channel", translate("channel"))
127 function chan.cfgvalue(self, section)
128         return self.map:get(section, "Channel")
129         or self.map:get(section, "Frequency")
130         or "-"
131 end
132
133 t2:option(DummyValue, "Encryption key", translate("iwscan_encr"))
134
135 t2:option(DummyValue, "Signal level", translate("iwscan_signal"))
136
137 t2:option(DummyValue, "Noise level", translate("iwscan_noise"))
138
139
140 -- Routes --
141 r = SimpleForm("routes", "Standardrouten")
142 r.submit = false
143 r.reset = false
144
145 local routes = {}
146 for i, route in ipairs(luci.sys.net.routes()) do
147         if route.dest:prefix() == 0 then
148                 routes[#routes+1] = route
149         end
150 end
151
152 v = r:section(Table, routes)
153
154 net = v:option(DummyValue, "iface", translate("network"))
155 function net.cfgvalue(self, section)
156         return luci.tools.webadmin.iface_get_network(routes[section].device)
157         or routes[section].device
158 end
159
160 target  = v:option(DummyValue, "target", translate("target"))
161 function target.cfgvalue(self, section)
162         return routes[section].dest:network():string()
163 end
164
165 netmask = v:option(DummyValue, "netmask", translate("netmask"))
166 function netmask.cfgvalue(self, section)
167         return routes[section].dest:mask():string()
168 end
169
170 gateway = v:option(DummyValue, "gateway", translate("gateway"))
171 function gateway.cfgvalue(self, section)
172         return routes[section].gateway:string()
173 end
174
175 metric = v:option(DummyValue, "metric", translate("metric"))
176 function metric.cfgvalue(self, section)
177         return routes[section].metric
178 end
179
180
181 local routes6 = {}
182 for i, route in ipairs(luci.sys.net.routes6() or {}) do
183         if route.dest:prefix() == 0 then
184                 routes6[#routes6+1] = route
185         end
186 end
187
188 if #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
214 end
215
216 return f, m, r