wireless.htm 12 KB

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