keep ota around for alter queries to it
This commit is contained in:
@@ -1,36 +1,22 @@
|
||||
//offer ota and config mode
|
||||
|
||||
use crate::{
|
||||
config::PlantControllerConfig,
|
||||
do_secure_pump, get_version,
|
||||
hal::PLANT_COUNT,
|
||||
log::LogMessage,
|
||||
plant_state::{MoistureSensorState, PlantState},
|
||||
VersionInfo, BOARD_ACCESS,
|
||||
};
|
||||
use alloc::boxed::Box;
|
||||
use crate::{get_version, log::LogMessage, BOARD_ACCESS};
|
||||
use alloc::string::{String, ToString};
|
||||
use alloc::sync::Arc;
|
||||
use alloc::vec::Vec;
|
||||
use anyhow::{bail, Context};
|
||||
use chrono::DateTime;
|
||||
use core::fmt::{Debug, Display, Pointer};
|
||||
use core::future::Future;
|
||||
use core::fmt::{Debug, Display};
|
||||
use core::net::{IpAddr, Ipv4Addr, SocketAddr};
|
||||
use core::result::Result::Ok;
|
||||
use core::sync::atomic::AtomicBool;
|
||||
use edge_http::io::server::{Connection, DefaultServer, Handler, Server};
|
||||
use core::sync::atomic::{AtomicBool, Ordering};
|
||||
use edge_http::io::server::{Connection, Handler, Server};
|
||||
use edge_http::io::Error;
|
||||
use edge_http::Method;
|
||||
use edge_nal::TcpBind;
|
||||
use edge_nal_embassy::{Tcp, TcpAccept, TcpBuffers};
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_net::tcp::TcpSocket;
|
||||
use embassy_net::{IpListenEndpoint, Stack};
|
||||
use embassy_time::{Duration, Instant, Timer};
|
||||
use edge_nal_embassy::{Tcp, TcpBuffers};
|
||||
use embassy_net::Stack;
|
||||
use embassy_time::Instant;
|
||||
use embedded_io_async::{Read, Write};
|
||||
use esp_println::{print, println};
|
||||
use esp_wifi::wifi::WifiController;
|
||||
use esp_println::println;
|
||||
use log::info;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -148,13 +134,6 @@ pub struct NightLampCommand {
|
||||
// anyhow::Ok(Some(json))
|
||||
// }
|
||||
//
|
||||
// fn get_config(
|
||||
// _request: &mut Request<&mut EspHttpConnection>,
|
||||
// ) -> Result<Option<std::string::String>, anyhow::Error> {
|
||||
// let mut board = BOARD_ACCESS.lock().expect("Should never fail");
|
||||
// let json = serde_json::to_string(&board.board_hal.get_config())?;
|
||||
// anyhow::Ok(Some(json))
|
||||
// }
|
||||
//
|
||||
// fn backup_config(
|
||||
// request: &mut Request<&mut EspHttpConnection>,
|
||||
@@ -218,23 +197,6 @@ pub struct NightLampCommand {
|
||||
// }
|
||||
//
|
||||
|
||||
//
|
||||
|
||||
//
|
||||
// fn get_log(
|
||||
// _request: &mut Request<&mut EspHttpConnection>,
|
||||
// ) -> Result<Option<std::string::String>, anyhow::Error> {
|
||||
// let output = crate::log::get_log();
|
||||
// anyhow::Ok(Some(serde_json::to_string(&output)?))
|
||||
// }
|
||||
//
|
||||
// fn get_log_localization_config() -> Result<std::string::String, anyhow::Error> {
|
||||
// anyhow::Ok(serde_json::to_string(
|
||||
// &LogMessage::to_log_localisation_config(),
|
||||
// )?)
|
||||
// }
|
||||
//
|
||||
|
||||
//
|
||||
// fn pump_test(
|
||||
// request: &mut Request<&mut EspHttpConnection>,
|
||||
@@ -347,7 +309,9 @@ pub struct NightLampCommand {
|
||||
// }
|
||||
// }
|
||||
|
||||
struct HttpHandler;
|
||||
struct HttpHandler {
|
||||
reboot_now: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
impl Handler for HttpHandler {
|
||||
type Error<E>
|
||||
@@ -389,6 +353,12 @@ impl Handler for HttpHandler {
|
||||
conn.write_all(include_bytes!("bundle.js")).await?;
|
||||
Some(200)
|
||||
}
|
||||
"/reboot" => {
|
||||
let mut board = BOARD_ACCESS.get().await.lock().await;
|
||||
board.board_hal.get_esp().set_restart_to_conf(true);
|
||||
self.reboot_now.store(true, Ordering::Relaxed);
|
||||
Some(200)
|
||||
}
|
||||
&_ => {
|
||||
let json = match path {
|
||||
"/version" => Some(get_version_web(conn).await),
|
||||
@@ -398,6 +368,7 @@ impl Handler for HttpHandler {
|
||||
"/get_config" => Some(get_config(conn).await),
|
||||
"/files" => Some(list_files(conn).await),
|
||||
"/log_localization" => Some(get_log_localization_config(conn).await),
|
||||
"/log" => Some(get_log(conn).await),
|
||||
_ => None,
|
||||
};
|
||||
match json {
|
||||
@@ -425,6 +396,23 @@ impl Handler for HttpHandler {
|
||||
}
|
||||
}
|
||||
|
||||
// .fn_handler("/reboot", Method::Post, move |_| {
|
||||
// BOARD_ACCESS
|
||||
// .lock()
|
||||
// .unwrap()
|
||||
// .board_hal
|
||||
// .get_esp()
|
||||
// .set_restart_to_conf(true);
|
||||
// reboot_now_for_reboot.store(true, std::sync::atomic::Ordering::Relaxed);
|
||||
// anyhow::Ok(())
|
||||
|
||||
async fn get_log<T, const N: usize>(
|
||||
_request: &mut Connection<'_, T, N>,
|
||||
) -> Result<Option<String>, anyhow::Error> {
|
||||
let output = crate::log::get_log().await;
|
||||
anyhow::Ok(Some(serde_json::to_string(&output)?))
|
||||
}
|
||||
|
||||
async fn get_log_localization_config<T, const N: usize>(
|
||||
_request: &mut Connection<'_, T, N>,
|
||||
) -> Result<Option<String>, anyhow::Error> {
|
||||
@@ -512,7 +500,7 @@ pub async fn httpd(reboot_now: Arc<AtomicBool>, stack: Stack<'static>) {
|
||||
|
||||
let mut server: Server<2, 512, 10> = Server::new();
|
||||
server
|
||||
.run(Some(5000), acceptor, HttpHandler)
|
||||
.run(Some(5000), acceptor, HttpHandler { reboot_now })
|
||||
.await
|
||||
.expect("TODO: panic message");
|
||||
println!("Wait for connection...");
|
||||
@@ -790,47 +778,6 @@ pub async fn httpd(reboot_now: Arc<AtomicBool>, stack: Stack<'static>) {
|
||||
//server
|
||||
}
|
||||
//
|
||||
// fn cors_response(
|
||||
// request: Request<&mut EspHttpConnection>,
|
||||
// status: u16,
|
||||
// body: &str,
|
||||
// ) -> Result<(), anyhow::Error> {
|
||||
// let headers = [
|
||||
// ("Access-Control-Allow-Origin", "*"),
|
||||
// ("Access-Control-Allow-Headers", "*"),
|
||||
// ("Access-Control-Allow-Methods", "*"),
|
||||
// ];
|
||||
// let mut response = request.into_response(status, None, &headers)?;
|
||||
// response.write(body.as_bytes())?;
|
||||
// response.flush()?;
|
||||
// anyhow::Ok(())
|
||||
// }
|
||||
//
|
||||
// type AnyhowHandler =
|
||||
// fn(&mut Request<&mut EspHttpConnection>) -> Result<Option<std::string::String>, anyhow::Error>;
|
||||
// fn handle_error_to500(
|
||||
// mut request: Request<&mut EspHttpConnection>,
|
||||
// chain: AnyhowHandler,
|
||||
// ) -> Result<(), anyhow::Error> {
|
||||
// let error = chain(&mut request);
|
||||
// match error {
|
||||
// Ok(answer) => match answer {
|
||||
// Some(json) => {
|
||||
// cors_response(request, 200, &json)?;
|
||||
// }
|
||||
// None => {
|
||||
// cors_response(request, 200, "")?;
|
||||
// }
|
||||
// },
|
||||
// Err(err) => {
|
||||
// let error_text = err.to_string();
|
||||
// log::info!("error handling process {}", error_text);
|
||||
// cors_response(request, 500, &error_text)?;
|
||||
// }
|
||||
// }
|
||||
// anyhow::Ok(())
|
||||
// }
|
||||
//
|
||||
// fn read_up_to_bytes_from_request(
|
||||
// request: &mut Request<&mut EspHttpConnection<'_>>,
|
||||
// limit: Option<usize>,
|
||||
|
||||
Reference in New Issue
Block a user