refactor: create network module

- move NetworkMode and SntpMode enums
This commit is contained in:
2026-05-17 12:19:22 +02:00
parent 2cfb2607a9
commit cd4d0cc683
2 changed files with 33 additions and 27 deletions

View File

@@ -68,6 +68,7 @@ mod fat_error;
mod hal; mod hal;
mod log; mod log;
mod mqtt; mod mqtt;
mod network;
mod plant_state; mod plant_state;
mod tank; mod tank;
mod webserver; mod webserver;
@@ -121,21 +122,7 @@ pub struct PumpResult {
overcurrent_ma: Option<u16>, overcurrent_ma: Option<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<()> { async fn safe_main(spawner: Spawner) -> FatResult<()> {
info!("Startup Rust"); info!("Startup Rust");
@@ -235,10 +222,10 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
info!("No wifi configured"); 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; //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; Timer::after_millis(100).await;
NetworkMode::Offline network::NetworkMode::OFFLINE
}; };
if matches!(network_mode, NetworkMode::Offline) && to_config { if matches!(network_mode, network::NetworkMode::OFFLINE) && to_config {
info!("Could not connect to station and config mode forced, switching to ap mode!"); info!("Could not connect to station and config mode forced, switching to ap mode!");
let res = { let res = {
@@ -271,7 +258,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
timezone_time timezone_time
); );
if let NetworkMode::Wifi { ref ip_address, .. } = network_mode { if let network::NetworkMode::WIFI { ref ip_address, .. } = network_mode {
publish_firmware_info(&mut board, version, ip_address, &timezone_time.to_rfc3339()).await; publish_firmware_info(&mut board, version, ip_address, &timezone_time.to_rfc3339()).await;
publish_battery_state(&mut board).await.unwrap_or_else(|e| { publish_battery_state(&mut board).await.unwrap_or_else(|e| {
error!("Error publishing battery state {e}"); error!("Error publishing battery state {e}");
@@ -281,15 +268,15 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
log( log(
LogMessage::StartupInfo, LogMessage::StartupInfo,
matches!(network_mode, NetworkMode::Wifi { .. }) as u32, matches!(network_mode, network::NetworkMode::WIFI { .. }) as u32,
matches!( matches!(
network_mode, network_mode,
NetworkMode::Wifi { network::NetworkMode::WIFI {
sntp: SntpMode::Sync { .. }, sntp: network::SntpMode::SYNC { .. },
.. ..
} }
) as u32, ) as u32,
matches!(network_mode, NetworkMode::Wifi { mqtt: true, .. }) matches!(network_mode, network::NetworkMode::WIFI { mqtt: true, .. })
.to_string() .to_string()
.as_str(), .as_str(),
"", "",
@@ -941,13 +928,13 @@ async fn try_connect_wifi_sntp_mqtt(
board: &mut MutexGuard<'static, CriticalSectionRawMutex, HAL<'static>>, board: &mut MutexGuard<'static, CriticalSectionRawMutex, HAL<'static>>,
stack_store: &mut OptionLock<Stack<'static>>, stack_store: &mut OptionLock<Stack<'static>>,
spawner: Spawner, spawner: Spawner,
) -> NetworkMode { ) -> network::NetworkMode {
let nw_conf = &board.board_hal.get_config().network.clone(); let nw_conf = &board.board_hal.get_config().network.clone();
match board.board_hal.get_esp().wifi(nw_conf, spawner).await { match board.board_hal.get_esp().wifi(nw_conf, spawner).await {
Ok(stack) => { Ok(stack) => {
stack_store.replace(stack); stack_store.replace(stack);
let sntp_mode: SntpMode = match board.board_hal.get_esp().sntp(1000 * 10, stack).await { let sntp_mode: network::SntpMode = match board.board_hal.get_esp().sntp(1000 * 10, stack).await {
Ok(new_time) => { Ok(new_time) => {
info!("Using time from sntp {}", new_time.to_rfc3339()); info!("Using time from sntp {}", new_time.to_rfc3339());
let _ = board let _ = board
@@ -955,12 +942,12 @@ async fn try_connect_wifi_sntp_mqtt(
.get_rtc_module() .get_rtc_module()
.set_rtc_time(&new_time) .set_rtc_time(&new_time)
.await; .await;
SntpMode::Sync { current: new_time } network::SntpMode::SYNC { current: new_time }
} }
Err(err) => { Err(err) => {
warn!("sntp error: {err}"); warn!("sntp error: {err}");
board.board_hal.general_fault(true).await; board.board_hal.general_fault(true).await;
SntpMode::Offline network::SntpMode::OFFLINE
} }
}; };
@@ -988,7 +975,7 @@ async fn try_connect_wifi_sntp_mqtt(
None => String::from("No IP"), None => String::from("No IP"),
}, },
}; };
NetworkMode::Wifi { network::NetworkMode::WIFI {
sntp: sntp_mode, sntp: sntp_mode,
mqtt: mqtt_connected, mqtt: mqtt_connected,
ip_address: ip, ip_address: ip,
@@ -997,7 +984,7 @@ async fn try_connect_wifi_sntp_mqtt(
Err(err) => { Err(err) => {
info!("Offline mode due to {err}"); info!("Offline mode due to {err}");
board.board_hal.general_fault(true).await; board.board_hal.general_fault(true).await;
NetworkMode::Offline network::NetworkMode::OFFLINE
} }
} }
} }

View File

@@ -0,0 +1,19 @@
use alloc::string::String;
use chrono::{DateTime, Utc};
use serde::Serialize;
#[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,
}