148 lines
4.2 KiB
Rust
148 lines
4.2 KiB
Rust
use crate::hal::esp::Esp;
|
|
use crate::hal::rtc::{BackupHeader, RTCModuleInteraction};
|
|
use alloc::vec::Vec;
|
|
//use crate::hal::water::TankSensor;
|
|
use crate::alloc::boxed::Box;
|
|
use crate::hal::{deep_sleep, BoardInteraction, FreePeripherals, Sensor};
|
|
use crate::{
|
|
config::PlantControllerConfig,
|
|
hal::battery::{BatteryInteraction, NoBatteryMonitor},
|
|
};
|
|
use anyhow::{bail, Result};
|
|
use async_trait::async_trait;
|
|
use chrono::{DateTime, Utc};
|
|
use measurements::{Current, Voltage};
|
|
|
|
pub struct Initial<'a> {
|
|
//pub(crate) general_fault: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>,
|
|
pub(crate) esp: Esp<'a>,
|
|
pub(crate) config: PlantControllerConfig,
|
|
pub(crate) battery: Box<dyn BatteryInteraction + Send>,
|
|
pub rtc: Box<dyn RTCModuleInteraction + Send>,
|
|
}
|
|
|
|
struct NoRTC {}
|
|
|
|
#[async_trait]
|
|
impl RTCModuleInteraction for NoRTC {
|
|
async fn get_backup_info(&mut self) -> Result<BackupHeader> {
|
|
bail!("Please configure board revision")
|
|
}
|
|
|
|
async fn get_backup_config(&mut self) -> Result<Vec<u8>> {
|
|
bail!("Please configure board revision")
|
|
}
|
|
|
|
async fn backup_config(&mut self, _bytes: &[u8]) -> Result<()> {
|
|
bail!("Please configure board revision")
|
|
}
|
|
|
|
async fn get_rtc_time(&mut self) -> Result<DateTime<Utc>> {
|
|
bail!("Please configure board revision")
|
|
}
|
|
|
|
async fn set_rtc_time(&mut self, _time: &DateTime<Utc>) -> Result<()> {
|
|
bail!("Please configure board revision")
|
|
}
|
|
}
|
|
|
|
pub(crate) fn create_initial_board(
|
|
//free_pins: FreePeripherals,
|
|
_fs_mount_error: bool,
|
|
config: PlantControllerConfig,
|
|
esp: Esp<'static>,
|
|
) -> Result<Box<dyn BoardInteraction<'static> + Send>> {
|
|
log::info!("Start initial");
|
|
// let mut general_fault = PinDriver::input_output(free_pins.gpio6.downgrade())?;
|
|
// general_fault.set_pull(Pull::Floating)?;
|
|
// general_fault.set_low()?;
|
|
//
|
|
// if fs_mount_error {
|
|
// general_fault.set_high()?
|
|
// }
|
|
let v = Initial {
|
|
//general_fault,
|
|
config,
|
|
esp,
|
|
battery: Box::new(NoBatteryMonitor {}),
|
|
rtc: Box::new(NoRTC {}),
|
|
};
|
|
Ok(Box::new(v))
|
|
}
|
|
|
|
#[async_trait]
|
|
impl<'a> BoardInteraction<'a> for Initial<'a> {
|
|
// fn get_tank_sensor(&mut self) -> Option<&mut TankSensor<'a>> {
|
|
// None
|
|
// }
|
|
|
|
fn get_esp(&mut self) -> &mut Esp<'a> {
|
|
&mut self.esp
|
|
}
|
|
|
|
fn get_config(&mut self) -> &PlantControllerConfig {
|
|
&self.config
|
|
}
|
|
|
|
fn get_battery_monitor(&mut self) -> &mut Box<dyn BatteryInteraction + Send> {
|
|
&mut self.battery
|
|
}
|
|
|
|
fn get_rtc_module(&mut self) -> &mut Box<dyn RTCModuleInteraction + Send> {
|
|
&mut self.rtc
|
|
}
|
|
|
|
fn set_charge_indicator(&mut self, _charging: bool) -> Result<()> {
|
|
bail!("Please configure board revision")
|
|
}
|
|
|
|
fn deep_sleep(&mut self, duration_in_ms: u64) -> ! {
|
|
deep_sleep(duration_in_ms)
|
|
}
|
|
fn is_day(&self) -> bool {
|
|
false
|
|
}
|
|
fn light(&mut self, _enable: bool) -> Result<()> {
|
|
bail!("Please configure board revision")
|
|
}
|
|
|
|
async fn pump(&mut self, _plant: usize, _enable: bool) -> Result<()> {
|
|
bail!("Please configure board revision")
|
|
}
|
|
|
|
async fn pump_current(&mut self, _plant: usize) -> Result<Current> {
|
|
bail!("Please configure board revision")
|
|
}
|
|
|
|
async fn fault(&mut self, _plant: usize, _enable: bool) -> Result<()> {
|
|
bail!("Please configure board revision")
|
|
}
|
|
|
|
async fn measure_moisture_hz(&mut self, _plant: usize, _sensor: Sensor) -> Result<f32> {
|
|
bail!("Please configure board revision")
|
|
}
|
|
|
|
async fn general_fault(&mut self, _enable: bool) {
|
|
//let _ = self.general_fault.set_state(enable.into());
|
|
}
|
|
|
|
async fn test(&mut self) -> Result<()> {
|
|
bail!("Please configure board revision")
|
|
}
|
|
|
|
async fn set_config(&mut self, config: PlantControllerConfig) -> anyhow::Result<()> {
|
|
self.config = config;
|
|
//TODO
|
|
// self.esp.save_config(&self.config)?;
|
|
anyhow::Ok(())
|
|
}
|
|
|
|
async fn get_mptt_voltage(&mut self) -> Result<Voltage> {
|
|
bail!("Please configure board revision")
|
|
}
|
|
|
|
async fn get_mptt_current(&mut self) -> Result<Current> {
|
|
bail!("Please configure board revision")
|
|
}
|
|
}
|