chore: cargo fmt
This commit is contained in:
@@ -6,10 +6,15 @@ use anyhow::{anyhow, bail, Context};
|
||||
use chrono::{DateTime, Utc};
|
||||
use embedded_svc::ipv4::IpInfo;
|
||||
use embedded_svc::mqtt::client::QoS::{AtLeastOnce, ExactlyOnce};
|
||||
use embedded_svc::wifi::{AccessPointConfiguration, AccessPointInfo, AuthMethod, ClientConfiguration, Configuration};
|
||||
use embedded_svc::wifi::{
|
||||
AccessPointConfiguration, AccessPointInfo, AuthMethod, ClientConfiguration, Configuration,
|
||||
};
|
||||
use esp_idf_hal::delay::Delay;
|
||||
use esp_idf_hal::gpio::{Level, PinDriver};
|
||||
use esp_idf_svc::mqtt::client::{EspMqttClient, LwtConfiguration, MqttClientConfiguration};
|
||||
use esp_idf_svc::sntp;
|
||||
use esp_idf_svc::sntp::SyncStatus;
|
||||
use esp_idf_svc::systime::EspSystemTime;
|
||||
use esp_idf_svc::wifi::config::{ScanConfig, ScanType};
|
||||
use esp_idf_svc::wifi::EspWifi;
|
||||
use esp_idf_sys::{esp_spiffs_info, vTaskDelay};
|
||||
@@ -23,10 +28,6 @@ use std::str::FromStr;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use esp_idf_svc::sntp;
|
||||
use esp_idf_svc::sntp::SyncStatus;
|
||||
use esp_idf_svc::systime::EspSystemTime;
|
||||
|
||||
|
||||
#[link_section = ".rtc.data"]
|
||||
static mut LAST_WATERING_TIMESTAMP: [i64; PLANT_COUNT] = [0; PLANT_COUNT];
|
||||
@@ -37,7 +38,6 @@ static mut LOW_VOLTAGE_DETECTED: bool = false;
|
||||
#[link_section = ".rtc.data"]
|
||||
static mut RESTART_TO_CONF: bool = false;
|
||||
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
pub struct FileInfo {
|
||||
filename: String,
|
||||
@@ -61,16 +61,15 @@ pub struct FileSystemSizeInfo {
|
||||
|
||||
pub struct MqttClient<'a> {
|
||||
mqtt_client: EspMqttClient<'a>,
|
||||
base_topic: heapless::String<64>
|
||||
base_topic: heapless::String<64>,
|
||||
}
|
||||
pub struct ESP<'a> {
|
||||
pub(crate) mqtt_client: Option<MqttClient<'a>>,
|
||||
pub(crate) wifi_driver: EspWifi<'a>,
|
||||
pub(crate) boot_button: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, esp_idf_hal::gpio::Input>,
|
||||
pub(crate) delay: Delay
|
||||
pub(crate) delay: Delay,
|
||||
}
|
||||
|
||||
|
||||
impl ESP<'_> {
|
||||
const SPIFFS_PARTITION_NAME: &'static str = "storage";
|
||||
const CONFIG_FILE: &'static str = "/spiffs/config.cfg";
|
||||
@@ -131,9 +130,7 @@ impl ESP<'_> {
|
||||
}
|
||||
|
||||
pub(crate) fn low_voltage_in_cycle(&mut self) -> bool {
|
||||
unsafe {
|
||||
LOW_VOLTAGE_DETECTED
|
||||
}
|
||||
unsafe { LOW_VOLTAGE_DETECTED }
|
||||
}
|
||||
pub(crate) fn store_consecutive_pump_count(&mut self, plant: usize, count: u32) {
|
||||
unsafe {
|
||||
@@ -153,13 +150,9 @@ impl ESP<'_> {
|
||||
}
|
||||
|
||||
pub(crate) fn wifi_ap(&mut self) -> anyhow::Result<()> {
|
||||
let ssid = match self.load_config(){
|
||||
Ok(config) => {
|
||||
config.network.ap_ssid.clone()
|
||||
}
|
||||
Err(_) => {
|
||||
heapless::String::from_str("PlantCtrl Emergency Mode").unwrap()
|
||||
}
|
||||
let ssid = match self.load_config() {
|
||||
Ok(config) => config.network.ap_ssid.clone(),
|
||||
Err(_) => heapless::String::from_str("PlantCtrl Emergency Mode").unwrap(),
|
||||
};
|
||||
|
||||
let apconfig = AccessPointConfiguration {
|
||||
@@ -168,17 +161,17 @@ impl ESP<'_> {
|
||||
ssid_hidden: false,
|
||||
..Default::default()
|
||||
};
|
||||
self.wifi_driver.set_configuration(&Configuration::AccessPoint(apconfig))?;
|
||||
self.wifi_driver
|
||||
.set_configuration(&Configuration::AccessPoint(apconfig))?;
|
||||
self.wifi_driver.start()?;
|
||||
anyhow::Ok(())
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn wifi(
|
||||
&mut self,
|
||||
network_config: &NetworkConfig
|
||||
) -> anyhow::Result<IpInfo> {
|
||||
let ssid = network_config.ssid.clone().ok_or(anyhow!("No ssid configured"))?;
|
||||
pub(crate) fn wifi(&mut self, network_config: &NetworkConfig) -> anyhow::Result<IpInfo> {
|
||||
let ssid = network_config
|
||||
.ssid
|
||||
.clone()
|
||||
.ok_or(anyhow!("No ssid configured"))?;
|
||||
let password = network_config.password.clone();
|
||||
let max_wait = network_config.max_wait;
|
||||
|
||||
@@ -298,7 +291,6 @@ impl ESP<'_> {
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn list_files(&self) -> FileList {
|
||||
let storage = CString::new(Self::SPIFFS_PARTITION_NAME).unwrap();
|
||||
|
||||
@@ -367,7 +359,7 @@ impl ESP<'_> {
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn init_rtc_deepsleep_memory(&self, init_rtc_store: bool, to_config_mode: bool){
|
||||
pub(crate) fn init_rtc_deepsleep_memory(&self, init_rtc_store: bool, to_config_mode: bool) {
|
||||
if init_rtc_store {
|
||||
unsafe {
|
||||
LAST_WATERING_TIMESTAMP = [0; PLANT_COUNT];
|
||||
@@ -544,9 +536,9 @@ impl ESP<'_> {
|
||||
match round_trip_ok.load(std::sync::atomic::Ordering::Relaxed) {
|
||||
true => {
|
||||
println!("Round trip registered, proceeding");
|
||||
self.mqtt_client = Some(MqttClient{
|
||||
self.mqtt_client = Some(MqttClient {
|
||||
mqtt_client: client,
|
||||
base_topic: base_topic_copy
|
||||
base_topic: base_topic_copy,
|
||||
});
|
||||
return anyhow::Ok(());
|
||||
}
|
||||
@@ -569,11 +561,7 @@ impl ESP<'_> {
|
||||
}
|
||||
bail!("Mqtt did not fire connection callback in time");
|
||||
}
|
||||
pub(crate) fn mqtt_publish(
|
||||
&mut self,
|
||||
subtopic: &str,
|
||||
message: &[u8],
|
||||
) -> anyhow::Result<()> {
|
||||
pub(crate) fn mqtt_publish(&mut self, subtopic: &str, message: &[u8]) -> anyhow::Result<()> {
|
||||
if self.mqtt_client.is_none() {
|
||||
return anyhow::Ok(());
|
||||
}
|
||||
@@ -587,10 +575,7 @@ impl ESP<'_> {
|
||||
}
|
||||
let client = self.mqtt_client.as_mut().unwrap();
|
||||
let mut full_topic: heapless::String<256> = heapless::String::new();
|
||||
if full_topic
|
||||
.push_str(client.base_topic.as_str())
|
||||
.is_err()
|
||||
{
|
||||
if full_topic.push_str(client.base_topic.as_str()).is_err() {
|
||||
println!("Some error assembling full_topic 1");
|
||||
bail!("Some error assembling full_topic 1")
|
||||
};
|
||||
@@ -598,7 +583,9 @@ impl ESP<'_> {
|
||||
println!("Some error assembling full_topic 2");
|
||||
bail!("Some error assembling full_topic 2")
|
||||
};
|
||||
let publish = client.mqtt_client.publish(&full_topic, ExactlyOnce, true, message);
|
||||
let publish = client
|
||||
.mqtt_client
|
||||
.publish(&full_topic, ExactlyOnce, true, message);
|
||||
Delay::new(10).delay_ms(50);
|
||||
match publish {
|
||||
OkStd(message_id) => {
|
||||
@@ -621,4 +608,4 @@ impl ESP<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user