new ota logic

This commit is contained in:
2026-03-15 19:57:19 +01:00
parent 07ab69075a
commit c61a586595
15 changed files with 313 additions and 400 deletions

View File

@@ -5,7 +5,7 @@ use crate::plant_state::{MoistureSensorState, PlantState};
use crate::tank::determine_tank_state;
use crate::{get_version, BOARD_ACCESS};
use alloc::format;
use alloc::string::{String, ToString};
use alloc::string::String;
use alloc::vec::Vec;
use chrono_tz::Tz;
use core::str::FromStr;
@@ -43,7 +43,6 @@ where
plant_state.push(PlantState::read_hardware_state(moistures, i, &mut board).await);
}
let a = Vec::from_iter(plant_state.iter().map(|s| match &s.sensor_a {
MoistureSensorState::Disabled => "disabled".to_string(),
MoistureSensorState::MoistureValue {
raw_hz,
moisture_percent,
@@ -53,7 +52,6 @@ where
MoistureSensorState::SensorError(err) => format!("{err:?}"),
}));
let b = Vec::from_iter(plant_state.iter().map(|s| match &s.sensor_b {
MoistureSensorState::Disabled => "disabled".to_string(),
MoistureSensorState::MoistureValue {
raw_hz,
moisture_percent,
@@ -128,11 +126,7 @@ pub(crate) async fn get_battery_state<T, const N: usize>(
_request: &mut Connection<'_, T, N>,
) -> FatResult<Option<String>> {
let mut board = BOARD_ACCESS.get().await.lock().await;
let battery_state = board
.board_hal
.get_battery_monitor()
.get_state()
.await?;
let battery_state = board.board_hal.get_battery_monitor().get_state().await?;
Ok(Some(serde_json::to_string(&battery_state)?))
}

View File

@@ -4,7 +4,8 @@ use crate::BOARD_ACCESS;
use edge_http::io::server::Connection;
use edge_http::Method;
use edge_nal::io::{Read, Write};
use log::info;
use esp_hal_ota::OtaError;
use log::{error, info};
pub(crate) async fn ota_operations<T, const N: usize>(
conn: &mut Connection<'_, T, { N }>,
@@ -28,27 +29,45 @@ where
Some(200)
}
Method::Post => {
let mut offset = 0_usize;
let size = read_up_to_bytes_from_request(conn, Some(4)).await?;
let flash_size = u32::from_le_bytes(size[..4].try_into().unwrap());
info!("flash size: {flash_size}");
let crc32 = read_up_to_bytes_from_request(conn, Some(4)).await?;
let target_crc = u32::from_le_bytes(crc32[..4].try_into().unwrap());
info!("crc32: {target_crc}");
BOARD_ACCESS
.get()
.await
.lock()
.await
.board_hal
.get_esp()
.ota
.ota_begin(flash_size, target_crc)?;
let mut chunk = 0;
loop {
let buf = read_up_to_bytes_from_request(conn, Some(4096)).await?;
if buf.is_empty() {
info!("file request for ota finished");
let mut board = BOARD_ACCESS.get().await.lock().await;
board.board_hal.get_esp().finalize_ota().await?;
break;
error!("Upload finished, but no enough data received.");
return Err(OtaError::WrongCRC)?;
} else {
let mut board = BOARD_ACCESS.get().await.lock().await;
board.board_hal.progress(chunk as u32).await;
// Erase next block if we are at a 4K boundary (including the first block at offset 0)
info!("erasing and writing block 0x{offset:x}");
board
.board_hal
.get_esp()
.write_ota(offset as u32, &buf)
.await?;
let finsihed = board.board_hal.get_esp().ota.ota_write_chunk(&buf)?;
if finsihed {
board.board_hal.get_esp().ota.ota_flush(true, true)?;
board.board_hal.get_esp().set_restart_to_conf(true);
break;
}
info!(
"Progress: {}%",
(board.board_hal.get_esp().ota.get_ota_progress() * 100.0) as u8
);
}
offset += buf.len();
chunk += 1;
}
BOARD_ACCESS

View File

@@ -1,6 +1,6 @@
use crate::config::PlantControllerConfig;
use crate::fat_error::FatResult;
use crate::hal::{esp_set_time, Detection, DetectionRequest};
use crate::hal::{esp_set_time, Detection};
use crate::webserver::read_up_to_bytes_from_request;
use crate::{do_secure_pump, BOARD_ACCESS};
use alloc::string::{String, ToString};
@@ -142,9 +142,6 @@ where
board.board_hal.can_power(can_power_request.state).await?;
let enable = can_power_request.state;
info!(
"set can power to {enable}"
);
info!("set can power to {enable}");
Ok(None)
}