mode from env at compile time, reconn loop, try to compile with real signer

This commit is contained in:
Evan Feenstra
2022-06-10 16:45:15 -07:00
parent da144c355a
commit dc816934dd
4 changed files with 58 additions and 18 deletions

View File

@@ -4,6 +4,7 @@ use embedded_svc::mqtt::client::utils::Connection as MqttConnection;
use esp_idf_svc::mqtt::client::*;
use anyhow::Result;
use log::*;
use std::time::Duration;
use std::thread;
use esp_idf_sys::{self};
use esp_idf_sys::EspError;
@@ -12,16 +13,15 @@ use std::sync::{mpsc};
pub const TOPIC: &str = "sphinx";
pub const RETURN_TOPIC: &str = "sphinx-return";
pub const CLIENT_ID: &str = "sphinx-1";
pub const USERNAME: &str = "sphinx-key";
pub const PASSWORD: &str = "sphinx-key-pass";
pub fn make_client(broker: &str) -> Result<(
pub fn make_client(broker: &str, client_id: &str) -> Result<(
EspMqttClient<ConnState<MessageImpl, EspError>>,
MqttConnection<Condvar, MessageImpl, EspError>
MqttConnection<Condvar, MessageImpl, EspError>,
)> {
let conf = MqttClientConfiguration {
client_id: Some(CLIENT_ID),
client_id: Some(client_id),
buffer_size: 2048,
task_stack: 12288,
username: Some(USERNAME),
@@ -34,7 +34,16 @@ pub fn make_client(broker: &str) -> Result<(
let b = format!("mqtt://{}", broker);
println!("===> CONNECT TO {}", b);
// let (mut client, mut connection) = EspMqttClient::new_with_conn(b, &conf)?;
let cc = EspMqttClient::new_with_conn(b, &conf)?;
let cc = loop {
match EspMqttClient::new_with_conn(b.clone(), &conf) {
Ok(c_c) => {
break c_c
},
Err(_) => {
thread::sleep(Duration::from_secs(1));
}
}
};
//
info!("MQTT client started");
@@ -86,19 +95,8 @@ pub fn start_listening(
//info!("MQTT connection loop exit");
});
// log::info!("lock mqtt mutex guard");
// let mut client = mqtt.lock().unwrap();
log::info!("SUBSCRIBE TO {}", TOPIC);
client.subscribe(TOPIC, QoS::AtMostOnce)?;
log::info!("PUBLISH {} to {}", "READY", RETURN_TOPIC);
client.publish(
RETURN_TOPIC,
QoS::AtMostOnce,
false,
format!("READY").as_bytes(),
)?;
Ok(client)
}

View File

@@ -1,2 +1,3 @@
pub mod events;
pub mod config;
pub mod mode;

View File

@@ -0,0 +1,32 @@
use std::fmt;
pub enum Mode {
Test,
Signer,
}
impl fmt::Debug for Mode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Mode::Test => write!(f, "Test"),
Mode::Signer => write!(f, "Signer"),
}
}
}
impl Mode {
pub fn from_env(mode: Option<&'static str>) -> Self {
match mode {
Some(m) => {
match m {
"TEST" => Self::Test,
"test" => Self::Test,
"SIGNER" => Self::Signer,
"signer" => Self::Signer,
_ => Self::Signer,
}
},
None => Self::Signer,
}
}
}

View File

@@ -3,7 +3,7 @@ mod conn;
mod core;
mod periph;
use crate::core::{events::*, config::*};
use crate::core::{events::*, config::*, mode::Mode};
use crate::periph::led::Led;
use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported
@@ -17,7 +17,11 @@ use esp_idf_svc::nvs_storage::EspNvsStorage;
use embedded_svc::storage::Storage;
use embedded_svc::wifi::Wifi;
const MODE: Option<&'static str> = option_env!("MODE");
fn main() -> Result<()> {
let mode = Mode::from_env(MODE);
println!("MODE? {:?}", mode);
// Temporary. Will disappear once ESP-IDF 4.4 is released, but for now it is necessary to call this function once,
// or else some patches to the runtime implemented by esp-idf-sys might not link properly.
esp_idf_sys::link_patches();
@@ -35,8 +39,13 @@ fn main() -> Result<()> {
let wifi = start_wifi_client(default_nvs.clone(), &exist)?;
let (tx, rx) = mpsc::channel();
let client_id = match mode {
Mode::Signer => "sphinx-1",
Mode::Test => "test-1"
};
// _conn needs to stay in scope or its dropped
let (mqtt, connection) = conn::mqtt::make_client(&exist.broker)?;
let (mqtt, connection) = conn::mqtt::make_client(&exist.broker, client_id)?;
let mqtt_client = conn::mqtt::start_listening(mqtt, connection, tx)?;
// this blocks forever... the "main thread"