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,

source

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.
source

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.

source

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

source

pub fn settings(&self) -> &Settings

Get the current settings from miniconf.

source

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.

Auto Trait Implementations§

§

impl<Settings, Stack, Clock, const MESSAGE_SIZE: usize> RefUnwindSafe for MqttClient<Settings, Stack, Clock, MESSAGE_SIZE>where Clock: RefUnwindSafe, Settings: RefUnwindSafe, Stack: RefUnwindSafe, <Clock as Clock>::T: RefUnwindSafe, <Stack as TcpClientStack>::TcpSocket: RefUnwindSafe,

§

impl<Settings, Stack, Clock, const MESSAGE_SIZE: usize> Send for MqttClient<Settings, Stack, Clock, MESSAGE_SIZE>where Clock: Send, Settings: Send, Stack: Send, <Clock as Clock>::T: Send, <Stack as TcpClientStack>::TcpSocket: Send,

§

impl<Settings, Stack, Clock, const MESSAGE_SIZE: usize> Sync for MqttClient<Settings, Stack, Clock, MESSAGE_SIZE>where Clock: Sync, Settings: Sync, Stack: Sync, <Clock as Clock>::T: Sync, <Stack as TcpClientStack>::TcpSocket: Sync,

§

impl<Settings, Stack, Clock, const MESSAGE_SIZE: usize> Unpin for MqttClient<Settings, Stack, Clock, MESSAGE_SIZE>where Clock: Unpin, Settings: Unpin, Stack: Unpin, <Clock as Clock>::T: Unpin, <Stack as TcpClientStack>::TcpSocket: Unpin,

§

impl<Settings, Stack, Clock, const MESSAGE_SIZE: usize> UnwindSafe for MqttClient<Settings, Stack, Clock, MESSAGE_SIZE>where Clock: UnwindSafe, Settings: UnwindSafe, Stack: UnwindSafe, <Clock as Clock>::T: UnwindSafe, <Stack as TcpClientStack>::TcpSocket: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.