From dcd6798504edd902bc87f862d8dcaaff17597c8b Mon Sep 17 00:00:00 2001 From: Evan Feenstra Date: Fri, 31 Mar 2023 12:35:04 -0700 Subject: [PATCH] more updates to mqtt --- sphinx-key/src/clear.rs | 2 +- sphinx-key/src/conn/http.rs | 3 +- sphinx-key/src/conn/mqtt.rs | 85 +++++++++++++++++++++++++++++-------- sphinx-key/src/conn/wifi.rs | 2 +- sphinx-key/src/main.rs | 4 +- 5 files changed, 73 insertions(+), 23 deletions(-) diff --git a/sphinx-key/src/clear.rs b/sphinx-key/src/clear.rs index 6ef33bd..11cc0e1 100644 --- a/sphinx-key/src/clear.rs +++ b/sphinx-key/src/clear.rs @@ -1,8 +1,8 @@ use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported use embedded_svc::storage::StorageBase; +use esp_idf_svc::nvs::EspNvsStorage; use esp_idf_svc::nvs::*; -use esp_idf_svc::nvs_storage::EspNvsStorage; use std::sync::Arc; diff --git a/sphinx-key/src/conn/http.rs b/sphinx-key/src/conn/http.rs index da3fca6..d2b9bb4 100644 --- a/sphinx-key/src/conn/http.rs +++ b/sphinx-key/src/conn/http.rs @@ -4,8 +4,9 @@ use sphinx_signer::sphinx_glyph::control::Config; use serde::Deserialize; use std::sync::{Arc, Condvar, Mutex}; -use embedded_svc::http::server::registry::Registry; +// use embedded_svc::http::server::registry::Registry; use embedded_svc::http::server::*; +use embedded_svc::httpd::registry::Registry; use embedded_svc::httpd::Result; use esp_idf_svc::httpd as idf; diff --git a/sphinx-key/src/conn/mqtt.rs b/sphinx-key/src/conn/mqtt.rs index 92d4ef7..7611bad 100644 --- a/sphinx-key/src/conn/mqtt.rs +++ b/sphinx-key/src/conn/mqtt.rs @@ -3,14 +3,14 @@ use sphinx_signer::sphinx_glyph::topics; use anyhow::Result; use embedded_svc::mqtt::client::utils::ConnState; -use embedded_svc::mqtt::client::utils::Connection as MqttConnection; use embedded_svc::mqtt::client::{Connection, Event, Message as MqttMessage, MessageImpl, QoS}; +use embedded_svc::utils::mqtt::client::Connection as MqttConnection; +use embedded_svc::utils::mutex::Condvar; use esp_idf_svc::mqtt::client::*; use esp_idf_sys::EspError; use esp_idf_sys::{self}; use log::*; use std::sync::mpsc; -use std::sync::Condvar; use std::thread; pub const QOS: QoS = QoS::ExactlyOnce; @@ -20,10 +20,8 @@ pub fn make_client( client_id: &str, username: &str, password: &str, -) -> Result<( - EspMqttClient>, - MqttConnection, -)> { + tx: mpsc::Sender, +) -> Result>> { log::info!("make_client with id {}", client_id); let conf = MqttClientConfiguration { client_id: Some(client_id), @@ -37,20 +35,11 @@ pub fn make_client( }; let b = format!("mqtt://{}", broker); - // let (mut client, mut connection) = EspMqttClient::new_with_conn(b, &conf)?; - let cc = EspMqttClient::new_with_conn(b, &conf)?; + let (mut client, mut connection) = EspMqttClient::new_with_conn(b, &conf)?; + // let cc = EspMqttClient::new_with_conn(b, &conf)?; info!("MQTT client started"); - Ok(cc) -} - -pub fn start_listening( - client: EspMqttClient>, - mut connection: MqttConnection, - tx: mpsc::Sender, -) -> Result>> { - // must start pumping before subscribe or publish will not work thread::spawn(move || { info!("MQTT Listening for messages"); loop { @@ -102,5 +91,65 @@ pub fn start_listening( //info!("MQTT connection loop exit"); }); - Ok(client) + Ok(cc) } + +// pub fn start_listening( +// client: EspMqttClient>, +// mut connection: MqttConnection, +// tx: mpsc::Sender, +// ) -> Result>> { +// // must start pumping before subscribe or publish will not work +// thread::spawn(move || { +// info!("MQTT Listening for messages"); +// loop { +// match connection.next() { +// Some(msg) => match msg { +// Err(e) => match e.to_string().as_ref() { +// "ESP_FAIL" => { +// error!("ESP_FAIL msg!"); +// } +// _ => error!("Unknown error: {}", e), +// }, +// Ok(msg) => match msg { +// Event::BeforeConnect => info!("RECEIVED BeforeConnect MESSAGE"), +// Event::Connected(_flag) => { +// info!("RECEIVED Connected MESSAGE"); +// tx.send(CoreEvent::Connected) +// .expect("couldnt send Event::Connected"); +// } +// Event::Disconnected => { +// warn!("RECEIVED Disconnected MESSAGE"); +// tx.send(CoreEvent::Disconnected) +// .expect("couldnt send Event::Disconnected"); +// } +// Event::Subscribed(_mes_id) => info!("RECEIVED Subscribed MESSAGE"), +// Event::Unsubscribed(_mes_id) => info!("RECEIVED Unsubscribed MESSAGE"), +// Event::Published(_mes_id) => info!("RECEIVED Published MESSAGE"), +// Event::Received(msg) => { +// let topic_opt = msg.topic(); +// if let Some(topic) = topic_opt { +// match topic { +// topics::VLS => tx +// .send(CoreEvent::VlsMessage(msg.data().to_vec())) +// .expect("couldnt send Event::VlsMessage"), +// topics::CONTROL => tx +// .send(CoreEvent::Control(msg.data().to_vec())) +// .expect("couldnt send Event::Control"), +// _ => log::warn!("unrecognized topic {}", topic), +// }; +// } else { +// log::warn!("empty topic in msg!!!"); +// } +// } +// Event::Deleted(_mes_id) => info!("RECEIVED Deleted MESSAGE"), +// }, +// }, +// None => break, +// } +// } +// //info!("MQTT connection loop exit"); +// }); + +// Ok(client) +// } diff --git a/sphinx-key/src/conn/wifi.rs b/sphinx-key/src/conn/wifi.rs index b510e07..6210b04 100644 --- a/sphinx-key/src/conn/wifi.rs +++ b/sphinx-key/src/conn/wifi.rs @@ -30,7 +30,7 @@ pub fn start_client( let sysloop = EspSystemEventLoop::take()?; - let mut wifi = Box::new(EspWifi::new(modem, sysloop, default_nvs)?); + let mut wifi = Box::new(EspWifi::new(modem, sysloop, Some(default_nvs))?); let ap_infos = wifi.scan()?; let ssid = config.ssid.as_str(); let pass = config.pass.as_str(); diff --git a/sphinx-key/src/main.rs b/sphinx-key/src/main.rs index 9c4af2e..c69e022 100644 --- a/sphinx-key/src/main.rs +++ b/sphinx-key/src/main.rs @@ -142,8 +142,8 @@ fn make_and_launch_client( let token = ctrlr.make_auth_token().expect("couldnt make auth token"); log::info!("PUBKEY {} TOKEN {}", &pubkey, &token); - let (mqtt, connection) = conn::mqtt::make_client(&config.broker, CLIENT_ID, &pubkey, &token)?; - let mqtt_client = conn::mqtt::start_listening(mqtt, connection, tx)?; + let mqtt_client = conn::mqtt::make_client(&config.broker, CLIENT_ID, &pubkey, &token, tx)?; + // let mqtt_client = conn::mqtt::start_listening(mqtt, connection, tx)?; // this blocks forever... the "main thread" let do_log = true;