Merge pull request #5 from philippe44/WiFi-Manager

Wi fi manager (#22)
This commit is contained in:
Sébastien
2019-09-18 14:45:00 -04:00
committed by GitHub
3 changed files with 169 additions and 229 deletions

View File

@@ -1,6 +1,3 @@
var recovery = 0;
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() {
@@ -14,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;
@@ -23,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){
@@ -194,11 +197,13 @@ $(document).ready(function(){
$("#otadiv").hide();
}
//first time the page loads: attempt get the connection status and start the wifi scan
//first time the page loads: attempt to get the connection status and start the wifi scan
refreshAP();
getConfig();
//start timers
startCheckStatusInterval();
startRefreshAPInterval();
getConfig();
});
function performConnect(conntype){
@@ -263,6 +268,7 @@ function rssiToIcon(rssi){
}
function refreshAP(){
if (!enableTimers) return;
$.getJSON( "/ap.json", function( data ) {
if(data.length > 0){
//sort by signal strength
@@ -274,8 +280,6 @@ function refreshAP(){
refreshAPHTML(apList);
}
});
//TODO daduke
// RepeatRefreshAPInterval();
}
function refreshAPHTML(data){
@@ -289,6 +293,7 @@ function refreshAPHTML(data){
}
function checkStatus(){
if (!enableTimers) return;
$.getJSON( "/status.json", function( data ) {
if(data.hasOwnProperty('ssid') && data['ssid'] != ""){
if(data["ssid"] === selectedSSID){
@@ -310,6 +315,8 @@ function checkStatus(){
$( "#connect-success" ).append("<p>Your IP address now is: " + text(data["ip"]) + "</p>");
$( "#connect-success" ).show();
$( "#connect-fail" ).hide();
enableTimers = false;
}
else if(data["urc"] === 1){
//failed attempt
@@ -329,6 +336,8 @@ function checkStatus(){
$( "#loading" ).hide();
$( "#connect-fail" ).show();
$( "#connect-success" ).hide();
enableTimers = true;
}
}
else if(data.hasOwnProperty('urc') && data['urc'] === 0){
@@ -341,10 +350,7 @@ function checkStatus(){
$("#gw").text(data["gw"]);
$("#wifi-status").slideDown( "fast", function() {});
}
//TODO daduke
console.log("stopping timers..");
stopCheckStatusInterval();
stopRefreshAPInterval
enableTimers = false;
}
}
else if(data.hasOwnProperty('urc') && data['urc'] === 2){
@@ -352,6 +358,7 @@ stopRefreshAPInterval
if($("#wifi-status").is(":visible")){
$("#wifi-status").slideUp( "fast", function() {});
}
enableTimers = true;
}
})
.fail(function() {
@@ -404,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') {
@@ -450,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);
}
}

View File

@@ -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 /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 /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 /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};
@@ -262,16 +246,15 @@ void http_server_netconn_serve(struct netconn *conn) {
ESP_LOGD(TAG,"No matching command found for name %s", autoexec_name);
break;
}
i++;
i++;
} while(1);
free(buff);
netconn_write(conn, json_end, strlen(json_end), NETCONN_NOCOPY);
ESP_LOGD(TAG,"%s", json_end);
}
}
else if(strstr(line, "POST /factory.json ")){
guided_factory();
}
else if(strstr(line, "POST /config.json ")){
ESP_LOGI(TAG,"Serving POST config.json");
@@ -304,7 +287,6 @@ void http_server_netconn_serve(struct netconn *conn) {
ESP_LOGD(TAG,"Looking for command name %s.", autoexec_name);
autoexec_value = http_server_get_header(save_ptr, autoexec_name, &lenS);
if(autoexec_value ){
if(lenS < MAX_COMMAND_LINE_SIZE ){
ESP_LOGD(TAG, "http_server_netconn_serve: config.json/ call, with %s: %s, length %i", autoexec_key, autoexec_value, lenS);
@@ -323,19 +305,17 @@ void http_server_netconn_serve(struct netconn *conn) {
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); //200ok
//reboot esp if autoexec1 was modified
if (i > 1) {
ESP_LOGD(TAG,"autoexec1 changed, triggering reboot");
esp_restart();
}
}
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, "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;
@@ -362,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");

View File

@@ -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">