canbus sensor stub and pcnt impl
This commit is contained in:
216
rust/src/main.rs
216
rust/src/main.rs
@@ -17,7 +17,7 @@ use crate::config::{NetworkConfig, PlantConfig};
|
||||
use crate::fat_error::FatResult;
|
||||
use crate::hal::esp::MQTT_STAY_ALIVE;
|
||||
use crate::hal::{esp_time, TIME_ACCESS};
|
||||
use crate::log::LOG_ACCESS;
|
||||
use crate::log::{log, LOG_ACCESS};
|
||||
use crate::tank::{determine_tank_state, TankError, TankState, WATER_FROZEN_THRESH};
|
||||
use crate::webserver::http_server;
|
||||
use crate::{
|
||||
@@ -326,7 +326,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
.await;
|
||||
}
|
||||
|
||||
let _dry_run = false;
|
||||
let dry_run = false;
|
||||
|
||||
let tank_state = determine_tank_state(&mut board).await;
|
||||
|
||||
@@ -383,7 +383,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
}
|
||||
}
|
||||
|
||||
let mut _water_frozen = false;
|
||||
let mut water_frozen = false;
|
||||
let water_temp: FatResult<f32> = match board.board_hal.get_tank_sensor() {
|
||||
Ok(sensor) => sensor.water_temperature_c().await,
|
||||
Err(e) => Err(e),
|
||||
@@ -391,7 +391,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
|
||||
if let Ok(res) = water_temp {
|
||||
if res < WATER_FROZEN_THRESH {
|
||||
_water_frozen = true;
|
||||
water_frozen = true;
|
||||
}
|
||||
}
|
||||
info!("Water temp is {}", water_temp.as_ref().unwrap_or(&0.));
|
||||
@@ -411,74 +411,81 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
|
||||
publish_plant_states(&mut board, &timezone_time.clone(), &plantstate).await;
|
||||
|
||||
// let pump_required = plantstate
|
||||
// .iter()
|
||||
// .zip(&board.board_hal.get_config().plants)
|
||||
// .any(|(it, conf)| it.needs_to_be_watered(conf, &timezone_time))
|
||||
// && !water_frozen;
|
||||
// if pump_required {
|
||||
// log(LogMessage::EnableMain, dry_run as u32, 0, "", "");
|
||||
// for (plant_id, (state, plant_config)) in plantstate
|
||||
// .iter()
|
||||
// .zip(&board.board_hal.get_config().plants.clone())
|
||||
// .enumerate()
|
||||
// {
|
||||
// if state.needs_to_be_watered(plant_config, &timezone_time) {
|
||||
// let pump_count = board.board_hal.get_esp().consecutive_pump_count(plant_id) + 1;
|
||||
// board
|
||||
// .board_hal
|
||||
// .get_esp()
|
||||
// .store_consecutive_pump_count(plant_id, pump_count);
|
||||
//
|
||||
// let pump_ineffective = pump_count > plant_config.max_consecutive_pump_count as u32;
|
||||
// if pump_ineffective {
|
||||
// log(
|
||||
// LogMessage::ConsecutivePumpCountLimit,
|
||||
// pump_count,
|
||||
// plant_config.max_consecutive_pump_count as u32,
|
||||
// &(plant_id + 1).to_string(),
|
||||
// "",
|
||||
// );
|
||||
// board.board_hal.fault(plant_id, true).await?;
|
||||
// }
|
||||
// log(
|
||||
// LogMessage::PumpPlant,
|
||||
// (plant_id + 1) as u32,
|
||||
// plant_config.pump_time_s as u32,
|
||||
// &dry_run.to_string(),
|
||||
// "",
|
||||
// );
|
||||
// board
|
||||
// .board_hal
|
||||
// .get_esp()
|
||||
// .store_last_pump_time(plant_id, cur);
|
||||
// board.board_hal.get_esp().last_pump_time(plant_id);
|
||||
// //state.active = true;
|
||||
//
|
||||
// pump_info(plant_id, true, pump_ineffective, 0, 0, 0, false).await;
|
||||
//
|
||||
// let result = do_secure_pump(plant_id, plant_config, dry_run).await?;
|
||||
// board.board_hal.pump(plant_id, false).await?;
|
||||
// pump_info(
|
||||
// plant_id,
|
||||
// false,
|
||||
// pump_ineffective,
|
||||
// result.median_current_ma,
|
||||
// result.max_current_ma,
|
||||
// result.min_current_ma,
|
||||
// result.error,
|
||||
// )
|
||||
// .await;
|
||||
// } else if !state.pump_in_timeout(plant_config, &timezone_time) {
|
||||
// // plant does not need to be watered and is not in timeout
|
||||
// // -> reset consecutive pump count
|
||||
// board
|
||||
// .board_hal
|
||||
// .get_esp()
|
||||
// .store_consecutive_pump_count(plant_id, 0);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
let pump_required = plantstate
|
||||
.iter()
|
||||
.zip(&board.board_hal.get_config().plants)
|
||||
.any(|(it, conf)| it.needs_to_be_watered(conf, &timezone_time))
|
||||
&& !water_frozen;
|
||||
if pump_required {
|
||||
LOG_ACCESS
|
||||
.lock()
|
||||
.await
|
||||
.log(LogMessage::EnableMain, dry_run as u32, 0, "", "")
|
||||
.await;
|
||||
for (plant_id, (state, plant_config)) in plantstate
|
||||
.iter()
|
||||
.zip(&board.board_hal.get_config().plants.clone())
|
||||
.enumerate()
|
||||
{
|
||||
if state.needs_to_be_watered(plant_config, &timezone_time) {
|
||||
let pump_count = board.board_hal.get_esp().consecutive_pump_count(plant_id) + 1;
|
||||
board
|
||||
.board_hal
|
||||
.get_esp()
|
||||
.store_consecutive_pump_count(plant_id, pump_count);
|
||||
|
||||
let pump_ineffective = pump_count > plant_config.max_consecutive_pump_count as u32;
|
||||
if pump_ineffective {
|
||||
log(
|
||||
LogMessage::ConsecutivePumpCountLimit,
|
||||
pump_count,
|
||||
plant_config.max_consecutive_pump_count as u32,
|
||||
&(plant_id + 1).to_string(),
|
||||
"",
|
||||
)
|
||||
.await;
|
||||
board.board_hal.fault(plant_id, true).await?;
|
||||
}
|
||||
log(
|
||||
LogMessage::PumpPlant,
|
||||
(plant_id + 1) as u32,
|
||||
plant_config.pump_time_s as u32,
|
||||
&dry_run.to_string(),
|
||||
"",
|
||||
)
|
||||
.await;
|
||||
board
|
||||
.board_hal
|
||||
.get_esp()
|
||||
.store_last_pump_time(plant_id, cur);
|
||||
board.board_hal.get_esp().last_pump_time(plant_id);
|
||||
//state.active = true;
|
||||
|
||||
pump_info(plant_id, true, pump_ineffective, 0, 0, 0, false).await;
|
||||
|
||||
let result = do_secure_pump(&mut board, plant_id, plant_config, dry_run).await?;
|
||||
//stop pump regardless of prior result//todo refactor to inner?
|
||||
board.board_hal.pump(plant_id, false).await?;
|
||||
pump_info(
|
||||
plant_id,
|
||||
false,
|
||||
pump_ineffective,
|
||||
result.median_current_ma,
|
||||
result.max_current_ma,
|
||||
result.min_current_ma,
|
||||
result.error,
|
||||
)
|
||||
.await;
|
||||
} else if !state.pump_in_timeout(plant_config, &timezone_time) {
|
||||
// plant does not need to be watered and is not in timeout
|
||||
// -> reset consecutive pump count
|
||||
board
|
||||
.board_hal
|
||||
.get_esp()
|
||||
.store_consecutive_pump_count(plant_id, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
info!("state of charg");
|
||||
let is_day = board.board_hal.is_day();
|
||||
@@ -671,17 +678,14 @@ pub async fn do_secure_pump(
|
||||
let high_current = current_ma > plant_config.max_pump_current_ma;
|
||||
if high_current {
|
||||
if first_error {
|
||||
LOG_ACCESS
|
||||
.lock()
|
||||
.await
|
||||
.log(
|
||||
LogMessage::PumpOverCurrent,
|
||||
plant_id as u32 + 1,
|
||||
current_ma as u32,
|
||||
plant_config.max_pump_current_ma.to_string().as_str(),
|
||||
step.to_string().as_str(),
|
||||
)
|
||||
.await;
|
||||
log(
|
||||
LogMessage::PumpOverCurrent,
|
||||
plant_id as u32 + 1,
|
||||
current_ma as u32,
|
||||
plant_config.max_pump_current_ma.to_string().as_str(),
|
||||
step.to_string().as_str(),
|
||||
)
|
||||
.await;
|
||||
board.board_hal.general_fault(true).await;
|
||||
board.board_hal.fault(plant_id, true).await?;
|
||||
if !plant_config.ignore_current_error {
|
||||
@@ -694,17 +698,14 @@ pub async fn do_secure_pump(
|
||||
let low_current = current_ma < plant_config.min_pump_current_ma;
|
||||
if low_current {
|
||||
if first_error {
|
||||
LOG_ACCESS
|
||||
.lock()
|
||||
.await
|
||||
.log(
|
||||
LogMessage::PumpOpenLoopCurrent,
|
||||
plant_id as u32 + 1,
|
||||
current_ma as u32,
|
||||
plant_config.min_pump_current_ma.to_string().as_str(),
|
||||
step.to_string().as_str(),
|
||||
)
|
||||
.await;
|
||||
log(
|
||||
LogMessage::PumpOpenLoopCurrent,
|
||||
plant_id as u32 + 1,
|
||||
current_ma as u32,
|
||||
plant_config.min_pump_current_ma.to_string().as_str(),
|
||||
step.to_string().as_str(),
|
||||
)
|
||||
.await;
|
||||
board.board_hal.general_fault(true).await;
|
||||
board.board_hal.fault(plant_id, true).await?;
|
||||
if !plant_config.ignore_current_error {
|
||||
@@ -718,17 +719,14 @@ pub async fn do_secure_pump(
|
||||
Err(err) => {
|
||||
if !plant_config.ignore_current_error {
|
||||
info!("Error getting pump current: {}", err);
|
||||
LOG_ACCESS
|
||||
.lock()
|
||||
.await
|
||||
.log(
|
||||
LogMessage::PumpMissingSensorCurrent,
|
||||
plant_id as u32,
|
||||
0,
|
||||
"",
|
||||
"",
|
||||
)
|
||||
.await;
|
||||
log(
|
||||
LogMessage::PumpMissingSensorCurrent,
|
||||
plant_id as u32,
|
||||
0,
|
||||
"",
|
||||
"",
|
||||
)
|
||||
.await;
|
||||
error = true;
|
||||
break;
|
||||
} else {
|
||||
@@ -1003,21 +1001,21 @@ async fn wait_infinity(
|
||||
led_count %= 8;
|
||||
led_count += 1;
|
||||
for i in 0..8 {
|
||||
let _ = board.board_hal.fault(i, i < led_count);
|
||||
let _ = board.board_hal.fault(i, i < led_count).await;
|
||||
}
|
||||
}
|
||||
WaitType::ConfigButton => {
|
||||
// Alternating pattern: 1010 1010 -> 0101 0101
|
||||
pattern_step = (pattern_step + 1) % 2;
|
||||
for i in 0..8 {
|
||||
let _ = board.board_hal.fault(i, (i + pattern_step) % 2 == 0);
|
||||
let _ = board.board_hal.fault(i, (i + pattern_step) % 2 == 0).await;
|
||||
}
|
||||
}
|
||||
WaitType::MqttConfig => {
|
||||
// Moving dot pattern
|
||||
pattern_step = (pattern_step + 1) % 8;
|
||||
for i in 0..8 {
|
||||
let _ = board.board_hal.fault(i, i == pattern_step);
|
||||
let _ = board.board_hal.fault(i, i == pattern_step).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1031,7 +1029,7 @@ async fn wait_infinity(
|
||||
|
||||
// Clear all LEDs
|
||||
for i in 0..8 {
|
||||
let _ = board.board_hal.fault(i, false);
|
||||
let _ = board.board_hal.fault(i, false).await;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user