refactor: move NetworkMode and SntpMode enums to network module
This commit is contained in:
@@ -121,21 +121,7 @@ pub struct PumpResult {
|
|||||||
pump_time_s: u16,
|
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<()> {
|
async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||||
info!("Startup Rust");
|
info!("Startup Rust");
|
||||||
@@ -225,10 +211,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 = {
|
||||||
@@ -262,7 +248,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;
|
publish_battery_state(&mut board).await;
|
||||||
let _ = publish_mppt_state(&mut board).await;
|
let _ = publish_mppt_state(&mut board).await;
|
||||||
@@ -270,15 +256,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(),
|
||||||
"",
|
"",
|
||||||
@@ -751,13 +737,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
|
||||||
@@ -765,12 +751,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
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -798,7 +784,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,
|
||||||
@@ -807,7 +793,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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1,19 @@
|
|||||||
// Network stack spawning module
|
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,
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user