6 releases (breaking)
0.5.0 | Aug 29, 2024 |
---|---|
0.4.0 | Jun 13, 2024 |
0.3.0 | Nov 1, 2023 |
0.2.0 | Jun 22, 2023 |
0.1.0 | Mar 28, 2022 |
#1328 in Embedded development
Used in booster
17KB
268 lines
minireq
Minimal MQTT-based request/response library
lib.rs
:
MQTT Request/response Handling
Overview
This library is intended to be an easy way to handle inbound requests. You can subscribe to topics belonging to some prefix, and Minireq will ensure that these topics are published to the MQTT broker upon connection to handle discoverability.
Minireq also simplifies the process of generating responses to the inbound request automatically.
Example
use embedded_io::Write;
#[derive(serde::Serialize, serde::Deserialize)]
struct Request {
data: u32,
}
// Handler function for processing an incoming request.
pub fn test_handler(
data: &[u8],
mut output_buffer: &mut [u8]
) -> Result<usize, &'static str> {
// Deserialize the request.
let mut request: Request = serde_json_core::from_slice(data).unwrap().0;
let response = request.data.wrapping_add(1);
let start = output_buffer.len();
write!(output_buffer, "{}", response).unwrap();
Ok(start - output_buffer.len())
}
let mqtt: minimq::Minimq<'_, _, _, minimq::broker::IpBroker> = minireq::minimq::Minimq::new(
// Constructor
);
// Construct the client
let mut client: minireq::Minireq<_, _, _> = minireq::Minireq::new(
"prefix/device",
mqtt,
)
.unwrap();
// We want to listen for any messages coming in on the "prefix/device/command/test" topic
client.subscribe("test").unwrap();
// ...
loop {
// In your main execution loop, continually poll the client to process incoming requests.
client.poll(|command, data, buffer| {
match command {
"test" => test_handler(data, buffer),
_ => unreachable!(),
}
}).unwrap();
}
Dependencies
~2–2.8MB
~55K SLoC