2
0

code.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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. showMessage('please wait for the ESP32 to reboot', 'WARNING');
  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;
  394. if (conntype == 'manual') {
  395. //Grab the manual SSID and PWD
  396. selectedSSID=$('#manual_ssid').val();
  397. pwd = $("#manual_pwd").val();
  398. dhcpname= $("#dhcp-name2").val();;
  399. }else{
  400. pwd = $("#pwd").val();
  401. dhcpname= $("#dhcp-name1").val();;
  402. }
  403. //reset connection
  404. $( "#loading" ).show();
  405. $( "#connect-success" ).hide();
  406. $( "#connect-fail" ).hide();
  407. $( "#ok-connect" ).prop("disabled",true);
  408. $( "#ssid-wait" ).text(selectedSSID);
  409. $( "#connect" ).slideUp( "fast", function() {});
  410. $( "#connect_manual" ).slideUp( "fast", function() {});
  411. $( "#connect-wait" ).slideDown( "fast", function() {});
  412. $.ajax({
  413. url: '/connect.json',
  414. dataType: 'json',
  415. method: 'POST',
  416. cache: false,
  417. headers: { 'X-Custom-ssid': selectedSSID, 'X-Custom-pwd': pwd, 'X-Custom-host_name': dhcpname },
  418. data: { 'timestamp': Date.now()},
  419. error: function (xhr, ajaxOptions, thrownError) {
  420. console.log(xhr.status);
  421. console.log(thrownError);
  422. if (thrownError != '') showMessage(thrownError, 'ERROR');
  423. }
  424. });
  425. //now we can re-set the intervals regardless of result
  426. startCheckStatusInterval();
  427. startRefreshAPInterval();
  428. }
  429. function rssiToIcon(rssi){
  430. if(rssi >= -60){
  431. return 'w0';
  432. }
  433. else if(rssi >= -67){
  434. return 'w1';
  435. }
  436. else if(rssi >= -75){
  437. return 'w2';
  438. }
  439. else{
  440. return 'w3';
  441. }
  442. }
  443. function refreshAP(){
  444. if (!enableAPTimer) return;
  445. $.getJSON( "/ap.json", function( data ) {
  446. if(data.length > 0){
  447. //sort by signal strength
  448. data.sort(function (a, b) {
  449. var x = a["rssi"]; var y = b["rssi"];
  450. return ((x < y) ? 1 : ((x > y) ? -1 : 0));
  451. });
  452. apList = data;
  453. refreshAPHTML(apList);
  454. }
  455. });
  456. }
  457. function refreshAPHTML(data){
  458. var h = "";
  459. data.forEach(function(e, idx, array) {
  460. 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);
  461. h += "\n";
  462. });
  463. $( "#wifi-list" ).html(h)
  464. }
  465. function checkStatus(){
  466. RepeatCheckStatusInterval();
  467. if (!enableStatusTimer) return;
  468. if (blockAjax) return;
  469. blockAjax = true;
  470. $.getJSON( "/status.json", function( data ) {
  471. if (data.hasOwnProperty('ssid') && data['ssid'] != ""){
  472. if (data["ssid"] === selectedSSID){
  473. //that's a connection attempt
  474. if (data["urc"] === 0){
  475. //got connection
  476. $("#connected-to span").text(data["ssid"]);
  477. $("#connect-details h1").text(data["ssid"]);
  478. $("#ip").text(data["ip"]);
  479. $("#netmask").text(data["netmask"]);
  480. $("#gw").text(data["gw"]);
  481. $("#wifi-status").slideDown( "fast", function() {});
  482. $("span#foot-wifi").html(", SSID: <strong>"+data["ssid"]+"</strong>, IP: <strong>"+data["ip"]+"</strong>");
  483. //unlock the wait screen if needed
  484. $( "#ok-connect" ).prop("disabled",false);
  485. //update wait screen
  486. $( "#loading" ).hide();
  487. $( "#connect-success" ).append("<p>Your IP address now is: " + text(data["ip"]) + "</p>");
  488. $( "#connect-success" ).show();
  489. $( "#connect-fail" ).hide();
  490. enableAPTimer = false;
  491. if (!recovery) enableStatusTimer = false;
  492. }
  493. else if (data["urc"] === 1){
  494. //failed attempt
  495. $("#connected-to span").text('');
  496. $("#connect-details h1").text('');
  497. $("#ip").text('0.0.0.0');
  498. $("#netmask").text('0.0.0.0');
  499. $("#gw").text('0.0.0.0');
  500. $("span#foot-wifi").html("");
  501. //don't show any connection
  502. $("#wifi-status").slideUp( "fast", function() {});
  503. //unlock the wait screen
  504. $( "#ok-connect" ).prop("disabled",false);
  505. //update wait screen
  506. $( "#loading" ).hide();
  507. $( "#connect-fail" ).show();
  508. $( "#connect-success" ).hide();
  509. enableAPTimer = true;
  510. enableStatusTimer = true;
  511. }
  512. }
  513. else if (data.hasOwnProperty('urc') && data['urc'] === 0){
  514. //ESP32 is already connected to a wifi without having the user do anything
  515. if( !($("#wifi-status").is(":visible")) ){
  516. $("#connected-to span").text(data["ssid"]);
  517. $("#connect-details h1").text(data["ssid"]);
  518. $("#ip").text(data["ip"]);
  519. $("#netmask").text(data["netmask"]);
  520. $("#gw").text(data["gw"]);
  521. $("#wifi-status").slideDown( "fast", function() {});
  522. $("span#foot-wifi").html(", SSID: <strong>"+data["ssid"]+"</strong>, IP: <strong>"+data["ip"]+"</strong>");
  523. }
  524. enableAPTimer = false;
  525. if (!recovery) enableStatusTimer = false;
  526. }
  527. }
  528. else if (data.hasOwnProperty('urc') && data['urc'] === 2){
  529. //that's a manual disconnect
  530. if($("#wifi-status").is(":visible")){
  531. $("#wifi-status").slideUp( "fast", function() {});
  532. $("span#foot-wifi").html("");
  533. }
  534. enableAPTimer = true;
  535. enableStatusTimer = true;
  536. }
  537. if (data.hasOwnProperty('recovery')) {
  538. if (data["recovery"] === 1) {
  539. recovery = true;
  540. $("#otadiv").show();
  541. $('a[href^="#tab-audio"]').hide();
  542. $('a[href^="#tab-gpio"]').hide();
  543. $('a[href^="#tab-nvs"]').show();
  544. $("footer.footer").removeClass('sl');
  545. $("footer.footer").addClass('recovery');
  546. $("#boot-button").html('Reboot');
  547. $("#boot-form").attr('action', '/reboot.json');
  548. enableStatusTimer = true;
  549. } else {
  550. recovery = false;
  551. $("#otadiv").hide();
  552. $('a[href^="#tab-audio"]').show();
  553. $('a[href^="#tab-gpio"]').show();
  554. $('a[href^="#tab-nvs"]').hide();
  555. $("footer.footer").removeClass('recovery');
  556. $("footer.footer").addClass('sl');
  557. $("#boot-button").html('Recovery');
  558. $("#boot-form").attr('action', '/recovery.json');
  559. enableStatusTimer = false;
  560. }
  561. }
  562. if (data.hasOwnProperty('project_name') && data['project_name'] != ''){
  563. pname = data['project_name'];
  564. }
  565. if (data.hasOwnProperty('version') && data['version'] != ''){
  566. ver = data['version'];
  567. $("span#foot-fw").html("fw: <strong>"+ver+"</strong>, mode: <strong>"+pname+"</strong>");
  568. }
  569. if (data.hasOwnProperty('ota_pct') && data['ota_pct'] != 0){
  570. otapct = data['ota_pct'];
  571. $('.progress-bar').css('width', otapct+'%').attr('aria-valuenow', otapct);
  572. $('.progress-bar').html(otapct+'%');
  573. }
  574. if (data.hasOwnProperty('ota_dsc') && data['ota_dsc'] != ''){
  575. otadsc = data['ota_dsc'];
  576. $("span#flash-status").html(otadsc);
  577. if (otadsc.match(/Error:/) || otapct > 95) {
  578. blockFlashButton = false;
  579. enableStatusTimer = true;
  580. }
  581. } else {
  582. $("span#flash-status").html('');
  583. }
  584. if (data.hasOwnProperty('message') && data['message'] != ''){
  585. var msg = data['message'].text;
  586. var severity = data['message'].severity;
  587. if (msg != lastMsg) {
  588. showMessage(msg, severity);
  589. lastMsg = msg;
  590. }
  591. }
  592. blockAjax = false;
  593. })
  594. .fail(function() {
  595. showMessage('Could not get status.json!', 'ERROR');
  596. blockAjax = false;
  597. });
  598. }
  599. function getConfig() {
  600. $.getJSON("/config.json", function(data) {
  601. for (var key in data) {
  602. if (data.hasOwnProperty(key)) {
  603. if (key == 'autoexec') {
  604. if (data["autoexec"] === "1") {
  605. $("#autoexec-cb")[0].checked=true;
  606. } else {
  607. $("#autoexec-cb")[0].checked=false;
  608. }
  609. } else if (key == 'autoexec1') {
  610. $("textarea#autoexec1").val(data[key]);
  611. } else if (key == 'host_name') {
  612. $("dhcp-name1").val(data[key]);
  613. $("dhcp-name2").val(data[key]);
  614. }
  615. $("tbody#nvsTable").append(
  616. "<tr>"+
  617. "<td>"+key+"</td>"+
  618. "<td class='value'>"+
  619. "<input type='text' class='form-control nvs' id='"+key+"'>"+
  620. "</td>"+
  621. "</tr>"
  622. );
  623. $("input#"+key).val(data[key]);
  624. }
  625. }
  626. $("tbody#nvsTable").append(
  627. "<tr>"+
  628. "<td>"+
  629. "<input type='text' class='form-control' id='nvs-new-key' placeholder='new key'>"+
  630. "</td>"+
  631. "<td>"+
  632. "<input type='text' class='form-control' id='nvs-new-value' placeholder='new value'>"+
  633. "</td>"+
  634. "</tr>"
  635. );
  636. })
  637. .fail(function() {
  638. showMessage('Could not get config.json!', 'ERROR');
  639. console.log("failed to fetch config!");
  640. });
  641. }
  642. function showMessage(message, severity) {
  643. if (severity == 'INFO') {
  644. $('#message').css('background', '#6af');
  645. } else if (severity == 'WARNING') {
  646. $('#message').css('background', '#ff0');
  647. } else {
  648. $('#message').css('background', '#f00');
  649. }
  650. $('#message').html(message);
  651. $("#content").fadeTo("slow", 0.3, function() {
  652. $("#message").show(500).delay(5000).hide(500, function() {
  653. $("#content").fadeTo("slow", 1.0);
  654. });
  655. });
  656. }