Compare commits
16 Commits
refactor/m
...
legacy/v3-
| Author | SHA1 | Date | |
|---|---|---|---|
|
4cc70f96de
|
|||
|
c90174be27
|
|||
|
b569fa4b04
|
|||
|
f71ca7ec6d
|
|||
|
28f7ae20ef
|
|||
|
52049c456e
|
|||
|
e3b7648a3f
|
|||
|
08ee9018cf
|
|||
|
c2929a65ae
|
|||
|
0b386b0ca3
|
|||
|
0ab1ea3635
|
|||
|
ae73f12d1c
|
|||
|
cfe1c2c6d8
|
|||
|
578379c0d9
|
|||
|
2ff219a1cb
|
|||
|
96023c8dc3
|
9
.gitignore
vendored
9
.gitignore
vendored
@@ -8,6 +8,15 @@ target
|
||||
Cargo.lock
|
||||
node_modules/
|
||||
rust/src/webserver/bundle.js
|
||||
rust/src/webserver/bundle.js.gz
|
||||
rust/src/webserver/index.html
|
||||
rust/src/webserver/index.html.gz
|
||||
rust/src_webpack/bundle.js
|
||||
rust/src_webpack/bundle.js.gz
|
||||
rust/src_webpack/index.html
|
||||
rust/src_webpack/index.html.gz
|
||||
rust/build/
|
||||
rust/image.bin
|
||||
rust/target/
|
||||
rust/Cargo.lock
|
||||
rust/src_webpack/node_modules/
|
||||
|
||||
22
rust/all.sh
Executable file
22
rust/all.sh
Executable file
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
"${SCRIPT_DIR}/build_website.sh"
|
||||
|
||||
cargo build --release
|
||||
espflash save-image \
|
||||
--bootloader "${SCRIPT_DIR}/bootloader.bin" \
|
||||
--partition-table "${SCRIPT_DIR}/partitions.csv" \
|
||||
--chip esp32c6 \
|
||||
target/riscv32imac-unknown-none-elf/release/plant-ctrl2 \
|
||||
"${SCRIPT_DIR}/image.bin"
|
||||
|
||||
espflash flash --monitor \
|
||||
--bootloader "${SCRIPT_DIR}/bootloader.bin" \
|
||||
--chip esp32c6 \
|
||||
--baud 921600 \
|
||||
--partition-table "${SCRIPT_DIR}/partitions.csv" \
|
||||
target/riscv32imac-unknown-none-elf/release/plant-ctrl2
|
||||
@@ -1,5 +1,3 @@
|
||||
use std::process::Command;
|
||||
|
||||
use vergen::EmitBuilder;
|
||||
|
||||
fn linker_be_nice() {
|
||||
@@ -50,72 +48,9 @@ fn linker_be_nice() {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
webpack();
|
||||
linker_be_nice();
|
||||
// Non-existent path causes Cargo to always re-run this script,
|
||||
// keeping VERGEN_BUILD_TIMESTAMP fresh on every build.
|
||||
println!("cargo:rerun-if-changed=ALWAYS_REBUILD_SENTINEL");
|
||||
let _ = EmitBuilder::builder().all_git().all_build().emit();
|
||||
}
|
||||
|
||||
fn webpack() {
|
||||
//println!("cargo:rerun-if-changed=./src/src_webpack");
|
||||
Command::new("rm")
|
||||
.arg("./src/webserver/bundle.js.gz")
|
||||
.output()
|
||||
.unwrap();
|
||||
|
||||
match Command::new("cmd").spawn() {
|
||||
Ok(_) => {
|
||||
println!("Assuming build on windows");
|
||||
let output = Command::new("cmd")
|
||||
.arg("/K")
|
||||
.arg("npx")
|
||||
.arg("webpack")
|
||||
.current_dir("./src_webpack")
|
||||
.output()
|
||||
.unwrap();
|
||||
println!("status: {}", output.status);
|
||||
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
|
||||
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
|
||||
assert!(output.status.success());
|
||||
|
||||
// move webpack results to rust webserver src
|
||||
let _ = Command::new("cmd")
|
||||
.arg("/K")
|
||||
.arg("move")
|
||||
.arg("./src_webpack/bundle.js.gz")
|
||||
.arg("./src/webserver")
|
||||
.output()
|
||||
.unwrap();
|
||||
let _ = Command::new("cmd")
|
||||
.arg("/K")
|
||||
.arg("move")
|
||||
.arg("./src_webpack/index.html.gz")
|
||||
.arg("./src/webserver")
|
||||
.output()
|
||||
.unwrap();
|
||||
}
|
||||
Err(_) => {
|
||||
println!("Assuming build on linux");
|
||||
let output = Command::new("npx")
|
||||
.arg("webpack")
|
||||
.current_dir("./src_webpack")
|
||||
.output()
|
||||
.unwrap();
|
||||
println!("status: {}", output.status);
|
||||
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
|
||||
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
|
||||
assert!(output.status.success());
|
||||
|
||||
// move webpack results to rust webserver src
|
||||
let _ = Command::new("mv")
|
||||
.arg("./src_webpack/bundle.js.gz")
|
||||
.arg("./src/webserver")
|
||||
.output()
|
||||
.unwrap();
|
||||
let _ = Command::new("mv")
|
||||
.arg("./src_webpack/index.html.gz")
|
||||
.arg("./src/webserver")
|
||||
.output()
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
21
rust/build_website.sh
Executable file
21
rust/build_website.sh
Executable file
@@ -0,0 +1,21 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
WEBPACK_DIR="${SCRIPT_DIR}/src_webpack"
|
||||
WEBSERVER_DIR="${SCRIPT_DIR}/src/webserver"
|
||||
|
||||
rm -f "${WEBSERVER_DIR}/index.html.gz"
|
||||
rm -f "${WEBSERVER_DIR}/bundle.js.gz"
|
||||
rm -f "${WEBPACK_DIR}/index.html.gz"
|
||||
rm -f "${WEBPACK_DIR}/bundle.js.gz"
|
||||
rm -f "${WEBPACK_DIR}/index.html"
|
||||
rm -f "${WEBPACK_DIR}/bundle.js"
|
||||
|
||||
pushd "${WEBPACK_DIR}"
|
||||
npm install
|
||||
npx webpack build
|
||||
cp index.html.gz "${WEBSERVER_DIR}/index.html.gz"
|
||||
cp bundle.js.gz "${WEBSERVER_DIR}/bundle.js.gz"
|
||||
popd
|
||||
7
rust/erase_ota.sh
Executable file
7
rust/erase_ota.sh
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
cargo espflash erase-parts otadata --partition-table "${SCRIPT_DIR}/partitions.csv"
|
||||
15
rust/flash.sh
Executable file
15
rust/flash.sh
Executable file
@@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
"${SCRIPT_DIR}/build_website.sh"
|
||||
|
||||
cargo build --release
|
||||
espflash flash --monitor \
|
||||
--bootloader "${SCRIPT_DIR}/bootloader.bin" \
|
||||
--chip esp32c6 \
|
||||
--baud 921600 \
|
||||
--partition-table "${SCRIPT_DIR}/partitions.csv" \
|
||||
target/riscv32imac-unknown-none-elf/release/plant-ctrl2
|
||||
17
rust/image_build.sh
Executable file
17
rust/image_build.sh
Executable file
@@ -0,0 +1,17 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
rm -f "${SCRIPT_DIR}/image.bin"
|
||||
|
||||
"${SCRIPT_DIR}/build_website.sh"
|
||||
|
||||
cargo build --release
|
||||
espflash save-image \
|
||||
--bootloader "${SCRIPT_DIR}/bootloader.bin" \
|
||||
--partition-table "${SCRIPT_DIR}/partitions.csv" \
|
||||
--chip esp32c6 \
|
||||
target/riscv32imac-unknown-none-elf/release/plant-ctrl2 \
|
||||
"${SCRIPT_DIR}/image.bin"
|
||||
164
rust/src/main.rs
164
rust/src/main.rs
@@ -26,7 +26,7 @@ use crate::{
|
||||
config::BoardVersion::INITIAL,
|
||||
hal::{PlantHal, HAL, PLANT_COUNT},
|
||||
};
|
||||
use ::log::{info, warn, error};
|
||||
use ::log::{error, info, warn};
|
||||
use alloc::borrow::ToOwned;
|
||||
use alloc::string::{String, ToString};
|
||||
use alloc::sync::Arc;
|
||||
@@ -122,8 +122,6 @@ pub struct PumpResult {
|
||||
pump_time_s: u16,
|
||||
}
|
||||
|
||||
|
||||
|
||||
async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
info!("Startup Rust");
|
||||
|
||||
@@ -197,10 +195,15 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
info!("No wifi configured, starting initial config mode");
|
||||
|
||||
let esp = board.board_hal.get_esp();
|
||||
let ssid = esp.load_config().await
|
||||
let ssid = esp
|
||||
.load_config()
|
||||
.await
|
||||
.map(|config| config.network.ap_ssid.to_string())
|
||||
.unwrap_or_else(|_| String::from("PlantCtrl Emergency Mode"));
|
||||
let device = esp.interface_ap.take().context("AP interface already taken")?;
|
||||
let device = esp
|
||||
.interface_ap
|
||||
.take()
|
||||
.context("AP interface already taken")?;
|
||||
let stack = network::wifi_ap(ssid, device, &esp.controller, &mut esp.rng, spawner).await?;
|
||||
|
||||
let reboot_now = Arc::new(AtomicBool::new(false));
|
||||
@@ -217,7 +220,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
info!("No wifi configured");
|
||||
//the current sensors require this amount to stabilize, in the case of Wi-Fi this is already handled due to connect timings;
|
||||
Timer::after_millis(100).await;
|
||||
network::NetworkMode::OFFLINE
|
||||
network::NetworkMode::OFFLINE
|
||||
};
|
||||
|
||||
if matches!(network_mode, network::NetworkMode::OFFLINE) && to_config {
|
||||
@@ -225,14 +228,18 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
|
||||
let res = {
|
||||
let esp = board.board_hal.get_esp();
|
||||
let ssid = esp.load_config().await
|
||||
let ssid = esp
|
||||
.load_config()
|
||||
.await
|
||||
.map(|config| config.network.ap_ssid.to_string())
|
||||
.unwrap_or_else(|_| String::from("PlantCtrl Emergency Mode"));
|
||||
let device = match esp.interface_ap.take() {
|
||||
Some(d) => d,
|
||||
None => {
|
||||
use crate::fat_error::FatError;
|
||||
return Err(FatError::String { error: "AP interface already taken".to_string() });
|
||||
return Err(FatError::String {
|
||||
error: "AP interface already taken".to_string(),
|
||||
});
|
||||
}
|
||||
};
|
||||
network::wifi_ap(ssid, device, &esp.controller, &mut esp.rng, spawner).await
|
||||
@@ -302,7 +309,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
log(LogMessage::NormalRun, 0, 0, "", "");
|
||||
}
|
||||
|
||||
let _dry_run = false;
|
||||
let dry_run = false;
|
||||
|
||||
let tank_state = determine_tank_state(&mut board).await;
|
||||
|
||||
@@ -339,7 +346,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
}
|
||||
}
|
||||
|
||||
let mut _water_frozen = false;
|
||||
let mut water_frozen = false;
|
||||
let water_temp: FatResult<f32> = match board.board_hal.get_tank_sensor() {
|
||||
Ok(sensor) => sensor.water_temperature_c().await,
|
||||
Err(e) => Err(e),
|
||||
@@ -347,7 +354,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
|
||||
if let Ok(res) = water_temp {
|
||||
if res < WATER_FROZEN_THRESH {
|
||||
_water_frozen = true;
|
||||
water_frozen = true;
|
||||
}
|
||||
}
|
||||
info!("Water temp is {}", water_temp.as_ref().unwrap_or(&0.));
|
||||
@@ -367,74 +374,70 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
|
||||
publish_plant_states(&mut board, &timezone_time.clone(), &plantstate).await;
|
||||
|
||||
// let pump_required = plantstate
|
||||
// .iter()
|
||||
// .zip(&board.board_hal.get_config().plants)
|
||||
// .any(|(it, conf)| it.needs_to_be_watered(conf, &timezone_time))
|
||||
// && !water_frozen;
|
||||
// if pump_required {
|
||||
// log(LogMessage::EnableMain, dry_run as u32, 0, "", "");
|
||||
// for (plant_id, (state, plant_config)) in plantstate
|
||||
// .iter()
|
||||
// .zip(&board.board_hal.get_config().plants.clone())
|
||||
// .enumerate()
|
||||
// {
|
||||
// if state.needs_to_be_watered(plant_config, &timezone_time) {
|
||||
// let pump_count = board.board_hal.get_esp().consecutive_pump_count(plant_id) + 1;
|
||||
// board
|
||||
// .board_hal
|
||||
// .get_esp()
|
||||
// .store_consecutive_pump_count(plant_id, pump_count);
|
||||
//
|
||||
// let pump_ineffective = pump_count > plant_config.max_consecutive_pump_count as u32;
|
||||
// if pump_ineffective {
|
||||
// log(
|
||||
// LogMessage::ConsecutivePumpCountLimit,
|
||||
// pump_count,
|
||||
// plant_config.max_consecutive_pump_count as u32,
|
||||
// &(plant_id + 1).to_string(),
|
||||
// "",
|
||||
// );
|
||||
// board.board_hal.fault(plant_id, true).await?;
|
||||
// }
|
||||
// log(
|
||||
// LogMessage::PumpPlant,
|
||||
// (plant_id + 1) as u32,
|
||||
// plant_config.pump_time_s as u32,
|
||||
// &dry_run.to_string(),
|
||||
// "",
|
||||
// );
|
||||
// board
|
||||
// .board_hal
|
||||
// .get_esp()
|
||||
// .store_last_pump_time(plant_id, cur);
|
||||
// board.board_hal.get_esp().last_pump_time(plant_id);
|
||||
// //state.active = true;
|
||||
//
|
||||
// pump_info(plant_id, true, pump_ineffective, 0, 0, 0, false).await;
|
||||
//
|
||||
// let result = do_secure_pump(plant_id, plant_config, dry_run).await?;
|
||||
// board.board_hal.pump(plant_id, false).await?;
|
||||
// pump_info(
|
||||
// plant_id,
|
||||
// false,
|
||||
// pump_ineffective,
|
||||
// result.median_current_ma,
|
||||
// result.max_current_ma,
|
||||
// result.min_current_ma,
|
||||
// result.error,
|
||||
// )
|
||||
// .await;
|
||||
// } else if !state.pump_in_timeout(plant_config, &timezone_time) {
|
||||
// // plant does not need to be watered and is not in timeout
|
||||
// // -> reset consecutive pump count
|
||||
// board
|
||||
// .board_hal
|
||||
// .get_esp()
|
||||
// .store_consecutive_pump_count(plant_id, 0);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
let pump_required = plantstate
|
||||
.iter()
|
||||
.zip(&board.board_hal.get_config().plants)
|
||||
.any(|(it, conf)| it.needs_to_be_watered(conf, &timezone_time))
|
||||
&& !water_frozen;
|
||||
if pump_required {
|
||||
log(LogMessage::EnableMain, dry_run as u32, 0, "", "");
|
||||
for (plant_id, (state, plant_config)) in plantstate
|
||||
.iter()
|
||||
.zip(&board.board_hal.get_config().plants.clone())
|
||||
.enumerate()
|
||||
{
|
||||
if state.needs_to_be_watered(plant_config, &timezone_time) {
|
||||
let pump_count = board.board_hal.get_esp().consecutive_pump_count(plant_id) + 1;
|
||||
board
|
||||
.board_hal
|
||||
.get_esp()
|
||||
.store_consecutive_pump_count(plant_id, pump_count);
|
||||
let pump_ineffective = pump_count > plant_config.max_consecutive_pump_count as u32;
|
||||
if pump_ineffective {
|
||||
log(
|
||||
LogMessage::ConsecutivePumpCountLimit,
|
||||
pump_count,
|
||||
plant_config.max_consecutive_pump_count as u32,
|
||||
&(plant_id + 1).to_string(),
|
||||
"",
|
||||
);
|
||||
board.board_hal.fault(plant_id, true).await?;
|
||||
}
|
||||
log(
|
||||
LogMessage::PumpPlant,
|
||||
(plant_id + 1) as u32,
|
||||
plant_config.pump_time_s as u32,
|
||||
&dry_run.to_string(),
|
||||
"",
|
||||
);
|
||||
board
|
||||
.board_hal
|
||||
.get_esp()
|
||||
.store_last_pump_time(plant_id, cur);
|
||||
board.board_hal.get_esp().last_pump_time(plant_id);
|
||||
pump_info(plant_id, true, pump_ineffective, 0, 0, 0, false).await;
|
||||
let result = do_secure_pump(&mut board, plant_id, plant_config, dry_run).await?;
|
||||
board.board_hal.pump(plant_id, false).await?;
|
||||
pump_info(
|
||||
plant_id,
|
||||
false,
|
||||
pump_ineffective,
|
||||
result.median_current_ma,
|
||||
result.max_current_ma,
|
||||
result.min_current_ma,
|
||||
result.error,
|
||||
)
|
||||
.await;
|
||||
} else if !state.pump_in_timeout(plant_config, &timezone_time) {
|
||||
// plant does not need to be watered and is not in timeout
|
||||
// -> reset consecutive pump count
|
||||
board
|
||||
.board_hal
|
||||
.get_esp()
|
||||
.store_consecutive_pump_count(plant_id, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
info!("state of charg");
|
||||
let is_day = board.board_hal.is_day();
|
||||
@@ -558,7 +561,8 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
|
||||
if stay_alive {
|
||||
let reboot_now = Arc::new(AtomicBool::new(false));
|
||||
let _webserver = http_server(reboot_now.clone(), stack.take().unwrap());
|
||||
|
||||
spawner.spawn(http_server(reboot_now.clone(), stack.take().unwrap())?);
|
||||
wait_infinity(board, WaitType::MqttConfig, reboot_now.clone(), UTC).await;
|
||||
} else {
|
||||
//TODO wait for all mqtt publishes?
|
||||
|
||||
@@ -30,59 +30,80 @@ use core::result::Result::Ok;
|
||||
use core::sync::atomic::{AtomicBool, Ordering};
|
||||
use edge_http::io::server::{Connection, Handler, Server};
|
||||
use edge_http::Method;
|
||||
use edge_nal::TcpBind;
|
||||
use edge_nal::io::{Read, Write};
|
||||
use edge_nal::TcpBind;
|
||||
use edge_nal_embassy::{Tcp, TcpBuffers};
|
||||
use embassy_net::Stack;
|
||||
use embassy_time::Instant;
|
||||
use log::info;
|
||||
use log::{error, info};
|
||||
|
||||
// fn ota(
|
||||
// request: &mut Request<&mut EspHttpConnection>,
|
||||
// ) -> Result<Option<std::string::String>, anyhow::Error> {
|
||||
// let mut board = BOARD_ACCESS.lock().unwrap();
|
||||
// let mut ota = OtaUpdate::begin()?;
|
||||
// log::info!("start ota");
|
||||
//
|
||||
// //having a larger buffer is not really faster, requires more stack and prevents the progress bar from working ;)
|
||||
// const BUFFER_SIZE: usize = 512;
|
||||
// let mut buffer: [u8; BUFFER_SIZE] = [0; BUFFER_SIZE];
|
||||
// let mut total_read: usize = 0;
|
||||
// let mut lastiter = 0;
|
||||
// loop {
|
||||
// let read = request.read(&mut buffer)?;
|
||||
// total_read += read;
|
||||
// let to_write = &buffer[0..read];
|
||||
// //delay for watchdog and wifi stuff
|
||||
// board.board_hal.get_esp().delay.delay_ms(1);
|
||||
//
|
||||
// let iter = (total_read / 1024) % 8;
|
||||
// if iter != lastiter {
|
||||
// board.board_hal.general_fault(iter % 5 == 0);
|
||||
// for i in 0..PLANT_COUNT {
|
||||
// let _ = board.board_hal.fault(i, iter == i);
|
||||
// }
|
||||
// lastiter = iter;
|
||||
// }
|
||||
//
|
||||
// ota.write(to_write)?;
|
||||
// if read == 0 {
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// log::info!("wrote bytes ota {total_read}");
|
||||
// log::info!("finish ota");
|
||||
// let partition = ota.raw_partition();
|
||||
// log::info!("finalizing and changing boot partition to {partition:?}");
|
||||
//
|
||||
// let mut finalizer = ota.finalize()?;
|
||||
// log::info!("changing boot partition");
|
||||
// board.board_hal.get_esp().set_restart_to_conf(true);
|
||||
// drop(board);
|
||||
// finalizer.set_as_boot_partition()?;
|
||||
// anyhow::Ok(None)
|
||||
// }
|
||||
//
|
||||
pub(crate) async fn ota_operations<T, const N: usize>(
|
||||
conn: &mut Connection<'_, T, { N }>,
|
||||
method: Method,
|
||||
) -> Result<Option<u32>, FatError>
|
||||
where
|
||||
T: Read + Write,
|
||||
{
|
||||
Ok(match method {
|
||||
Method::Options => {
|
||||
conn.initiate_response(
|
||||
200,
|
||||
Some("OK"),
|
||||
&[
|
||||
("Access-Control-Allow-Origin", "*"),
|
||||
("Access-Control-Allow-Headers", "*"),
|
||||
("Access-Control-Allow-Methods", "*"),
|
||||
],
|
||||
)
|
||||
.await?;
|
||||
Some(200)
|
||||
}
|
||||
Method::Post => {
|
||||
let mut offset = 0_usize;
|
||||
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;
|
||||
} 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)
|
||||
board
|
||||
.board_hal
|
||||
.get_esp()
|
||||
.write_ota(offset as u32, &buf)
|
||||
.await?;
|
||||
}
|
||||
offset += buf.len();
|
||||
chunk += 1;
|
||||
}
|
||||
BOARD_ACCESS
|
||||
.get()
|
||||
.await
|
||||
.lock()
|
||||
.await
|
||||
.board_hal
|
||||
.clear_progress()
|
||||
.await;
|
||||
conn.initiate_response(
|
||||
200,
|
||||
Some("OK"),
|
||||
&[
|
||||
("Access-Control-Allow-Origin", "*"),
|
||||
("Access-Control-Allow-Headers", "*"),
|
||||
("Access-Control-Allow-Methods", "*"),
|
||||
],
|
||||
)
|
||||
.await?;
|
||||
Some(200)
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
struct HTTPRequestRouter {
|
||||
reboot_now: Arc<AtomicBool>,
|
||||
@@ -105,7 +126,12 @@ impl Handler for HTTPRequestRouter {
|
||||
let path = headers.path;
|
||||
|
||||
let prefix = "/file?filename=";
|
||||
let status = if path.starts_with(prefix) {
|
||||
let status = if path == "/ota" {
|
||||
ota_operations(conn, method).await.map_err(|e| {
|
||||
error!("Error handling ota: {e}");
|
||||
e
|
||||
})?
|
||||
} else if path.starts_with(prefix) {
|
||||
file_operations(conn, method, &path, &prefix).await?
|
||||
} else {
|
||||
match method {
|
||||
@@ -195,12 +221,18 @@ where
|
||||
let mut data_store = Vec::new();
|
||||
let mut total_read = 0;
|
||||
loop {
|
||||
let left = max_read - total_read;
|
||||
let mut buf = [0_u8; 64];
|
||||
let read = request.read(&mut buf).await?;
|
||||
let s_buf = if buf.len() <= left {
|
||||
&mut buf
|
||||
} else {
|
||||
&mut buf[0..left]
|
||||
};
|
||||
let read = request.read(s_buf).await?;
|
||||
if read == 0 {
|
||||
break;
|
||||
}
|
||||
let actual_data = &buf[0..read];
|
||||
let actual_data = &s_buf[0..read];
|
||||
total_read += read;
|
||||
if total_read > max_read {
|
||||
bail!("Request too large {total_read} > {max_read}");
|
||||
|
||||
@@ -157,7 +157,13 @@ export interface Moistures {
|
||||
export interface VersionInfo {
|
||||
git_hash: string,
|
||||
build_time: string,
|
||||
partition: string
|
||||
current: string,
|
||||
slot0_state: string,
|
||||
slot1_state: string,
|
||||
heap_total: number,
|
||||
heap_used: number,
|
||||
heap_free: number,
|
||||
heap_max_used: number,
|
||||
}
|
||||
|
||||
export interface BatteryState {
|
||||
@@ -189,4 +195,4 @@ export interface TankInfo {
|
||||
/// water temperature
|
||||
water_temp: number | null,
|
||||
temp_sensor_error: string | null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ import {
|
||||
FileList, SolarState, PumpTestResult
|
||||
} from "./api";
|
||||
import {SolarView} from "./solarview";
|
||||
import {toast} from "./toast";
|
||||
|
||||
export class Controller {
|
||||
loadTankInfo(): Promise<void> {
|
||||
@@ -200,15 +201,22 @@ export class Controller {
|
||||
}, false);
|
||||
ajax.addEventListener("load", () => {
|
||||
controller.progressview.removeProgress("ota_upload")
|
||||
controller.reboot();
|
||||
const status = ajax.status;
|
||||
if (status >= 200 && status < 300) {
|
||||
controller.reboot();
|
||||
} else {
|
||||
const statusText = ajax.statusText || "";
|
||||
const body = ajax.responseText || "";
|
||||
toast.error(`OTA update error (${status}${statusText ? ' ' + statusText : ''}): ${body}`);
|
||||
}
|
||||
}, false);
|
||||
ajax.addEventListener("error", () => {
|
||||
alert("Error ota")
|
||||
controller.progressview.removeProgress("ota_upload")
|
||||
toast.error("OTA upload failed due to a network error.");
|
||||
}, false);
|
||||
ajax.addEventListener("abort", () => {
|
||||
alert("abort ota")
|
||||
controller.progressview.removeProgress("ota_upload")
|
||||
toast.error("OTA upload was aborted.");
|
||||
}, false);
|
||||
ajax.open("POST", PUBLIC_URL + "/ota");
|
||||
ajax.send(file);
|
||||
@@ -569,4 +577,4 @@ window.addEventListener("beforeunload", (event) => {
|
||||
event.returnValue = confirmationMessage; // This will trigger the browser's default dialog
|
||||
return confirmationMessage;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,23 +1,27 @@
|
||||
<style>
|
||||
.otakey{
|
||||
min-width: 100px;
|
||||
}
|
||||
.otavalue{
|
||||
flex-grow: 1;
|
||||
}
|
||||
.otaform {
|
||||
min-width: 100px;
|
||||
flex-grow: 1;
|
||||
}
|
||||
.otachooser {
|
||||
min-width: 100px;
|
||||
width: 100%;
|
||||
}
|
||||
.otakey {
|
||||
min-width: 100px;
|
||||
}
|
||||
|
||||
.otavalue {
|
||||
flex-grow: 1;
|
||||
}
|
||||
|
||||
.otaform {
|
||||
min-width: 100px;
|
||||
flex-grow: 1;
|
||||
}
|
||||
|
||||
.otachooser {
|
||||
min-width: 100px;
|
||||
width: 100%;
|
||||
}
|
||||
</style>
|
||||
<div class="flexcontainer">
|
||||
<div class="subtitle">
|
||||
Current Firmware
|
||||
</div>
|
||||
<button style="margin-left: auto;" type="button" id="refresh_firmware_info">Refresh</button>
|
||||
</div>
|
||||
<div class="flexcontainer">
|
||||
<span class="otakey">Buildtime:</span>
|
||||
@@ -28,14 +32,46 @@
|
||||
<span class="otavalue" id="firmware_githash"></span>
|
||||
</div>
|
||||
<div class="flexcontainer">
|
||||
<span class="otakey">Partition:</span>
|
||||
<span class="otavalue" id="firmware_partition"></span>
|
||||
<span class="otakey">Partition:</span>
|
||||
<span class="otavalue" id="firmware_partition"></span>
|
||||
</div>
|
||||
<div class="flexcontainer">
|
||||
<span class="otakey">State0:</span>
|
||||
<span class="otavalue" id="firmware_state0"></span>
|
||||
</div>
|
||||
<div class="flexcontainer">
|
||||
<span class="otakey">State1:</span>
|
||||
<span class="otavalue" id="firmware_state1"></span>
|
||||
</div>
|
||||
<div class="flexcontainer">
|
||||
<form class="otaform" id="upload_form" method="post">
|
||||
<input class="otachooser" type="file" name="file1" id="firmware_file"><br>
|
||||
</form>
|
||||
</div>
|
||||
<div class="flexcontainer">
|
||||
<div class="subtitle">
|
||||
Heap Memory
|
||||
</div>
|
||||
<div></div>
|
||||
</div>
|
||||
<div class="flexcontainer">
|
||||
<span class="otakey">Free:</span>
|
||||
<span class="otavalue" id="heap_free"></span>
|
||||
</div>
|
||||
<div class="flexcontainer">
|
||||
<span class="otakey">Used:</span>
|
||||
<span class="otavalue" id="heap_used"></span>
|
||||
</div>
|
||||
<div class="flexcontainer">
|
||||
<span class="otakey">Total:</span>
|
||||
<span class="otavalue" id="heap_total"></span>
|
||||
</div>
|
||||
<div class="flexcontainer">
|
||||
<span class="otakey">Peak used:</span>
|
||||
<span class="otavalue" id="heap_max_used"></span>
|
||||
</div>
|
||||
|
||||
|
||||
<div class="display:flex">
|
||||
<button style="margin-left: 16px; margin-top: 8px;" class="col-6" type="button" id="test">Self-Test</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,22 +1,38 @@
|
||||
import { Controller } from "./main";
|
||||
import {Controller} from "./main";
|
||||
import {VersionInfo} from "./api";
|
||||
|
||||
function fmtBytes(n: number): string {
|
||||
return `${n} B (${(n / 1024).toFixed(1)} KiB)`;
|
||||
}
|
||||
|
||||
export class OTAView {
|
||||
readonly file1Upload: HTMLInputElement;
|
||||
readonly firmware_buildtime: HTMLDivElement;
|
||||
readonly firmware_githash: HTMLDivElement;
|
||||
readonly firmware_partition: HTMLDivElement;
|
||||
readonly firmware_state0: HTMLDivElement;
|
||||
readonly firmware_state1: HTMLDivElement;
|
||||
readonly heap_free: HTMLDivElement;
|
||||
readonly heap_used: HTMLDivElement;
|
||||
readonly heap_total: HTMLDivElement;
|
||||
readonly heap_max_used: HTMLDivElement;
|
||||
|
||||
constructor(controller: Controller) {
|
||||
(document.getElementById("firmwareview") as HTMLElement).innerHTML = require("./ota.html")
|
||||
|
||||
let test = document.getElementById("test") as HTMLButtonElement;
|
||||
let test = document.getElementById("test") as HTMLButtonElement;
|
||||
let refresh = document.getElementById("refresh_firmware_info") as HTMLButtonElement;
|
||||
|
||||
this.firmware_buildtime = document.getElementById("firmware_buildtime") as HTMLDivElement;
|
||||
this.firmware_githash = document.getElementById("firmware_githash") as HTMLDivElement;
|
||||
this.firmware_partition = document.getElementById("firmware_partition") as HTMLDivElement;
|
||||
this.firmware_state0 = document.getElementById("firmware_state0") as HTMLDivElement;
|
||||
this.firmware_state1 = document.getElementById("firmware_state1") as HTMLDivElement;
|
||||
this.heap_free = document.getElementById("heap_free") as HTMLDivElement;
|
||||
this.heap_used = document.getElementById("heap_used") as HTMLDivElement;
|
||||
this.heap_total = document.getElementById("heap_total") as HTMLDivElement;
|
||||
this.heap_max_used = document.getElementById("heap_max_used") as HTMLDivElement;
|
||||
|
||||
|
||||
const file = document.getElementById("firmware_file") as HTMLInputElement;
|
||||
this.file1Upload = file
|
||||
this.file1Upload.onchange = () => {
|
||||
@@ -31,11 +47,21 @@ export class OTAView {
|
||||
test.onclick = () => {
|
||||
controller.selfTest();
|
||||
}
|
||||
|
||||
refresh.onclick = () => {
|
||||
controller.version();
|
||||
}
|
||||
}
|
||||
|
||||
setVersion(versionInfo: VersionInfo) {
|
||||
this.firmware_buildtime.innerText = versionInfo.build_time;
|
||||
this.firmware_githash.innerText = versionInfo.git_hash;
|
||||
this.firmware_partition.innerText = versionInfo.partition;
|
||||
this.firmware_partition.innerText = versionInfo.current;
|
||||
this.firmware_state0.innerText = versionInfo.slot0_state;
|
||||
this.firmware_state1.innerText = versionInfo.slot1_state;
|
||||
this.heap_free.innerText = fmtBytes(versionInfo.heap_free);
|
||||
this.heap_used.innerText = fmtBytes(versionInfo.heap_used);
|
||||
this.heap_total.innerText = fmtBytes(versionInfo.heap_total);
|
||||
this.heap_max_used.innerText = fmtBytes(versionInfo.heap_max_used);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
94
rust/src_webpack/src/toast.ts
Normal file
94
rust/src_webpack/src/toast.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
class ToastService {
|
||||
private container: HTMLElement;
|
||||
private stylesInjected = false;
|
||||
|
||||
constructor() {
|
||||
this.container = this.ensureContainer();
|
||||
this.injectStyles();
|
||||
}
|
||||
|
||||
info(message: string, timeoutMs: number = 5000) {
|
||||
const el = this.createToast(message, 'info');
|
||||
this.container.appendChild(el);
|
||||
// Auto-dismiss after timeout
|
||||
const timer = window.setTimeout(() => this.dismiss(el), timeoutMs);
|
||||
// Dismiss on click immediately
|
||||
el.addEventListener('click', () => {
|
||||
window.clearTimeout(timer);
|
||||
this.dismiss(el);
|
||||
});
|
||||
}
|
||||
|
||||
error(message: string) {
|
||||
console.error(message);
|
||||
const el = this.createToast(message, 'error');
|
||||
this.container.appendChild(el);
|
||||
// Only dismiss on click
|
||||
el.addEventListener('click', () => this.dismiss(el));
|
||||
}
|
||||
|
||||
private dismiss(el: HTMLElement) {
|
||||
if (!el.parentElement) return;
|
||||
el.parentElement.removeChild(el);
|
||||
}
|
||||
|
||||
private createToast(message: string, type: 'info' | 'error'): HTMLElement {
|
||||
const div = document.createElement('div');
|
||||
div.className = `toast ${type}`;
|
||||
div.textContent = message;
|
||||
div.setAttribute('role', 'status');
|
||||
div.setAttribute('aria-live', 'polite');
|
||||
return div;
|
||||
}
|
||||
|
||||
private ensureContainer(): HTMLElement {
|
||||
let container = document.getElementById('toast-container');
|
||||
if (!container) {
|
||||
container = document.createElement('div');
|
||||
container.id = 'toast-container';
|
||||
document.body.appendChild(container);
|
||||
}
|
||||
return container;
|
||||
}
|
||||
|
||||
private injectStyles() {
|
||||
if (this.stylesInjected) return;
|
||||
const style = document.createElement('style');
|
||||
style.textContent = `
|
||||
#toast-container {
|
||||
position: fixed;
|
||||
top: 12px;
|
||||
right: 12px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
z-index: 9999;
|
||||
}
|
||||
.toast {
|
||||
max-width: 320px;
|
||||
padding: 10px 12px;
|
||||
border-radius: 6px;
|
||||
box-shadow: 0 2px 6px rgba(0,0,0,0.2);
|
||||
cursor: pointer;
|
||||
user-select: none;
|
||||
font-family: sans-serif;
|
||||
font-size: 14px;
|
||||
line-height: 1.3;
|
||||
}
|
||||
.toast.info {
|
||||
background-color: #d4edda; /* green-ish */
|
||||
color: #155724;
|
||||
border-left: 4px solid #28a745;
|
||||
}
|
||||
.toast.error {
|
||||
background-color: #f8d7da; /* red-ish */
|
||||
color: #721c24;
|
||||
border-left: 4px solid #dc3545;
|
||||
}
|
||||
`;
|
||||
document.head.appendChild(style);
|
||||
this.stylesInjected = true;
|
||||
}
|
||||
}
|
||||
|
||||
export const toast = new ToastService();
|
||||
Reference in New Issue
Block a user