Compare commits
3 Commits
f70ce6a108
...
53819484fb
| Author | SHA1 | Date | |
|---|---|---|---|
|
53819484fb
|
|||
|
1151d099cf
|
|||
|
3feaacd460
|
@@ -94,6 +94,21 @@ impl WaitType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Default)]
|
||||||
|
/// Light State tracking data for mqtt
|
||||||
|
struct LightState {
|
||||||
|
/// is enabled in config
|
||||||
|
enabled: bool,
|
||||||
|
/// led is on
|
||||||
|
active: bool,
|
||||||
|
/// led should not be on at this time of day
|
||||||
|
out_of_work_hour: bool,
|
||||||
|
/// the battery is low so do not use led
|
||||||
|
battery_low: bool,
|
||||||
|
/// the sun is up
|
||||||
|
is_day: bool,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
pub struct PumpResult {
|
pub struct PumpResult {
|
||||||
median_current_ma: u16,
|
median_current_ma: u16,
|
||||||
@@ -105,6 +120,22 @@ 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");
|
||||||
|
|
||||||
@@ -193,10 +224,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;
|
||||||
mqtt::NetworkMode::OFFLINE
|
NetworkMode::OFFLINE
|
||||||
};
|
};
|
||||||
|
|
||||||
if matches!(network_mode, mqtt::NetworkMode::OFFLINE) && to_config {
|
if matches!(network_mode, 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 = {
|
||||||
@@ -230,7 +261,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
|||||||
timezone_time
|
timezone_time
|
||||||
);
|
);
|
||||||
|
|
||||||
if let mqtt::NetworkMode::WIFI { ref ip_address, .. } = network_mode {
|
if let 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;
|
||||||
@@ -238,15 +269,15 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
|||||||
|
|
||||||
log(
|
log(
|
||||||
LogMessage::StartupInfo,
|
LogMessage::StartupInfo,
|
||||||
matches!(network_mode, mqtt::NetworkMode::WIFI { .. }) as u32,
|
matches!(network_mode, NetworkMode::WIFI { .. }) as u32,
|
||||||
matches!(
|
matches!(
|
||||||
network_mode,
|
network_mode,
|
||||||
mqtt::NetworkMode::WIFI {
|
NetworkMode::WIFI {
|
||||||
sntp: mqtt::SntpMode::SYNC { .. },
|
sntp: SntpMode::SYNC { .. },
|
||||||
..
|
..
|
||||||
}
|
}
|
||||||
) as u32,
|
) as u32,
|
||||||
matches!(network_mode, mqtt::NetworkMode::WIFI { mqtt: true, .. })
|
matches!(network_mode, NetworkMode::WIFI { mqtt: true, .. })
|
||||||
.to_string()
|
.to_string()
|
||||||
.as_str(),
|
.as_str(),
|
||||||
"",
|
"",
|
||||||
@@ -420,7 +451,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
|||||||
.unwrap_or(BatteryState::Unknown);
|
.unwrap_or(BatteryState::Unknown);
|
||||||
|
|
||||||
info!("Battery state is {:?}", battery_state);
|
info!("Battery state is {:?}", battery_state);
|
||||||
let mut light_state = mqtt::LightState {
|
let mut light_state = LightState {
|
||||||
enabled: board.board_hal.get_config().night_lamp.enabled,
|
enabled: board.board_hal.get_config().night_lamp.enabled,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
@@ -719,13 +750,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,
|
||||||
) -> mqtt::NetworkMode {
|
) -> 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: mqtt::SntpMode = match board.board_hal.get_esp().sntp(1000 * 10, stack).await {
|
let sntp_mode: 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
|
||||||
@@ -733,12 +764,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;
|
||||||
mqtt::SntpMode::SYNC { current: new_time }
|
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;
|
||||||
mqtt::SntpMode::OFFLINE
|
SntpMode::OFFLINE
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -766,7 +797,7 @@ async fn try_connect_wifi_sntp_mqtt(
|
|||||||
None => String::from("No IP"),
|
None => String::from("No IP"),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
mqtt::NetworkMode::WIFI {
|
NetworkMode::WIFI {
|
||||||
sntp: sntp_mode,
|
sntp: sntp_mode,
|
||||||
mqtt: mqtt_connected,
|
mqtt: mqtt_connected,
|
||||||
ip_address: ip,
|
ip_address: ip,
|
||||||
@@ -775,7 +806,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;
|
||||||
mqtt::NetworkMode::OFFLINE
|
NetworkMode::OFFLINE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,10 +5,10 @@ use crate::hal::PlantHal;
|
|||||||
use crate::log::{log, LogMessage};
|
use crate::log::{log, LogMessage};
|
||||||
use alloc::string::String;
|
use alloc::string::String;
|
||||||
use alloc::{format, string::ToString};
|
use alloc::{format, string::ToString};
|
||||||
use chrono::{DateTime, Utc};
|
|
||||||
use core::sync::atomic::Ordering;
|
use core::sync::atomic::Ordering;
|
||||||
use embassy_executor::Spawner;
|
use embassy_executor::Spawner;
|
||||||
use embassy_net::Stack;
|
use embassy_net::Stack;
|
||||||
|
use embassy_sync::once_lock::OnceLock;
|
||||||
use embassy_time::{Duration, Timer, WithTimeout};
|
use embassy_time::{Duration, Timer, WithTimeout};
|
||||||
use log::info;
|
use log::info;
|
||||||
use mcutie::{
|
use mcutie::{
|
||||||
@@ -16,18 +16,8 @@ use mcutie::{
|
|||||||
QoS, Topic,
|
QoS, Topic,
|
||||||
};
|
};
|
||||||
use portable_atomic::AtomicBool;
|
use portable_atomic::AtomicBool;
|
||||||
use embassy_sync::once_lock::OnceLock;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Default)]
|
|
||||||
pub struct LightState {
|
|
||||||
pub enabled: bool,
|
|
||||||
pub active: bool,
|
|
||||||
pub out_of_work_hour: bool,
|
|
||||||
pub battery_low: bool,
|
|
||||||
pub is_day: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Default)]
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Default)]
|
||||||
pub struct PumpInfo {
|
pub struct PumpInfo {
|
||||||
pub enabled: bool,
|
pub enabled: bool,
|
||||||
@@ -43,22 +33,6 @@ pub struct Solar {
|
|||||||
pub voltage_ma: u32,
|
pub voltage_ma: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[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,
|
|
||||||
}
|
|
||||||
|
|
||||||
static MQTT_CONNECTED_EVENT_RECEIVED: AtomicBool = AtomicBool::new(false);
|
static MQTT_CONNECTED_EVENT_RECEIVED: AtomicBool = AtomicBool::new(false);
|
||||||
static MQTT_ROUND_TRIP_RECEIVED: AtomicBool = AtomicBool::new(false);
|
static MQTT_ROUND_TRIP_RECEIVED: AtomicBool = AtomicBool::new(false);
|
||||||
pub static MQTT_STAY_ALIVE: AtomicBool = AtomicBool::new(false);
|
pub static MQTT_STAY_ALIVE: AtomicBool = AtomicBool::new(false);
|
||||||
|
|||||||
Reference in New Issue
Block a user