wifi.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <link rel="stylesheet" href="../../script/semantic/semantic.min.css">
  6. <script type="text/javascript" src="../../script/jquery.min.js"></script>
  7. <script type="text/javascript" src="../../script/semantic/semantic.min.js"></script>
  8. <title>WiFi Settings</title>
  9. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  10. <style>
  11. </style>
  12. </head>
  13. <body>
  14. <div class="ui container">
  15. <div class="ui horizontal segments">
  16. <div class="ui segment">
  17. <h4 class="ui header">
  18. <i class="signal icon"></i>
  19. <div class="content">
  20. <span id="SSID">Loading...</span>
  21. <div class="sub header">WiFi SSID</div>
  22. </div>
  23. </h4>
  24. </div>
  25. <div class="ui segment">
  26. <h4 class="ui header">
  27. <i class="globe icon"></i>
  28. <div class="content">
  29. <span id="Interface">Loading...</span>
  30. <div class="sub header">Interface</div>
  31. </div>
  32. </h4>
  33. </div>
  34. </div>
  35. <!-- WiFi Interface Toggle -->
  36. <div class="ui segment">
  37. <p>Wireless Interface Power Toggle</p>
  38. <div class="ui green message" id="updateSucceedMessage" style="display:none;">
  39. <p><i class="checkmark icon"></i> Interface Status Updated</p>
  40. </div>
  41. <form class="ui form" id="wlanlist">
  42. <p><i class="remove icon"></i> No wlan interface attached</p>
  43. </form>
  44. </div>
  45. <!-- WiFi Scanning Results-->
  46. <div>
  47. <table class="ui very basic celled table">
  48. <thead>
  49. <tr>
  50. <th>Wifi Access Points</th>
  51. <th>Signal Level (Quality)</th>
  52. <th>Physical Address</th>
  53. <th>Connect</th>
  54. <th>Remove</th>
  55. </tr>
  56. </thead>
  57. <div class="ui container">
  58. <button class="ui small button" onclick="initWiFiScan();"><i class="refresh icon"></i> Rescan</button>
  59. </div>
  60. <tbody id="wifilist">
  61. <tr>
  62. <td><i class="loading spinner icon"></i> Scanning Nearby WiFi Access Points</td>
  63. </tr>
  64. </tbody>
  65. </table>
  66. </div>
  67. <br><br><br>
  68. </div>
  69. <!-- WiFi Connection Interface-->
  70. <div class="ui modal">
  71. <i class="close icon"></i>
  72. <div class="header">
  73. <span id="wifiName"></span>
  74. </div>
  75. <div class="image content">
  76. <div class="ui mini image">
  77. <img id="wifiImage" src="">
  78. </div>
  79. <div class="description" style="min-width:400px;">
  80. <div class="ui header">Credential Required</div>
  81. <div class="ui labeled fluid input" style="margin-top:12px;">
  82. <div class="ui teal label">
  83. SSID
  84. </div>
  85. <input type="text" value="" id="ssid" readonly=true>
  86. </div>
  87. <div class="ui labeled action fluid input" style="margin-top:12px;">
  88. <div class="ui teal label">
  89. Password
  90. </div>
  91. <input type="password" id="pwd">
  92. <button class="ui icon black button" onmousedown="showPassword();" onmouseup="hidePassword();" ontouchstart="showPassword();" ontouchend="hidePassword();">
  93. <i class="eye icon"></i>
  94. </button>
  95. </div>
  96. </div>
  97. </div>
  98. <div class="actions">
  99. <div class="ui black deny button">
  100. Cancel
  101. </div>
  102. <div class="ui positive right labeled icon button" onclick="confirmConnection();">
  103. Connect
  104. <i class="checkmark icon"></i>
  105. </div>
  106. </div>
  107. </div>
  108. <script>
  109. var retryCount = 0;
  110. //Delete all previous modal if exists
  111. //Funny bug of Semantic ui
  112. for (var i =1; i < $(".ui.modal").length; i++){
  113. $($(".ui.modal")[i]).remove();
  114. }
  115. initWiFiScan();
  116. initWlanPower();
  117. function initWiFiScan(){
  118. $("#wifilist").html(` <tr>
  119. <td><i class="loading spinner icon"></i> Scanning Nearby WiFi Access Points</td>
  120. </tr>`);
  121. $.get("../../system/network/scanWifi",function(data){
  122. if (data.error != undefined){
  123. console.log(data.error);
  124. if (retryCount > 5){
  125. //Stop retrtying
  126. $("#wifilist").html(` <tr>
  127. <td><i class="remove icon"></i> WiFi Scanning Failed</td>
  128. </tr>`);
  129. return
  130. }
  131. //Unknown error. Rescan later
  132. setTimeout(function(){
  133. initWiFiScan();
  134. },3000);
  135. retryCount++;
  136. }else{
  137. retryCount = 0;
  138. $("#wifilist").html("");
  139. for (var i = 0; i < data.length; i++){
  140. var thisWiFi = data[i];
  141. var signalLevel = thisWiFi.SignalLevel.split(" ").shift();
  142. var signalIcon = "signal_0.svg"
  143. var warninglevel = 0;
  144. try{
  145. signalLevel = parseInt(signalLevel);
  146. if (signalLevel > -50){
  147. signalIcon = "signal_5.svg";
  148. }else if (signalLevel > -60){
  149. signalIcon = "signal_4.svg";
  150. }else if (signalLevel > -70){
  151. signalIcon = "signal_3.svg";
  152. }else if (signalLevel > -80){
  153. signalIcon = "signal_2.svg";
  154. warninglevel = 1;
  155. }else{
  156. signalIcon = "signal_1.svg";
  157. warninglevel = 2;
  158. }
  159. }catch(ex){
  160. }
  161. //This WiFi might be too week to connect
  162. classname = "";
  163. if (warninglevel == 0){
  164. classname = "positive";
  165. }
  166. //Only add remove button if the WiFi is connected before
  167. removeHTML = "";
  168. if (thisWiFi.ConnectedBefore){
  169. removeHTML = `<a href="#">Remove</a>`;
  170. }
  171. //Check if this WiFi has been connected. If yes, hide the connect button
  172. var connBtn = ` <td class="selectable" conncetedBefore="${thisWiFi.ConnectedBefore}" image="${signalIcon}" targetESSID="${thisWiFi.ESSID}" onclick="connectThis(this, event);">
  173. <a href="#">Connect</a>
  174. </td>`;
  175. if (thisWiFi.ESSID == $("#SSID").text().trim()){
  176. connBtn = `<td conncetedBefore="${thisWiFi.ConnectedBefore}">
  177. <i class="checkmark icon"></i>
  178. </td>`;
  179. }
  180. $("#wifilist").append(`<tr class="${classname}">
  181. <td>
  182. <h4 class="ui image header">
  183. <img src="../network/img/${signalIcon}" class="ui mini rounded image">
  184. <div class="content">
  185. ${thisWiFi.ESSID}
  186. <div class="sub header"> ${thisWiFi.Frequency} (Channel ${thisWiFi.Channel})
  187. </div>
  188. </div>
  189. </h4>
  190. </td>
  191. <td>
  192. ${thisWiFi.SignalLevel} ( ${thisWiFi.Quality})
  193. </td>
  194. <td>
  195. ${thisWiFi.Address}
  196. </td>
  197. ${connBtn}
  198. <td class="selectable" image="${signalIcon}" targetESSID="${thisWiFi.ESSID}" onclick="removeThis(this, event);">
  199. ${removeHTML}
  200. </td>
  201. </tr>`);
  202. }
  203. }
  204. });
  205. }
  206. function initWlanPower(){
  207. $.get("../../system/network/power", function(data){
  208. $("#wlanlist").html("");
  209. if (data.error != undefined){
  210. console.log(data.error);
  211. }else{
  212. data.forEach(interface => {
  213. var checked = "false";
  214. if (interface.Running){
  215. checked = "true";
  216. }
  217. $("#wlanlist").append(`<div class="field"><div class="ui toggle checkbox" id="${interface.Name}">
  218. <input type="checkbox" name="${interface.Name}" tabindex="0" class="hidden">
  219. <label><i class="wifi icon"></i> ${interface.Name}</label>
  220. </div></div>`);
  221. if (interface.Running){
  222. $("#" + interface.Name).checkbox("check");
  223. }else{
  224. $("#" + interface.Name).checkbox("uncheck");
  225. }
  226. $("#" + interface.Name).find("input").attr("onchange","handleInterfacePowerToggle(this);")
  227. });
  228. }
  229. });
  230. }
  231. function handleInterfacePowerToggle(object){
  232. var toggleState = object.checked;
  233. var interfaeName = $(object).attr("name");
  234. var status = "on";
  235. if (toggleState == true){
  236. status = "on";
  237. }else{
  238. status = "off";
  239. }
  240. $.ajax({
  241. url: "../../system/network/power",
  242. data: {status: status, interface: interfaeName},
  243. success: function(data){
  244. console.log(data);
  245. initWlanPower();
  246. $("#updateSucceedMessage").stop().finish().slideDown("fast").delay(5000).slideUp("fast");
  247. }
  248. })
  249. }
  250. function removeThis(obj, event){
  251. event.preventDefault();
  252. targetSSID = $(obj).attr("targetESSID");
  253. $.ajax({
  254. url: "../../system/network/removeWifi",
  255. data: {ESSID: targetSSID},
  256. success: function(data){
  257. if (data.error !== undefined){
  258. alert(data.error)
  259. }else{
  260. resetInterface();
  261. }
  262. }
  263. });
  264. }
  265. function resetInterface(obj=undefined){
  266. $("#wifilist .selectable").removeClass("disabled");
  267. if (obj !== undefined){
  268. $(obj).html(`<a href="#">Connect</a>`);
  269. }
  270. initWiFiScan();
  271. }
  272. function connectThis(obj, event){
  273. $("#pwd").val("");
  274. targetSSID = $(obj).attr("targetESSID");
  275. event.preventDefault();
  276. //Create DOM elements
  277. $("#wifiName").text(targetSSID);
  278. $("#wifiImage").attr('src',"../network/img/" + $(obj).attr("image"));
  279. $("#ssid").val(targetSSID);
  280. //Check if this is connected before. If yes, send out AP switch request
  281. if ($(obj).attr("conncetedBefore") == "true"){
  282. var ESSID = $(obj).attr("targetESSID");
  283. $("#wifilist .selectable").addClass("disabled");
  284. $(obj).html(`<div class="ui active centered inline loader"></div>`);
  285. $.ajax({
  286. url: "../../system/network/connectWifi",
  287. data: {"ESSID": ESSID, "ConnType":"switch"},
  288. method: "POST",
  289. success: function(data){
  290. console.log(data);
  291. initWifiInfo();
  292. resetInterface(obj);
  293. },
  294. error: function(){
  295. $("#wifilist .selectable").removeClass("disabled");
  296. resetInterface(obj);
  297. },
  298. timeout: 30000
  299. });
  300. }else{
  301. $('.ui.modal').modal('show');
  302. }
  303. }
  304. function confirmConnection(){
  305. var ESSID = $("#ssid").val();
  306. var PWD = $("#pwd").val();
  307. $("#wifilist .selectable").addClass("disabled");
  308. $.ajax({
  309. url: "../../system/network/connectWifi",
  310. data: {"ESSID": ESSID, "pwd": PWD},
  311. method: "POST",
  312. success: function(data){
  313. console.log(data);
  314. initWifiInfo();
  315. resetInterface();
  316. },
  317. error: function(){
  318. $("#wifilist .selectable").removeClass("disabled");
  319. resetInterface();
  320. },
  321. timeout: 30000
  322. });
  323. }
  324. function showPassword(){
  325. $("#pwd").attr("type","text");
  326. }
  327. function hidePassword(){
  328. $("#pwd").attr("type","password");
  329. }
  330. //Also init WiFi view in this interface
  331. initWifiInfo();
  332. function initWifiInfo(){
  333. $.ajax({
  334. url: "../../system/network/wifiinfo",
  335. method:"GET",
  336. success: function(data){
  337. if (data.error !== undefined){
  338. $("#SSID").text("Access Failed");
  339. $("#Interface").text("loopback");
  340. }else{
  341. $("#SSID").text(data.ESSID);
  342. $("#Interface").text(data.Interface);
  343. }
  344. }
  345. });
  346. }
  347. </script>
  348. </body>
  349. </html>