further components and bootstrap initial

This commit is contained in:
2024-12-20 03:29:29 +01:00
parent 5fedbec433
commit 58b63fc8ee
22 changed files with 3347 additions and 436 deletions

View File

@@ -39,6 +39,10 @@ struct VersionInfo<'a> {
struct LoadData<'a> {
rtc: &'a str,
native: &'a str,
}
#[derive(Serialize, Debug)]
struct Moistures {
moisture_a: Vec<u8>,
moisture_b: Vec<u8>,
}
@@ -67,18 +71,10 @@ fn write_time(
anyhow::Ok(None)
}
fn get_data(
fn get_live_moisture(
_request: &mut Request<&mut EspHttpConnection>,
) -> Result<Option<std::string::String>, anyhow::Error> {
let mut board = BOARD_ACCESS.lock().unwrap();
let native = board
.time()
.and_then(|t| Ok(t.to_rfc3339()))
.unwrap_or("error".to_string());
let rtc = board
.get_rtc_time()
.and_then(|t| Ok(t.to_rfc3339()))
.unwrap_or("error".to_string());
let mut a: Vec<u8> = Vec::new();
let mut b: Vec<u8> = Vec::new();
@@ -107,11 +103,31 @@ fn get_data(
}
}
let data = Moistures {
moisture_a: a,
moisture_b: b,
};
let json = serde_json::to_string(&data)?;
anyhow::Ok(Some(json))
}
fn get_data(
_request: &mut Request<&mut EspHttpConnection>,
) -> Result<Option<std::string::String>, anyhow::Error> {
let mut board = BOARD_ACCESS.lock().unwrap();
let native = board
.time()
.and_then(|t| Ok(t.to_rfc3339()))
.unwrap_or("error".to_string());
let rtc = board
.get_rtc_time()
.and_then(|t| Ok(t.to_rfc3339()))
.unwrap_or("error".to_string());
let data = LoadData {
rtc: rtc.as_str(),
native: native.as_str(),
moisture_a: a,
moisture_b: b,
};
let json = serde_json::to_string(&data)?;
@@ -145,8 +161,11 @@ fn set_config(
fn get_version(
_request: &mut Request<&mut EspHttpConnection>,
) -> Result<Option<std::string::String>, anyhow::Error> {
let version_info = VersionInfo {
git_hash: env!("VERGEN_GIT_DESCRIBE"),
let branch = env!("VERGEN_GIT_BRANCH").to_owned();
let hash = &env!("VERGEN_GIT_SHA")[0..8];
let version_info: VersionInfo<'_> = VersionInfo {
git_hash: &(branch + "@" + hash),
build_time: env!("VERGEN_BUILD_TIMESTAMP"),
};
anyhow::Ok(Some(serde_json::to_string(&version_info)?))
@@ -282,10 +301,15 @@ pub fn httpd(_reboot_now: Arc<AtomicBool>) -> Box<EspHttpServer<'static>> {
.unwrap();
server
.fn_handler("/data", Method::Get, |request| {
.fn_handler("/time", Method::Get, |request| {
handle_error_to500(request, get_data)
})
.unwrap();
server
.fn_handler("/moisture", Method::Get, |request| {
handle_error_to500(request, get_live_moisture)
})
.unwrap();
server
.fn_handler("/time", Method::Post, |request| {
handle_error_to500(request, write_time)
@@ -311,6 +335,11 @@ pub fn httpd(_reboot_now: Arc<AtomicBool>) -> Box<EspHttpServer<'static>> {
handle_error_to500(request, ota)
})
.unwrap();
server
.fn_handler("/ota", Method::Options, |request| {
cors_response(request, 200, "")
})
.unwrap();
server
.fn_handler("/get_config", Method::Get, move |request| {
handle_error_to500(request, get_config)
@@ -476,6 +505,14 @@ pub fn httpd(_reboot_now: Arc<AtomicBool>) -> Box<EspHttpServer<'static>> {
})
.unwrap();
server
.fn_handler("/bootstrap-grid.css", Method::Get, |request| {
request
.into_ok_response()?
.write(include_bytes!("bootstrap-grid.css"))?;
anyhow::Ok(())
})
.unwrap();
server
}
fn cors_response(
@@ -483,7 +520,10 @@ fn cors_response(
status: u16,
body: &str,
) -> Result<(), anyhow::Error> {
let headers = [("Access-Control-Allow-Origin", "*")];
let headers = [
("Access-Control-Allow-Origin", "*"),
("Access-Control-Allow-Headers", "*")
];
let mut response = request.into_response(status, None, &headers)?;
response.write(body.as_bytes())?;
response.flush()?;