-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #220 from quartiq/mqtt-rs-rework
mqtt rs rework
- Loading branch information
Showing
14 changed files
with
562 additions
and
542 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,17 @@ | ||
# `miniconf` MQTT Client | ||
|
||
This package contains a MQTT client exposing a [`miniconf`](https://crates.io/crates/miniconf) interface via MQTT using [`minimq`](https://crates.io/crates/minimq). | ||
|
||
## Command types | ||
|
||
| Command | Node | Response Topic | Payload | | ||
| --- | --- | --- | --- | | ||
| Get | Leaf | set | empty | | ||
| List | Internal | set | empty | | ||
| Dump | | not set | empty | | ||
| Set | Leaf | | some | | ||
| Error | Internal | | some | | ||
|
||
## Notes | ||
|
||
* A list command will also list paths that are absent at runtime. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,114 +1,61 @@ | ||
use heapless::String; | ||
use miniconf::Tree; | ||
use minimq::Publication; | ||
use serde::{Deserialize, Serialize}; | ||
use std::time::Duration; | ||
use std_embedded_nal::Stack; | ||
use std_embedded_time::StandardClock; | ||
|
||
#[derive(Clone, Default, Tree, Debug)] | ||
struct NestedSettings { | ||
struct Inner { | ||
frame_rate: u32, | ||
} | ||
|
||
#[derive(Copy, Clone, Default, Debug, Serialize, Deserialize)] | ||
enum Gain { | ||
#[default] | ||
G1, | ||
G10, | ||
G100, | ||
} | ||
|
||
#[derive(Clone, Default, Tree, Debug)] | ||
struct Settings { | ||
stream: String<32>, | ||
#[tree(depth = 1)] | ||
inner: NestedSettings, | ||
|
||
afe: [Gain; 2], | ||
#[tree(depth = 1)] | ||
inner: Inner, | ||
#[tree(depth = 1)] | ||
amplitude: [f32; 2], | ||
|
||
array: [i32; 4], | ||
#[tree(depth = 1)] | ||
opt: Option<i32>, | ||
exit: bool, | ||
} | ||
|
||
async fn mqtt_client() { | ||
// Construct a Minimq client to the broker for publishing requests. | ||
let mut buffer = [0u8; 1024]; | ||
let mut mqtt: minimq::Minimq<'_, _, _, minimq::broker::NamedBroker<Stack>> = | ||
minimq::Minimq::new( | ||
Stack, | ||
StandardClock::default(), | ||
minimq::ConfigBuilder::new( | ||
minimq::broker::NamedBroker::new("localhost", Stack).unwrap(), | ||
&mut buffer, | ||
) | ||
.client_id("tester") | ||
.unwrap() | ||
.keepalive_interval(60), | ||
); | ||
|
||
// Wait for the broker connection | ||
while !mqtt.client().is_connected() { | ||
mqtt.poll(|_client, _topic, _message, _properties| {}) | ||
.unwrap(); | ||
tokio::time::sleep(Duration::from_millis(100)).await; | ||
} | ||
|
||
log::info!("Test client connected"); | ||
|
||
// Wait momentarily for the other client to connect. | ||
tokio::time::sleep(Duration::from_secs(1)).await; | ||
|
||
// Configure settings. | ||
mqtt.client() | ||
.publish( | ||
Publication::new(b"32.4") | ||
.topic("sample/prefix/settings/amplitude/0") | ||
.finish() | ||
.unwrap(), | ||
) | ||
.unwrap(); | ||
tokio::time::sleep(Duration::from_millis(100)).await; | ||
|
||
mqtt.client() | ||
.publish( | ||
Publication::new(b"10") | ||
.topic("sample/prefix/settings/inner/frame_rate") | ||
.finish() | ||
.unwrap(), | ||
) | ||
.unwrap(); | ||
tokio::time::sleep(Duration::from_millis(100)).await; | ||
|
||
mqtt.client() | ||
.publish( | ||
Publication::new(b"true") | ||
.topic("sample/prefix/settings/exit") | ||
.finish() | ||
.unwrap(), | ||
) | ||
.unwrap(); | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
env_logger::init(); | ||
|
||
// Spawn a task to send MQTT messages. | ||
tokio::task::spawn(async move { mqtt_client().await }); | ||
|
||
let mut buffer = [0u8; 1024]; | ||
let localhost: minimq::embedded_nal::IpAddr = "127.0.0.1".parse().unwrap(); | ||
|
||
// Construct a settings configuration interface. | ||
let mut client: miniconf_mqtt::MqttClient<'_, _, _, _, minimq::broker::IpBroker, 2> = | ||
miniconf_mqtt::MqttClient::new( | ||
Stack, | ||
"sample/prefix", | ||
StandardClock::default(), | ||
minimq::ConfigBuilder::new(localhost.into(), &mut buffer).keepalive_interval(60), | ||
) | ||
.unwrap(); | ||
let mut client = miniconf_mqtt::MqttClient::new( | ||
Stack, | ||
"dt/sinara/dual-iir/01-02-03-04-05-06", | ||
StandardClock::default(), | ||
minimq::ConfigBuilder::<'_, minimq::broker::IpBroker>::new(localhost.into(), &mut buffer) | ||
.keepalive_interval(60), | ||
) | ||
.unwrap(); | ||
|
||
let mut settings = Settings::default(); | ||
loop { | ||
while !settings.exit { | ||
tokio::time::sleep(Duration::from_millis(10)).await; | ||
if client.update(&mut settings).unwrap() { | ||
println!("Settings updated: {:?}", settings); | ||
} | ||
|
||
if settings.exit { | ||
break; | ||
} | ||
|
||
tokio::time::sleep(Duration::from_millis(10)).await; | ||
} | ||
println!("Exiting on request"); | ||
} |
Oops, something went wrong.