From d66ac8c0f14186ddacc5d8425ce6f4a9dcb54634 Mon Sep 17 00:00:00 2001 From: _index Date: Fri, 27 Dec 2024 01:00:51 +0100 Subject: [PATCH] add responsiveness with threading --- src/http/wg_routes.rs | 18 ++++++++++++------ src/http/wifi_routes.rs | 22 ++++++++++++++++------ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/http/wg_routes.rs b/src/http/wg_routes.rs index 64f9173..7042ec6 100644 --- a/src/http/wg_routes.rs +++ b/src/http/wg_routes.rs @@ -1,4 +1,5 @@ use std::sync::{Arc, Mutex}; +use std::thread; use anyhow::Error; use esp_idf_svc::http::server::{EspHttpServer, Method}; @@ -18,9 +19,6 @@ pub fn set_routes( let nvs_set = Arc::clone(&nvs); let wifi_check = Arc::clone(&wifi); - let nvs_wg = Arc::clone(&nvs); - let wifi_sync = Arc::clone(&wifi); - move |mut request| { let wifi_check = wifi_check.lock().unwrap(); @@ -53,8 +51,13 @@ pub fn set_routes( drop(nvs_set); - wireguard::sync_sntp(Arc::clone(&wifi_sync))?; - wireguard::start_wg_tunnel(Arc::clone(&nvs_wg))?; + let wifi = Arc::clone(&wifi); + let nvs = Arc::clone(&nvs); + + thread::spawn(move || { + _ = wireguard::sync_sntp(Arc::clone(&wifi)); + _ = wireguard::start_wg_tunnel(Arc::clone(&nvs)); + }); let connection = request.connection(); @@ -66,7 +69,9 @@ pub fn set_routes( // Handler to disconnect from the wireguard peer http_server.fn_handler("/disconnect-wg", Method::Post, move |mut request| { - wireguard::end_wg_tunnel()?; + thread::spawn(|| { + _ = wireguard::end_wg_tunnel(); + }); let connection = request.connection(); @@ -76,6 +81,7 @@ pub fn set_routes( })?; // Handler to get current wireguard status (connected/disconnected) + // TODO! FIX THIS CALLBACK http_server.fn_handler("/wg-status", Method::Get, move |mut request| { let connection = request.connection(); diff --git a/src/http/wifi_routes.rs b/src/http/wifi_routes.rs index 34c1cb6..fa5d90a 100644 --- a/src/http/wifi_routes.rs +++ b/src/http/wifi_routes.rs @@ -1,4 +1,5 @@ use std::sync::{Arc, Mutex}; +use std::thread; use anyhow::Error; use esp_idf_svc::http::server::{EspHttpServer, Method}; @@ -16,6 +17,7 @@ pub fn set_routes( // Handler to disconnect from wifi http_server.fn_handler("/disconnect-wifi", Method::Get, { let wifi = Arc::clone(&wifi); + move |mut request| { wifi::disconnect(Arc::clone(&wifi))?; @@ -29,10 +31,10 @@ pub fn set_routes( // Handler to connect to wifi http_server.fn_handler("/connect-wifi", Method::Post, { - let nvs = Arc::clone(&nvs); + let nvs_set = Arc::clone(&nvs); let wifi = Arc::clone(&wifi); + move |mut request| { - let mut nvs_set = nvs.lock().unwrap(); let mut body = Vec::new(); let mut buffer = [0_u8; 128]; @@ -44,8 +46,9 @@ pub fn set_routes( } } - let form_data = String::from_utf8(body)?; - let wifi_conf: NvsWifi = serde_urlencoded::from_str(form_data.as_str())?; + let wifi_conf: NvsWifi = serde_urlencoded::from_str(String::from_utf8(body)?.as_str())?; + + let mut nvs_set = nvs_set.lock().unwrap(); NvsWifi::set_field(&mut nvs_set, NvsKeys::STA_SSID, wifi_conf.sta_ssid.clean_string().as_str())?; NvsWifi::set_field(&mut nvs_set, NvsKeys::STA_PASSWD, wifi_conf.sta_passwd.clean_string().as_str())?; @@ -53,9 +56,14 @@ pub fn set_routes( drop(nvs_set); - wifi::set_configuration(Arc::clone(&nvs), Arc::clone(&wifi))?; - wifi::connect(Arc::clone(&wifi))?; + + let nvs_thread = Arc::clone(&nvs); + let wifi = Arc::clone(&wifi); + thread::spawn(move || { + _ = wifi::set_configuration(Arc::clone(&nvs_thread), Arc::clone(&wifi)); + _ = wifi::connect(Arc::clone(&wifi)); + }); let connection = request.connection(); connection.initiate_response(204, Some("OK"), &[("Content-Type", "text/html")])?; @@ -67,6 +75,7 @@ pub fn set_routes( // Handler to get available wifis http_server.fn_handler("/wifi", Method::Get, { let wifi = Arc::clone(&wifi); + move |request| { let mut wifi = wifi.lock().unwrap(); @@ -165,6 +174,7 @@ pub fn set_routes( // Handler to get current wifi status (connected/disconnected) http_server.fn_handler("/wifi-status", Method::Get, { let wifi = Arc::clone(&wifi); + move |mut request| { let wifi = wifi.lock().unwrap();