code.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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. var data = { 'timestamp': Date.now() };
  147. autoexec = (this.checked)?1:0;
  148. data['autoexec'] = autoexec;
  149. $.ajax({
  150. url: '/config.json',
  151. dataType: 'json',
  152. method: 'POST',
  153. cache: false,
  154. headers: { "X-Custom-autoexec": autoexec },
  155. data: data,
  156. error: function (xhr, ajaxOptions, thrownError) {
  157. console.log(xhr.status);
  158. console.log(thrownError);
  159. if (thrownError != '') showMessage(thrownError);
  160. }
  161. });
  162. console.log('sent config JSON with headers:', autoexec);
  163. console.log('now triggering reboot');
  164. $.ajax({
  165. url: '/reboot.json',
  166. dataType: 'json',
  167. method: 'POST',
  168. cache: false,
  169. data: { 'timestamp': Date.now()},
  170. error: function (xhr, ajaxOptions, thrownError) {
  171. console.log(xhr.status);
  172. console.log(thrownError);
  173. if (thrownError != '') showMessage(thrownError);
  174. }
  175. });
  176. });
  177. $("input#save-autoexec1").on("click", function() {
  178. var data = { 'timestamp': Date.now() };
  179. autoexec1 = $("#autoexec1").val();
  180. data['autoexec1'] = autoexec1;
  181. $.ajax({
  182. url: '/config.json',
  183. dataType: 'json',
  184. method: 'POST',
  185. cache: false,
  186. headers: { "X-Custom-autoexec1": autoexec1 },
  187. data: data,
  188. error: function (xhr, ajaxOptions, thrownError) {
  189. console.log(xhr.status);
  190. console.log(thrownError);
  191. if (thrownError != '') showMessage(thrownError);
  192. }
  193. });
  194. console.log('sent config JSON with headers:', autoexec1);
  195. });
  196. $("input#save-gpio").on("click", function() {
  197. var data = { 'timestamp': Date.now() };
  198. var headers = {};
  199. $("input.gpio").each(function() {
  200. var id = $(this)[0].id;
  201. var pin = $(this).val();
  202. if (pin != '') {
  203. headers["X-Custom-"+id] = pin;
  204. data[id] = pin;
  205. }
  206. });
  207. $.ajax({
  208. url: '/config.json',
  209. dataType: 'json',
  210. method: 'POST',
  211. cache: false,
  212. headers: headers,
  213. data: data,
  214. error: function (xhr, ajaxOptions, thrownError) {
  215. console.log(xhr.status);
  216. console.log(thrownError);
  217. if (thrownError != '') showMessage(thrownError);
  218. }
  219. });
  220. console.log('sent config JSON with headers:', JSON.stringify(headers));
  221. });
  222. $("#save-nvs").on("click", function() {
  223. var headers = {};
  224. var data = { 'timestamp': Date.now() };
  225. $("input.nvs").each(function() {
  226. var key = $(this)[0].id;
  227. var val = $(this).val();
  228. if (key != '') {
  229. headers["X-Custom-"+key] = val;
  230. data[key] = val;
  231. }
  232. });
  233. var key = $("#nvs-new-key").val();
  234. var val = $("#nvs-new-value").val();
  235. if (key != '') {
  236. headers["X-Custom-"+key] = val;
  237. data[key] = val;
  238. }
  239. $.ajax({
  240. url: '/config.json',
  241. dataType: 'json',
  242. method: 'POST',
  243. cache: false,
  244. headers: headers,
  245. data: data,
  246. error: function (xhr, ajaxOptions, thrownError) {
  247. console.log(xhr.status);
  248. console.log(thrownError);
  249. if (thrownError != '') showMessage(thrownError);
  250. }
  251. });
  252. console.log('sent config JSON with headers:', JSON.stringify(headers));
  253. });
  254. $("#flash").on("click", function() {
  255. var data = { 'timestamp': Date.now() };
  256. if (blockFlashButton) return;
  257. blockFlashButton = true;
  258. var url = $("#fwurl").val();
  259. data['fwurl'] = url;
  260. $.ajax({
  261. url: '/config.json',
  262. dataType: 'json',
  263. method: 'POST',
  264. cache: false,
  265. headers: { "X-Custom-fwurl": url },
  266. data: data,
  267. error: function (xhr, ajaxOptions, thrownError) {
  268. console.log(xhr.status);
  269. console.log(thrownError);
  270. if (thrownError != '') showMessage(thrownError);
  271. }
  272. });
  273. enableStatusTimer = true;
  274. });
  275. $("#generate-command").on("click", function() {
  276. var commandLine = commandHeader + '-n ' + $("#player").val();
  277. if (output == 'bt') {
  278. commandLine += ' -o "BT -n \'' + $("#btsink").val() + '\'" -R -Z 192000';
  279. } else if (output == 'spdif') {
  280. commandLine += ' -o SPDIF -R -Z 192000';
  281. } else {
  282. commandLine += ' -o I2S';
  283. }
  284. if ($("#optional").val() != '') {
  285. commandLine += ' ' + $("#optional").val();
  286. }
  287. $("#autoexec1").val(commandLine);
  288. });
  289. $('[name=audio]').on("click", function(){
  290. if (this.id == 'bt') {
  291. $("#btsinkdiv").show(200);
  292. output = 'bt';
  293. } else if (this.id == 'spdif') {
  294. $("#btsinkdiv").hide(200);
  295. output = 'spdif';
  296. } else {
  297. $("#btsinkdiv").hide(200);
  298. output = 'i2s';
  299. }
  300. });
  301. $('#fwcheck').on("click", function(){
  302. $("#releaseTable").html("");
  303. $.getJSON(releaseURL, function(data) {
  304. var i=0;
  305. data.forEach(function(release) {
  306. var url = '';
  307. release.assets.forEach(function(asset) {
  308. if (asset.name.match(/\.bin$/)) {
  309. url = asset.browser_download_url;
  310. }
  311. });
  312. var [ver, idf, cfg, branch] = release.name.split('#');
  313. var body = release.body;
  314. body = body.replace(/\'/ig, "\"");
  315. body = body.replace(/[\s\S]+(### Revision Log[\s\S]+)### ESP-IDF Version Used[\s\S]+/, "$1");
  316. body = body.replace(/- \(.+?\) /g, "- ");
  317. var [date, time] = release.created_at.split('T');
  318. var trclass = (i++ > 6)?' hide':'';
  319. $("#releaseTable").append(
  320. "<tr class='release"+trclass+"'>"+
  321. "<td data-toggle='tooltip' title='"+body+"'>"+ver+"</td>"+
  322. "<td>"+idf+"</td>"+
  323. "<td>"+date+"</td>"+
  324. "<td>"+cfg+"</td>"+
  325. "<td>"+branch+"</td>"+
  326. "<td><input id='generate-command' type='button' class='btn btn-success' value='Select' data-url='"+url+"' onclick='setURL(this);' /></td>"+
  327. "</tr>"
  328. );
  329. });
  330. if (i > 7) {
  331. $("#releaseTable").append(
  332. "<tr id='showall'>"+
  333. "<td colspan='6'>"+
  334. "<input type='button' id='showallbutton' class='btn btn-info' value='Show older releases' />"+
  335. "</td>"+
  336. "</tr>"
  337. );
  338. $('#showallbutton').on("click", function(){
  339. $("tr.hide").removeClass("hide");
  340. $("tr#showall").addClass("hide");
  341. });
  342. }
  343. $("#searchfw").css("display", "inline");
  344. })
  345. .fail(function() {
  346. alert("failed to fetch release history!");
  347. });
  348. });
  349. $('input#searchinput').on("input", function(){
  350. var s = $('input#searchinput').val();
  351. var re = new RegExp(s, "gi");
  352. if (s.length == 0) {
  353. $("tr.release").removeClass("hide");
  354. } else if (s.length < 3) {
  355. $("tr.release").addClass("hide");
  356. } else {
  357. $("tr.release").addClass("hide");
  358. $("tr.release").each(function(tr){
  359. $(this).find('td').each (function() {
  360. if ($(this).html().match(re)) {
  361. $(this).parent().removeClass('hide');
  362. }
  363. });
  364. });
  365. }
  366. });
  367. //first time the page loads: attempt to get the connection status and start the wifi scan
  368. refreshAP();
  369. getConfig();
  370. //start timers
  371. startCheckStatusInterval();
  372. startRefreshAPInterval();
  373. $('[data-toggle="tooltip"]').tooltip({
  374. html: true,
  375. placement : 'right',
  376. });
  377. });
  378. function setURL(button) {
  379. var url = button.dataset.url;
  380. $("#fwurl").val(url);
  381. $('[data-url^="http"]').addClass("btn-success").removeClass("btn-danger");
  382. $('[data-url="'+url+'"]').addClass("btn-danger").removeClass("btn-success");
  383. }
  384. function performConnect(conntype){
  385. //stop the status refresh. This prevents a race condition where a status
  386. //request would be refreshed with wrong ip info from a previous connection
  387. //and the request would automatically shows as succesful.
  388. stopCheckStatusInterval();
  389. //stop refreshing wifi list
  390. stopRefreshAPInterval();
  391. var pwd;
  392. if (conntype == 'manual') {
  393. //Grab the manual SSID and PWD
  394. selectedSSID=$('#manual_ssid').val();
  395. pwd = $("#manual_pwd").val();
  396. }else{
  397. pwd = $("#pwd").val();
  398. }
  399. //reset connection
  400. $( "#loading" ).show();
  401. $( "#connect-success" ).hide();
  402. $( "#connect-fail" ).hide();
  403. $( "#ok-connect" ).prop("disabled",true);
  404. $( "#ssid-wait" ).text(selectedSSID);
  405. $( "#connect" ).slideUp( "fast", function() {});
  406. $( "#connect_manual" ).slideUp( "fast", function() {});
  407. $( "#connect-wait" ).slideDown( "fast", function() {});
  408. $.ajax({
  409. url: '/connect.json',
  410. dataType: 'json',
  411. method: 'POST',
  412. cache: false,
  413. headers: { 'X-Custom-ssid': selectedSSID, 'X-Custom-pwd': pwd },
  414. data: { 'timestamp': Date.now()},
  415. error: function (xhr, ajaxOptions, thrownError) {
  416. console.log(xhr.status);
  417. console.log(thrownError);
  418. if (thrownError != '') showMessage(thrownError);
  419. }
  420. });
  421. //now we can re-set the intervals regardless of result
  422. startCheckStatusInterval();
  423. startRefreshAPInterval();
  424. }
  425. function rssiToIcon(rssi){
  426. if(rssi >= -60){
  427. return 'w0';
  428. }
  429. else if(rssi >= -67){
  430. return 'w1';
  431. }
  432. else if(rssi >= -75){
  433. return 'w2';
  434. }
  435. else{
  436. return 'w3';
  437. }
  438. }
  439. function refreshAP(){
  440. if (!enableAPTimer) return;
  441. $.getJSON( "/ap.json", function( data ) {
  442. if(data.length > 0){
  443. //sort by signal strength
  444. data.sort(function (a, b) {
  445. var x = a["rssi"]; var y = b["rssi"];
  446. return ((x < y) ? 1 : ((x > y) ? -1 : 0));
  447. });
  448. apList = data;
  449. refreshAPHTML(apList);
  450. }
  451. });
  452. }
  453. function refreshAPHTML(data){
  454. var h = "";
  455. data.forEach(function(e, idx, array) {
  456. 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);
  457. h += "\n";
  458. });
  459. $( "#wifi-list" ).html(h)
  460. }
  461. function checkStatus(){
  462. RepeatCheckStatusInterval();
  463. if (!enableStatusTimer) return;
  464. if (blockAjax) return;
  465. blockAjax = true;
  466. $.getJSON( "/status.json", function( data ) {
  467. if (data.hasOwnProperty('ssid') && data['ssid'] != ""){
  468. if (data["ssid"] === selectedSSID){
  469. //that's a connection attempt
  470. if (data["urc"] === 0){
  471. //got connection
  472. $("#connected-to span").text(data["ssid"]);
  473. $("#connect-details h1").text(data["ssid"]);
  474. $("#ip").text(data["ip"]);
  475. $("#netmask").text(data["netmask"]);
  476. $("#gw").text(data["gw"]);
  477. $("#wifi-status").slideDown( "fast", function() {});
  478. $("span#foot-wifi").html(", SSID: <strong>"+data["ssid"]+"</strong>, IP: <strong>"+data["ip"]+"</strong>");
  479. //unlock the wait screen if needed
  480. $( "#ok-connect" ).prop("disabled",false);
  481. //update wait screen
  482. $( "#loading" ).hide();
  483. $( "#connect-success" ).append("<p>Your IP address now is: " + text(data["ip"]) + "</p>");
  484. $( "#connect-success" ).show();
  485. $( "#connect-fail" ).hide();
  486. enableAPTimer = false;
  487. if (!recovery) enableStatusTimer = false;
  488. }
  489. else if (data["urc"] === 1){
  490. //failed attempt
  491. $("#connected-to span").text('');
  492. $("#connect-details h1").text('');
  493. $("#ip").text('0.0.0.0');
  494. $("#netmask").text('0.0.0.0');
  495. $("#gw").text('0.0.0.0');
  496. $("span#foot-wifi").html("");
  497. //don't show any connection
  498. $("#wifi-status").slideUp( "fast", function() {});
  499. //unlock the wait screen
  500. $( "#ok-connect" ).prop("disabled",false);
  501. //update wait screen
  502. $( "#loading" ).hide();
  503. $( "#connect-fail" ).show();
  504. $( "#connect-success" ).hide();
  505. enableAPTimer = true;
  506. enableStatusTimer = true;
  507. }
  508. }
  509. else if (data.hasOwnProperty('urc') && data['urc'] === 0){
  510. //ESP32 is already connected to a wifi without having the user do anything
  511. if( !($("#wifi-status").is(":visible")) ){
  512. $("#connected-to span").text(data["ssid"]);
  513. $("#connect-details h1").text(data["ssid"]);
  514. $("#ip").text(data["ip"]);
  515. $("#netmask").text(data["netmask"]);
  516. $("#gw").text(data["gw"]);
  517. $("#wifi-status").slideDown( "fast", function() {});
  518. $("span#foot-wifi").html(", SSID: <strong>"+data["ssid"]+"</strong>, IP: <strong>"+data["ip"]+"</strong>");
  519. }
  520. enableAPTimer = false;
  521. if (!recovery) enableStatusTimer = false;
  522. }
  523. }
  524. else if (data.hasOwnProperty('urc') && data['urc'] === 2){
  525. //that's a manual disconnect
  526. if($("#wifi-status").is(":visible")){
  527. $("#wifi-status").slideUp( "fast", function() {});
  528. $("span#foot-wifi").html("");
  529. }
  530. enableAPTimer = true;
  531. enableStatusTimer = true;
  532. }
  533. if (data.hasOwnProperty('recovery')) {
  534. if (data["recovery"] === 1) {
  535. recovery = true;
  536. $("#otadiv").show();
  537. $('a[href^="#tab-audio"]').hide();
  538. $('a[href^="#tab-gpio"]').hide();
  539. $('a[href^="#tab-nvs"]').show();
  540. $("footer.footer").removeClass('sl');
  541. $("footer.footer").addClass('recovery');
  542. $("#boot-button").html('Reboot');
  543. $("#boot-form").attr('action', '/reboot.json');
  544. enableStatusTimer = true;
  545. } else {
  546. recovery = false;
  547. $("#otadiv").hide();
  548. $('a[href^="#tab-audio"]').show();
  549. $('a[href^="#tab-gpio"]').show();
  550. $('a[href^="#tab-nvs"]').hide();
  551. $("footer.footer").removeClass('recovery');
  552. $("footer.footer").addClass('sl');
  553. $("#boot-button").html('Recovery');
  554. $("#boot-form").attr('action', '/recovery.json');
  555. enableStatusTimer = false;
  556. }
  557. }
  558. if (data.hasOwnProperty('project_name') && data['project_name'] != ''){
  559. pname = data['project_name'];
  560. }
  561. if (data.hasOwnProperty('version') && data['version'] != ''){
  562. ver = data['version'];
  563. $("span#foot-fw").html("fw: <strong>"+ver+"</strong>, mode: <strong>"+pname+"</strong>");
  564. }
  565. if (data.hasOwnProperty('ota_pct') && data['ota_pct'] != 0){
  566. otapct = data['ota_pct'];
  567. $('.progress-bar').css('width', otapct+'%').attr('aria-valuenow', otapct);
  568. $('.progress-bar').html(otapct+'%');
  569. }
  570. if (data.hasOwnProperty('ota_dsc') && data['ota_dsc'] != ''){
  571. otadsc = data['ota_dsc'];
  572. $("span#flash-status").html(otadsc);
  573. if (otadsc.match(/Error:/) || otapct > 95) {
  574. blockFlashButton = false;
  575. enableStatusTimer = true;
  576. }
  577. } else {
  578. $("span#flash-status").html('');
  579. }
  580. if (data.hasOwnProperty('message') && data['message'] != ''){
  581. var msg = data['message'];
  582. if (msg != lastMsg) {
  583. showMessage(msg);
  584. lastMsg = msg;
  585. }
  586. }
  587. blockAjax = false;
  588. })
  589. .fail(function() {
  590. blockAjax = false;
  591. });
  592. }
  593. function getConfig() {
  594. $.getJSON("/config.json", function(data) {
  595. for (var key in data) {
  596. if (data.hasOwnProperty(key)) {
  597. if (key == 'autoexec') {
  598. if (data["autoexec"] === "1") {
  599. $("#autoexec-cb")[0].checked=true;
  600. } else {
  601. $("#autoexec-cb")[0].checked=false;
  602. }
  603. } else if (key == 'autoexec1') {
  604. $("input#autoexec1").val(data["autoexec1"]);
  605. }
  606. $("tbody#nvsTable").append(
  607. "<tr>"+
  608. "<td>"+key+"</td>"+
  609. "<td class='value'>"+
  610. "<input type='text' class='form-control nvs' id='"+key+"'>"+
  611. "</td>"+
  612. "</tr>"
  613. );
  614. $("input#"+key).val(data[key]);
  615. }
  616. }
  617. $("tbody#nvsTable").append(
  618. "<tr>"+
  619. "<td>"+
  620. "<input type='text' class='form-control' id='nvs-new-key' placeholder='new key'>"+
  621. "</td>"+
  622. "<td>"+
  623. "<input type='text' class='form-control' id='nvs-new-value' placeholder='new value'>"+
  624. "</td>"+
  625. "</tr>"
  626. );
  627. })
  628. .fail(function() {
  629. console.log("failed to fetch config!");
  630. });
  631. }
  632. function showMessage(message) {
  633. $('#message').html(message);
  634. $("#content").fadeTo("slow", 0.3, function() {
  635. $("#message").show(500).delay(5000).hide(500, function() {
  636. $("#content").fadeTo("slow", 1.0);
  637. });
  638. });
  639. }