3 Commits

2 changed files with 46 additions and 41 deletions

View File

@@ -94,6 +94,21 @@ impl WaitType {
}
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Default)]
/// Light State tracking data for mqtt
struct LightState {
/// is enabled in config
enabled: bool,
/// led is on
active: bool,
/// led should not be on at this time of day
out_of_work_hour: bool,
/// the battery is low so do not use led
battery_low: bool,
/// the sun is up
is_day: bool,
}
#[derive(Serialize)]
pub struct PumpResult {
median_current_ma: u16,
@@ -105,6 +120,22 @@ pub struct PumpResult {
pump_time_s: u16,
}
#[derive(Serialize, Debug, PartialEq)]
enum SntpMode {
OFFLINE,
SYNC { current: DateTime<Utc> },
}
#[derive(Serialize, Debug, PartialEq)]
enum NetworkMode {
WIFI {
sntp: SntpMode,
mqtt: bool,
ip_address: String,
},
OFFLINE,
}
async fn safe_main(spawner: Spawner) -> FatResult<()> {
info!("Startup Rust");
@@ -193,10 +224,10 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
info!("No wifi configured");
//the current sensors require this amount to stabilize, in the case of Wi-Fi this is already handled due to connect timings;
Timer::after_millis(100).await;
mqtt::NetworkMode::OFFLINE
NetworkMode::OFFLINE
};
if matches!(network_mode, mqtt::NetworkMode::OFFLINE) && to_config {
if matches!(network_mode, NetworkMode::OFFLINE) && to_config {
info!("Could not connect to station and config mode forced, switching to ap mode!");
let res = {
@@ -230,7 +261,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
timezone_time
);
if let mqtt::NetworkMode::WIFI { ref ip_address, .. } = network_mode {
if let NetworkMode::WIFI { ref ip_address, .. } = network_mode {
publish_firmware_info(&mut board, version, ip_address, &timezone_time.to_rfc3339()).await;
publish_battery_state(&mut board).await;
let _ = publish_mppt_state(&mut board).await;
@@ -238,15 +269,15 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
log(
LogMessage::StartupInfo,
matches!(network_mode, mqtt::NetworkMode::WIFI { .. }) as u32,
matches!(network_mode, NetworkMode::WIFI { .. }) as u32,
matches!(
network_mode,
mqtt::NetworkMode::WIFI {
sntp: mqtt::SntpMode::SYNC { .. },
NetworkMode::WIFI {
sntp: SntpMode::SYNC { .. },
..
}
) as u32,
matches!(network_mode, mqtt::NetworkMode::WIFI { mqtt: true, .. })
matches!(network_mode, NetworkMode::WIFI { mqtt: true, .. })
.to_string()
.as_str(),
"",
@@ -420,7 +451,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
.unwrap_or(BatteryState::Unknown);
info!("Battery state is {:?}", battery_state);
let mut light_state = mqtt::LightState {
let mut light_state = LightState {
enabled: board.board_hal.get_config().night_lamp.enabled,
..Default::default()
};
@@ -719,13 +750,13 @@ async fn try_connect_wifi_sntp_mqtt(
board: &mut MutexGuard<'static, CriticalSectionRawMutex, HAL<'static>>,
stack_store: &mut OptionLock<Stack<'static>>,
spawner: Spawner,
) -> mqtt::NetworkMode {
) -> NetworkMode {
let nw_conf = &board.board_hal.get_config().network.clone();
match board.board_hal.get_esp().wifi(nw_conf, spawner).await {
Ok(stack) => {
stack_store.replace(stack);
let sntp_mode: mqtt::SntpMode = match board.board_hal.get_esp().sntp(1000 * 10, stack).await {
let sntp_mode: SntpMode = match board.board_hal.get_esp().sntp(1000 * 10, stack).await {
Ok(new_time) => {
info!("Using time from sntp {}", new_time.to_rfc3339());
let _ = board
@@ -733,12 +764,12 @@ async fn try_connect_wifi_sntp_mqtt(
.get_rtc_module()
.set_rtc_time(&new_time)
.await;
mqtt::SntpMode::SYNC { current: new_time }
SntpMode::SYNC { current: new_time }
}
Err(err) => {
warn!("sntp error: {err}");
board.board_hal.general_fault(true).await;
mqtt::SntpMode::OFFLINE
SntpMode::OFFLINE
}
};
@@ -766,7 +797,7 @@ async fn try_connect_wifi_sntp_mqtt(
None => String::from("No IP"),
},
};
mqtt::NetworkMode::WIFI {
NetworkMode::WIFI {
sntp: sntp_mode,
mqtt: mqtt_connected,
ip_address: ip,
@@ -775,7 +806,7 @@ async fn try_connect_wifi_sntp_mqtt(
Err(err) => {
info!("Offline mode due to {err}");
board.board_hal.general_fault(true).await;
mqtt::NetworkMode::OFFLINE
NetworkMode::OFFLINE
}
}
}

View File

@@ -5,10 +5,10 @@ use crate::hal::PlantHal;
use crate::log::{log, LogMessage};
use alloc::string::String;
use alloc::{format, string::ToString};
use chrono::{DateTime, Utc};
use core::sync::atomic::Ordering;
use embassy_executor::Spawner;
use embassy_net::Stack;
use embassy_sync::once_lock::OnceLock;
use embassy_time::{Duration, Timer, WithTimeout};
use log::info;
use mcutie::{
@@ -16,18 +16,8 @@ use mcutie::{
QoS, Topic,
};
use portable_atomic::AtomicBool;
use embassy_sync::once_lock::OnceLock;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, PartialEq, Default)]
pub struct LightState {
pub enabled: bool,
pub active: bool,
pub out_of_work_hour: bool,
pub battery_low: bool,
pub is_day: bool,
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Default)]
pub struct PumpInfo {
pub enabled: bool,
@@ -43,22 +33,6 @@ pub struct Solar {
pub voltage_ma: u32,
}
#[derive(Serialize, Debug, PartialEq)]
pub enum SntpMode {
OFFLINE,
SYNC { current: DateTime<Utc> },
}
#[derive(Serialize, Debug, PartialEq)]
pub enum NetworkMode {
WIFI {
sntp: SntpMode,
mqtt: bool,
ip_address: String,
},
OFFLINE,
}
static MQTT_CONNECTED_EVENT_RECEIVED: AtomicBool = AtomicBool::new(false);
static MQTT_ROUND_TRIP_RECEIVED: AtomicBool = AtomicBool::new(false);
pub static MQTT_STAY_ALIVE: AtomicBool = AtomicBool::new(false);