extract rtc module, extract tank module, fix backupview refresh, switch to embedded storage for eeprom

This commit is contained in:
2025-06-25 01:18:36 +02:00
parent 5fb8705d9a
commit 6b711e29fc
14 changed files with 851 additions and 869 deletions

View File

@@ -3,7 +3,7 @@ use crate::{
hal::{PlantHal, HAL, PLANT_COUNT},
webserver::httpd,
};
use anyhow::bail;
use anyhow::{bail, Context};
use chrono::{DateTime, Datelike, Timelike, Utc};
use chrono_tz::Tz::{self, UTC};
use esp_idf_hal::delay::Delay;
@@ -40,6 +40,12 @@ enum WaitType {
MqttConfig,
}
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Solar {
current_ma: u32,
voltage_ma: u32,
}
impl WaitType {
fn blink_pattern(&self) -> u32 {
match self {
@@ -258,6 +264,7 @@ fn safe_main() -> anyhow::Result<()> {
timezone_time,
);
publish_battery_state(&mut board);
let _ = publish_mppt_state(&mut board);
}
log(
@@ -326,8 +333,12 @@ fn safe_main() -> anyhow::Result<()> {
}
let mut water_frozen = false;
let water_temp = board
.board_hal
.get_tank_sensor()
.context("no sensor")
.and_then(|f| f.water_temperature_c());
let water_temp = obtain_tank_temperature(&mut board);
if let Ok(res) = water_temp {
if res < WATER_FROZEN_THRESH {
water_frozen = true;
@@ -565,28 +576,6 @@ fn update_charge_indicator(board: &mut MutexGuard<HAL>) {
}
}
fn obtain_tank_temperature(board: &mut MutexGuard<HAL>) -> anyhow::Result<f32> {
//multisample should be moved to water_temperature_c
let mut attempt = 1;
let water_temp: Result<f32, anyhow::Error> = loop {
let temp = board.board_hal.water_temperature_c();
match &temp {
Ok(res) => {
println!("Water temp is {}", res);
break temp;
}
Err(err) => {
println!("Could not get water temp {} attempt {}", err, attempt)
}
}
if attempt == 5 {
break temp;
}
attempt += 1;
};
water_temp
}
fn publish_tank_state(
board: &mut MutexGuard<HAL>,
tank_state: &TankState,
@@ -743,6 +732,24 @@ fn pump_info(
};
}
fn publish_mppt_state(board: &mut MutexGuard<'_, HAL<'_>>) -> anyhow::Result<()> {
let current = board.board_hal.get_mptt_current()?;
let voltage = board.board_hal.get_mptt_voltage()?;
let solar_state = Solar {
current_ma: current.as_milliamperes() as u32,
voltage_ma: voltage.as_millivolts() as u32,
};
if let Ok(serialized_solar_state_bytes) =
serde_json::to_string(&solar_state).map(|s| s.into_bytes())
{
let _ = board
.board_hal
.get_esp()
.mqtt_publish("/mppt", &serialized_solar_state_bytes);
}
Ok(())
}
fn publish_battery_state(board: &mut MutexGuard<'_, HAL<'_>>) {
let state = board.board_hal.get_battery_monitor().get_battery_state();
if let Ok(serialized_battery_state_bytes) =