luci-mod-freifunk: Fix "Undefined reference to net.iwdata"
[project/luci.git] / modules / luci-mod-freifunk / luasrc / view / freifunk / public_status.htm
1 <%
2 local utl = require "luci.util"
3 local sys = require "luci.sys"
4 local twa = require "luci.tools.webadmin"
5 local ip  = require "luci.ip"
6
7 -- System
8
9 local sysinfo = utl.ubus("system", "info") or { }
10 local boardinfo = utl.ubus("system", "board") or { }
11
12 local loads = sysinfo.load or { 0, 0, 0 }
13 local meminfo = sysinfo.memory or {
14         total = 0,
15         free = 0,
16         buffered = 0,
17         shared = 0
18 }
19
20 local uptime = twa.date_format(sysinfo.uptime or 0)
21 local time = os.date("%a, %d %b %Y, %H:%M:%S")
22 local load = string.format("%.2f, %.2f, %.2f", loads[1] / 65535.0, loads[2] / 65535.0, loads[3] / 65535.0)
23
24 local mem = string.format(
25         "%.2f MB (%.2f %s, %.2f %s, %.2f %s)",
26         meminfo.total / 1024 / 1024,
27         (meminfo.total - meminfo.free) / 1024 / 1024,
28         tostring(i18n.translate("used")),
29         meminfo.free / 1024 / 1024,
30         tostring(i18n.translate("free")),
31         meminfo.buffered / 1024 / 1024,
32         tostring(i18n.translate("buffered"))
33 )
34
35 local interval = 5
36
37 -- wireless
38 local ntm = require "luci.model.network".init()
39 local devices  = ntm:get_wifidevs()
40 local netlist = { }
41 local netdevs = { }
42 local dev
43 for _, dev in ipairs(devices) do
44         local net
45         for _, net in ipairs(dev:get_wifinets()) do
46                 netlist[#netlist+1] = net:ifname()
47                 netdevs[net:ifname()] = dev:name()
48         end
49 end
50 local has_iwinfo = pcall(require, "iwinfo")
51
52
53 -- Find default routes
54
55 local _, r, def4, def6
56
57 for _, r in ipairs(ip.routes({ type = 1, dest_exact = "0.0.0.0/0" })) do
58         def4 = {
59                 gateway = r.gw:string(),
60                 dest = r.dest:string(),
61                 dev = r.dev,
62                 metr = r.metric or 0
63         }
64         break
65 end
66
67 for _, r in ipairs(ip.routes({ type = 1, dest_exact = "::/0" })) do
68         def6 = {
69                 gateway = r.gw:string(),
70                 dest = r.dest:string(),
71                 dev = r.dev,
72                 metr = r.metric or 0
73         }
74         break
75 end
76
77
78 if luci.http.formvalue("status") == "1" then
79         local rv = { }
80         for dev in pairs(netdevs) do
81                 local j = { id = dev }
82                 local iw = luci.sys.wifi.getiwinfo(dev)
83                 if iw then
84                         local f
85                         for _, f in ipairs({
86                                 "channel", "txpower", "bitrate", "signal", "noise",
87                                 "quality", "quality_max", "mode", "ssid", "bssid", "encryption", "ifname"
88                         }) do
89                                 j[f] = iw[f]
90                         end
91                 end
92                 rv[#rv+1] = j
93         end
94
95
96         rv[#rv+1] = {
97                 time = time,
98                 uptime = uptime,
99                 load = load,
100                 mem = mem,
101                 defroutev4 = def4,
102                 defroutev6 = def6
103         }
104
105         luci.http.prepare_content("application/json")
106         luci.http.write_json(rv)
107         return
108 end
109 -%>
110
111 <%+header%>
112
113 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
114
115 <script type="text/javascript">//<![CDATA[
116          XHR.poll(<%=interval%> , '<%=REQUEST_URI%>', { status: 1 },
117                 function(x, st)
118                 {
119                         if (st)
120                         {
121                                 for( var i = 0; i < st.length; i++ )
122                                 {
123                                         var iw = st[i];
124                                         var is_assoc = (iw.bssid && iw.channel);
125                                         var p = (100 / iw.quality_max * iw.quality);
126                                         var q = is_assoc ? p : -1;
127
128                                         var icon;
129                                         if (q < 0)
130                                                 icon = "<%=resource%>/icons/signal-none.png";
131                                         else if (q == 0)
132                                                 icon = "<%=resource%>/icons/signal-0.png";
133                                         else if (q < 25)
134                                                 icon = "<%=resource%>/icons/signal-0-25.png";
135                                         else if (q < 50)
136                                                 icon = "<%=resource%>/icons/signal-25-50.png";
137                                         else if (q < 75)
138                                                 icon = "<%=resource%>/icons/signal-50-75.png";
139                                         else
140                                                 icon = "<%=resource%>/icons/signal-75-100.png";
141
142                                         var power = document.getElementById(iw.id + '-txpower');
143                                         if (power)
144                                                 power.innerHTML = String.format('%s dbm', iw.txpower);
145
146                                         var signal = document.getElementById(iw.id + '-signal');
147                                         if (signal)
148                                                 signal.innerHTML = String.format(
149                                                         '<img src="%s" title="Signal: %s db / Noise: %s db" alt="Signal Quality" />',
150                                                         icon, iw.signal, iw.noise
151                                                 );
152
153                                         var bitrate = document.getElementById(iw.id + '-bitrate');
154                                         if (bitrate)
155                                                 bitrate.innerHTML = String.format('%s Mb/s', iw.bitrate ? iw.bitrate / 1000 : '?');
156
157                                         var ssid = document.getElementById(iw.id + '-ssid');
158                                         if (ssid)
159                                                 ssid.innerHTML = iw.ssid;
160
161                                         var bssid = document.getElementById(iw.id + '-bssid');
162                                         if (bssid)
163                                                 bssid.innerHTML = iw.bssid;
164
165                                         var channel = document.getElementById(iw.id + '-channel');
166                                         if (channel)
167                                                 channel.innerHTML = iw.channel;
168
169                                         var mode = document.getElementById(iw.id + '-mode');
170                                         if (mode)
171                                         mode.innerHTML = iw.mode;
172                                 }
173
174                                 i = st.length - 1
175                                 var u
176
177                                 if (u = document.getElementById('dynuptime'))
178                                         u.innerHTML = st[i].uptime;
179
180                                 if (u = document.getElementById('dynload'))
181                                         u.innerHTML = st[i].load;
182
183                                 if (u = document.getElementById('dynmem'))
184                                         u.innerHTML = st[i].mem;
185
186                                 if (u = document.getElementById('dyntime'))
187                                         u.innerHTML = st[i].time;
188
189                                 if (st[i].defroutev4)
190                                 {
191                                         if (u = document.getElementById('v4dst'))
192                                                 u.innerHTML = st[i].defroutev4.dest;
193
194                                         if (u = document.getElementById('v4gw'))
195                                                 u.innerHTML = st[i].defroutev4.gateway;
196
197                                         if (u = document.getElementById('v4dev'))
198                                                 u.innerHTML = st[i].defroutev4.dev;
199
200                                         if (u = document.getElementById('v4metr'))
201                                                 u.innerHTML = st[i].defroutev4.metr;
202                                 }
203
204                                 if (st[i].defroutev6)
205                                 {
206                                         if (u = document.getElementById('v6dst'))
207                                                 u.innerHTML = st[i].defroutev6.dest;
208
209                                         if (u = document.getElementById('v6gw'))
210                                                 u.innerHTML = st[i].defroutev6.gateway;
211
212                                         if (u = document.getElementById('v6dev'))
213                                                 u.innerHTML = st[i].defroutev6.dev;
214
215                                         if (u = document.getElementById('v6metr'))
216                                                 u.innerHTML = st[i].defroutev6.metr;
217                                 }
218                         }
219                 }
220         );
221 //]]></script>
222
223 <div class="cbi-map">
224         <h2><%:System%></h2>
225         <div class="cbi-section-node">
226                 <div class="cbi-value"><label class="cbi-value-title"><%:System%></label><div class="cbi-value-field"><%=boardinfo.system or "?"%></div></div>
227                 <div class="cbi-value"><label class="cbi-value-title"><%:Model%></label><div class="cbi-value-field"><%=boardinfo.model or "?"%></div></div>
228                 <div class="cbi-value"><label class="cbi-value-title"><%:Load%></label><div class="cbi-value-field" id="dynload"><%=load%></div></div>
229                 <div class="cbi-value"><label class="cbi-value-title"><%:Memory%></label><div class="cbi-value-field" id="dynmem"><%=mem%></div></div>
230                 <div class="cbi-value"><label class="cbi-value-title"><%:Local Time%></label><div class="cbi-value-field" id="dyntime"><%=time%></div></div>
231                 <div class="cbi-value"><label class="cbi-value-title"><%:Uptime%></label><div class="cbi-value-field" id="dynuptime"><%=uptime%></div></div>
232         </div>
233 </div>
234
235 <% if devices[1] then %>
236
237 <div class="cbi-map">
238         <h2><%:Wireless Overview%></h2>
239
240                 <% if not has_iwinfo then %>
241                         <div class="errorbox">
242                                 <strong><%:Package libiwinfo required!%></strong><br />
243                                 <%_The <em>libiwinfo</em> package is not installed. You must install this component for working wireless configuration!%>
244                         </div>
245                 <% end %>
246
247                 <div class="cbi-section">
248                         <div class="cbi-section-node">
249                                 <table class="cbi-section-table">
250                                         <tr class="cbi-section-table-titles">
251                                                 <th class="cbi-section-table-cell"><%:Signal%></th>
252                                                 <th class="cbi-section-table-cell"><%:Bitrate%></th>
253                                                 <th class="cbi-section-table-cell"><%:SSID%></th>
254                                                 <th class="cbi-section-table-cell"><%:BSSID%></th>
255                                                 <th class="cbi-section-table-cell"><%:Channel%></th>
256                                                 <th class="cbi-section-table-cell"><%:Mode%></th>
257                                                 <th class="cbi-section-table-cell"><%:TX%>-<%:Power%></th>
258                                                 <th class="cbi-section-table-cell"><%:Interface%></th>
259                                         </tr>
260         <%
261         for _, dev in ipairs(devices) do
262         local net
263                 for _, net in ipairs(dev:get_wifinets()) do
264                         netlist[#netlist+1] = net:ifname()
265                         netdevs[net:ifname()] = dev:name()
266
267                         if net.iwinfo.signal and net.iwinfo.bssid then
268                                 local signal = net.iwinfo.signal or "N/A"
269                                 local noise = net.iwinfo.noise or "N/A"
270                                 local q = net.iwinfo.quality or "0"
271                                 local qmax = net.iwinfo.quality_max or "100"
272                                 local qperc = q / qmax * 100
273
274                                 if qperc == 0 then
275                                         icon = "signal-none.png"
276                                 elseif qperc < 26 then
277                                         icon = "signal-0-25.png"
278                                 elseif qperc < 51 then
279                                         icon = "signal-25-50.png"
280                                 elseif qperc < 76 then
281                                         icon = "signal-50-75.png"
282                                 elseif qperc < 100 then
283                                         icon = "signal-75-100.png"
284                                 else
285                                         icon = "signal-0.png"
286                                 end
287
288                                 signal_string = "<img src='"..resource.."/icons/"..icon.."' title='Signal: "..signal.." db / Noise: "..noise.." db' alt='Signal Quality'></img>"
289
290                                 local ssid = net.iwinfo.ssid or "N/A"
291                                 local bssid = net.iwinfo.bssid or "N/A"
292                                 local chan = net.iwinfo.channel or "N/A"
293                                 local mode = net.iwinfo.mode or "N/A"
294                                 local txpwr = net.iwinfo.txpower or "N/A"
295                                 if txpwr ~= "N/A" then
296                                         txpwr = txpwr.." dbm"
297                                 end
298                                 local bitrate = net.iwinfo.bitrate or "N/A"
299                                 if bitrate ~= "N/A" then
300                                         bitrate = ( bitrate / 1000 ).."Mb/s"
301                                 end
302                                 local interface = net.iwinfo.ifname or "N/A"
303         %>
304                                                 <tr class="cbi-section-table-row cbi-rowstyle-1">
305                                                 <td class="cbi-value-field" id="<%=net:ifname()%>-signal"><%=signal_string%></td>
306                                                 <td class="cbi-value-field" id="<%=net:ifname()%>-bitrate"><%=bitrate%></td>
307                                                 <td class="cbi-value-field" id="<%=net:ifname()%>-ssid"><%=ssid%></td>
308                                                 <td class="cbi-value-field" id="<%=net:ifname()%>-bssid"><%=bssid%></td>
309                                                 <td class="cbi-value-field" id="<%=net:ifname()%>-channel"><%=chan%></td>
310                                                 <td class="cbi-value-field" id="<%=net:ifname()%>-mode"><%=mode%></td>
311                                                 <td class="cbi-value-field" id="<%=net:ifname()%>-txpower"><%=txpwr%></td>
312                                                 <td class="cbi-value-field"><%=interface%></td>
313                                         </tr>
314                         <% end
315                 end
316         end %>
317                         </table>
318                 </div>
319         </div>
320 </div>
321 <% end %>
322
323 <div class="cbi-map">
324         <h2><%:Default routes%></h2>
325                 <div class="cbi-section">
326                         <div class="cbi-section-node">
327
328 <% if not def4 and not def6 then %>
329         <%:No default routes known.%>
330 <%else%>
331                                 <table class="cbi-section-table">
332                                                 <tr class="cbi-section-table-titles">
333                                                         <th class="cbi-section-table-cell"><%:Network%></th>
334                                                         <th class="cbi-section-table-cell"><%:Interface%></th>
335                                                         <th class="cbi-section-table-cell"><%:Gateway%></th>
336                                                         <th class="cbi-section-table-cell"><%:Metric%></th>
337                                                 </tr>
338
339         <% if def4 then %>
340                                                 <tr class="cbi-section-table-row cbi-rowstyle-1">
341                                                         <td class="cbi-value-field" id="v4dst"><%=def4.dest%></td>
342                                                         <td class="cbi-value-field" id="v4dev"><%=def4.dev%></td>
343                                                         <td class="cbi-value-field" id="v4gw"><%=def4.gateway%></td>
344                                                         <td class="cbi-value-field" id="v4metr"><%=def4.metr%></td>
345                                                 </tr>
346
347         <% end
348         if def6 then %>
349
350                                                 <tr class="cbi-section-table-row cbi-rowstyle-2">
351                                                         <td class="cbi-value-field" id="v6dst"><%=def6.dest%></td>
352                                                         <td class="cbi-value-field" id="v6dev"><%=def6.dev%></td>
353                                                         <td class="cbi-value-field" id="v6gw"><%=def6.gateway%></td>
354                                                         <td class="cbi-value-field" id="v6metr"><%=def6.metr%></td>
355                                                 </tr>
356
357         <% end %>
358
359                                 </table>
360 <% end %>
361                 </div>
362         </div>
363 </div>
364 <%+footer%>