refactor: move try_connect_wifi_sntp_mqtt to network module

This commit is contained in:
2026-05-10 13:34:34 +02:00
parent 18095349f3
commit 6889ba4561
2 changed files with 79 additions and 84 deletions

View File

@@ -211,7 +211,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
let mut stack: OptionLock<Stack> = OptionLock::empty(); let mut stack: OptionLock<Stack> = OptionLock::empty();
let network_mode = if board.board_hal.get_config().network.ssid.is_some() { let network_mode = if board.board_hal.get_config().network.ssid.is_some() {
try_connect_wifi_sntp_mqtt(&mut board, &mut stack, spawner).await network::try_connect_wifi_sntp_mqtt(&mut board, &mut stack, spawner).await
} else { } else {
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;
@@ -740,88 +740,6 @@ async fn publish_firmware_info(
.await; .await;
mqtt::publish("/state", "online").await; mqtt::publish("/state", "online").await;
} }
macro_rules! mk_static {
($t:ty,$val:expr) => {{
static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
#[deny(unused_attributes)]
let x = STATIC_CELL.uninit().write(($val));
x
}};
}
async fn try_connect_wifi_sntp_mqtt(
board: &mut MutexGuard<'static, CriticalSectionRawMutex, HAL<'static>>,
stack_store: &mut OptionLock<Stack<'static>>,
spawner: Spawner,
) -> network::NetworkMode {
let nw_conf = &board.board_hal.get_config().network.clone();
let esp = board.board_hal.get_esp();
let device = match esp.interface_sta.take() {
Some(d) => d,
None => {
info!("Offline mode due to STA interface already taken");
board.board_hal.general_fault(true).await;
return network::NetworkMode::OFFLINE;
}
};
match network::wifi(nw_conf, device, &esp.controller, &mut esp.rng, spawner).await {
Ok(stack) => {
stack_store.replace(stack);
let sntp_mode: network::SntpMode = match network::sntp(1000 * 10, stack).await {
Ok(new_time) => {
info!("Using time from sntp {}", new_time.to_rfc3339());
let _ = board
.board_hal
.get_rtc_module()
.set_rtc_time(&new_time)
.await;
network::SntpMode::SYNC { current: new_time }
}
Err(err) => {
warn!("sntp error: {err}");
board.board_hal.general_fault(true).await;
network::SntpMode::OFFLINE
}
};
let mqtt_connected = if board.board_hal.get_config().network.mqtt_url.is_some() {
let nw_config = board.board_hal.get_config().network.clone();
let nw_config = mk_static!(NetworkConfig, nw_config);
match mqtt::mqtt_init(nw_config, stack, spawner).await {
Ok(_) => {
info!("Mqtt connection ready");
true
}
Err(err) => {
warn!("Could not connect mqtt due to {err}");
false
}
}
} else {
false
};
let ip = match stack.config_v4() {
Some(config) => config.address.address().to_string(),
None => match stack.config_v6() {
Some(config) => config.address.address().to_string(),
None => String::from("No IP"),
},
};
network::NetworkMode::WIFI {
sntp: sntp_mode,
mqtt: mqtt_connected,
ip_address: ip,
}
}
Err(err) => {
info!("Offline mode due to {err}");
board.board_hal.general_fault(true).await;
network::NetworkMode::OFFLINE
}
}
}
async fn pump_info( async fn pump_info(
plant_id: usize, plant_id: usize,
pump_active: bool, pump_active: bool,

View File

@@ -1,6 +1,8 @@
use crate::bail; use crate::bail;
use crate::config::NetworkConfig; use crate::config::NetworkConfig;
use crate::fat_error::{ContextExt, FatError, FatResult}; use crate::fat_error::{ContextExt, FatError, FatResult};
use crate::hal::{PlantHal, HAL};
use crate::mqtt;
use alloc::string::{String, ToString}; use alloc::string::{String, ToString};
use alloc::sync::Arc; use alloc::sync::Arc;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
@@ -10,8 +12,9 @@ use embassy_net::dns::DnsQueryType;
use embassy_net::udp::{PacketMetadata, UdpSocket}; use embassy_net::udp::{PacketMetadata, UdpSocket};
use embassy_net::{DhcpConfig, Runner, Stack, StackResources, StaticConfigV4}; use embassy_net::{DhcpConfig, Runner, Stack, StackResources, StaticConfigV4};
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::mutex::Mutex; use embassy_sync::mutex::{Mutex, MutexGuard};
use embassy_time::{Duration, Timer, WithTimeout}; use embassy_time::{Duration, Timer, WithTimeout};
use option_lock::OptionLock;
use edge_dhcp::{ use edge_dhcp::{
io::{self, DEFAULT_SERVER_PORT}, io::{self, DEFAULT_SERVER_PORT},
server::{Server, ServerOptions}, server::{Server, ServerOptions},
@@ -348,3 +351,77 @@ pub async fn wifi(
info!("Connected WIFI, dhcp: {:?}", stack.config_v4()); info!("Connected WIFI, dhcp: {:?}", stack.config_v4());
Ok(*stack) Ok(*stack)
} }
pub async fn try_connect_wifi_sntp_mqtt(
board: &mut MutexGuard<'static, CriticalSectionRawMutex, HAL<'static>>,
stack_store: &mut OptionLock<Stack<'static>>,
spawner: Spawner,
) -> NetworkMode {
let nw_conf = &board.board_hal.get_config().network.clone();
let esp = board.board_hal.get_esp();
let device = match esp.interface_sta.take() {
Some(d) => d,
None => {
info!("Offline mode due to STA interface already taken");
board.board_hal.general_fault(true).await;
return NetworkMode::OFFLINE;
}
};
match wifi(nw_conf, device, &esp.controller, &mut esp.rng, spawner).await {
Ok(stack) => {
stack_store.replace(stack);
let sntp_mode: SntpMode = match sntp(1000 * 10, stack).await {
Ok(new_time) => {
info!("Using time from sntp {}", new_time.to_rfc3339());
let _ = board
.board_hal
.get_rtc_module()
.set_rtc_time(&new_time)
.await;
SntpMode::SYNC { current: new_time }
}
Err(err) => {
warn!("sntp error: {err}");
board.board_hal.general_fault(true).await;
SntpMode::OFFLINE
}
};
let mqtt_connected = if board.board_hal.get_config().network.mqtt_url.is_some() {
let nw_config = board.board_hal.get_config().network.clone();
let nw_config = mk_static!(NetworkConfig, nw_config);
match mqtt::mqtt_init(nw_config, stack, spawner).await {
Ok(_) => {
info!("Mqtt connection ready");
true
}
Err(err) => {
warn!("Could not connect mqtt due to {err}");
false
}
}
} else {
false
};
let ip = match stack.config_v4() {
Some(config) => config.address.address().to_string(),
None => match stack.config_v6() {
Some(config) => config.address.address().to_string(),
None => String::from("No IP"),
},
};
NetworkMode::WIFI {
sntp: sntp_mode,
mqtt: mqtt_connected,
ip_address: ip,
}
}
Err(err) => {
info!("Offline mode due to {err}");
board.board_hal.general_fault(true).await;
NetworkMode::OFFLINE
}
}
}