code.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. var recovery = 0;
  2. var commandHeader = 'squeezelite -b 500:2000 -d all=info ';
  3. // First, checks if it isn't implemented yet.
  4. if (!String.prototype.format) {
  5. String.prototype.format = function() {
  6. var args = arguments;
  7. return this.replace(/{(\d+)}/g, function(match, number) {
  8. return typeof args[number] != 'undefined'
  9. ? args[number]
  10. : match
  11. ;
  12. });
  13. };
  14. }
  15. var apList = null;
  16. var selectedSSID = "";
  17. var refreshAPInterval = null;
  18. var checkStatusInterval = null;
  19. var StatusIntervalActive = false;
  20. var ConfigIntervalActive = false;
  21. var RefreshAPIIntervalActive = false;
  22. function stopCheckStatusInterval(){
  23. if(checkStatusInterval != null){
  24. clearTimeout(checkStatusInterval);
  25. checkStatusInterval = null;
  26. }
  27. StatusIntervalActive = false;
  28. }
  29. function stopRefreshAPInterval(){
  30. if(refreshAPInterval != null){
  31. clearTimeout(refreshAPInterval);
  32. refreshAPInterval = null;
  33. }
  34. RefreshAPIIntervalActive = false;
  35. }
  36. function startCheckStatusInterval(){
  37. StatusIntervalActive = true;
  38. checkStatusInterval = setTimeout(checkStatus, 950);
  39. }
  40. function startRefreshAPInterval(){
  41. RefreshAPIIntervalActive = true;
  42. refreshAPInterval = setTimeout(refreshAP, 2800);
  43. }
  44. function RepeatCheckStatusInterval(){
  45. if(StatusIntervalActive)
  46. startCheckStatusInterval();
  47. }
  48. function RepeatCheckConfigInterval(){
  49. if(ConfigIntervalActive)
  50. startCheckConfigInterval();
  51. }
  52. function RepeatRefreshAPInterval(){
  53. if(RefreshAPIIntervalActive)
  54. startRefreshAPInterval()
  55. }
  56. $(document).ready(function(){
  57. $("#wifi-status").on("click", ".ape", function() {
  58. $( "#wifi" ).slideUp( "fast", function() {});
  59. $( "#connect-details" ).slideDown( "fast", function() {});
  60. });
  61. $("#manual_add").on("click", ".ape", function() {
  62. selectedSSID = $(this).text();
  63. $( "#ssid-pwd" ).text(selectedSSID);
  64. $( "#wifi" ).slideUp( "fast", function() {});
  65. $( "#connect_manual" ).slideDown( "fast", function() {});
  66. $( "#connect" ).slideUp( "fast", function() {});
  67. //update wait screen
  68. $( "#loading" ).show();
  69. $( "#connect-success" ).hide();
  70. $( "#connect-fail" ).hide();
  71. });
  72. $("#wifi-list").on("click", ".ape", function() {
  73. selectedSSID = $(this).text();
  74. $( "#ssid-pwd" ).text(selectedSSID);
  75. $( "#wifi" ).slideUp( "fast", function() {});
  76. $( "#connect_manual" ).slideUp( "fast", function() {});
  77. $( "#connect" ).slideDown( "fast", function() {});
  78. //update wait screen
  79. $( "#loading" ).show();
  80. $( "#connect-success" ).hide();
  81. $( "#connect-fail" ).hide();
  82. });
  83. $("#cancel").on("click", function() {
  84. selectedSSID = "";
  85. $( "#connect" ).slideUp( "fast", function() {});
  86. $( "#connect_manual" ).slideUp( "fast", function() {});
  87. $( "#wifi" ).slideDown( "fast", function() {});
  88. });
  89. $("#manual_cancel").on("click", function() {
  90. selectedSSID = "";
  91. $( "#connect" ).slideUp( "fast", function() {});
  92. $( "#connect_manual" ).slideUp( "fast", function() {});
  93. $( "#wifi" ).slideDown( "fast", function() {});
  94. });
  95. $("#join").on("click", function() {
  96. performConnect();
  97. });
  98. $("#manual_join").on("click", function() {
  99. performConnect($(this).data('connect'));
  100. });
  101. $("#ok-details").on("click", function() {
  102. $( "#connect-details" ).slideUp( "fast", function() {});
  103. $( "#wifi" ).slideDown( "fast", function() {});
  104. });
  105. $("#ok-credits").on("click", function() {
  106. $( "#credits" ).slideUp( "fast", function() {});
  107. $( "#app" ).slideDown( "fast", function() {});
  108. });
  109. $("#acredits").on("click", function(event) {
  110. event.preventDefault();
  111. $( "#app" ).slideUp( "fast", function() {});
  112. $( "#credits" ).slideDown( "fast", function() {});
  113. });
  114. $("#ok-connect").on("click", function() {
  115. $( "#connect-wait" ).slideUp( "fast", function() {});
  116. $( "#wifi" ).slideDown( "fast", function() {});
  117. });
  118. $("#disconnect").on("click", function() {
  119. $( "#connect-details-wrap" ).addClass('blur');
  120. $( "#diag-disconnect" ).slideDown( "fast", function() {});
  121. });
  122. $("#no-disconnect").on("click", function() {
  123. $( "#diag-disconnect" ).slideUp( "fast", function() {});
  124. $( "#connect-details-wrap" ).removeClass('blur');
  125. });
  126. $("#yes-disconnect").on("click", function() {
  127. stopCheckStatusInterval();
  128. selectedSSID = "";
  129. $( "#diag-disconnect" ).slideUp( "fast", function() {});
  130. $( "#connect-details-wrap" ).removeClass('blur');
  131. $.ajax({
  132. url: '/connect.json',
  133. dataType: 'json',
  134. method: 'DELETE',
  135. cache: false,
  136. data: { 'timestamp': Date.now()}
  137. });
  138. startCheckStatusInterval();
  139. $( "#connect-details" ).slideUp( "fast", function() {});
  140. $( "#wifi" ).slideDown( "fast", function() {})
  141. });
  142. $("#update-command").click(function() {
  143. updateAutoexec();
  144. });
  145. $("#generate-command").click(function() {
  146. generateCommand();
  147. });
  148. $('[name=audio]').click(function(){
  149. selectOutput(this);
  150. });
  151. if (recovery) {
  152. $("#recoverydiv").hide();
  153. $("#otadiv").show();
  154. $("#command_line").hide();
  155. $("#wifi").hide();
  156. } else {
  157. $("#recoverydiv").show();
  158. $("#otadiv").hide();
  159. }
  160. //first time the page loads: attempt get the connection status and start the wifi scan
  161. refreshAP();
  162. startCheckStatusInterval();
  163. startRefreshAPInterval();
  164. getConfig();
  165. });
  166. function performConnect(conntype){
  167. //stop the status refresh. This prevents a race condition where a status
  168. //request would be refreshed with wrong ip info from a previous connection
  169. //and the request would automatically shows as succesful.
  170. stopCheckStatusInterval();
  171. //stop refreshing wifi list
  172. stopRefreshAPInterval();
  173. var pwd;
  174. if (conntype == 'manual') {
  175. //Grab the manual SSID and PWD
  176. selectedSSID=$('#manual_ssid').val();
  177. pwd = $("#manual_pwd").val();
  178. }else{
  179. pwd = $("#pwd").val();
  180. }
  181. //reset connection
  182. $( "#loading" ).show();
  183. $( "#connect-success" ).hide();
  184. $( "#connect-fail" ).hide();
  185. $( "#ok-connect" ).prop("disabled",true);
  186. $( "#ssid-wait" ).text(selectedSSID);
  187. $( "#connect" ).slideUp( "fast", function() {});
  188. $( "#connect_manual" ).slideUp( "fast", function() {});
  189. $( "#connect-wait" ).slideDown( "fast", function() {});
  190. $.ajax({
  191. url: '/connect.json',
  192. dataType: 'json',
  193. method: 'POST',
  194. cache: false,
  195. headers: { 'X-Custom-ssid': selectedSSID, 'X-Custom-pwd': pwd },
  196. data: { 'timestamp': Date.now()}
  197. });
  198. //now we can re-set the intervals regardless of result
  199. startCheckStatusInterval();
  200. startRefreshAPInterval();
  201. }
  202. function rssiToIcon(rssi){
  203. if(rssi >= -60){
  204. return 'w0';
  205. }
  206. else if(rssi >= -67){
  207. return 'w1';
  208. }
  209. else if(rssi >= -75){
  210. return 'w2';
  211. }
  212. else{
  213. return 'w3';
  214. }
  215. }
  216. function refreshAP(){
  217. $.getJSON( "/ap.json", function( data ) {
  218. if(data.length > 0){
  219. //sort by signal strength
  220. data.sort(function (a, b) {
  221. var x = a["rssi"]; var y = b["rssi"];
  222. return ((x < y) ? 1 : ((x > y) ? -1 : 0));
  223. });
  224. apList = data;
  225. refreshAPHTML(apList);
  226. }
  227. });
  228. RepeatRefreshAPInterval();
  229. }
  230. function refreshAPHTML(data){
  231. var h = "";
  232. data.forEach(function(e, idx, array) {
  233. 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);
  234. h += "\n";
  235. });
  236. $( "#wifi-list" ).html(h)
  237. }
  238. function checkStatus(){
  239. $.getJSON( "/status.json", function( data ) {
  240. if(data.hasOwnProperty('autoexec1') && data['autoexec1'] != ""){
  241. $("#autoexec1_current").text(data["autoexec1"]);
  242. }
  243. if(data.hasOwnProperty('ssid') && data['ssid'] != ""){
  244. if(data["ssid"] === selectedSSID){
  245. //that's a connection attempt
  246. if(data["urc"] === 0){
  247. //got connection
  248. $("#connected-to span").text(data["ssid"]);
  249. $("#connect-details h1").text(data["ssid"]);
  250. $("#ip").text(data["ip"]);
  251. $("#netmask").text(data["netmask"]);
  252. $("#gw").text(data["gw"]);
  253. $("#wifi-status").slideDown( "fast", function() {});
  254. //unlock the wait screen if needed
  255. $( "#ok-connect" ).prop("disabled",false);
  256. //update wait screen
  257. $( "#loading" ).hide();
  258. $( "#connect-success" ).append("<p>Your IP address now is: " + text(data["ip"]) + "</p>");
  259. $( "#connect-success" ).show();
  260. $( "#connect-fail" ).hide();
  261. }
  262. else if(data["urc"] === 1){
  263. //failed attempt
  264. $("#connected-to span").text('');
  265. $("#connect-details h1").text('');
  266. $("#ip").text('0.0.0.0');
  267. $("#netmask").text('0.0.0.0');
  268. $("#gw").text('0.0.0.0');
  269. //don't show any connection
  270. $("#wifi-status").slideUp( "fast", function() {});
  271. //unlock the wait screen
  272. $( "#ok-connect" ).prop("disabled",false);
  273. //update wait screen
  274. $( "#loading" ).hide();
  275. $( "#connect-fail" ).show();
  276. $( "#connect-success" ).hide();
  277. }
  278. }
  279. else if(data.hasOwnProperty('urc') && data['urc'] === 0){
  280. //ESP32 is already connected to a wifi without having the user do anything
  281. if( !($("#wifi-status").is(":visible")) ){
  282. $("#connected-to span").text(data["ssid"]);
  283. $("#connect-details h1").text(data["ssid"]);
  284. $("#ip").text(data["ip"]);
  285. $("#netmask").text(data["netmask"]);
  286. $("#gw").text(data["gw"]);
  287. $("#wifi-status").slideDown( "fast", function() {});
  288. }
  289. }
  290. }
  291. else if(data.hasOwnProperty('urc') && data['urc'] === 2){
  292. //that's a manual disconnect
  293. if($("#wifi-status").is(":visible")){
  294. $("#wifi-status").slideUp( "fast", function() {});
  295. }
  296. }
  297. })
  298. .fail(function() {
  299. //don't do anything, the server might be down while esp32 recalibrates radio
  300. });
  301. RepeatCheckStatusInterval();
  302. }
  303. function getConfig() {
  304. $.getJSON("/config.json", function(data) {
  305. if (data.hasOwnProperty('autoexec')) {
  306. if (data["autoexec"] === 1) {
  307. console.log('turn on autoexec');
  308. $("#autoexec-cb")[0].checked=true;
  309. } else {
  310. console.log('turn off autoexec');
  311. $("#autoexec-cb")[0].checked=false;
  312. $("#autoexec-command").hide(200);
  313. }
  314. }
  315. if (data.hasOwnProperty('list')) {
  316. data.list.forEach(function(line) {
  317. let key = Object.keys(line)[0];
  318. let val = Object.values(line)[0];
  319. console.log(key, val);
  320. if (key == 'autoexec1') {
  321. $("#autoexec1").val(val);
  322. }
  323. });
  324. }
  325. })
  326. .fail(function() {
  327. console.log("failed to fetch config!");
  328. });
  329. }
  330. function updateAutoexec(){
  331. autoexec = ($("#autoexec-cb")[0].checked)?1:0;
  332. autoexec1 = $("#autoexec1").val();
  333. $.ajax({
  334. url: '/config.json',
  335. dataType: 'json',
  336. method: 'POST',
  337. cache: false,
  338. headers: { "X-Custom-autoexec": autoexec, "X-Custom-autoexec1": autoexec1 },
  339. data: { 'timestamp': Date.now() }
  340. });
  341. console.log('sent config JSON with headers:', autoexec, autoexec1);
  342. }
  343. function performFactory(){
  344. // $( "#ok-connect" ).prop("disabled",true);
  345. // $( "#ssid-wait" ).text(selectedSSID);
  346. // $( "#connect" ).slideUp( "fast", function() {});
  347. // $( "#connect_manual" ).slideUp( "fast", function() {});
  348. // $( "#connect-wait" ).slideDown( "fast", function() {});
  349. // // todo: should we update the UI here?
  350. $.ajax({
  351. url: '/factory.json',
  352. dataType: 'json',
  353. method: 'POST',
  354. cache: false,
  355. data: { 'timestamp': Date.now()}
  356. });
  357. }
  358. var output = '';
  359. function selectOutput(el) {
  360. if ($(el).attr('id') == 'bt') {
  361. $("#btsinkdiv").show(200);
  362. output = 'bt';
  363. } else if ($(el).attr('id') == 'spdif') {
  364. $("#btsinkdiv").hide(200);
  365. output = 'spdif';
  366. } else {
  367. $("#btsinkdiv").hide(200);
  368. output = 'i2s';
  369. }
  370. }
  371. function generateCommand() {
  372. var commandLine = commandHeader + '-n ' + $("#player").val();
  373. if (output == 'bt') {
  374. commandLine += ' -o "BT -n \'' + $("#btsink").val() + '\'" -R -Z 192000';
  375. } else if (output == 'spdif') {
  376. commandLine += ' -o SPDIF -R -Z 192000';
  377. } else {
  378. commandLine += ' -o I2S';
  379. }
  380. if ($("#optional").val() != '') {
  381. commandLine += ' ' + $("#optional").val();
  382. }
  383. $("#autoexec1").val(commandLine);
  384. }