465143ebce5e631be2c07756eea094188e7845a9
[project/luci.git] / modules / admin-full / luasrc / view / admin_status / wireless.htm
1 <%#
2 LuCI - Lua Configuration Interface
3 Copyright 2011 Jo-Philipp Wich <xm@subsignal.org>
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 -%>
12
13 <%-
14 local ntm = require "luci.model.network".init()
15
16 local dev
17 local devices = { }
18 for _, dev in luci.util.vspairs(luci.sys.net.devices()) do
19 if dev:match("^wlan%d") or dev:match("^ath%d") or dev:match("^wl%d") then
20 devices[#devices+1] = dev
21 end
22 end
23
24 local curdev = luci.dispatcher.context.requestpath
25 curdev = curdev[#curdev] ~= "wireless" and curdev[#curdev] or devices[1]
26 -%>
27
28 <%+header%>
29
30 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
31 <script type="text/javascript">//<![CDATA[
32 var bwxhr = new XHR();
33
34 var G, G2;
35 var TIME = 0;
36 var RATE = 1;
37 var RSSI = 2;
38 var NOISE = 3;
39
40 var width = 760;
41 var height = 300;
42 var step = 5;
43
44 var data_wanted = Math.floor(width / step);
45 var data_fill = 0;
46 var data_stamp = 0;
47
48 var data_rssi = [ ];
49 var data_noise = [ ];
50 var data_rate = [ ];
51
52 var line_rssi;
53 var line_noise;
54 var line_rate;
55
56 var label_25, label_25_2;
57 var label_50, label_50_2;
58 var label_75, label_75_2;
59
60 var label_rssi_cur;
61 var label_rssi_avg;
62 var label_rssi_peak;
63
64 var label_noise_cur;
65 var label_noise_avg;
66 var label_noise_peak;
67
68 var label_rate_cur;
69 var label_rate_avg;
70 var label_rate_peak;
71
72 var label_scale;
73
74
75 /* wait for SVG */
76 window.setTimeout(
77 function() {
78 var svg = document.getElementById('iwsvg');
79 var svg2 = document.getElementById('iwsvg2');
80
81 try {
82 G = svg.getSVGDocument
83 ? svg.getSVGDocument() : svg.contentDocument;
84 G2 = svg2.getSVGDocument
85 ? svg2.getSVGDocument() : svg2.contentDocument;
86 }
87 catch(e) {
88 G = document.embeds['iwsvg'].getSVGDocument();
89 G2 = document.embeds['iwsvg2'].getSVGDocument();
90 }
91
92 if (!G || !G2)
93 {
94 window.setTimeout(arguments.callee, 1000);
95 }
96 else
97 {
98 /* find sizes */
99 width = svg.offsetWidth - 2;
100 height = svg.offsetHeight - 2;
101 data_wanted = Math.ceil(width / step);
102
103 /* prefill datasets */
104 for (var i = 0; i < data_wanted; i++)
105 {
106 data_rssi[i] = 0;
107 data_noise[i] = 0;
108 data_rate[i] = 0;
109 }
110
111 /* find svg elements */
112 line_rssi = G.getElementById('rssi');
113 line_noise = G.getElementById('noise');
114 line_rate = G2.getElementById('rate');
115
116 label_25 = G.getElementById('label_25');
117 label_50 = G.getElementById('label_50');
118 label_75 = G.getElementById('label_75');
119 label_25_2 = G2.getElementById('label_25');
120 label_50_2 = G2.getElementById('label_50');
121 label_75_2 = G2.getElementById('label_75');
122
123 label_rssi_cur = document.getElementById('rssi_bw_cur');
124 label_rssi_avg = document.getElementById('rssi_bw_avg');
125 label_rssi_peak = document.getElementById('rssi_bw_peak');
126
127 label_noise_cur = document.getElementById('noise_bw_cur');
128 label_noise_avg = document.getElementById('noise_bw_avg');
129 label_noise_peak = document.getElementById('noise_bw_peak');
130
131 label_rate_cur = document.getElementById('rate_bw_cur');
132 label_rate_avg = document.getElementById('rate_bw_avg');
133 label_rate_peak = document.getElementById('rate_bw_peak');
134
135 label_scale = document.getElementById('scale');
136 label_scale_2 = document.getElementById('scale2');
137
138
139 /* plot horizontal time interval lines */
140 for (var i = width % (step * 60); i < width; i += step * 60)
141 {
142 var line = G.createElementNS('http://www.w3.org/2000/svg', 'line');
143 line.setAttribute('x1', i);
144 line.setAttribute('y1', 0);
145 line.setAttribute('x2', i);
146 line.setAttribute('y2', '100%');
147 line.setAttribute('style', 'stroke:black;stroke-width:0.1');
148
149 var text = G.createElementNS('http://www.w3.org/2000/svg', 'text');
150 text.setAttribute('x', i + 5);
151 text.setAttribute('y', 15);
152 text.setAttribute('style', 'fill:#999999; font-size:9pt');
153 text.appendChild(G.createTextNode(Math.round((width - i) / step / 60) + 'm'));
154
155 label_25.parentNode.appendChild(line);
156 label_25.parentNode.appendChild(text);
157
158
159 var line2 = G2.createElementNS('http://www.w3.org/2000/svg', 'line');
160 line2.setAttribute('x1', i);
161 line2.setAttribute('y1', 0);
162 line2.setAttribute('x2', i);
163 line2.setAttribute('y2', '100%');
164 line2.setAttribute('style', 'stroke:black;stroke-width:0.1');
165
166 var text2 = G2.createElementNS('http://www.w3.org/2000/svg', 'text');
167 text2.setAttribute('x', i + 5);
168 text2.setAttribute('y', 15);
169 text2.setAttribute('style', 'fill:#999999; font-size:9pt');
170 text2.appendChild(G.createTextNode(Math.round((width - i) / step / 60) + 'm'));
171
172 label_25_2.parentNode.appendChild(line2);
173 label_25_2.parentNode.appendChild(text2);
174 }
175
176 label_scale.innerHTML = String.format('<%:(%d minute window, %d second interval)%>', data_wanted / 60, 3);
177 label_scale_2.innerHTML = String.format('<%:(%d minute window, %d second interval)%>', data_wanted / 60, 3);
178
179 /* render datasets, start update interval */
180 XHR.poll(3, '<%=build_url("admin/status/realtime/wireless_status", curdev)%>', null,
181 function(x, data)
182 {
183 var noise_floor = 255;
184 var rate_floor = 60000;
185
186 for (var i = 0; i < data.length; i++) {
187 noise_floor = Math.min(noise_floor, data[i][NOISE]);
188 rate_floor = Math.min(rate_floor, data[i][RATE]);
189 }
190
191 noise_floor -= 5;
192
193 var data_max = 0;
194 var data_scale = 0;
195 var data_max_2 = 0;
196 var data_scale_2 = 0;
197
198 var data_rssi_avg = 0;
199 var data_noise_avg = 0;
200 var data_rate_avg = 0;
201
202 var data_rssi_peak = 0;
203 var data_noise_peak = 0;
204 var data_rate_peak = 0;
205
206 for (var i = data_stamp ? 0 : 1; i < data.length; i++)
207 {
208 /* skip overlapping entries */
209 if (data[i][TIME] <= data_stamp)
210 continue;
211
212 data_rssi.push(data[i][RSSI] - noise_floor);
213 data_noise.push(data[i][NOISE] - noise_floor);
214 data_rate.push(Math.floor(data[i][RATE] / 1000));
215 }
216
217 /* cut off outdated entries */
218 data_rssi = data_rssi.slice(data_rssi.length - data_wanted, data_rssi.length);
219 data_noise = data_noise.slice(data_noise.length - data_wanted, data_noise.length);
220 data_rate = data_rate.slice(data_rate.length - data_wanted, data_rate.length);
221
222 /* find peak */
223 for (var i = 0; i < data_rssi.length; i++)
224 {
225 data_max = Math.max(data_max, data_rssi[i]);
226 data_max_2 = Math.max(data_max_2, data_rate[i]);
227
228 data_rssi_peak = Math.max(data_rssi_peak, data_rssi[i]);
229 data_noise_peak = Math.max(data_noise_peak, data_noise[i]);
230 data_rate_peak = Math.max(data_rate_peak, data_rate[i]);
231
232 if (i > 0)
233 {
234 data_rssi_avg = (data_rssi_avg + data_rssi[i]) / 2;
235 data_noise_avg = (data_noise_avg + data_noise[i]) / 2;
236 data_rate_avg = (data_rate_avg + data_rate[i]) / 2;
237 }
238 else
239 {
240 data_rssi_avg = data_rssi[i];
241 data_noise_avg = data_noise[i];
242 data_rate_avg = data_rate[i];
243 }
244 }
245
246 /* remember current timestamp, calculate horizontal scale */
247 data_stamp = data[data.length-1][TIME];
248 data_scale = (height / (data_max * 1.1)).toFixed(1);
249 data_scale_2 = (height / (data_max_2 * 1.1)).toFixed(1);
250
251 /* plot data */
252 var pt_rssi = '0,' + height;
253 var pt_noise = '0,' + height;
254 var pt_rate = '0,' + height;
255
256 var y_rssi = 0;
257 var y_noise = 0;
258 var y_rate = 0;
259
260 for (var i = 0; i < data_rssi.length; i++)
261 {
262 var x = i * step;
263
264 y_rssi = height - Math.floor(data_rssi[i] * data_scale);
265 y_noise = height - Math.floor(data_noise[i] * data_scale);
266 y_rate = height - Math.floor(data_rate[i] * data_scale_2);
267
268 y_rssi -= Math.floor(y_rssi % (1/data_scale));
269 y_noise -= Math.floor(y_noise % (1/data_scale));
270
271 pt_rssi += ' ' + x + ',' + y_rssi;
272 pt_noise += ' ' + x + ',' + y_noise;
273 pt_rate += ' ' + x + ',' + y_rate;
274 }
275
276 pt_rssi += ' ' + width + ',' + y_rssi + ' ' + width + ',' + height;
277 pt_noise += ' ' + width + ',' + y_noise + ' ' + width + ',' + height;
278 pt_rate += ' ' + width + ',' + y_rate + ' ' + width + ',' + height;
279
280 line_rssi.setAttribute('points', pt_rssi);
281 line_noise.setAttribute('points', pt_noise);
282 line_rate.setAttribute('points', pt_rate);
283
284 function wireless_label(dbm, noise)
285 {
286 if (noise)
287 return String.format("%d <%:dBm%> (SNR %d <%:dBm%>)", noise_floor + dbm - 255, dbm - noise);
288 else
289 return String.format("%d <%:dBm%>", noise_floor + dbm - 255);
290 }
291
292 function rate_label(mbit)
293 {
294 return String.format("%d <%:Mbit/s%>", mbit);
295 }
296
297 label_25.firstChild.data = wireless_label(1.1 * 0.25 * data_max);
298 label_50.firstChild.data = wireless_label(1.1 * 0.50 * data_max);
299 label_75.firstChild.data = wireless_label(1.1 * 0.75 * data_max);
300
301 label_25_2.firstChild.data = rate_label(1.1 * 0.25 * data_max_2);
302 label_50_2.firstChild.data = rate_label(1.1 * 0.50 * data_max_2);
303 label_75_2.firstChild.data = rate_label(1.1 * 0.75 * data_max_2);
304
305 label_rssi_cur.innerHTML = wireless_label(data_rssi[data_rssi.length-1], data_noise[data_noise.length-1]).nobr();
306 label_noise_cur.innerHTML = wireless_label(data_noise[data_noise.length-1]).nobr();
307
308 label_rssi_avg.innerHTML = wireless_label(data_rssi_avg, data_noise_avg).nobr();
309 label_noise_avg.innerHTML = wireless_label(data_noise_avg).nobr();
310
311 label_rssi_peak.innerHTML = wireless_label(data_rssi_peak, data_noise_peak).nobr();
312 label_noise_peak.innerHTML = wireless_label(data_noise_peak).nobr();
313
314 label_rate_cur.innerHTML = rate_label(data_rate[data_rate.length-1]);
315 label_rate_avg.innerHTML = rate_label(data_rate_avg);
316 label_rate_peak.innerHTML = rate_label(data_rate_peak);
317 }
318 );
319 }
320 }, 1000
321 );
322 //]]></script>
323
324 <h2><a id="content" name="content"><%:Realtime Wireless%></a></h2>
325
326 <ul class="cbi-tabmenu">
327 <% for _, dev in ipairs(devices) do %>
328 <li class="cbi-tab<%= dev == curdev and "" or "-disabled" %>"><a href="<%=pcdata(dev)%>"><%=pcdata(dev)%></a></li>
329 <% end %>
330 </ul>
331
332 <embed id="iwsvg" style="width:100%; height:300px; border:1px solid #000000; background-color:#FFFFFF" src="<%=resource%>/wireless.svg" />
333 <div style="text-align:right"><small id="scale">-</small></div>
334 <br />
335
336 <table style="width:100%; table-layout:fixed" cellspacing="5">
337 <tr>
338 <td style="text-align:right; vertical-align:top"><strong style="border-bottom:2px solid blue"><%:Signal:%></strong></td>
339 <td id="rssi_bw_cur">0 <%:dBm%></td>
340
341 <td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td>
342 <td id="rssi_bw_avg">0 <%:dBm%></td>
343
344 <td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td>
345 <td id="rssi_bw_peak">0 <%:dBm%></td>
346 </tr>
347 <tr>
348 <td style="text-align:right; vertical-align:top"><strong style="border-bottom:2px solid red"><%:Noise:%></strong></td>
349 <td id="noise_bw_cur">0 <%:dBm%></td>
350
351 <td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td>
352 <td id="noise_bw_avg">0 <%:dBm%></td>
353
354 <td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td>
355 <td id="noise_bw_peak">0 <%:dBm%></td>
356 </tr>
357 </table>
358
359 <br />
360
361 <embed id="iwsvg2" style="width:100%; height:300px; border:1px solid #000000; background-color:#FFFFFF" src="<%=resource%>/wifirate.svg" />
362 <div style="text-align:right"><small id="scale2">-</small></div>
363 <br />
364
365 <table style="width:100%; table-layout:fixed" cellspacing="5">
366 <tr>
367 <td style="text-align:right; vertical-align:top"><strong style="border-bottom:2px solid green"><%:Phy Rate:%></strong></td>
368 <td id="rate_bw_cur">0 MBit/s</td>
369
370 <td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td>
371 <td id="rate_bw_avg">0 MBit/s</td>
372
373 <td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td>
374 <td id="rate_bw_peak">0 MBit/s</td>
375 </tr>
376 </table>
377
378 <%+footer%>