code.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. // First, checks if it isn't implemented yet.
  2. if (!String.prototype.format) {
  3. String.prototype.format = function() {
  4. var args = arguments;
  5. return this.replace(/{(\d+)}/g, function(match, number) {
  6. return typeof args[number] != 'undefined'
  7. ? args[number]
  8. : match
  9. ;
  10. });
  11. };
  12. }
  13. var releaseURL = 'https://api.github.com/repos/sle118/squeezelite-esp32/releases';
  14. var recovery = false;
  15. var enableAPTimer = true;
  16. var enableStatusTimer = true;
  17. var commandHeader = 'squeezelite -b 500:2000 -d all=info ';
  18. var pname, ver, otapct, otadsc;
  19. var blockAjax = false;
  20. var blockFlashButton = false;
  21. var lastMsg = '';
  22. var apList = null;
  23. var selectedSSID = "";
  24. var refreshAPInterval = null;
  25. var checkStatusInterval = null;
  26. var StatusIntervalActive = false;
  27. var RefreshAPIIntervalActive = false;
  28. var output = '';
  29. function stopCheckStatusInterval(){
  30. if(checkStatusInterval != null){
  31. clearTimeout(checkStatusInterval);
  32. checkStatusInterval = null;
  33. }
  34. StatusIntervalActive = false;
  35. }
  36. function stopRefreshAPInterval(){
  37. if(refreshAPInterval != null){
  38. clearTimeout(refreshAPInterval);
  39. refreshAPInterval = null;
  40. }
  41. RefreshAPIIntervalActive = false;
  42. }
  43. function startCheckStatusInterval(){
  44. StatusIntervalActive = true;
  45. checkStatusInterval = setTimeout(checkStatus, 3000);
  46. }
  47. function startRefreshAPInterval(){
  48. RefreshAPIIntervalActive = true;
  49. refreshAPInterval = setTimeout(refreshAP, 2800);
  50. }
  51. function RepeatCheckStatusInterval(){
  52. if(StatusIntervalActive)
  53. startCheckStatusInterval();
  54. }
  55. function RepeatRefreshAPInterval(){
  56. if(RefreshAPIIntervalActive)
  57. startRefreshAPInterval();
  58. }
  59. $(document).ready(function(){
  60. $("#wifi-status").on("click", ".ape", function() {
  61. $( "#wifi" ).slideUp( "fast", function() {});
  62. $( "#connect-details" ).slideDown( "fast", function() {});
  63. });
  64. $("#manual_add").on("click", ".ape", function() {
  65. selectedSSID = $(this).text();
  66. $( "#ssid-pwd" ).text(selectedSSID);
  67. $( "#wifi" ).slideUp( "fast", function() {});
  68. $( "#connect_manual" ).slideDown( "fast", function() {});
  69. $( "#connect" ).slideUp( "fast", function() {});
  70. //update wait screen
  71. $( "#loading" ).show();
  72. $( "#connect-success" ).hide();
  73. $( "#connect-fail" ).hide();
  74. });
  75. $("#wifi-list").on("click", ".ape", function() {
  76. selectedSSID = $(this).text();
  77. $( "#ssid-pwd" ).text(selectedSSID);
  78. $( "#wifi" ).slideUp( "fast", function() {});
  79. $( "#connect_manual" ).slideUp( "fast", function() {});
  80. $( "#connect" ).slideDown( "fast", function() {});
  81. //update wait screen
  82. $( "#loading" ).show();
  83. $( "#connect-success" ).hide();
  84. $( "#connect-fail" ).hide();
  85. });
  86. $("#cancel").on("click", function() {
  87. selectedSSID = "";
  88. $( "#connect" ).slideUp( "fast", function() {});
  89. $( "#connect_manual" ).slideUp( "fast", function() {});
  90. $( "#wifi" ).slideDown( "fast", function() {});
  91. });
  92. $("#manual_cancel").on("click", function() {
  93. selectedSSID = "";
  94. $( "#connect" ).slideUp( "fast", function() {});
  95. $( "#connect_manual" ).slideUp( "fast", function() {});
  96. $( "#wifi" ).slideDown( "fast", function() {});
  97. });
  98. $("#join").on("click", function() {
  99. performConnect();
  100. });
  101. $("#manual_join").on("click", function() {
  102. performConnect($(this).data('connect'));
  103. });
  104. $("#ok-details").on("click", function() {
  105. $( "#connect-details" ).slideUp( "fast", function() {});
  106. $( "#wifi" ).slideDown( "fast", function() {});
  107. });
  108. $("#ok-credits").on("click", function() {
  109. $( "#credits" ).slideUp( "fast", function() {});
  110. $( "#app" ).slideDown( "fast", function() {});
  111. });
  112. $("#acredits").on("click", function(event) {
  113. event.preventDefault();
  114. $( "#app" ).slideUp( "fast", function() {});
  115. $( "#credits" ).slideDown( "fast", function() {});
  116. });
  117. $("#ok-connect").on("click", function() {
  118. $( "#connect-wait" ).slideUp( "fast", function() {});
  119. $( "#wifi" ).slideDown( "fast", function() {});
  120. });
  121. $("#disconnect").on("click", function() {
  122. $( "#connect-details-wrap" ).addClass('blur');
  123. $( "#diag-disconnect" ).slideDown( "fast", function() {});
  124. });
  125. $("#no-disconnect").on("click", function() {
  126. $( "#diag-disconnect" ).slideUp( "fast", function() {});
  127. $( "#connect-details-wrap" ).removeClass('blur');
  128. });
  129. $("#yes-disconnect").on("click", function() {
  130. stopCheckStatusInterval();
  131. selectedSSID = "";
  132. $( "#diag-disconnect" ).slideUp( "fast", function() {});
  133. $( "#connect-details-wrap" ).removeClass('blur');
  134. $.ajax({
  135. url: '/connect.json',
  136. dataType: 'json',
  137. method: 'DELETE',
  138. cache: false,
  139. data: { 'timestamp': Date.now()}
  140. });
  141. startCheckStatusInterval();
  142. $( "#connect-details" ).slideUp( "fast", function() {});
  143. $( "#wifi" ).slideDown( "fast", function() {})
  144. });
  145. $("input#autoexec-cb").on("click", function() {
  146. showMessage('please wait for the ESP32 to reboot', 'WARNING');
  147. var data = { 'timestamp': Date.now() };
  148. autoexec = (this.checked)?1:0;
  149. data['autoexec'] = autoexec;
  150. $.ajax({
  151. url: '/config.json',
  152. dataType: 'json',
  153. method: 'POST',
  154. cache: false,
  155. headers: { "X-Custom-autoexec": autoexec },
  156. data: data,
  157. error: function (xhr, ajaxOptions, thrownError) {
  158. console.log(xhr.status);
  159. console.log(thrownError);
  160. if (thrownError != '') showMessage(thrownError, 'ERROR');
  161. }
  162. });
  163. console.log('sent config JSON with headers:', autoexec);
  164. console.log('now triggering reboot');
  165. $.ajax({
  166. url: '/reboot.json',
  167. dataType: 'json',
  168. method: 'POST',
  169. cache: false,
  170. data: { 'timestamp': Date.now()},
  171. error: function (xhr, ajaxOptions, thrownError) {
  172. console.log(xhr.status);
  173. console.log(thrownError);
  174. if (thrownError != '') showMessage(thrownError, 'ERROR');
  175. }
  176. });
  177. });
  178. $("input#save-autoexec1").on("click", function() {
  179. var data = { 'timestamp': Date.now() };
  180. autoexec1 = $("#autoexec1").val();
  181. data['autoexec1'] = autoexec1;
  182. $.ajax({
  183. url: '/config.json',
  184. dataType: 'json',
  185. method: 'POST',
  186. cache: false,
  187. headers: { "X-Custom-autoexec1": autoexec1 },
  188. data: data,
  189. error: function (xhr, ajaxOptions, thrownError) {
  190. console.log(xhr.status);
  191. console.log(thrownError);
  192. if (thrownError != '') showMessage(thrownError, 'ERROR');
  193. }
  194. });
  195. console.log('sent config JSON with headers:', autoexec1);
  196. });
  197. $("input#save-gpio").on("click", function() {
  198. var data = { 'timestamp': Date.now() };
  199. var headers = {};
  200. $("input.gpio").each(function() {
  201. var id = $(this)[0].id;
  202. var pin = $(this).val();
  203. if (pin != '') {
  204. headers["X-Custom-"+id] = pin;
  205. data[id] = pin;
  206. }
  207. });
  208. $.ajax({
  209. url: '/config.json',
  210. dataType: 'json',
  211. method: 'POST',
  212. cache: false,
  213. headers: headers,
  214. data: data,
  215. error: function (xhr, ajaxOptions, thrownError) {
  216. console.log(xhr.status);
  217. console.log(thrownError);
  218. if (thrownError != '') showMessage(thrownError, 'ERROR');
  219. }
  220. });
  221. console.log('sent config JSON with headers:', JSON.stringify(headers));
  222. });
  223. $("#save-nvs").on("click", function() {
  224. var headers = {};
  225. var data = { 'timestamp': Date.now() };
  226. $("input.nvs").each(function() {
  227. var key = $(this)[0].id;
  228. var val = $(this).val();
  229. if (key != '') {
  230. headers["X-Custom-"+key] = val;
  231. data[key] = val;
  232. }
  233. });
  234. var key = $("#nvs-new-key").val();
  235. var val = $("#nvs-new-value").val();
  236. if (key != '') {
  237. headers["X-Custom-"+key] = val;
  238. data[key] = val;
  239. }
  240. $.ajax({
  241. url: '/config.json',
  242. dataType: 'json',
  243. method: 'POST',
  244. cache: false,
  245. headers: headers,
  246. data: data,
  247. error: function (xhr, ajaxOptions, thrownError) {
  248. console.log(xhr.status);
  249. console.log(thrownError);
  250. if (thrownError != '') showMessage(thrownError, 'ERROR');
  251. }
  252. });
  253. console.log('sent config JSON with headers:', JSON.stringify(headers));
  254. });
  255. $("#flash").on("click", function() {
  256. var data = { 'timestamp': Date.now() };
  257. if (blockFlashButton) return;
  258. blockFlashButton = true;
  259. var url = $("#fwurl").val();
  260. data['fwurl'] = url;
  261. $.ajax({
  262. url: '/config.json',
  263. dataType: 'json',
  264. method: 'POST',
  265. cache: false,
  266. headers: { "X-Custom-fwurl": url },
  267. data: data,
  268. error: function (xhr, ajaxOptions, thrownError) {
  269. console.log(xhr.status);
  270. console.log(thrownError);
  271. if (thrownError != '') showMessage(thrownError, 'ERROR');
  272. }
  273. });
  274. enableStatusTimer = true;
  275. });
  276. $("#generate-command").on("click", function() {
  277. var commandLine = commandHeader + '-n ' + $("#player").val();
  278. if (output == 'bt') {
  279. commandLine += ' -o "BT -n \'' + $("#btsink").val() + '\'" -R -Z 192000';
  280. } else if (output == 'spdif') {
  281. commandLine += ' -o SPDIF -R -Z 192000';
  282. } else {
  283. commandLine += ' -o I2S';
  284. }
  285. if ($("#optional").val() != '') {
  286. commandLine += ' ' + $("#optional").val();
  287. }
  288. $("#autoexec1").val(commandLine);
  289. });
  290. $('[name=audio]').on("click", function(){
  291. if (this.id == 'bt') {
  292. $("#btsinkdiv").show(200);
  293. output = 'bt';
  294. } else if (this.id == 'spdif') {
  295. $("#btsinkdiv").hide(200);
  296. output = 'spdif';
  297. } else {
  298. $("#btsinkdiv").hide(200);
  299. output = 'i2s';
  300. }
  301. });
  302. $('#fwcheck').on("click", function(){
  303. $("#releaseTable").html("");
  304. $.getJSON(releaseURL, function(data) {
  305. var i=0;
  306. data.forEach(function(release) {
  307. var url = '';
  308. release.assets.forEach(function(asset) {
  309. if (asset.name.match(/\.bin$/)) {
  310. url = asset.browser_download_url;
  311. }
  312. });
  313. var [ver, idf, cfg, branch] = release.name.split('#');
  314. var body = release.body;
  315. body = body.replace(/\'/ig, "\"");
  316. body = body.replace(/[\s\S]+(### Revision Log[\s\S]+)### ESP-IDF Version Used[\s\S]+/, "$1");
  317. body = body.replace(/- \(.+?\) /g, "- ");
  318. var [date, time] = release.created_at.split('T');
  319. var trclass = (i++ > 6)?' hide':'';
  320. $("#releaseTable").append(
  321. "<tr class='release"+trclass+"'>"+
  322. "<td data-toggle='tooltip' title='"+body+"'>"+ver+"</td>"+
  323. "<td>"+idf+"</td>"+
  324. "<td>"+date+"</td>"+
  325. "<td>"+cfg+"</td>"+
  326. "<td>"+branch+"</td>"+
  327. "<td><input id='generate-command' type='button' class='btn btn-success' value='Select' data-url='"+url+"' onclick='setURL(this);' /></td>"+
  328. "</tr>"
  329. );
  330. });
  331. if (i > 7) {
  332. $("#releaseTable").append(
  333. "<tr id='showall'>"+
  334. "<td colspan='6'>"+
  335. "<input type='button' id='showallbutton' class='btn btn-info' value='Show older releases' />"+
  336. "</td>"+
  337. "</tr>"
  338. );
  339. $('#showallbutton').on("click", function(){
  340. $("tr.hide").removeClass("hide");
  341. $("tr#showall").addClass("hide");
  342. });
  343. }
  344. $("#searchfw").css("display", "inline");
  345. })
  346. .fail(function() {
  347. alert("failed to fetch release history!");
  348. });
  349. });
  350. $('input#searchinput').on("input", function(){
  351. var s = $('input#searchinput').val();
  352. var re = new RegExp(s, "gi");
  353. if (s.length == 0) {
  354. $("tr.release").removeClass("hide");
  355. } else if (s.length < 3) {
  356. $("tr.release").addClass("hide");
  357. } else {
  358. $("tr.release").addClass("hide");
  359. $("tr.release").each(function(tr){
  360. $(this).find('td').each (function() {
  361. if ($(this).html().match(re)) {
  362. $(this).parent().removeClass('hide');
  363. }
  364. });
  365. });
  366. }
  367. });
  368. //first time the page loads: attempt to get the connection status and start the wifi scan
  369. refreshAP();
  370. getConfig();
  371. //start timers
  372. startCheckStatusInterval();
  373. startRefreshAPInterval();
  374. $('[data-toggle="tooltip"]').tooltip({
  375. html: true,
  376. placement : 'right',
  377. });
  378. });
  379. function setURL(button) {
  380. var url = button.dataset.url;
  381. $("#fwurl").val(url);
  382. $('[data-url^="http"]').addClass("btn-success").removeClass("btn-danger");
  383. $('[data-url="'+url+'"]').addClass("btn-danger").removeClass("btn-success");
  384. }
  385. function performConnect(conntype){
  386. //stop the status refresh. This prevents a race condition where a status
  387. //request would be refreshed with wrong ip info from a previous connection
  388. //and the request would automatically shows as succesful.
  389. stopCheckStatusInterval();
  390. //stop refreshing wifi list
  391. stopRefreshAPInterval();
  392. var pwd;
  393. var dhcpname = $("#dhcp-name").val();;
  394. if (conntype == 'manual') {
  395. //Grab the manual SSID and PWD
  396. selectedSSID=$('#manual_ssid').val();
  397. pwd = $("#manual_pwd").val();
  398. }else{
  399. pwd = $("#pwd").val();
  400. }
  401. //reset connection
  402. $( "#loading" ).show();
  403. $( "#connect-success" ).hide();
  404. $( "#connect-fail" ).hide();
  405. $( "#ok-connect" ).prop("disabled",true);
  406. $( "#ssid-wait" ).text(selectedSSID);
  407. $( "#connect" ).slideUp( "fast", function() {});
  408. $( "#connect_manual" ).slideUp( "fast", function() {});
  409. $( "#connect-wait" ).slideDown( "fast", function() {});
  410. $.ajax({
  411. url: '/connect.json',
  412. dataType: 'json',
  413. method: 'POST',
  414. cache: false,
  415. headers: { 'X-Custom-ssid': selectedSSID, 'X-Custom-pwd': pwd, 'X-Custom-host_name': dhcpname },
  416. data: { 'timestamp': Date.now()},
  417. error: function (xhr, ajaxOptions, thrownError) {
  418. console.log(xhr.status);
  419. console.log(thrownError);
  420. if (thrownError != '') showMessage(thrownError, 'ERROR');
  421. }
  422. });
  423. //now we can re-set the intervals regardless of result
  424. startCheckStatusInterval();
  425. startRefreshAPInterval();
  426. }
  427. function rssiToIcon(rssi){
  428. if(rssi >= -60){
  429. return 'w0';
  430. }
  431. else if(rssi >= -67){
  432. return 'w1';
  433. }
  434. else if(rssi >= -75){
  435. return 'w2';
  436. }
  437. else{
  438. return 'w3';
  439. }
  440. }
  441. function refreshAP(){
  442. if (!enableAPTimer) return;
  443. $.getJSON( "/ap.json", function( data ) {
  444. if(data.length > 0){
  445. //sort by signal strength
  446. data.sort(function (a, b) {
  447. var x = a["rssi"]; var y = b["rssi"];
  448. return ((x < y) ? 1 : ((x > y) ? -1 : 0));
  449. });
  450. apList = data;
  451. refreshAPHTML(apList);
  452. }
  453. });
  454. }
  455. function refreshAPHTML(data){
  456. var h = "";
  457. data.forEach(function(e, idx, array) {
  458. h += '<div class="ape{0}"><div class="{1}"><div class="{2}">{3}</div></div></div>'.format(idx === array.length - 1?'':' brdb', rssiToIcon(e.rssi), e.auth==0?'':'pw',e.ssid);
  459. h += "\n";
  460. });
  461. $( "#wifi-list" ).html(h)
  462. }
  463. function checkStatus(){
  464. RepeatCheckStatusInterval();
  465. if (!enableStatusTimer) return;
  466. if (blockAjax) return;
  467. blockAjax = true;
  468. $.getJSON( "/status.json", function( data ) {
  469. if (data.hasOwnProperty('ssid') && data['ssid'] != ""){
  470. if (data["ssid"] === selectedSSID){
  471. //that's a connection attempt
  472. if (data["urc"] === 0){
  473. //got connection
  474. $("#connected-to span").text(data["ssid"]);
  475. $("#connect-details h1").text(data["ssid"]);
  476. $("#ip").text(data["ip"]);
  477. $("#netmask").text(data["netmask"]);
  478. $("#gw").text(data["gw"]);
  479. $("#wifi-status").slideDown( "fast", function() {});
  480. $("span#foot-wifi").html(", SSID: <strong>"+data["ssid"]+"</strong>, IP: <strong>"+data["ip"]+"</strong>");
  481. //unlock the wait screen if needed
  482. $( "#ok-connect" ).prop("disabled",false);
  483. //update wait screen
  484. $( "#loading" ).hide();
  485. $( "#connect-success" ).append("<p>Your IP address now is: " + text(data["ip"]) + "</p>");
  486. $( "#connect-success" ).show();
  487. $( "#connect-fail" ).hide();
  488. enableAPTimer = false;
  489. if (!recovery) enableStatusTimer = false;
  490. }
  491. else if (data["urc"] === 1){
  492. //failed attempt
  493. $("#connected-to span").text('');
  494. $("#connect-details h1").text('');
  495. $("#ip").text('0.0.0.0');
  496. $("#netmask").text('0.0.0.0');
  497. $("#gw").text('0.0.0.0');
  498. $("span#foot-wifi").html("");
  499. //don't show any connection
  500. $("#wifi-status").slideUp( "fast", function() {});
  501. //unlock the wait screen
  502. $( "#ok-connect" ).prop("disabled",false);
  503. //update wait screen
  504. $( "#loading" ).hide();
  505. $( "#connect-fail" ).show();
  506. $( "#connect-success" ).hide();
  507. enableAPTimer = true;
  508. enableStatusTimer = true;
  509. }
  510. }
  511. else if (data.hasOwnProperty('urc') && data['urc'] === 0){
  512. //ESP32 is already connected to a wifi without having the user do anything
  513. if( !($("#wifi-status").is(":visible")) ){
  514. $("#connected-to span").text(data["ssid"]);
  515. $("#connect-details h1").text(data["ssid"]);
  516. $("#ip").text(data["ip"]);
  517. $("#netmask").text(data["netmask"]);
  518. $("#gw").text(data["gw"]);
  519. $("#wifi-status").slideDown( "fast", function() {});
  520. $("span#foot-wifi").html(", SSID: <strong>"+data["ssid"]+"</strong>, IP: <strong>"+data["ip"]+"</strong>");
  521. }
  522. enableAPTimer = false;
  523. if (!recovery) enableStatusTimer = false;
  524. }
  525. }
  526. else if (data.hasOwnProperty('urc') && data['urc'] === 2){
  527. //that's a manual disconnect
  528. if($("#wifi-status").is(":visible")){
  529. $("#wifi-status").slideUp( "fast", function() {});
  530. $("span#foot-wifi").html("");
  531. }
  532. enableAPTimer = true;
  533. enableStatusTimer = true;
  534. }
  535. if (data.hasOwnProperty('recovery')) {
  536. if (data["recovery"] === 1) {
  537. recovery = true;
  538. $("#otadiv").show();
  539. $('a[href^="#tab-audio"]').hide();
  540. $('a[href^="#tab-gpio"]').hide();
  541. $('a[href^="#tab-nvs"]').show();
  542. $("footer.footer").removeClass('sl');
  543. $("footer.footer").addClass('recovery');
  544. $("#boot-button").html('Reboot');
  545. $("#boot-form").attr('action', '/reboot.json');
  546. enableStatusTimer = true;
  547. } else {
  548. recovery = false;
  549. $("#otadiv").hide();
  550. $('a[href^="#tab-audio"]').show();
  551. $('a[href^="#tab-gpio"]').show();
  552. $('a[href^="#tab-nvs"]').hide();
  553. $("footer.footer").removeClass('recovery');
  554. $("footer.footer").addClass('sl');
  555. $("#boot-button").html('Recovery');
  556. $("#boot-form").attr('action', '/recovery.json');
  557. enableStatusTimer = false;
  558. }
  559. }
  560. if (data.hasOwnProperty('project_name') && data['project_name'] != ''){
  561. pname = data['project_name'];
  562. }
  563. if (data.hasOwnProperty('version') && data['version'] != ''){
  564. ver = data['version'];
  565. $("span#foot-fw").html("fw: <strong>"+ver+"</strong>, mode: <strong>"+pname+"</strong>");
  566. }
  567. if (data.hasOwnProperty('ota_pct') && data['ota_pct'] != 0){
  568. otapct = data['ota_pct'];
  569. $('.progress-bar').css('width', otapct+'%').attr('aria-valuenow', otapct);
  570. $('.progress-bar').html(otapct+'%');
  571. }
  572. if (data.hasOwnProperty('ota_dsc') && data['ota_dsc'] != ''){
  573. otadsc = data['ota_dsc'];
  574. $("span#flash-status").html(otadsc);
  575. if (otadsc.match(/Error:/) || otapct > 95) {
  576. blockFlashButton = false;
  577. enableStatusTimer = true;
  578. }
  579. } else {
  580. $("span#flash-status").html('');
  581. }
  582. if (data.hasOwnProperty('message') && data['message'] != ''){
  583. var msg = data['message'].text;
  584. var severity = data['message'].severity;
  585. if (msg != lastMsg) {
  586. showMessage(msg, severity);
  587. lastMsg = msg;
  588. }
  589. }
  590. blockAjax = false;
  591. })
  592. .fail(function() {
  593. blockAjax = false;
  594. });
  595. }
  596. function getConfig() {
  597. $.getJSON("/config.json", function(data) {
  598. for (var key in data) {
  599. if (data.hasOwnProperty(key)) {
  600. if (key == 'autoexec') {
  601. if (data["autoexec"] === "1") {
  602. $("#autoexec-cb")[0].checked=true;
  603. } else {
  604. $("#autoexec-cb")[0].checked=false;
  605. }
  606. } else if (key == 'autoexec1') {
  607. $("input#autoexec1").val(data["autoexec1"]);
  608. }
  609. $("tbody#nvsTable").append(
  610. "<tr>"+
  611. "<td>"+key+"</td>"+
  612. "<td class='value'>"+
  613. "<input type='text' class='form-control nvs' id='"+key+"'>"+
  614. "</td>"+
  615. "</tr>"
  616. );
  617. $("input#"+key).val(data[key]);
  618. }
  619. }
  620. $("tbody#nvsTable").append(
  621. "<tr>"+
  622. "<td>"+
  623. "<input type='text' class='form-control' id='nvs-new-key' placeholder='new key'>"+
  624. "</td>"+
  625. "<td>"+
  626. "<input type='text' class='form-control' id='nvs-new-value' placeholder='new value'>"+
  627. "</td>"+
  628. "</tr>"
  629. );
  630. })
  631. .fail(function() {
  632. console.log("failed to fetch config!");
  633. });
  634. }
  635. function showMessage(message, severity) {
  636. if (severity == 'INFO') {
  637. $('#message').css('background', '#6af');
  638. } else if (severity == 'WARNING') {
  639. $('#message').css('background', '#ff0');
  640. } else {
  641. $('#message').css('background', '#f00');
  642. }
  643. $('#message').html(message);
  644. $("#content").fadeTo("slow", 0.3, function() {
  645. $("#message").show(500).delay(5000).hide(500, function() {
  646. $("#content").fadeTo("slow", 1.0);
  647. });
  648. });
  649. }