Browse Source

code cleanup/reorder, enable reboot buttons

Christian Herzog 5 years ago
parent
commit
fd235df8fb
3 changed files with 148 additions and 219 deletions
  1. 105 21
      components/wifi-manager/code.js
  2. 42 37
      components/wifi-manager/http_server.c
  3. 1 161
      components/wifi-manager/index.html

+ 105 - 21
components/wifi-manager/code.js

@@ -1,7 +1,3 @@
-var recovery = 0;
-var enableTimers = true;
-var commandHeader = 'squeezelite -b 500:2000 -d all=info ';
-
 // First, checks if it isn't implemented yet.
 if (!String.prototype.format) {
   String.prototype.format = function() {
@@ -15,6 +11,10 @@ if (!String.prototype.format) {
   };
 }
 
+var recovery = 0;
+var enableTimers = true;
+var commandHeader = 'squeezelite -b 500:2000 -d all=info ';
+
 var apList = null;
 var selectedSSID = "";
 var refreshAPInterval = null; 
@@ -24,6 +24,8 @@ var StatusIntervalActive = false;
 var ConfigIntervalActive = false;
 var RefreshAPIIntervalActive = false;
 
+//TODO check
+var to = 0, set_int = 0;
 
 function stopCheckStatusInterval(){
 	if(checkStatusInterval != null){
@@ -409,23 +411,6 @@ function updateAutoexec(){
     console.log('sent config JSON with headers:', autoexec, autoexec1);
 }
 
-function performFactory(){
-// 	$( "#ok-connect" ).prop("disabled",true);
-// 	$( "#ssid-wait" ).text(selectedSSID);
-// 	$( "#connect" ).slideUp( "fast", function() {});
-// 	$( "#connect_manual" ).slideUp( "fast", function() {});
-// 	$( "#connect-wait" ).slideDown( "fast", function() {});
-// 	// todo: should we update the UI here? 
-	
-	$.ajax({
-		url: '/factory.json',
-		dataType: 'json',
-		method: 'POST',
-		cache: false,
-		data: { 'timestamp': Date.now()}
-	});
-}
-
 var output = '';
 function selectOutput(el) {
     if ($(el).attr('id') == 'bt') {
@@ -455,3 +440,102 @@ function generateCommand() {
     }
     $("#autoexec1").val(commandLine);
 }
+
+function handleClick(item) {
+    console.log(item);
+    if (item.id == 'autoexec-cb') {
+        if (item.checked) {
+            $("#autoexec-command").show(200);
+        } else {
+            $("#autoexec-command").hide(200);
+        }
+    } else if (item.id == 'recovery') {
+        $.ajax({
+            url: '/recovery.json',
+            dataType: 'json',
+            method: 'POST',
+            cache: false,
+            data: { 'timestamp': Date.now()}
+        });
+    } else if (item.id == 'reboot') {
+        $.ajax({
+            url: '/reboot.json',
+            dataType: 'json',
+            method: 'POST',
+            cache: false,
+            data: { 'timestamp': Date.now()}
+        });
+    }
+}
+
+//TODO daduke check
+function file_change() {
+        document.getElementById('update').disabled = 0;
+}
+
+function do_upload(f) {
+	var xhr = new XMLHttpRequest();
+
+        document.getElementById('update').disabled = 1;
+	//ws.close();
+	document.getElementById("progr").class = "progr-ok";
+
+	xhr.upload.addEventListener("progress", function(e) {
+		document.getElementById("progr").value = parseInt(e.loaded / e.total * 100);
+
+		if (e.loaded == e.total) {
+		//	document.getElementById("realpage").style.display = "none";
+		//	document.getElementById("waiting").style.display = "block";
+		}
+	
+	}, false);
+
+	xhr.onreadystatechange = function(e) {
+		   console.log("rs" + xhr.readyState + " status " + xhr.status); 
+		if (xhr.readyState == 4) {
+			/* it completed, for good or for ill */
+		//	document.getElementById("realpage").style.display = "none";
+		//	document.getElementById("waiting").style.display = "block";
+			document.getElementById("progr").class = "progr-ok";
+			console.log("upload reached state 4: xhr status " + xhr.status);
+			setTimeout(function() { window.location.href = location.origin + "/"; }, 9000 );
+		}
+	};
+
+	/* kill the heart timer */
+	clearInterval(set_int);
+
+	xhr.open("POST", f.action, true);
+	xhr.send(new FormData(f));
+
+	return false;
+}
+
+function heart_timer() {
+	var s;
+	
+	s = Math.round((95 * to) / (40 * 10)) / 100;
+	
+	if (s < 0) {
+		clearInterval(set_int);
+		set_int = 0;
+		
+		ws.close();
+		
+		document.getElementById("realpage").style.opacity = "0.3";
+	}
+		
+	
+	to--;
+	document.getElementById("heart").style.opacity = s;
+}
+
+
+function heartbeat()
+{
+	to = 40 * 10;
+	if (!set_int) {
+		set_int = setInterval(heart_timer, 100);
+	}
+		
+}

+ 42 - 37
components/wifi-manager/http_server.c

@@ -164,19 +164,26 @@ void http_server_netconn_serve(struct netconn *conn) {
 
 			}
 			else{
+                //static stuff
 				/* default page */
 				if(strstr(line, "GET / ")) {
 					netconn_write(conn, http_html_hdr, sizeof(http_html_hdr) - 1, NETCONN_NOCOPY);
 					netconn_write(conn, index_html_start, index_html_end - index_html_start, NETCONN_NOCOPY);
 				}
+				else if(strstr(line, "GET /code.js ")) {
+					netconn_write(conn, http_js_hdr, sizeof(http_js_hdr) - 1, NETCONN_NOCOPY);
+					netconn_write(conn, code_js_start, code_js_end - code_js_start, NETCONN_NOCOPY);
+				}
 				else if(strstr(line, "GET /jquery.js ")) {
 					netconn_write(conn, http_jquery_gz_hdr, sizeof(http_jquery_gz_hdr) - 1, NETCONN_NOCOPY);
 					netconn_write(conn, jquery_gz_start, jquery_gz_end - jquery_gz_start, NETCONN_NOCOPY);
 				}
-				else if(strstr(line, "GET /code.js ")) {
-					netconn_write(conn, http_js_hdr, sizeof(http_js_hdr) - 1, NETCONN_NOCOPY);
-					netconn_write(conn, code_js_start, code_js_end - code_js_start, NETCONN_NOCOPY);
+				else if(strstr(line, "GET /style.css ")) {
+					netconn_write(conn, http_css_hdr, sizeof(http_css_hdr) - 1, NETCONN_NOCOPY);
+					netconn_write(conn, style_css_start, style_css_end - style_css_start, NETCONN_NOCOPY);
 				}
+
+                //dynamic stuff
 				else if(strstr(line, "GET /ap.json ")) {
 					/* if we can get the mutex, write the last version of the AP list */
 					ESP_LOGI(TAG,"Processing ap.json request");
@@ -194,29 +201,6 @@ void http_server_netconn_serve(struct netconn *conn) {
 					ESP_LOGI(TAG,"Starting wifi scan");
 					wifi_manager_scan_async();
 				}
-				else if(strstr(line, "GET /style.css ")) {
-					netconn_write(conn, http_css_hdr, sizeof(http_css_hdr) - 1, NETCONN_NOCOPY);
-					netconn_write(conn, style_css_start, style_css_end - style_css_start, NETCONN_NOCOPY);
-				}
-				else if(strstr(line, "GET /status.json ")){
-					ESP_LOGI(TAG,"Serving status.json");
-					if(wifi_manager_lock_json_buffer(( TickType_t ) 10)){
-						char *buff = wifi_manager_get_ip_info_json();
-						if(buff){
-							netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY);
-							netconn_write(conn, buff, strlen(buff), NETCONN_NOCOPY);
-
-							wifi_manager_unlock_json_buffer();
-						}
-						else{
-							netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
-						}
-					}
-					else{
-						netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
-						ESP_LOGE(TAG, "http_server_netconn_serve: GET /status failed to obtain mutex");
-					}
-				}
 				else if(strstr(line, "GET /config.json ")){
 					ESP_LOGI(TAG,"Serving config.json");
 					char autoexec_name[21]={0};
@@ -271,9 +255,6 @@ void http_server_netconn_serve(struct netconn *conn) {
 
 					}
 				}
-				else if(strstr(line, "POST /factory.json ")){
-					guided_factory();
-				}
 				else if(strstr(line, "POST /config.json ")){
 					ESP_LOGI(TAG,"Serving POST config.json");
 
@@ -334,14 +315,7 @@ void http_server_netconn_serve(struct netconn *conn) {
 						netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
 						ESP_LOGE(TAG, "http_server_netconn_serve: GET /status failed to obtain mutex");
 					}
-				}
-
-				else if(strstr(line, "DELETE /connect.json ")) {
-					ESP_LOGI(TAG, "http_server_netconn_serve: DELETE /connect.json");
-					/* request a disconnection from wifi and forget about it */
-					wifi_manager_disconnect_async();
-					netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); /* 200 ok */
-				}
+				} 
 				else if(strstr(line, "POST /connect.json ")) {
 					ESP_LOGI(TAG, "http_server_netconn_serve: POST /connect.json");
 					bool found = false;
@@ -368,6 +342,37 @@ void http_server_netconn_serve(struct netconn *conn) {
 					}
 
 				}
+				else if(strstr(line, "DELETE /connect.json ")) {
+					ESP_LOGI(TAG, "http_server_netconn_serve: DELETE /connect.json");
+					/* request a disconnection from wifi and forget about it */
+					wifi_manager_disconnect_async();
+					netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); /* 200 ok */
+				}
+				else if(strstr(line, "POST /reboot.json ")){
+					esp_restart();
+				}
+				else if(strstr(line, "POST /recovery.json ")){
+					guided_factory();
+				}
+				else if(strstr(line, "GET /status.json ")){
+					ESP_LOGI(TAG,"Serving status.json");
+					if(wifi_manager_lock_json_buffer(( TickType_t ) 10)){
+						char *buff = wifi_manager_get_ip_info_json();
+						if(buff){
+							netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY);
+							netconn_write(conn, buff, strlen(buff), NETCONN_NOCOPY);
+
+							wifi_manager_unlock_json_buffer();
+						}
+						else{
+							netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
+						}
+					}
+					else{
+						netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
+						ESP_LOGE(TAG, "http_server_netconn_serve: GET /status failed to obtain mutex");
+					}
+				}
 				else{
 					netconn_write(conn, http_400_hdr, sizeof(http_400_hdr) - 1, NETCONN_NOCOPY);
 					ESP_LOGE(TAG, "bad request");

+ 1 - 161
components/wifi-manager/index.html

@@ -4,172 +4,12 @@
 		<meta charset="utf-8"/>
 		<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
 		<meta name="apple-mobile-web-app-capable" content="yes" />
-		<script src="/jquery.js"></script>
 		<link rel="stylesheet" href="/style.css">
+		<script src="/jquery.js"></script>
 		<script src="/code.js"></script>
 		<title>esp32-wifi-manager</title>
 	</head>
 
-	<script>
-var ws, sel, host, old, once = 0, jso, m;
-var to = 0, set_int = 0;
-
-
-
-function get_radio(name)
-{
-	var s = document.getElementsByName(name), sel;
-	for ( var i = 0; i < s.length; i++)
-	    if (s[i].checked) {
-	        sel = s[i].value;
-	        break;
-	    }
-
-	return sel;
-}
-
-function get_radio_index(name)
-{
-	var s = document.getElementsByName(name), i;
-
-	for (i = 0; i < s.length; i++)
-	    if (s[i].checked)
-	        return i;
-
-	return -1;
-}
-
-
-function do_reset()
-{
-	var s = "{\"reset\":\"1\"}";
-	try {
-		ws.send(s);
-	} catch(exception) {
-		alert('Sorry, there was a problem' + exception);
-	}
-
-	ws.close();
-	alert("Rebooting...");
-}
-
-function file_change()
-{
-        document.getElementById('update').disabled = 0;
-}
-
-
-
-function do_upload(f)
-{
-	var xhr = new XMLHttpRequest();
-
-        document.getElementById('update').disabled = 1;
-	//ws.close();
-	document.getElementById("progr").class = "progr-ok";
-
-	xhr.upload.addEventListener("progress", function(e) {
-		document.getElementById("progr").value = parseInt(e.loaded / e.total * 100);
-
-		if (e.loaded == e.total) {
-		//	document.getElementById("realpage").style.display = "none";
-		//	document.getElementById("waiting").style.display = "block";
-		}
-	
-	}, false);
-
-	xhr.onreadystatechange = function(e) {
-		   console.log("rs" + xhr.readyState + " status " + xhr.status); 
-		if (xhr.readyState == 4) {
-			/* it completed, for good or for ill */
-		//	document.getElementById("realpage").style.display = "none";
-		//	document.getElementById("waiting").style.display = "block";
-			document.getElementById("progr").class = "progr-ok";
-			console.log("upload reached state 4: xhr status " + xhr.status);
-			setTimeout(function() { window.location.href = location.origin + "/"; }, 9000 );
-		}
-	};
-
-	/* kill the heart timer */
-	clearInterval(set_int);
-
-	xhr.open("POST", f.action, true);
-	xhr.send(new FormData(f));
-
-	return false;
-}
-
-function do_settings(f)
-{
-	var xhr = new XMLHttpRequest();
-
-	xhr.onreadystatechange = function(e) {
-		   console.log("do_settings" + xhr.readyState + " status " + xhr.status); 
-		if (xhr.readyState == 4) {
-			document.getElementById("updsettings").style.opacity = "1.0";
-			document.getElementById("updsettings").disabled = 0;
-		}
-	};
-
-	xhr.open("POST", f.action, true);
-	document.getElementById("updsettings").style.opacity = "0.3";
-	document.getElementById("updsettings").disabled = 1;
-	xhr.send(new FormData(f));
-
-	return false;
-}
-
-function get_latest(n)
-{
-	if (n == 0)
-		ws.send("update-ota");
-	else
-		ws.send("update-factory");
-}
-
-
-function heart_timer() {
-	var s;
-	
-	s = Math.round((95 * to) / (40 * 10)) / 100;
-	
-	if (s < 0) {
-		clearInterval(set_int);
-		set_int = 0;
-		
-		ws.close();
-		
-		document.getElementById("realpage").style.opacity = "0.3";
-	}
-		
-	
-	to--;
-	document.getElementById("heart").style.opacity = s;
-}
-
-
-function heartbeat()
-{
-	to = 40 * 10;
-	if (!set_int) {
-		set_int = setInterval(heart_timer, 100);
-	}
-		
-}
-
-function handleClick(cb) {
-    console.log(cb);
-    if (cb.id == 'autoexec-cb') {
-        if (cb.checked) {
-            $("#autoexec-command").show(200);
-        } else {
-            $("#autoexec-command").hide(200);
-        }
-    } else if (cb.id == 'autoexec-cb') {
-    }
-}
-</script>
-
 	<body>
 		<div id="app">
 			<div id="app-wrap">