summaryrefslogtreecommitdiffhomepage
path: root/modules/admin-full/luasrc/view/admin_status/wireless.htm
blob: eb91ae649ffa45fca135aa690e3f08466f806420 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<%#
LuCI - Lua Configuration Interface
Copyright 2011 Jo-Philipp Wich <xm@subsignal.org>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

$Id$

-%>

<%-
	local ntm = require "luci.model.network".init()

	local dev
	local devices = { }
	for _, dev in luci.util.vspairs(luci.sys.net.devices()) do
		if dev:match("^wlan%d") or dev:match("^ath%d") or dev:match("^wl%d") then
			devices[#devices+1] = dev
		end
	end

	local curdev = luci.dispatcher.context.requestpath
	      curdev = curdev[#curdev] ~= "wireless" and curdev[#curdev] or devices[1]
-%>

<%+header%>

<script type="text/javascript" src="<%=resource%>/cbi.js"></script>
<script type="text/javascript">//<![CDATA[
	var bwxhr = new XHR();

	var G, G2;
	var TIME  = 0;
	var RATE  = 1;
	var RSSI  = 2;
	var NOISE = 3;

	var width  = 760;
	var height = 300;
	var step   = 5;

	var data_wanted = Math.floor(width / step);
	var data_fill   = 0;
	var data_stamp  = 0;

	var data_rssi = [ ];
	var data_noise = [ ];
	var data_rate = [ ];

	var line_rssi;
	var line_noise;
	var line_rate;

	var label_25, label_25_2;
	var label_50, label_50_2;
	var label_75, label_75_2;

	var label_rssi_cur;
	var label_rssi_avg;
	var label_rssi_peak;

	var label_noise_cur;
	var label_noise_avg;
	var label_noise_peak;

	var label_rate_cur;
	var label_rate_avg;
	var label_rate_peak;

	var label_scale;


	/* wait for SVG */
	window.setTimeout(
		function() {
			var svg = document.getElementById('iwsvg');
			var svg2 = document.getElementById('iwsvg2');

			try {
				G = svg.getSVGDocument
					? svg.getSVGDocument() : svg.contentDocument;
				G2 = svg2.getSVGDocument
					? svg2.getSVGDocument() : svg2.contentDocument;
			}
			catch(e) {
				G = document.embeds['iwsvg'].getSVGDocument();
				G2 = document.embeds['iwsvg2'].getSVGDocument();
			}

			if (!G || !G2)
			{
				window.setTimeout(arguments.callee, 1000);
			}
			else
			{
				/* find sizes */
				width       = svg.offsetWidth  - 2;
				height      = svg.offsetHeight - 2;
				data_wanted = Math.ceil(width / step);

				/* prefill datasets */
				for (var i = 0; i < data_wanted; i++)
				{
					data_rssi[i] = 0;
					data_noise[i] = 0;
					data_rate[i] = 0;
				}

				/* find svg elements */
				line_rssi = G.getElementById('rssi');
				line_noise = G.getElementById('noise');
				line_rate = G2.getElementById('rate');

				label_25 = G.getElementById('label_25');
				label_50 = G.getElementById('label_50');
				label_75 = G.getElementById('label_75');
				label_25_2 = G2.getElementById('label_25');
				label_50_2 = G2.getElementById('label_50');
				label_75_2 = G2.getElementById('label_75');

				label_rssi_cur  = document.getElementById('rssi_bw_cur');
				label_rssi_avg  = document.getElementById('rssi_bw_avg');
				label_rssi_peak = document.getElementById('rssi_bw_peak');

				label_noise_cur  = document.getElementById('noise_bw_cur');
				label_noise_avg  = document.getElementById('noise_bw_avg');
				label_noise_peak = document.getElementById('noise_bw_peak');

				label_rate_cur  = document.getElementById('rate_bw_cur');
				label_rate_avg  = document.getElementById('rate_bw_avg');
				label_rate_peak = document.getElementById('rate_bw_peak');

				label_scale   = document.getElementById('scale');
				label_scale_2 = document.getElementById('scale2');


				/* plot horizontal time interval lines */
				for (var i = step * 60; i < width; i += step * 60)
				{
					var line = G.createElementNS('http://www.w3.org/2000/svg', 'line');
						line.setAttribute('x1', i);
						line.setAttribute('y1', 0);
						line.setAttribute('x2', i);
						line.setAttribute('y2', '100%');
						line.setAttribute('style', 'stroke:black;stroke-width:0.1');

					var text = G.createElementNS('http://www.w3.org/2000/svg', 'text');
						text.setAttribute('x', i + 5);
						text.setAttribute('y', 15);
						text.setAttribute('style', 'fill:#999999; font-size:9pt');
						text.appendChild(G.createTextNode(Math.round(i / step / 60) + 'm'));

					label_25.parentNode.appendChild(line);
					label_25.parentNode.appendChild(text);


					var line2 = G2.createElementNS('http://www.w3.org/2000/svg', 'line');
						line2.setAttribute('x1', i);
						line2.setAttribute('y1', 0);
						line2.setAttribute('x2', i);
						line2.setAttribute('y2', '100%');
						line2.setAttribute('style', 'stroke:black;stroke-width:0.1');

					var text2 = G2.createElementNS('http://www.w3.org/2000/svg', 'text');
						text2.setAttribute('x', i + 5);
						text2.setAttribute('y', 15);
						text2.setAttribute('style', 'fill:#999999; font-size:9pt');
						text2.appendChild(G.createTextNode(Math.round(i / step / 60) + 'm'));

					label_25_2.parentNode.appendChild(line2);
					label_25_2.parentNode.appendChild(text2);
				}

				label_scale.innerHTML = String.format('<%:(%d minute window, %d second interval)%>', data_wanted / 60, 3);
				label_scale_2.innerHTML = String.format('<%:(%d minute window, %d second interval)%>', data_wanted / 60, 3);

				/* render datasets, start update interval */
				XHR.poll(3, '<%=build_url("admin/status/wireless_status", curdev)%>', null,
					function(x, data)
					{
						var noise_floor = 255;
						var rate_floor = 60000;

						for (var i = 0; i < data.length; i++) {
							noise_floor = Math.min(noise_floor, data[i][NOISE]);
							rate_floor = Math.min(rate_floor, data[i][RATE]);
						}

						noise_floor -= 5;

						var data_max   = 0;
						var data_scale = 0;
						var data_max_2   = 0;
						var data_scale_2 = 0;

						var data_rssi_avg = 0;
						var data_noise_avg = 0;
						var data_rate_avg = 0;

						var data_rssi_peak = 0;
						var data_noise_peak = 0;
						var data_rate_peak = 0;

						for (var i = data_stamp ? 0 : 1; i < data.length; i++)
						{
							/* skip overlapping entries */
							if (data[i][TIME] <= data_stamp)
								continue;

							data_rssi.push(data[i][RSSI] - noise_floor);
							data_noise.push(data[i][NOISE] - noise_floor);
							data_rate.push(Math.floor(data[i][RATE] / 100));
						}

						/* cut off outdated entries */
						data_rssi = data_rssi.slice(data_rssi.length - data_wanted, data_rssi.length);
						data_noise = data_noise.slice(data_noise.length - data_wanted, data_noise.length);
						data_rate = data_rate.slice(data_rate.length - data_wanted, data_rate.length);

						/* find peak */
						for (var i = 0; i < data_rssi.length; i++)
						{
							data_max = Math.max(data_max, data_rssi[i]);
							data_max_2 = Math.max(data_max_2, data_rate[i]);

							data_rssi_peak = Math.max(data_rssi_peak, data_rssi[i]);
							data_noise_peak = Math.max(data_noise_peak, data_noise[i]);
							data_rate_peak = Math.max(data_rate_peak, data_rate[i]);

							if (i > 0)
							{
								data_rssi_avg = (data_rssi_avg + data_rssi[i]) / 2;
								data_noise_avg = (data_noise_avg + data_noise[i]) / 2;
								data_rate_avg = (data_rate_avg + data_rate[i]) / 2;
							}
							else
							{
								data_rssi_avg = data_rssi[i];
								data_noise_avg = data_noise[i];
								data_rate_avg = data_rate[i];
							}
						}

						/* remember current timestamp, calculate horizontal scale */
						data_stamp = data[data.length-1][TIME];
						data_scale = (height / (data_max * 1.1)).toFixed(1);
						data_scale_2 = (height / (data_max_2 * 1.1)).toFixed(1);

						/* plot data */
						var pt_rssi = '0,' + height;
						var pt_noise = '0,' + height;
						var pt_rate = '0,' + height;

						var y_rssi = 0;
						var y_noise = 0;
						var y_rate = 0;

						for (var i = 0; i < data_rssi.length; i++)
						{
							var x = i * step;

							y_rssi = height - Math.floor(data_rssi[i] * data_scale);
							y_noise = height - Math.floor(data_noise[i] * data_scale);
							y_rate = height - Math.floor(data_rate[i] * data_scale_2);

							y_rssi -= Math.floor(y_rssi % (1/data_scale));
							y_noise -= Math.floor(y_noise % (1/data_scale));

							pt_rssi += ' ' + x + ',' + y_rssi;
							pt_noise += ' ' + x + ',' + y_noise;
							pt_rate += ' ' + x + ',' + y_rate;
						}

						pt_rssi += ' ' + width + ',' + y_rssi + ' ' + width + ',' + height;
						pt_noise += ' ' + width + ',' + y_noise + ' ' + width + ',' + height;
						pt_rate += ' ' + width + ',' + y_rate + ' ' + width + ',' + height;

						line_rssi.setAttribute('points', pt_rssi);
						line_noise.setAttribute('points', pt_noise);
						line_rate.setAttribute('points', pt_rate);

						function wireless_label(dbm, noise)
						{
							if (noise)
								return String.format("%d dBm (SNR %d dBm)", noise_floor + dbm - 255, dbm - noise);
							else
								return String.format("%d dBm", noise_floor + dbm - 255);
						}

						function rate_label(mbit)
						{
							return String.format("%d MBit/s", mbit);
						}

						label_25.firstChild.data = wireless_label(1.1 * 0.25 * data_max);
						label_50.firstChild.data = wireless_label(1.1 * 0.50 * data_max);
						label_75.firstChild.data = wireless_label(1.1 * 0.75 * data_max);

						label_25_2.firstChild.data = rate_label(1.1 * 0.25 * data_max_2);
						label_50_2.firstChild.data = rate_label(1.1 * 0.50 * data_max_2);
						label_75_2.firstChild.data = rate_label(1.1 * 0.75 * data_max_2);

						label_rssi_cur.innerHTML = wireless_label(data_rssi[data_rssi.length-1], data_noise[data_noise.length-1]);
						label_noise_cur.innerHTML = wireless_label(data_noise[data_noise.length-1]);

						label_rssi_avg.innerHTML = wireless_label(data_rssi_avg, data_noise_avg);
						label_noise_avg.innerHTML = wireless_label(data_noise_avg);

						label_rssi_peak.innerHTML = wireless_label(data_rssi_peak, data_noise_peak);
						label_noise_peak.innerHTML = wireless_label(data_noise_peak);

						label_rate_cur.innerHTML = rate_label(data_rate[data_rate.length-1]);
						label_rate_avg.innerHTML = rate_label(data_rate_avg);
						label_rate_peak.innerHTML = rate_label(data_rate_peak);
					}
				);
			}
		}, 1000
	);
//]]></script>

<h2><a id="content" name="content"><%:Realtime Wireless%></a></h2>

<ul class="cbi-tabmenu">
	<% for _, dev in ipairs(devices) do %>
		<li class="cbi-tab<%= dev == curdev and "" or "-disabled" %>"><a href="<%=pcdata(dev)%>"><%=pcdata(dev)%></a></li>
	<% end %>
</ul>

<embed id="iwsvg" style="width:100%; height:300px; border:1px solid #000000; background-color:#FFFFFF" src="<%=resource%>/wireless.svg" />
<div style="text-align:right"><small id="scale">-</small></div>
<br />

<table style="width:100%; table-layout:fixed" cellspacing="5">
	<tr>
		<td style="text-align:right; vertical-align:top"><strong style="border-bottom:2px solid blue"><%:Signal:%></strong></td>
		<td id="rssi_bw_cur">0 dBm</td>

		<td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td>
		<td id="rssi_bw_avg">0 dBm</td>

		<td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td>
		<td id="rssi_bw_peak">0 dBm</td>
	</tr>
	<tr>
		<td style="text-align:right; vertical-align:top"><strong style="border-bottom:2px solid red"><%:Noise:%></strong></td>
		<td id="noise_bw_cur">0 dBm</td>

		<td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td>
		<td id="noise_bw_avg">0 dBm</td>

		<td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td>
		<td id="noise_bw_peak">0 dBm</td>
	</tr>
</table>

<br />

<embed id="iwsvg2" style="width:100%; height:300px; border:1px solid #000000; background-color:#FFFFFF" src="<%=resource%>/wifirate.svg" />
<div style="text-align:right"><small id="scale2">-</small></div>
<br />

<table style="width:100%; table-layout:fixed" cellspacing="5">
	<tr>
		<td style="text-align:right; vertical-align:top"><strong style="border-bottom:2px solid green"><%:Phy Rate:%></strong></td>
		<td id="rate_bw_cur">0 MBit/s</td>

		<td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td>
		<td id="rate_bw_avg">0 MBit/s</td>

		<td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td>
		<td id="rate_bw_peak">0 MBit/s</td>
	</tr>
</table>

<%+footer%>