2
0

code.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. //TODO daduke
  229. // RepeatRefreshAPInterval();
  230. }
  231. function refreshAPHTML(data){
  232. var h = "";
  233. data.forEach(function(e, idx, array) {
  234. 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);
  235. h += "\n";
  236. });
  237. $( "#wifi-list" ).html(h)
  238. }
  239. function checkStatus(){
  240. $.getJSON( "/status.json", function( data ) {
  241. if(data.hasOwnProperty('ssid') && data['ssid'] != ""){
  242. if(data["ssid"] === selectedSSID){
  243. //that's a connection attempt
  244. if(data["urc"] === 0){
  245. //got connection
  246. $("#connected-to span").text(data["ssid"]);
  247. $("#connect-details h1").text(data["ssid"]);
  248. $("#ip").text(data["ip"]);
  249. $("#netmask").text(data["netmask"]);
  250. $("#gw").text(data["gw"]);
  251. $("#wifi-status").slideDown( "fast", function() {});
  252. //unlock the wait screen if needed
  253. $( "#ok-connect" ).prop("disabled",false);
  254. //update wait screen
  255. $( "#loading" ).hide();
  256. $( "#connect-success" ).append("<p>Your IP address now is: " + text(data["ip"]) + "</p>");
  257. $( "#connect-success" ).show();
  258. $( "#connect-fail" ).hide();
  259. }
  260. else if(data["urc"] === 1){
  261. //failed attempt
  262. $("#connected-to span").text('');
  263. $("#connect-details h1").text('');
  264. $("#ip").text('0.0.0.0');
  265. $("#netmask").text('0.0.0.0');
  266. $("#gw").text('0.0.0.0');
  267. //don't show any connection
  268. $("#wifi-status").slideUp( "fast", function() {});
  269. //unlock the wait screen
  270. $( "#ok-connect" ).prop("disabled",false);
  271. //update wait screen
  272. $( "#loading" ).hide();
  273. $( "#connect-fail" ).show();
  274. $( "#connect-success" ).hide();
  275. }
  276. }
  277. else if(data.hasOwnProperty('urc') && data['urc'] === 0){
  278. //ESP32 is already connected to a wifi without having the user do anything
  279. if( !($("#wifi-status").is(":visible")) ){
  280. $("#connected-to span").text(data["ssid"]);
  281. $("#connect-details h1").text(data["ssid"]);
  282. $("#ip").text(data["ip"]);
  283. $("#netmask").text(data["netmask"]);
  284. $("#gw").text(data["gw"]);
  285. $("#wifi-status").slideDown( "fast", function() {});
  286. }
  287. //TODO daduke
  288. console.log("stopping timers..");
  289. stopCheckStatusInterval();
  290. stopRefreshAPInterval
  291. }
  292. }
  293. else if(data.hasOwnProperty('urc') && data['urc'] === 2){
  294. //that's a manual disconnect
  295. if($("#wifi-status").is(":visible")){
  296. $("#wifi-status").slideUp( "fast", function() {});
  297. }
  298. }
  299. })
  300. .fail(function() {
  301. //don't do anything, the server might be down while esp32 recalibrates radio
  302. });
  303. RepeatCheckStatusInterval();
  304. }
  305. function getConfig() {
  306. $.getJSON("/config.json", function(data) {
  307. if (data.hasOwnProperty('autoexec')) {
  308. if (data["autoexec"] === 1) {
  309. console.log('turn on autoexec');
  310. $("#autoexec-cb")[0].checked=true;
  311. } else {
  312. console.log('turn off autoexec');
  313. $("#autoexec-cb")[0].checked=false;
  314. $("#autoexec-command").hide(200);
  315. }
  316. }
  317. if (data.hasOwnProperty('list')) {
  318. data.list.forEach(function(line) {
  319. let key = Object.keys(line)[0];
  320. let val = Object.values(line)[0];
  321. console.log(key, val);
  322. if (key == 'autoexec1') {
  323. $("#autoexec1").val(val);
  324. }
  325. });
  326. }
  327. })
  328. .fail(function() {
  329. console.log("failed to fetch config!");
  330. });
  331. }
  332. function updateAutoexec(){
  333. autoexec = ($("#autoexec-cb")[0].checked)?1:0;
  334. autoexec1 = $("#autoexec1").val();
  335. $.ajax({
  336. url: '/config.json',
  337. dataType: 'json',
  338. method: 'POST',
  339. cache: false,
  340. headers: { "X-Custom-autoexec": autoexec, "X-Custom-autoexec1": autoexec1 },
  341. data: { 'timestamp': Date.now() }
  342. });
  343. console.log('sent config JSON with headers:', autoexec, autoexec1);
  344. }
  345. function performFactory(){
  346. // $( "#ok-connect" ).prop("disabled",true);
  347. // $( "#ssid-wait" ).text(selectedSSID);
  348. // $( "#connect" ).slideUp( "fast", function() {});
  349. // $( "#connect_manual" ).slideUp( "fast", function() {});
  350. // $( "#connect-wait" ).slideDown( "fast", function() {});
  351. // // todo: should we update the UI here?
  352. $.ajax({
  353. url: '/factory.json',
  354. dataType: 'json',
  355. method: 'POST',
  356. cache: false,
  357. data: { 'timestamp': Date.now()}
  358. });
  359. }
  360. var output = '';
  361. function selectOutput(el) {
  362. if ($(el).attr('id') == 'bt') {
  363. $("#btsinkdiv").show(200);
  364. output = 'bt';
  365. } else if ($(el).attr('id') == 'spdif') {
  366. $("#btsinkdiv").hide(200);
  367. output = 'spdif';
  368. } else {
  369. $("#btsinkdiv").hide(200);
  370. output = 'i2s';
  371. }
  372. }
  373. function generateCommand() {
  374. var commandLine = commandHeader + '-n ' + $("#player").val();
  375. if (output == 'bt') {
  376. commandLine += ' -o "BT -n \'' + $("#btsink").val() + '\'" -R -Z 192000';
  377. } else if (output == 'spdif') {
  378. commandLine += ' -o SPDIF -R -Z 192000';
  379. } else {
  380. commandLine += ' -o I2S';
  381. }
  382. if ($("#optional").val() != '') {
  383. commandLine += ' ' + $("#optional").val();
  384. }
  385. $("#autoexec1").val(commandLine);
  386. }