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