From d2b18db250d3c1a69b892e664db16c5b6e0e73c9 Mon Sep 17 00:00:00 2001 From: ju6ge Date: Tue, 5 May 2026 20:53:07 +0200 Subject: [PATCH] refactor: move NetworkMode and SntpMode to mqtt module --- rust/src/main.rs | 42 +++++++++++++----------------------------- rust/src/mqtt.rs | 17 +++++++++++++++++ 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/rust/src/main.rs b/rust/src/main.rs index f8d1134..9953356 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -105,22 +105,6 @@ pub struct PumpResult { pump_time_s: u16, } -#[derive(Serialize, Debug, PartialEq)] -enum SntpMode { - OFFLINE, - SYNC { current: DateTime }, -} - -#[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"); @@ -209,10 +193,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; - NetworkMode::OFFLINE + mqtt::NetworkMode::OFFLINE }; - if matches!(network_mode, NetworkMode::OFFLINE) && to_config { + if matches!(network_mode, mqtt::NetworkMode::OFFLINE) && to_config { info!("Could not connect to station and config mode forced, switching to ap mode!"); let res = { @@ -246,7 +230,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> { timezone_time ); - if let NetworkMode::WIFI { ref ip_address, .. } = network_mode { + if let mqtt::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; @@ -254,15 +238,15 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> { log( LogMessage::StartupInfo, - matches!(network_mode, NetworkMode::WIFI { .. }) as u32, + matches!(network_mode, mqtt::NetworkMode::WIFI { .. }) as u32, matches!( network_mode, - NetworkMode::WIFI { - sntp: SntpMode::SYNC { .. }, + mqtt::NetworkMode::WIFI { + sntp: mqtt::SntpMode::SYNC { .. }, .. } ) as u32, - matches!(network_mode, NetworkMode::WIFI { mqtt: true, .. }) + matches!(network_mode, mqtt::NetworkMode::WIFI { mqtt: true, .. }) .to_string() .as_str(), "", @@ -757,13 +741,13 @@ async fn try_connect_wifi_sntp_mqtt( board: &mut MutexGuard<'static, CriticalSectionRawMutex, HAL<'static>>, stack_store: &mut OptionLock>, spawner: Spawner, -) -> NetworkMode { +) -> mqtt::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: SntpMode = match board.board_hal.get_esp().sntp(1000 * 10, stack).await { + let sntp_mode: mqtt::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 @@ -771,12 +755,12 @@ async fn try_connect_wifi_sntp_mqtt( .get_rtc_module() .set_rtc_time(&new_time) .await; - SntpMode::SYNC { current: new_time } + mqtt::SntpMode::SYNC { current: new_time } } Err(err) => { warn!("sntp error: {err}"); board.board_hal.general_fault(true).await; - SntpMode::OFFLINE + mqtt::SntpMode::OFFLINE } }; @@ -809,7 +793,7 @@ async fn try_connect_wifi_sntp_mqtt( None => String::from("No IP"), }, }; - NetworkMode::WIFI { + mqtt::NetworkMode::WIFI { sntp: sntp_mode, mqtt: mqtt_connected, ip_address: ip, @@ -818,7 +802,7 @@ async fn try_connect_wifi_sntp_mqtt( Err(err) => { info!("Offline mode due to {err}"); board.board_hal.general_fault(true).await; - NetworkMode::OFFLINE +mqtt::NetworkMode::OFFLINE } } } diff --git a/rust/src/mqtt.rs b/rust/src/mqtt.rs index e26c167..2731c93 100644 --- a/rust/src/mqtt.rs +++ b/rust/src/mqtt.rs @@ -4,6 +4,7 @@ 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; @@ -41,6 +42,22 @@ pub struct Solar { pub voltage_ma: u32, } +#[derive(Serialize, Debug, PartialEq)] +pub enum SntpMode { + OFFLINE, + SYNC { current: DateTime }, +} + +#[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);