refactor: create network module

- move NetworkMode and SntpMode enums
This commit is contained in:
2026-05-10 13:33:48 +02:00
parent 2c532359fc
commit 1d8af1b6c4
2 changed files with 33 additions and 27 deletions

View File

@@ -68,6 +68,7 @@ mod fat_error;
mod hal;
mod log;
mod mqtt;
mod network;
mod plant_state;
mod tank;
mod webserver;
@@ -120,21 +121,7 @@ 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");
@@ -224,10 +211,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
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!");
let res = {
@@ -261,7 +248,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
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_battery_state(&mut board).await;
let _ = publish_mppt_state(&mut board).await;
@@ -269,15 +256,15 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
log(
LogMessage::StartupInfo,
matches!(network_mode, NetworkMode::WIFI { .. }) as u32,
matches!(network_mode, network::NetworkMode::WIFI { .. }) as u32,
matches!(
network_mode,
NetworkMode::WIFI {
sntp: SntpMode::SYNC { .. },
network::NetworkMode::WIFI {
sntp: network::SntpMode::SYNC { .. },
..
}
) as u32,
matches!(network_mode, NetworkMode::WIFI { mqtt: true, .. })
matches!(network_mode, network::NetworkMode::WIFI { mqtt: true, .. })
.to_string()
.as_str(),
"",
@@ -750,13 +737,13 @@ async fn try_connect_wifi_sntp_mqtt(
board: &mut MutexGuard<'static, CriticalSectionRawMutex, HAL<'static>>,
stack_store: &mut OptionLock<Stack<'static>>,
spawner: Spawner,
) -> NetworkMode {
) -> network::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: network::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
@@ -764,12 +751,12 @@ async fn try_connect_wifi_sntp_mqtt(
.get_rtc_module()
.set_rtc_time(&new_time)
.await;
SntpMode::SYNC { current: new_time }
network::SntpMode::SYNC { current: new_time }
}
Err(err) => {
warn!("sntp error: {err}");
board.board_hal.general_fault(true).await;
SntpMode::OFFLINE
network::SntpMode::OFFLINE
}
};
@@ -797,7 +784,7 @@ async fn try_connect_wifi_sntp_mqtt(
None => String::from("No IP"),
},
};
NetworkMode::WIFI {
network::NetworkMode::WIFI {
sntp: sntp_mode,
mqtt: mqtt_connected,
ip_address: ip,
@@ -806,7 +793,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
network::NetworkMode::OFFLINE
}
}
}

19
rust/src/network.rs Normal file
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,
}