Compare commits
12 Commits
53819484fb
...
f70ce6a108
| Author | SHA1 | Date | |
|---|---|---|---|
| f70ce6a108 | |||
| a84a325852 | |||
| 29060cbc46 | |||
| 122f878b02 | |||
| d98fe9bb3c | |||
| 087d6e20cd | |||
| d2b18db250 | |||
| 5ac4edd5a0 | |||
| c735d4f1c5 | |||
| 7966efb273 | |||
| 41ef3dd3e9 | |||
| 050f51b9b8 |
@@ -94,21 +94,6 @@ 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,
|
||||||
@@ -120,22 +105,6 @@ 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");
|
||||||
|
|
||||||
@@ -224,10 +193,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
|
mqtt::NetworkMode::OFFLINE
|
||||||
};
|
};
|
||||||
|
|
||||||
if matches!(network_mode, NetworkMode::OFFLINE) && to_config {
|
if matches!(network_mode, mqtt::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 = {
|
||||||
@@ -261,7 +230,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
|||||||
timezone_time
|
timezone_time
|
||||||
);
|
);
|
||||||
|
|
||||||
if let NetworkMode::WIFI { ref ip_address, .. } = network_mode {
|
if let mqtt::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;
|
||||||
@@ -269,15 +238,15 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
|||||||
|
|
||||||
log(
|
log(
|
||||||
LogMessage::StartupInfo,
|
LogMessage::StartupInfo,
|
||||||
matches!(network_mode, NetworkMode::WIFI { .. }) as u32,
|
matches!(network_mode, mqtt::NetworkMode::WIFI { .. }) as u32,
|
||||||
matches!(
|
matches!(
|
||||||
network_mode,
|
network_mode,
|
||||||
NetworkMode::WIFI {
|
mqtt::NetworkMode::WIFI {
|
||||||
sntp: SntpMode::SYNC { .. },
|
sntp: mqtt::SntpMode::SYNC { .. },
|
||||||
..
|
..
|
||||||
}
|
}
|
||||||
) as u32,
|
) as u32,
|
||||||
matches!(network_mode, NetworkMode::WIFI { mqtt: true, .. })
|
matches!(network_mode, mqtt::NetworkMode::WIFI { mqtt: true, .. })
|
||||||
.to_string()
|
.to_string()
|
||||||
.as_str(),
|
.as_str(),
|
||||||
"",
|
"",
|
||||||
@@ -451,7 +420,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 = LightState {
|
let mut light_state = mqtt::LightState {
|
||||||
enabled: board.board_hal.get_config().night_lamp.enabled,
|
enabled: board.board_hal.get_config().night_lamp.enabled,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
@@ -750,13 +719,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 {
|
) -> mqtt::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: mqtt::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
|
||||||
@@ -764,12 +733,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 }
|
mqtt::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
|
mqtt::SntpMode::OFFLINE
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -797,7 +766,7 @@ async fn try_connect_wifi_sntp_mqtt(
|
|||||||
None => String::from("No IP"),
|
None => String::from("No IP"),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
NetworkMode::WIFI {
|
mqtt::NetworkMode::WIFI {
|
||||||
sntp: sntp_mode,
|
sntp: sntp_mode,
|
||||||
mqtt: mqtt_connected,
|
mqtt: mqtt_connected,
|
||||||
ip_address: ip,
|
ip_address: ip,
|
||||||
@@ -806,7 +775,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
|
mqtt::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,8 +16,18 @@ 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,
|
||||||
@@ -33,6 +43,22 @@ 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