From 3768eefca4910405b7571964030042c992f55736 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Tue, 9 Jul 2024 17:46:02 +0200 Subject: [PATCH 1/3] mqtt: fix publish logic/error path, test alive, doctest --- miniconf_mqtt/src/lib.rs | 20 +++++++++++--------- py/test.sh | 4 ++++ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/miniconf_mqtt/src/lib.rs b/miniconf_mqtt/src/lib.rs index 7eececd6..ac3ec682 100644 --- a/miniconf_mqtt/src/lib.rs +++ b/miniconf_mqtt/src/lib.rs @@ -216,7 +216,6 @@ impl From for minimq::Property<'static> { /// # Example /// ``` /// use miniconf::Tree; -/// use miniconf_mqtt::MqttClient; /// /// #[derive(Tree, Clone, Default)] /// struct Settings { @@ -225,11 +224,11 @@ impl From for minimq::Property<'static> { /// /// let mut buffer = [0u8; 1024]; /// let localhost: minimq::embedded_nal::IpAddr = "127.0.0.1".parse().unwrap(); -/// let mut client: MqttClient<'_, _, _, _, minimq::broker::IpBroker, 1> = MqttClient::new( +/// let mut client = miniconf_mqtt::MqttClient::new( /// std_embedded_nal::Stack::default(), /// "quartiq/application/12345", // prefix /// std_embedded_time::StandardClock::default(), -/// minimq::ConfigBuilder::new(localhost.into(), &mut buffer), +/// minimq::ConfigBuilder::::new(localhost.into(), &mut buffer), /// ) /// .unwrap(); /// let mut settings = Settings::default(); @@ -324,7 +323,7 @@ where } sm::States::Init => { info!("Republishing"); - self.publish("").ok(); + self.publish(None).ok(); } sm::States::Multipart => { if self.pending.response_topic.is_some() { @@ -350,7 +349,6 @@ where .retain() .finish() .unwrap(); // Note(unwrap): has topic - self.mqtt.client().publish(msg).is_ok() } @@ -367,9 +365,13 @@ where /// # Note /// This is intended to be used if modification of a setting had side effects that affected /// another setting. - pub fn publish(&mut self, path: &str) -> Result<(), Error> { - self.pending = Multipart::default().root(&Path::<_, SEPARATOR>::from(path))?; + pub fn publish(&mut self, path: Option<&str>) -> Result<(), Error> { + let mut m = Multipart::default(); + if let Some(path) = path { + m = m.root(&Path::<_, SEPARATOR>::from(path))?; + } self.state.process_event(sm::Events::Multipart)?; + self.pending = m; Ok(()) } @@ -529,7 +531,7 @@ where (state.state() == &sm::States::Single) .then_some(()) .ok_or("Pending multipart response") - .and_then(|()| Multipart::::try_from(properties)) + .and_then(|()| Multipart::try_from(properties)) .map_or_else( |err| { Self::respond(err, ResponseCode::Error, properties, client) @@ -538,7 +540,7 @@ where |m| { *pending = m.root(&path).unwrap(); // Note(unwrap) checked that it's TooShort but valid leaf state.process_event(sm::Events::Multipart).unwrap(); - // Response comes through iter_list/iter_dump + // Responses comes through iter_list/iter_dump }, ); } diff --git a/py/test.sh b/py/test.sh index 88425b73..552ed318 100644 --- a/py/test.sh +++ b/py/test.sh @@ -23,6 +23,10 @@ DUT_PID=$! REPUB=$(timeout --foreground 3 mosquitto_sub -t "$PREFIX/+/settings/#" -h localhost | wc -l) test $REPUB != 9 && exit 1 +# test alive-ness +ALIVE=$(timeout --foreground 1 mosquitto_sub -t "$PREFIX/+/alive" -h localhost -F '%p' || true) +test $ALIVE != 1 && exit 1 + # no discover SET python -m miniconf -b localhost $PREFIX/id '/stream="192.0.2.16:9293"' # discover miniconf command From 1928217337e8f71fdfaccba6897447406cc9a053 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Tue, 9 Jul 2024 17:49:32 +0200 Subject: [PATCH 2/3] remove unnecessary code --- miniconf_mqtt/src/lib.rs | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/miniconf_mqtt/src/lib.rs b/miniconf_mqtt/src/lib.rs index ac3ec682..07fd3f52 100644 --- a/miniconf_mqtt/src/lib.rs +++ b/miniconf_mqtt/src/lib.rs @@ -39,7 +39,7 @@ type Iter = NodeIter, SEP #[derive(Debug, PartialEq)] pub enum Error { /// Miniconf - Miniconf(miniconf::Error<()>), + Miniconf(miniconf::Traversal), /// State machine State(sm::Error), /// Minimq @@ -52,20 +52,9 @@ impl From for Error { } } -impl From> for Error { - fn from(value: miniconf::Error) -> Self { - Self::Miniconf(match value { - miniconf::Error::Finalization(_) => miniconf::Error::Finalization(()), - miniconf::Error::Inner(depth, _) => miniconf::Error::Inner(depth, ()), - miniconf::Error::Traversal(t) => miniconf::Error::Traversal(t), - _ => unimplemented!(), - }) - } -} - impl From for Error { fn from(value: miniconf::Traversal) -> Self { - Self::Miniconf(value.into()) + Self::Miniconf(value) } } From 7f7a5911790ec604fdfcbeaceae19f4e4ee5a04d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Tue, 9 Jul 2024 17:53:20 +0200 Subject: [PATCH 3/3] bump env_logger --- miniconf_mqtt/Cargo.toml | 2 +- miniconf_mqtt/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/miniconf_mqtt/Cargo.toml b/miniconf_mqtt/Cargo.toml index 9c499c5b..307a3775 100644 --- a/miniconf_mqtt/Cargo.toml +++ b/miniconf_mqtt/Cargo.toml @@ -29,7 +29,7 @@ name = "mqtt" [dev-dependencies] machine = "0.3" -env_logger = "0.10" +env_logger = "0.11" std-embedded-nal = { git = "https://gitlab.com/ryan-summers/std-embedded-nal", branch = "feature/0.8" } tokio = { version = "1.9", features = ["rt-multi-thread", "time", "macros"] } std-embedded-time = "0.1" diff --git a/miniconf_mqtt/src/lib.rs b/miniconf_mqtt/src/lib.rs index 07fd3f52..66efae14 100644 --- a/miniconf_mqtt/src/lib.rs +++ b/miniconf_mqtt/src/lib.rs @@ -529,7 +529,7 @@ where |m| { *pending = m.root(&path).unwrap(); // Note(unwrap) checked that it's TooShort but valid leaf state.process_event(sm::Events::Multipart).unwrap(); - // Responses comes through iter_list/iter_dump + // Responses come through iter_list/iter_dump }, ); }