From 53bd1f8ccedd8e06454b4795ad88c62ecd881901 Mon Sep 17 00:00:00 2001 From: ju6ge Date: Thu, 20 Mar 2025 22:39:17 +0100 Subject: [PATCH] tank information web server api endpoint --- rust/src/webserver/webserver.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/rust/src/webserver/webserver.rs b/rust/src/webserver/webserver.rs index f4d5d10..625c8f0 100644 --- a/rust/src/webserver/webserver.rs +++ b/rust/src/webserver/webserver.rs @@ -1,7 +1,8 @@ //offer ota and config mode use crate::{ - get_version, log::LogMessage, map_range_moisture, plant_hal::PLANT_COUNT, BOARD_ACCESS, + determine_tank_state, get_version, log::LogMessage, map_range_moisture, plant_hal::PLANT_COUNT, + BOARD_ACCESS, }; use anyhow::bail; use chrono::DateTime; @@ -240,6 +241,18 @@ fn pump_test( anyhow::Ok(None) } +fn tank_info( + _request: &mut Request<&mut EspHttpConnection>, +) -> Result, anyhow::Error> { + let mut board = BOARD_ACCESS.lock().unwrap(); + let config = board.get_config()?; + let tank_info = determine_tank_state(&mut board, &config); + let water_temp = board.water_temperature_c(); + Ok(Some(serde_json::to_string( + &tank_info.as_mqtt_info(&config.tank, water_temp), + )?)) +} + fn night_lamp_test( request: &mut Request<&mut EspHttpConnection>, ) -> Result, anyhow::Error> { @@ -407,6 +420,11 @@ pub fn httpd(reboot_now: Arc) -> Box> { handle_error_to500(request, write_time) }) .unwrap(); + server + .fn_handler("/tank", Method::Get, |request| { + handle_error_to500(request, tank_info) + }) + .unwrap(); server .fn_handler("/pumptest", Method::Post, |request| { handle_error_to500(request, pump_test)