Struct miniconf::MqttClient
source · pub struct MqttClient<Settings, Stack, Clock, const MESSAGE_SIZE: usize>where
Settings: Miniconf + Clone,
Stack: TcpClientStack,
Clock: Clock,{ /* private fields */ }Expand description
MQTT settings interface.
Design
The MQTT client places the Miniconf paths <path> at the MQTT <prefix>/settings/<path> topic,
where <prefix> is provided in the client constructor.
It publishes its alive-ness as a 1 to <prefix>/alive and sets a will to publish 0 there when
it is disconnected.
Limitations
The MQTT client logs failures to subscribe to the settings topic, but does not re-attempt to connect to it when errors occur.
The client only supports paths up to 128 byte length and maximum depth of 8. Keepalive interval and re-publication timeout are fixed to 60 and 2 seconds respectively.
Example
use miniconf::{MqttClient, Miniconf};
#[derive(Miniconf, Clone, Default)]
struct Settings {
foo: bool,
}
let mut client: MqttClient<Settings, _, _, 256> = MqttClient::new(
std_embedded_nal::Stack::default(),
"", // client_id auto-assign
"quartiq/application/12345", // prefix
"127.0.0.1".parse().unwrap(),
std_embedded_time::StandardClock::default(),
Settings::default(),
)
.unwrap();
client.handled_update(|path, old_settings, new_settings| {
if new_settings.foo {
return Err("Foo!");
}
*old_settings = new_settings.clone();
Ok(())
}).unwrap();Implementations§
source§impl<Settings, Stack, Clock, const MESSAGE_SIZE: usize> MqttClient<Settings, Stack, Clock, MESSAGE_SIZE>where
Settings: Miniconf + Clone,
Stack: TcpClientStack,
Clock: Clock + Clone,
impl<Settings, Stack, Clock, const MESSAGE_SIZE: usize> MqttClient<Settings, Stack, Clock, MESSAGE_SIZE>where Settings: Miniconf + Clone, Stack: TcpClientStack, Clock: Clock + Clone,
sourcepub fn new(
stack: Stack,
client_id: &str,
prefix: &str,
broker: IpAddr,
clock: Clock,
settings: Settings
) -> Result<Self, Error<Stack::Error>>
pub fn new( stack: Stack, client_id: &str, prefix: &str, broker: IpAddr, clock: Clock, settings: Settings ) -> Result<Self, Error<Stack::Error>>
Construct a new MQTT settings interface.
Args
stack- The network stack to use for communication.client_id- The ID of the MQTT client. May be an empty string for auto-assigning.prefix- The MQTT device prefix to use for this device.broker- The IP address of the MQTT broker to use.clock- The clock for managing the MQTT connection.settings- The initial settings values.
sourcepub fn handled_update<F, E>(
&mut self,
handler: F
) -> Result<bool, Error<Stack::Error>>where
F: FnMut(&str, &mut Settings, &Settings) -> Result<(), E>,
E: AsRef<str>,
pub fn handled_update<F, E>( &mut self, handler: F ) -> Result<bool, Error<Stack::Error>>where F: FnMut(&str, &mut Settings, &Settings) -> Result<(), E>, E: AsRef<str>,
Update the MQTT interface and service the network. Pass any settings changes to the handler supplied.
Args
handler- A closure called with updated settings that can be used to apply current settings or validate the configuration. Arguments are (path, old_settings, new_settings).
Returns
True if the settings changed. False otherwise.
sourcepub fn update(&mut self) -> Result<bool, Error<Stack::Error>>
pub fn update(&mut self) -> Result<bool, Error<Stack::Error>>
Update the settings from the network stack without any specific handling.
Returns
True if the settings changed. False otherwise
sourcepub fn force_republish(&mut self)
pub fn force_republish(&mut self)
Force republication of the current settings.
Note
This is intended to be used if modification of a setting had side effects that affected another setting.