code.js 11 KB

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