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

@@ -293,7 +293,7 @@ fn safe_main() -> anyhow::Result<()> {
board.general_fault(true);
}
}
if (config.network.mqtt_url.is_some()){
if (config.network.mqtt_url.is_some()) {
match board.mqtt(&config) {
Ok(_) => {
println!("Mqtt connection ready");
@@ -319,8 +319,11 @@ fn safe_main() -> anyhow::Result<()> {
match board.wifi_ap(Some(config.network.ap_ssid.clone())) {
Ok(_) => {
println!("Started ap, continuing")
},
Err(err) => println!("Could not start config override ap mode due to {}", err.to_string()),
}
Err(err) => println!(
"Could not start config override ap mode due to {}",
err.to_string()
),
}
}

View File

@@ -515,7 +515,8 @@ impl PlantCtrlBoard<'_> {
}
pub fn wifi_ap(&mut self, ap_ssid: Option<heapless::String<32>>) -> Result<()> {
let ssid = ap_ssid.unwrap_or(heapless::String::from_str("PlantCtrl Emergency Mode").unwrap());
let ssid =
ap_ssid.unwrap_or(heapless::String::from_str("PlantCtrl Emergency Mode").unwrap());
let apconfig = AccessPointConfiguration {
ssid,
auth_method: AuthMethod::None,
@@ -644,7 +645,6 @@ impl PlantCtrlBoard<'_> {
}
pub fn get_rtc_time(&mut self) -> Result<DateTime<Utc>> {
match self.rtc.datetime() {
OkStd(rtc_time) => {
return Ok(rtc_time.and_utc());
@@ -757,7 +757,6 @@ impl PlantCtrlBoard<'_> {
bail!("Mqtt url was empty")
}
let last_will_topic = format!("{}/state", base_topic);
let mqtt_client_config = MqttClientConfiguration {
lwt: Some(LwtConfiguration {
@@ -835,20 +834,19 @@ impl PlantCtrlBoard<'_> {
}
esp_idf_svc::mqtt::client::EventPayload::BeforeConnect => {
println!("Mqtt before connect")
},
}
esp_idf_svc::mqtt::client::EventPayload::Subscribed(_) => {
println!("Mqtt subscribed")
},
}
esp_idf_svc::mqtt::client::EventPayload::Unsubscribed(_) => {
println!("Mqtt unsubscribed")
},
}
esp_idf_svc::mqtt::client::EventPayload::Published(_) => {
println!("Mqtt published")
},
}
esp_idf_svc::mqtt::client::EventPayload::Deleted(_) => {
println!("Mqtt deleted")
},
}
}
})?;

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()?;