2
0

code.js 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  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. if (!String.prototype.encodeHTML) {
  13. String.prototype.encodeHTML = function () {
  14. return this.replace(/&/g, '&')
  15. .replace(/</g, '&lt;')
  16. .replace(/>/g, '&gt;')
  17. .replace(/"/g, '&quot;')
  18. .replace(/'/g, '&apos;');
  19. };
  20. }
  21. var nvs_type_t = {
  22. NVS_TYPE_U8: 0x01,
  23. /*!< Type uint8_t */
  24. NVS_TYPE_I8: 0x11,
  25. /*!< Type int8_t */
  26. NVS_TYPE_U16: 0x02,
  27. /*!< Type uint16_t */
  28. NVS_TYPE_I16: 0x12,
  29. /*!< Type int16_t */
  30. NVS_TYPE_U32: 0x04,
  31. /*!< Type uint32_t */
  32. NVS_TYPE_I32: 0x14,
  33. /*!< Type int32_t */
  34. NVS_TYPE_U64: 0x08,
  35. /*!< Type uint64_t */
  36. NVS_TYPE_I64: 0x18,
  37. /*!< Type int64_t */
  38. NVS_TYPE_STR: 0x21,
  39. /*!< Type string */
  40. NVS_TYPE_BLOB: 0x42,
  41. /*!< Type blob */
  42. NVS_TYPE_ANY: 0xff /*!< Must be last */
  43. };
  44. pillcolors = {
  45. 'MESSAGING_INFO' : 'badge-success',
  46. 'MESSAGING_WARNING' : 'badge-warning',
  47. 'MESSAGING_ERROR' : 'badge-danger'
  48. }
  49. var task_state_t = {
  50. 0: "eRunning",
  51. /*!< A task is querying the state of itself, so must be running. */
  52. 1: "eReady",
  53. /*!< The task being queried is in a read or pending ready list. */
  54. 2: "eBlocked",
  55. /*!< The task being queried is in the Blocked state. */
  56. 3: "eSuspended",
  57. /*!< The task being queried is in the Suspended state, or is in the Blocked state with an infinite time out. */
  58. 4: "eDeleted"
  59. }
  60. var escapeHTML = function(unsafe) {
  61. return unsafe.replace(/[&<"']/g, function(m) {
  62. switch (m) {
  63. case '&':
  64. return '&amp;';
  65. case '<':
  66. return '&lt;';
  67. case '"':
  68. return '&quot;';
  69. default:
  70. return '&#039;';
  71. }
  72. });
  73. };
  74. function handleTemplateTypeRadio(outtype){
  75. if (outtype == 'bt') {
  76. $("#btsinkdiv").parent().show(200);
  77. $("#btsinkdiv").show();
  78. $('#bt').prop('checked',true);
  79. o_bt.setAttribute("display", "inline");
  80. o_spdif.setAttribute("display", "none");
  81. o_i2s.setAttribute("display", "none");
  82. output = 'bt';
  83. } else if (outtype == 'spdif') {
  84. $("#btsinkdiv").parent().hide(200);
  85. $("#btsinkdiv").show();
  86. $('#spdif').prop('checked',true);
  87. o_bt.setAttribute("display", "none");
  88. o_spdif.setAttribute("display", "inline");
  89. o_i2s.setAttribute("display", "none");
  90. output = 'spdif';
  91. } else {
  92. $("#btsinkdiv").parent().hide(200);
  93. $("#btsinkdiv").show();
  94. $('#i2s').prop('checked',true);
  95. o_bt.setAttribute("display", "none");
  96. o_spdif.setAttribute("display", "none");
  97. o_i2s.setAttribute("display", "inline");
  98. output = 'i2s';
  99. }
  100. }
  101. function handleExceptionResponse(xhr, ajaxOptions, thrownError){
  102. console.log(xhr.status);
  103. console.log(thrownError);
  104. if (thrownError != '') showLocalMessage(thrownError, 'MESSAGING_ERROR');
  105. }
  106. function HideCmdMessage(cmdname){
  107. $('#toast_'+cmdname).css('display','none');
  108. $('#toast_'+cmdname).removeClass('table-success').removeClass('table-warning').removeClass('table-danger').addClass('table-success');
  109. $('#msg_'+cmdname).html('');
  110. }
  111. function showCmdMessage(cmdname,msgtype, msgtext,append=false){
  112. color='table-success';
  113. if (msgtype == 'MESSAGING_WARNING') {
  114. color='table-warning';
  115. } else if (msgtype == 'MESSAGING_ERROR') {
  116. color ='table-danger';
  117. }
  118. $('#toast_'+cmdname).css('display','block');
  119. $('#toast_'+cmdname).removeClass('table-success').removeClass('table-warning').removeClass('table-danger').addClass(color);
  120. escapedtext=escapeHTML(msgtext.substring(0, msgtext.length - 1)).replace(/\n/g, '<br />');
  121. escapedtext=($('#msg_'+cmdname).html().length>0 && append?$('#msg_'+cmdname).html()+'<br/>':'')+escapedtext;
  122. $('#msg_'+cmdname).html(escapedtext);
  123. }
  124. var releaseURL = 'https://api.github.com/repos/sle118/squeezelite-esp32/releases';
  125. var recovery = false;
  126. var enableAPTimer = true;
  127. var enableStatusTimer = true;
  128. var commandHeader = 'squeezelite -b 500:2000 -d all=info -C 30 -W';
  129. var pname, ver, otapct, otadsc;
  130. var blockAjax = false;
  131. var blockFlashButton = false;
  132. var dblclickCounter = 0;
  133. var apList = null;
  134. var selectedSSID = "";
  135. var refreshAPInterval = null;
  136. var checkStatusInterval = null;
  137. var messagecount=0;
  138. var messageseverity="MESSAGING_INFO";
  139. var StatusIntervalActive = false;
  140. var RefreshAPIIntervalActive = false;
  141. var LastRecoveryState = null;
  142. var LastCommandsState = null;
  143. var output = '';
  144. Promise.prototype.delay = function(duration) {
  145. return this.then(function(value) {
  146. return new Promise(function(resolve) {
  147. setTimeout(function() {
  148. resolve(value)
  149. }, duration)
  150. })
  151. }, function(reason) {
  152. return new Promise(function(resolve, reject) {
  153. setTimeout(function() {
  154. reject(reason)
  155. }, duration)
  156. })
  157. })
  158. }
  159. function stopCheckStatusInterval() {
  160. if (checkStatusInterval != null) {
  161. clearTimeout(checkStatusInterval);
  162. checkStatusInterval = null;
  163. }
  164. StatusIntervalActive = false;
  165. }
  166. function stopRefreshAPInterval() {
  167. if (refreshAPInterval != null) {
  168. clearTimeout(refreshAPInterval);
  169. refreshAPInterval = null;
  170. }
  171. RefreshAPIIntervalActive = false;
  172. }
  173. function startCheckStatusInterval() {
  174. StatusIntervalActive = true;
  175. checkStatusInterval = setTimeout(checkStatus, 3000);
  176. }
  177. function startRefreshAPInterval() {
  178. RefreshAPIIntervalActive = true;
  179. refreshAPInterval = setTimeout(refreshAP(false), 4500); // leave enough time for the initial scan
  180. }
  181. function RepeatCheckStatusInterval() {
  182. if (StatusIntervalActive)
  183. startCheckStatusInterval();
  184. }
  185. function RepeatRefreshAPInterval() {
  186. if (RefreshAPIIntervalActive)
  187. startRefreshAPInterval();
  188. }
  189. function getConfigJson(slimMode) {
  190. var config = {};
  191. $("input.nvs").each(function() {
  192. var key = $(this)[0].id;
  193. var val = $(this).val();
  194. if (!slimMode) {
  195. var nvs_type = parseInt($(this)[0].attributes.nvs_type.nodeValue, 10);
  196. if (key != '') {
  197. config[key] = {};
  198. if (nvs_type == nvs_type_t.NVS_TYPE_U8 ||
  199. nvs_type == nvs_type_t.NVS_TYPE_I8 ||
  200. nvs_type == nvs_type_t.NVS_TYPE_U16 ||
  201. nvs_type == nvs_type_t.NVS_TYPE_I16 ||
  202. nvs_type == nvs_type_t.NVS_TYPE_U32 ||
  203. nvs_type == nvs_type_t.NVS_TYPE_I32 ||
  204. nvs_type == nvs_type_t.NVS_TYPE_U64 ||
  205. nvs_type == nvs_type_t.NVS_TYPE_I64) {
  206. config[key].value = parseInt(val);
  207. } else {
  208. config[key].value = val;
  209. }
  210. config[key].type = nvs_type;
  211. }
  212. } else {
  213. config[key] = val;
  214. }
  215. });
  216. var key = $("#nvs-new-key").val();
  217. var val = $("#nvs-new-value").val();
  218. if (key != '') {
  219. if (!slimMode) {
  220. config[key] = {};
  221. config[key].value = val;
  222. config[key].type = 33;
  223. } else {
  224. config[key] = val;
  225. }
  226. }
  227. return config;
  228. }
  229. function onFileLoad(elementId, event) {
  230. var data = {};
  231. try {
  232. data = JSON.parse(elementId.srcElement.result);
  233. } catch (e) {
  234. alert('Parsing failed!\r\n ' + e);
  235. }
  236. $("input.nvs").each(function() {
  237. var key = $(this)[0].id;
  238. var val = $(this).val();
  239. if (data[key]) {
  240. if (data[key] != val) {
  241. console.log("Changed " & key & " " & val & "==>" & data[key]);
  242. $(this).val(data[key]);
  243. }
  244. } else {
  245. console.log("Value " & key & " missing from file");
  246. }
  247. });
  248. }
  249. function onChooseFile(event, onLoadFileHandler) {
  250. if (typeof window.FileReader !== 'function')
  251. throw ("The file API isn't supported on this browser.");
  252. input = event.target;
  253. if (!input)
  254. throw ("The browser does not properly implement the event object");
  255. if (!input.files)
  256. throw ("This browser does not support the `files` property of the file input.");
  257. if (!input.files[0])
  258. return undefined;
  259. file = input.files[0];
  260. fr = new FileReader();
  261. fr.onload = onLoadFileHandler;
  262. fr.readAsText(file);
  263. input.value = "";
  264. }
  265. function delay_reboot(duration,cmdname, ota=false){
  266. url= (ota?'/reboot_ota.json':'/reboot.json');
  267. $("tbody#tasks").empty();
  268. $("#tasks_sect").css('visibility','collapse');
  269. Promise.resolve(cmdname).delay(duration).then(function(cmdname) {
  270. if(cmdname?.length >0){
  271. showCmdMessage(cmdname,'MESSAGING_WARNING','Rebooting the ESP32.\n',true);
  272. }
  273. else {
  274. showLocalMessage('Rebooting the ESP32.\n','MESSAGING_WARNING')
  275. }
  276. console.log('now triggering reboot');
  277. $.ajax({
  278. url: this.url,
  279. dataType: 'text',
  280. method: 'POST',
  281. cache: false,
  282. contentType: 'application/json; charset=utf-8',
  283. data: JSON.stringify({
  284. 'timestamp': Date.now()
  285. }),
  286. error: handleExceptionResponse,
  287. complete: function(response) {
  288. console.log('reboot call completed');
  289. Promise.resolve(cmdname).delay(6000).then(function(cmdname) {
  290. if(cmdname?.length >0) HideCmdMessage(cmdname);
  291. getCommands();
  292. getConfig();
  293. });
  294. }
  295. });
  296. });
  297. }
  298. function save_autoexec1(apply){
  299. showCmdMessage('cfg-audio-tmpl','MESSAGING_INFO',"Saving.\n",false);
  300. var commandLine = commandHeader + ' -n "' + $("#player").val() + '"';
  301. if (output == 'bt') {
  302. if($("#btsinkdiv").val()?.length!=0){
  303. commandLine += ' -o "BT -n \'' + $("#btsinkdiv").val() + '\'" -Z 192000';
  304. }
  305. else {
  306. showCmdMessage('cfg-audio-tmpl','MESSAGING_ERROR',"BT Sink Name required for output bluetooth.\n",true);
  307. return;
  308. }
  309. } else if (output == 'spdif') {
  310. commandLine += ' -o SPDIF -Z 192000';
  311. } else {
  312. commandLine += ' -o I2S';
  313. }
  314. if ($("#optional").val() != '') {
  315. commandLine += ' ' + $("#optional").val();
  316. }
  317. var data = {
  318. 'timestamp': Date.now()
  319. };
  320. autoexec = $("#disable-squeezelite").prop('checked') ? "0" : "1";
  321. data['config'] = {
  322. autoexec1: { value: commandLine, type: 33 },
  323. autoexec: { value: autoexec, type: 33 }
  324. }
  325. $.ajax({
  326. url: '/config.json',
  327. dataType: 'text',
  328. method: 'POST',
  329. cache: false,
  330. contentType: 'application/json; charset=utf-8',
  331. data: JSON.stringify(data),
  332. error: handleExceptionResponse,
  333. complete: function(response) {
  334. if(JSON.parse(response?.responseText)?.result == "OK"){
  335. showCmdMessage('cfg-audio-tmpl','MESSAGING_INFO',"Done.\n",true);
  336. if (apply) {
  337. delay_reboot(1500,"cfg-audio-tmpl");
  338. }
  339. }
  340. else if(response.responseText) {
  341. showCmdMessage('cfg-audio-tmpl','MESSAGING_WARNING',JSON.parse(response.responseText).Result + "\n",true);
  342. }
  343. else {
  344. showCmdMessage('cfg-audio-tmpl','MESSAGING_ERROR',response.responseText+'\n');
  345. }
  346. console.log(response.responseText);
  347. }
  348. });
  349. console.log('sent data:', JSON.stringify(data));
  350. }
  351. $(document).ready(function() {
  352. // $(".dropdown-item").on("click", function(e){
  353. // var linkText = $(e.relatedTarget).text(); // Get the link text
  354. // });
  355. $("input#show-commands")[0].checked = LastCommandsState == 1 ? true : false;
  356. $('a[href^="#tab-commands"]').hide();
  357. $("#load-nvs").click(function() {
  358. $("#nvsfilename").trigger('click');
  359. });
  360. $("#wifi-status").on("click", ".ape", function() {
  361. $("#wifi").slideUp("fast", function() {});
  362. $("#connect-details").slideDown("fast", function() {});
  363. });
  364. $("#clear-syslog").on("click",function(){
  365. messagecount=0;
  366. messageseverity="MESSAGING_INFO";
  367. $('#msgcnt').text('');
  368. $("#syslogTable").html('');
  369. });
  370. $("#manual_add").on("click", ".ape", function() {
  371. selectedSSID = $(this).text();
  372. $("#ssid-pwd").text(selectedSSID);
  373. $("#wifi").slideUp("fast", function() {});
  374. $("#connect_manual").slideDown("fast", function() {});
  375. $("#connect").slideUp("fast", function() {});
  376. //update wait screen
  377. $("#loading").show();
  378. $("#connect-success").hide();
  379. $("#connect-fail").hide();
  380. });
  381. $("#wifi-list").on("click", ".ape", function() {
  382. selectedSSID = $(this).text();
  383. $("#ssid-pwd").text(selectedSSID);
  384. $("#wifi").slideUp("fast", function() {});
  385. $("#connect_manual").slideUp("fast", function() {});
  386. $("#connect").slideDown("fast", function() {});
  387. //update wait screen
  388. $("#loading").show();
  389. $("#connect-success").hide();
  390. $("#connect-fail").hide();
  391. });
  392. $("#cancel").on("click", function() {
  393. selectedSSID = "";
  394. $("#connect").slideUp("fast", function() {});
  395. $("#connect_manual").slideUp("fast", function() {});
  396. $("#wifi").slideDown("fast", function() {});
  397. });
  398. $("#manual_cancel").on("click", function() {
  399. selectedSSID = "";
  400. $("#connect").slideUp("fast", function() {});
  401. $("#connect_manual").slideUp("fast", function() {});
  402. $("#wifi").slideDown("fast", function() {});
  403. });
  404. $("#join").on("click", function() {
  405. performConnect();
  406. });
  407. $("#manual_join").on("click", function() {
  408. performConnect($(this).data('connect'));
  409. });
  410. $("#ok-details").on("click", function() {
  411. $("#connect-details").slideUp("fast", function() {});
  412. $("#wifi").slideDown("fast", function() {});
  413. });
  414. $("#ok-credits").on("click", function() {
  415. $("#credits").slideUp("fast", function() {});
  416. $("#app").slideDown("fast", function() {});
  417. });
  418. $("#acredits").on("click", function(event) {
  419. event.preventDefault();
  420. $("#app").slideUp("fast", function() {});
  421. $("#credits").slideDown("fast", function() {});
  422. });
  423. $("#ok-connect").on("click", function() {
  424. $("#connect-wait").slideUp("fast", function() {});
  425. $("#wifi").slideDown("fast", function() {});
  426. });
  427. $("#disconnect").on("click", function() {
  428. $("#connect-details-wrap").addClass('blur');
  429. $("#diag-disconnect").slideDown("fast", function() {});
  430. });
  431. $("#no-disconnect").on("click", function() {
  432. $("#diag-disconnect").slideUp("fast", function() {});
  433. $("#connect-details-wrap").removeClass('blur');
  434. });
  435. $("#yes-disconnect").on("click", function() {
  436. stopCheckStatusInterval();
  437. selectedSSID = "";
  438. $("#diag-disconnect").slideUp("fast", function() {});
  439. $("#connect-details-wrap").removeClass('blur');
  440. $.ajax({
  441. url: '/connect.json',
  442. dataType: 'text',
  443. method: 'DELETE',
  444. cache: false,
  445. contentType: 'application/json; charset=utf-8',
  446. data: JSON.stringify({
  447. 'timestamp': Date.now()
  448. })
  449. });
  450. startCheckStatusInterval();
  451. $("#connect-details").slideUp("fast", function() {});
  452. $("#wifi").slideDown("fast", function() {})
  453. });
  454. $("input#show-commands").on("click", function() {
  455. this.checked = this.checked ? 1 : 0;
  456. if (this.checked) {
  457. $('a[href^="#tab-commands"]').show();
  458. LastCommandsState = 1;
  459. } else {
  460. LastCommandsState = 0;
  461. $('a[href^="#tab-commands"]').hide();
  462. }
  463. });
  464. $("input#show-nvs").on("click", function() {
  465. this.checked = this.checked ? 1 : 0;
  466. if (this.checked) {
  467. $('a[href^="#tab-nvs"]').show();
  468. } else {
  469. $('a[href^="#tab-nvs"]').hide();
  470. }
  471. });
  472. $("#save-as-nvs").on("click", function() {
  473. var data = {
  474. 'timestamp': Date.now()
  475. };
  476. var config = getConfigJson(true);
  477. const a = document.createElement("a");
  478. a.href = URL.createObjectURL(
  479. new Blob([JSON.stringify(config, null, 2)], {
  480. type: "text/plain"
  481. }));
  482. a.setAttribute("download", "nvs_config" + Date.now() + "json");
  483. document.body.appendChild(a);
  484. a.click();
  485. document.body.removeChild(a);
  486. console.log('sent config JSON with headers:', JSON.stringify(headers));
  487. console.log('sent config JSON with data:', JSON.stringify(data));
  488. });
  489. $("#save-nvs").on("click", function() {
  490. var headers = {};
  491. var data = {
  492. 'timestamp': Date.now()
  493. };
  494. var config = getConfigJson(false);
  495. data['config'] = config;
  496. $.ajax({
  497. url: '/config.json',
  498. dataType: 'text',
  499. method: 'POST',
  500. cache: false,
  501. headers: headers,
  502. contentType: 'application/json; charset=utf-8',
  503. data: JSON.stringify(data),
  504. error: handleExceptionResponse
  505. });
  506. console.log('sent config JSON with headers:', JSON.stringify(headers));
  507. console.log('sent config JSON with data:', JSON.stringify(data));
  508. });
  509. $("#fwUpload").on("click", function() {
  510. var upload_path = "/flash.json";
  511. var fileInput = document.getElementById("flashfilename").files;
  512. if (fileInput.length == 0) {
  513. alert("No file selected!");
  514. } else {
  515. var file = fileInput[0];
  516. var xhttp = new XMLHttpRequest();
  517. xhttp.onreadystatechange = function() {
  518. if (xhttp.readyState == 4) {
  519. if (xhttp.status == 200) {
  520. showLocalMessage(xhttp.responseText, 'MESSAGING_INFO')
  521. } else if (xhttp.status == 0) {
  522. showLocalMessage("Upload connection was closed abruptly!", 'MESSAGING_ERROR');
  523. } else {
  524. showLocalMessage(xhttp.status + " Error!\n" + xhttp.responseText, 'MESSAGING_ERROR');
  525. }
  526. }
  527. };
  528. xhttp.open("POST", upload_path, true);
  529. xhttp.send(file);
  530. }
  531. enableStatusTimer = true;
  532. });
  533. $("#flash").on("click", function() {
  534. var data = {
  535. 'timestamp': Date.now()
  536. };
  537. if (blockFlashButton) return;
  538. blockFlashButton = true;
  539. var url = $("#fwurl").val();
  540. data['config'] = {
  541. fwurl: {
  542. value: url,
  543. type: 33
  544. }
  545. };
  546. $.ajax({
  547. url: '/config.json',
  548. dataType: 'text',
  549. method: 'POST',
  550. cache: false,
  551. contentType: 'application/json; charset=utf-8',
  552. data: JSON.stringify(data),
  553. error: handleExceptionResponse
  554. });
  555. enableStatusTimer = true;
  556. });
  557. $('[name=output-tmpl]').on("click", function() {
  558. handleTemplateTypeRadio(this.id);
  559. });
  560. $('#fwcheck').on("click", function() {
  561. $("#releaseTable").html("");
  562. $("#fwbranch").empty();
  563. $.getJSON(releaseURL, function(data) {
  564. var i = 0;
  565. var branches = [];
  566. data.forEach(function(release) {
  567. namecomponents=release.name.split('#');
  568. ver=namecomponents[0];
  569. idf=namecomponents[1];
  570. cfg=namecomponents[2];
  571. branch=namecomponents[3];
  572. if (!branches.includes(branch)) {
  573. branches.push(branch);
  574. }
  575. });
  576. var fwb;
  577. branches.forEach(function(branch) {
  578. fwb += '<option value="' + branch + '">' + branch + '</option>';
  579. });
  580. $("#fwbranch").append(fwb);
  581. data.forEach(function(release) {
  582. var url = '';
  583. release.assets.forEach(function(asset) {
  584. if (asset.name.match(/\.bin$/)) {
  585. url = asset.browser_download_url;
  586. }
  587. });
  588. namecomponents = release.name.split('#');
  589. ver=namecomponents[0];
  590. idf=namecomponents[1];
  591. cfg=namecomponents[2];
  592. branch=namecomponents[3];
  593. var body = release.body;
  594. body = body.replace(/\'/ig, "\"");
  595. body = body.replace(/[\s\S]+(### Revision Log[\s\S]+)### ESP-IDF Version Used[\s\S]+/, "$1");
  596. body = body.replace(/- \(.+?\) /g, "- ");
  597. var [date, time] = release.created_at.split('T');
  598. var trclass = (i++ > 6) ? ' hide' : '';
  599. $("#releaseTable").append(
  600. "<tr class='release" + trclass + "'>" +
  601. "<td data-toggle='tooltip' title='" + body + "'>" + ver + "</td>" +
  602. "<td>" + date + "</td>" +
  603. "<td>" + cfg + "</td>" +
  604. "<td>" + idf + "</td>" +
  605. "<td>" + branch + "</td>" +
  606. "<td><input type='button' class='btn btn-success' value='Select' data-url='" + url + "' onclick='setURL(this);' /></td>" +
  607. "</tr>"
  608. );
  609. });
  610. if (i > 7) {
  611. $("#releaseTable").append(
  612. "<tr id='showall'>" +
  613. "<td colspan='6'>" +
  614. "<input type='button' id='showallbutton' class='btn btn-info' value='Show older releases' />" +
  615. "</td>" +
  616. "</tr>"
  617. );
  618. $('#showallbutton').on("click", function() {
  619. $("tr.hide").removeClass("hide");
  620. $("tr#showall").addClass("hide");
  621. });
  622. }
  623. $("#searchfw").css("display", "inline");
  624. })
  625. .fail(function() {
  626. alert("failed to fetch release history!");
  627. });
  628. });
  629. $('input#searchinput').on("input", function() {
  630. var s = $('input#searchinput').val();
  631. var re = new RegExp(s, "gi");
  632. if (s.length == 0) {
  633. $("tr.release").removeClass("hide");
  634. } else if (s.length < 3) {
  635. $("tr.release").addClass("hide");
  636. } else {
  637. $("tr.release").addClass("hide");
  638. $("tr.release").each(function(tr) {
  639. $(this).find('td').each(function() {
  640. if ($(this).html().match(re)) {
  641. $(this).parent().removeClass('hide');
  642. }
  643. });
  644. });
  645. }
  646. });
  647. $("#fwbranch").change(function(e) {
  648. var branch = this.value;
  649. var re = new RegExp('^' + branch + '$', "gi");
  650. $("tr.release").addClass("hide");
  651. $("tr.release").each(function(tr) {
  652. $(this).find('td').each(function() {
  653. console.log($(this).html());
  654. if ($(this).html().match(re)) {
  655. $(this).parent().removeClass('hide');
  656. }
  657. });
  658. });
  659. });
  660. $('#boot-button').on("click", function() {
  661. enableStatusTimer = true;
  662. });
  663. $('#reboot-button').on("click", function() {
  664. enableStatusTimer = true;
  665. });
  666. $('#updateAP').on("click", function() {
  667. refreshAP(true);
  668. console.log("refresh AP");
  669. });
  670. //first time the page loads: attempt to get the connection status and start the wifi scan
  671. refreshAP(false);
  672. getConfig();
  673. getCommands();
  674. //start timers
  675. startCheckStatusInterval();
  676. //startRefreshAPInterval();
  677. $('[data-toggle="tooltip"]').tooltip({
  678. html: true,
  679. placement: 'right',
  680. });
  681. });
  682. function setURL(button) {
  683. var url = button.dataset.url;
  684. $("#fwurl").val(url);
  685. $('[data-url^="http"]').addClass("btn-success").removeClass("btn-danger");
  686. $('[data-url="' + url + '"]').addClass("btn-danger").removeClass("btn-success");
  687. }
  688. function performConnect(conntype) {
  689. //stop the status refresh. This prevents a race condition where a status
  690. //request would be refreshed with wrong ip info from a previous connection
  691. //and the request would automatically shows as succesful.
  692. stopCheckStatusInterval();
  693. //stop refreshing wifi list
  694. stopRefreshAPInterval();
  695. var pwd;
  696. var dhcpname;
  697. if (conntype == 'manual') {
  698. //Grab the manual SSID and PWD
  699. selectedSSID = $('#manual_ssid').val();
  700. pwd = $("#manual_pwd").val();
  701. dhcpname = $("#dhcp-name2").val();;
  702. } else {
  703. pwd = $("#pwd").val();
  704. dhcpname = $("#dhcp-name1").val();;
  705. }
  706. //reset connection
  707. $("#loading").show();
  708. $("#connect-success").hide();
  709. $("#connect-fail").hide();
  710. $("#ok-connect").prop("disabled", true);
  711. $("#ssid-wait").text(selectedSSID);
  712. $("#connect").slideUp("fast", function() {});
  713. $("#connect_manual").slideUp("fast", function() {});
  714. $("#connect-wait").slideDown("fast", function() {});
  715. $.ajax({
  716. url: '/connect.json',
  717. dataType: 'text',
  718. method: 'POST',
  719. cache: false,
  720. // headers: { 'X-Custom-ssid': selectedSSID, 'X-Custom-pwd': pwd, 'X-Custom-host_name': dhcpname },
  721. contentType: 'application/json; charset=utf-8',
  722. data: JSON.stringify({
  723. 'timestamp': Date.now(),
  724. 'ssid': selectedSSID,
  725. 'pwd': pwd,
  726. 'host_name': dhcpname
  727. }),
  728. error: handleExceptionResponse
  729. });
  730. //now we can re-set the intervals regardless of result
  731. startCheckStatusInterval();
  732. startRefreshAPInterval();
  733. }
  734. function rssiToIcon(rssi) {
  735. if (rssi >= -60) {
  736. return 'w0';
  737. } else if (rssi >= -67) {
  738. return 'w1';
  739. } else if (rssi >= -75) {
  740. return 'w2';
  741. } else {
  742. return 'w3';
  743. }
  744. }
  745. function refreshAP(force) {
  746. if (!enableAPTimer && !force) return;
  747. $.getJSON("/scan.json", async function(data) {
  748. await sleep(2000);
  749. $.getJSON("/ap.json", function(data) {
  750. if (data.length > 0) {
  751. //sort by signal strength
  752. data.sort(function(a, b) {
  753. var x = a["rssi"];
  754. var y = b["rssi"];
  755. return ((x < y) ? 1 : ((x > y) ? -1 : 0));
  756. });
  757. apList = data;
  758. refreshAPHTML(apList);
  759. }
  760. });
  761. });
  762. }
  763. function refreshAPHTML(data) {
  764. var h = "";
  765. data.forEach(function(e, idx, array) {
  766. 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);
  767. h += "\n";
  768. });
  769. $("#wifi-list").html(h)
  770. }
  771. function getMessages() {
  772. $.getJSON("/messages.json?1", async function(data) {
  773. for (const msg of data) {
  774. var msg_age = msg["current_time"] - msg["sent_time"];
  775. var msg_time = new Date();
  776. msg_time.setTime(msg_time.getTime() - msg_age);
  777. switch (msg["class"]) {
  778. case "MESSAGING_CLASS_OTA":
  779. //message: "{"ota_dsc":"Erasing flash complete","ota_pct":0}"
  780. var ota_data = JSON.parse(msg["message"]);
  781. if (ota_data.hasOwnProperty('ota_pct') && ota_data['ota_pct'] != 0) {
  782. otapct = ota_data['ota_pct'];
  783. $('.progress-bar').css('width', otapct + '%').attr('aria-valuenow', otapct);
  784. $('.progress-bar').html(otapct + '%');
  785. }
  786. if (ota_data.hasOwnProperty('ota_dsc') && ota_data['ota_dsc'] != '') {
  787. otadsc = ota_data['ota_dsc'];
  788. $("span#flash-status").html(otadsc);
  789. if (msg.type == "MESSAGING_ERROR" || otapct > 95) {
  790. blockFlashButton = false;
  791. enableStatusTimer = true;
  792. }
  793. }
  794. break;
  795. case "MESSAGING_CLASS_STATS":
  796. // for task states, check structure : task_state_t
  797. var stats_data = JSON.parse(msg["message"]);
  798. console.log(msg_time.toLocaleString() + " - Number of tasks on the ESP32: " + stats_data["ntasks"]);
  799. console.log(msg_time.toLocaleString() + '\tname' + '\tcpu' + '\tstate' + '\tminstk' + '\tbprio' + '\tcprio' + '\tnum');
  800. if(stats_data["tasks"]){
  801. if($("#tasks_sect").css('visibility') =='collapse'){
  802. $("#tasks_sect").css('visibility','visible');
  803. }
  804. var trows="";
  805. stats_data["tasks"].sort(function(a, b){
  806. return (b.cpu-a.cpu);
  807. }).forEach(function(task) {
  808. console.log(msg_time.toLocaleString() + '\t' + task["nme"] + '\t' + task["cpu"] + '\t' + task_state_t[task["st"]] + '\t' + task["minstk"] + '\t' + task["bprio"] + '\t' + task["cprio"] + '\t' + task["num"]);
  809. trows+='<tr class="table-primary"><th scope="row">' + task["num"]+ '</th><td>' + task["nme"] + '</td><td>' + task["cpu"] + '</td><td>' + task_state_t[task["st"]] + '</td><td>' + task["minstk"]+ '</td><td>' + task["bprio"]+ '</td><td>' + task["cprio"] + '</td></tr>'
  810. });
  811. $("tbody#tasks").html(trows);
  812. }
  813. else if($("#tasks_sect").css('visibility') =='visible'){
  814. $("tbody#tasks").empty();
  815. $("#tasks_sect").css('visibility','collapse');
  816. }
  817. break;
  818. case "MESSAGING_CLASS_SYSTEM":
  819. var r = showMessage(msg,msg_time, msg_age);
  820. break;
  821. case "MESSAGING_CLASS_CFGCMD":
  822. var msgparts=msg["message"].split(/([^\n]*)\n(.*)/gs);
  823. showCmdMessage(msgparts[1],msg['type'],msgparts[2],true);
  824. break;
  825. default:
  826. break;
  827. }
  828. }
  829. })
  830. .fail( handleExceptionResponse);
  831. /*
  832. Minstk is minimum stack space left
  833. Bprio is base priority
  834. cprio is current priority
  835. nme is name
  836. st is task state. I provided a "typedef" that you can use to convert to text
  837. cpu is cpu percent used
  838. */
  839. }
  840. function handleRecoveryMode(data){
  841. if (data.hasOwnProperty('recovery')) {
  842. if (LastRecoveryState != data["recovery"]) {
  843. LastRecoveryState = data["recovery"];
  844. $("input#show-nvs")[0].checked = LastRecoveryState == 1 ? true : false;
  845. }
  846. if ($("input#show-nvs")[0].checked) {
  847. $('a[href^="#tab-nvs"]').show();
  848. } else {
  849. $('a[href^="#tab-nvs"]').hide();
  850. }
  851. enableStatusTimer = true;
  852. if (data["recovery"] === 1) {
  853. recovery = true;
  854. $("#reboot_ota_nav").show();
  855. $("#reboot_nav").hide();
  856. $("#otadiv").show();
  857. $('#uploaddiv').show();
  858. $("footer.footer").removeClass('sl');
  859. $("footer.footer").addClass('bg-warning');
  860. $("#mainnav").addClass('bg-warning');
  861. $('[name=secnav]').addClass('bg-warning');
  862. $("#boot-button").html('Reboot');
  863. $("#boot-form").attr('action', '/reboot_ota.json');
  864. } else {
  865. recovery = false;
  866. $("#reboot_ota_nav").hide();
  867. $("#reboot_nav").show();
  868. $("#otadiv").hide();
  869. $('#uploaddiv').hide();
  870. $("footer.footer").removeClass('bg-warning');
  871. $('#mainnav').removeClass('bg-warning');
  872. $('[name=secnav]').removeClass('bg-warning');
  873. $("footer.footer").addClass('sl');
  874. $("#boot-button").html('Recovery');
  875. $("#boot-form").attr('action', '/recovery.json');
  876. }
  877. }
  878. }
  879. function handleWifiStatus(data){
  880. if (data.hasOwnProperty('ssid') && data['ssid'] != "") {
  881. if (data["ssid"] === selectedSSID) {
  882. //that's a connection attempt
  883. if (data["urc"] === 0) {
  884. //got connection
  885. $("#connected-to span").text(data["ssid"]);
  886. $("#connect-details h1").text(data["ssid"]);
  887. $("#ip").text(data["ip"]);
  888. $("#netmask").text(data["netmask"]);
  889. $("#gw").text(data["gw"]);
  890. $("#wifi-status").slideDown("fast", function() {});
  891. $("span#foot-wifi").html(", SSID: <strong>" + data["ssid"] + "</strong>, IP: <strong>" + data["ip"] + "</strong>");
  892. //unlock the wait screen if needed
  893. $("#ok-connect").prop("disabled", false);
  894. //update wait screen
  895. $("#loading").hide();
  896. $("#connect-success").text("Your IP address now is: " + data["ip"]);
  897. $("#connect-success").show();
  898. $("#connect-fail").hide();
  899. enableAPTimer = false;
  900. } else if (data["urc"] === 1) {
  901. //failed attempt
  902. $("#connected-to span").text('');
  903. $("#connect-details h1").text('');
  904. $("#ip").text('0.0.0.0');
  905. $("#netmask").text('0.0.0.0');
  906. $("#gw").text('0.0.0.0');
  907. $("span#foot-wifi").html("");
  908. //don't show any connection
  909. $("#wifi-status").slideUp("fast", function() {});
  910. //unlock the wait screen
  911. $("#ok-connect").prop("disabled", false);
  912. //update wait screen
  913. $("#loading").hide();
  914. $("#connect-fail").show();
  915. $("#connect-success").hide();
  916. enableAPTimer = true;
  917. enableStatusTimer = true;
  918. }
  919. } else if (data.hasOwnProperty('urc') && data['urc'] === 0) {
  920. //ESP32 is already connected to a wifi without having the user do anything
  921. if (!($("#wifi-status").is(":visible"))) {
  922. $("#connected-to span").text(data["ssid"]);
  923. $("#connect-details h1").text(data["ssid"]);
  924. $("#ip").text(data["ip"]);
  925. $("#netmask").text(data["netmask"]);
  926. $("#gw").text(data["gw"]);
  927. $("#wifi-status").slideDown("fast", function() {});
  928. $("span#foot-wifi").html(", SSID: <strong>" + data["ssid"] + "</strong>, IP: <strong>" + data["ip"] + "</strong>");
  929. }
  930. enableAPTimer = false;
  931. }
  932. } else if (data.hasOwnProperty('urc') && data['urc'] === 2) {
  933. //that's a manual disconnect
  934. if ($("#wifi-status").is(":visible")) {
  935. $("#wifi-status").slideUp("fast", function() {});
  936. $("span#foot-wifi").html("");
  937. }
  938. enableAPTimer = true;
  939. enableStatusTimer = true;
  940. }
  941. }
  942. function checkStatus() {
  943. RepeatCheckStatusInterval();
  944. if (!enableStatusTimer) return;
  945. if (blockAjax) return;
  946. blockAjax = true;
  947. getMessages();
  948. $.getJSON("/status.json", function(data) {
  949. handleRecoveryMode(data);
  950. handleWifiStatus(data);
  951. if (data.hasOwnProperty('project_name') && data['project_name'] != '') {
  952. pname = data['project_name'];
  953. }
  954. if (data.hasOwnProperty('version') && data['version'] != '') {
  955. ver = data['version'];
  956. $("span#foot-fw").html("fw: <strong>" + ver + "</strong>, mode: <strong>" + pname + "</strong>");
  957. } else {
  958. $("span#flash-status").html('');
  959. }
  960. if (data.hasOwnProperty('Voltage')) {
  961. var voltage = data['Voltage'];
  962. var layer;
  963. /* Assuming Li-ion 18650s as a power source, 3.9V per cell, or above is treated
  964. as full charge (>75% of capacity). 3.4V is empty. The gauge is loosely
  965. following the graph here:
  966. https://learn.adafruit.com/li-ion-and-lipoly-batteries/voltages
  967. using the 0.2C discharge profile for the rest of the values.
  968. */
  969. if (voltage > 0) {
  970. if (inRange(voltage, 5.8, 6.8) || inRange(voltage, 8.8, 10.2)) {
  971. layer = bat0;
  972. } else if (inRange(voltage, 6.8, 7.4) || inRange(voltage, 10.2, 11.1)) {
  973. layer = bat1;
  974. } else if (inRange(voltage, 7.4, 7.5) || inRange(voltage, 11.1, 11.25)) {
  975. layer = bat2;
  976. } else if (inRange(voltage, 7.5, 7.8) || inRange(voltage, 11.25, 11.7)) {
  977. layer = bat3;
  978. } else {
  979. layer = bat4;
  980. }
  981. layer.setAttribute("display","inline");
  982. }
  983. }
  984. if (data.hasOwnProperty('Jack')) {
  985. var jack = data['Jack'];
  986. if (jack == '1') {
  987. o_jack.setAttribute("display", "inline");
  988. }
  989. }
  990. blockAjax = false;
  991. })
  992. .fail(function(xhr, ajaxOptions, thrownError) {
  993. handleExceptionResponse(xhr, ajaxOptions, thrownError);
  994. blockAjax = false;
  995. });
  996. }
  997. function runCommand(button, reboot) {
  998. cmdstring = button.attributes.cmdname.value;
  999. showCmdMessage(button.attributes.cmdname.value,'MESSAGING_INFO',"Executing.",false);
  1000. fields = document.getElementById("flds-" + cmdstring);
  1001. cmdstring += ' ';
  1002. if (fields) {
  1003. allfields = fields.querySelectorAll("select,input");
  1004. for (i = 0; i < allfields.length; i++) {
  1005. attr = allfields[i].attributes;
  1006. qts = '';
  1007. opt = '';
  1008. isSelect = allfields[i].attributes?.class?.value == "custom-select";
  1009. if ((isSelect && allfields[i].selectedIndex != 0) || !isSelect) {
  1010. if (attr.longopts.value !== "undefined") {
  1011. opt += '--' + attr.longopts.value;
  1012. } else if (attr.shortopts.value !== "undefined") {
  1013. opt = '-' + attr.shortopts.value;
  1014. }
  1015. if (attr.hasvalue.value == "true") {
  1016. if (allfields[i].value != '') {
  1017. qts = (/\s/.test(allfields[i].value)) ? '"' : '';
  1018. cmdstring += opt + ' '+ qts + allfields[i].value + qts + ' ';
  1019. }
  1020. } else {
  1021. // this is a checkbox
  1022. if (allfields[i].checked) cmdstring += opt + ' ';
  1023. }
  1024. }
  1025. }
  1026. }
  1027. console.log(cmdstring);
  1028. var data = {
  1029. 'timestamp': Date.now()
  1030. };
  1031. data['command'] = cmdstring;
  1032. $.ajax({
  1033. url: '/commands.json',
  1034. dataType: 'text',
  1035. method: 'POST',
  1036. cache: false,
  1037. contentType: 'application/json; charset=utf-8',
  1038. data: JSON.stringify(data),
  1039. error: handleExceptionResponse,
  1040. complete: function(response) {
  1041. //var returnedResponse = JSON.parse(response.responseText);
  1042. console.log(response.responseText);
  1043. if (response.responseText && JSON.parse(response.responseText).Result == "Success" && reboot) {
  1044. delay_reboot(2500,button.attributes.cmdname.value);
  1045. }
  1046. }
  1047. });
  1048. enableStatusTimer = true;
  1049. }
  1050. function getCommands() {
  1051. $.getJSON("/commands.json", function(data) {
  1052. console.log(data);
  1053. data.commands.forEach(function(command) {
  1054. if ($("#flds-" + command.name).length == 0) {
  1055. cmd_parts = command.name.split('-');
  1056. isConfig = cmd_parts[0]=='cfg';
  1057. targetDiv= '#tab-' + cmd_parts[0]+'-'+cmd_parts[1];
  1058. innerhtml = '';
  1059. //innerhtml+='<tr class="table-light"><td>'+(isConfig?'<h1>':'');
  1060. innerhtml += '<div class="card text-white bg-primary mb-3"><div class="card-header">' + escapeHTML(command.help).replace(/\n/g, '<br />') + '</div><div class="card-body">';
  1061. innerhtml += '<fieldset id="flds-' + command.name + '">';
  1062. if (command.hasOwnProperty("argtable")) {
  1063. command.argtable.forEach(function(arg) {
  1064. placeholder = arg?.datatype || '';
  1065. ctrlname = command.name + '-' + arg.longopts;
  1066. curvalue = data.values?. [command.name]?. [arg.longopts];
  1067. var attributes = 'hasvalue=' + arg.hasvalue + ' ';
  1068. //attributes +='datatype="'+arg.datatype+'" ';
  1069. attributes += 'longopts="' + arg.longopts + '" ';
  1070. attributes += 'shortopts="' + arg.shortopts + '" ';
  1071. attributes += 'checkbox=' + arg.checkbox + ' ';
  1072. attributes += 'cmdname="' + command.name + '" ';
  1073. attributes += 'id="' + ctrlname + '" name="' + ctrlname + '" hasvalue="' + arg.hasvalue + '" ';
  1074. extraclass = ((arg.mincount>0)?'bg-success':'');
  1075. if(arg.glossary == 'hidden'){
  1076. attributes += ' style="visibility: hidden;"';
  1077. }
  1078. if (arg.checkbox) {
  1079. innerhtml += '<div class="form-check"><label class="form-check-label">';
  1080. innerhtml += '<input type="checkbox" ' + attributes + ' class="form-check-input '+extraclass+'" value="" >' + arg.glossary.encodeHTML() + '<small class="form-text text-muted">Previous value: ' + (curvalue?"Checked":"Unchecked") + '</small></label>';
  1081. } else {
  1082. innerhtml += '<div class="form-group" ><label for="' + ctrlname + '">' + arg.glossary.encodeHTML() + '</label>';
  1083. if (placeholder.includes('|')) {
  1084. placeholder = placeholder.replace('<', '').replace('>', '');
  1085. innerhtml += '<select ' + attributes + ' class="form-control '+extraclass+'"';
  1086. placeholder = '--|' + placeholder;
  1087. placeholder.split('|').forEach(function(choice) {
  1088. innerhtml += '<option >' + choice + '</option>';
  1089. });
  1090. innerhtml += '</select>';
  1091. } else {
  1092. innerhtml += '<input type="text" class="form-control '+extraclass+'" placeholder="' + placeholder + '" ' + attributes + '>';
  1093. }
  1094. innerhtml += '<small class="form-text text-muted">Previous value: ' + (curvalue || '') + '</small>';
  1095. }
  1096. innerhtml += '</div>';
  1097. });
  1098. }
  1099. innerhtml +='<div style="margin-top: 16px;">';
  1100. innerhtml += '<div class="toast show" role="alert" aria-live="assertive" aria-atomic="true" style="display: none;" id="toast_'+command.name+'">';
  1101. innerhtml += '<div class="toast-header"><strong class="mr-auto">Result</strong><button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close" onclick="$(this).parent().parent().hide()">';
  1102. innerhtml += '<span aria-hidden="true">×</span></button></div><div class="toast-body" id="msg_'+command.name+'"></div></div>'
  1103. if (isConfig) {
  1104. innerhtml += '<button type="submit" class="btn btn-info" id="btn-save-' + command.name + '" cmdname="' + command.name + '" onclick="runCommand(this,false)">Save</button>';
  1105. innerhtml += '<button type="submit" class="btn btn-warning" id="btn-commit-' + command.name + '" cmdname="' + command.name + '" onclick="runCommand(this,true)">Apply</button>';
  1106. } else {
  1107. innerhtml += '<button type="submit" class="btn btn-success" id="btn-run-' + command.name + '" cmdname="' + command.name + '" onclick="runCommand(this,false)">Execute</button>';
  1108. }
  1109. innerhtml+= '</div></fieldset></div></div>';
  1110. if (isConfig) {
  1111. $(targetDiv).append(innerhtml);
  1112. } else {
  1113. $("#commands-list").append(innerhtml);
  1114. }
  1115. }
  1116. });
  1117. data.commands.forEach(function(command) {
  1118. $('[cmdname='+command.name+']:input').val('');
  1119. $('[cmdname='+command.name+']:checkbox').prop('checked',false);
  1120. if (command.hasOwnProperty("argtable")) {
  1121. command.argtable.forEach(function(arg) {
  1122. ctrlselector = '#' + command.name + '-' + arg.longopts;
  1123. ctrlValue = data.values?.[command.name]?.[arg.longopts];
  1124. if (arg.checkbox) {
  1125. $(ctrlselector)[0].checked = ctrlValue;
  1126. } else {
  1127. if(ctrlValue!=undefined) $(ctrlselector).val( ctrlValue ).change();
  1128. if ($(ctrlselector)[0].value.length == 0 && (arg?.datatype || '').includes('|')) {
  1129. $(ctrlselector)[0].value = '--';
  1130. }
  1131. }
  1132. });
  1133. }
  1134. });
  1135. })
  1136. .fail(function(xhr, ajaxOptions, thrownError) {
  1137. handleExceptionResponse(xhr, ajaxOptions, thrownError);
  1138. $("#commands-list").empty();
  1139. blockAjax = false;
  1140. });
  1141. }
  1142. function getConfig() {
  1143. $.getJSON("/config.json", function(entries) {
  1144. $("#nvsTable tr").remove();
  1145. data = entries.hasOwnProperty('config') ? entries.config : entries;
  1146. Object.keys(data).sort().forEach(function(key, i) {
  1147. if (data.hasOwnProperty(key)) {
  1148. val = data[key].value;
  1149. if (key == 'autoexec') {
  1150. if (data["autoexec"].value === "0") {
  1151. $("#disable-squeezelite")[0].checked = true;
  1152. } else {
  1153. $("#disable-squeezelite")[0].checked = false;
  1154. }
  1155. } else if (key == 'autoexec1') {
  1156. var re = /-o\s?(["][^"]*["]|[^-]+)/g;
  1157. var m = re.exec(val);
  1158. if (m[1].toUpperCase().startsWith('I2S')) {
  1159. handleTemplateTypeRadio('i2s');
  1160. } else if (m[1].toUpperCase().startsWith('SPDIF')) {
  1161. handleTemplateTypeRadio('spdif');
  1162. } else if (m[1].toUpperCase().startsWith('"BT')) {
  1163. handleTemplateTypeRadio('bt');
  1164. var re2=/["]BT\s*-n\s*'([^"]+)'/g;
  1165. var m2=re2.exec(m[1]);
  1166. if(m2.length>=2){
  1167. $("#btsinkdiv").val(m2[1]);
  1168. }
  1169. }
  1170. } else if (key == 'host_name') {
  1171. val = val.replaceAll('"', '');
  1172. $("input#dhcp-name1").val(val);
  1173. $("input#dhcp-name2").val(val);
  1174. $("#player").val(val);
  1175. }
  1176. $("tbody#nvsTable").append(
  1177. "<tr>" +
  1178. "<td>" + key + "</td>" +
  1179. "<td class='value'>" +
  1180. "<input type='text' class='form-control nvs' id='" + key + "' nvs_type=" + data[key].type + " >" +
  1181. "</td>" +
  1182. "</tr>"
  1183. );
  1184. $("input#" + key).val(data[key].value);
  1185. }
  1186. });
  1187. $("tbody#nvsTable").append("<tr><td><input type='text' class='form-control' id='nvs-new-key' placeholder='new key'></td><td><input type='text' class='form-control' id='nvs-new-value' placeholder='new value' nvs_type=33 ></td></tr>");
  1188. if (entries.hasOwnProperty('gpio')) {
  1189. $("tbody#gpiotable tr").remove();
  1190. entries.gpio.forEach(function(gpio_entry) {
  1191. cl = gpio_entry.fixed ? "table-secondary" : "table-primary";
  1192. $("tbody#gpiotable").append('<tr class=' + cl + '><th scope="row">' + gpio_entry.group + '</th><td>' + gpio_entry.name + '</td><td>' + gpio_entry.gpio + '</td><td>' + (gpio_entry.fixed ? 'Fixed':'Configuration') + '</td></tr>');
  1193. });
  1194. }
  1195. })
  1196. .fail(function(xhr, ajaxOptions, thrownError) {
  1197. handleExceptionResponse(xhr, ajaxOptions, thrownError);
  1198. blockAjax = false;
  1199. });
  1200. }
  1201. function showLocalMessage(message,severity, age = 0){
  1202. msg={
  1203. 'type':'Local',
  1204. 'message':message,
  1205. 'type' : severity
  1206. }
  1207. showMessage(msg,severity,age);
  1208. }
  1209. function showMessage(msg, msg_time,age = 0) {
  1210. color='table-success';
  1211. if (msg['type'] == 'MESSAGING_WARNING') {
  1212. color='table-warning';
  1213. if(messageseverity=='MESSAGING_INFO'){
  1214. messageseverity = 'MESSAGING_WARNING';
  1215. }
  1216. } else if (msg['type'] == 'MESSAGING_ERROR') {
  1217. if(messageseverity=='MESSAGING_INFO' || messageseverity=='MESSAGING_WARNING'){
  1218. messageseverity = 'MESSAGING_ERROR';
  1219. }
  1220. color ='table-danger';
  1221. }
  1222. if(++messagecount>0){
  1223. $('#msgcnt').removeClass('badge-success');
  1224. $('#msgcnt').removeClass('badge-warning');
  1225. $('#msgcnt').removeClass('badge-danger');
  1226. $('#msgcnt').addClass(pillcolors[messageseverity]);
  1227. $('#msgcnt').text(messagecount);
  1228. }
  1229. $("#syslogTable").append(
  1230. "<tr class='" + color + "'>" +
  1231. "<td>" + msg_time.toLocaleString() + "</td>" +
  1232. "<td>" + escapeHTML(msg["message"]).replace(/\n/g, '<br />') + "</td>" +
  1233. "</tr>"
  1234. );
  1235. }
  1236. function inRange(x, min, max) {
  1237. return ((x - min) * (x - max) <= 0);
  1238. }
  1239. function sleep(ms) {
  1240. return new Promise(resolve => setTimeout(resolve, ms));
  1241. }