Эта страница ещё не переведена
Ниже показан английский оригинал.
MqttPublishSteps
The steps qos and retain on the publish builder reopen, for one message, what the policy of the mount fixed.
MqttPublishSteps is in the prelude, and the steps appear on a builder over an MQTT publisher and on no other broker's.
A setting that a call leaves unset keeps the value of the policy.
A handler body that takes a step bounds its slot with Out<impl Publisher<Options = MqttPublishOptions>, Slot>, and that is the one place a body names a type of this crate.
The settings of one call travel as an MqttPublishOptions, and a test reads them back with with_options.
Example
A state change is sent exactly once and retained.
- Macros
- Manual
/// A state change is sent exactly once, and retained so a dashboard that starts later reads it.
#[subscriber(MqttTopic::new("valves/v1/position"))]
async fn track(
position: &Position,
Out(states): Out<impl Publisher<Options = MqttPublishOptions>, States>,
) -> HandlerOutcome {
let state = State {
open: position.percent > 0,
};
if states
.message(&state)
.qos(Qos::ExactlyOnce)
.retain(true)
.publish()
.await
.is_err()
{
return HandlerOutcome::retry();
}
HandlerOutcome::ack()
}
#[ruststream::app(runtime = embassy)]
fn app(broker: MqttBroker) -> EmbeddedApp<impl Mounts> {
EmbeddedApp::new().with_broker(broker, |b| {
b.include(track).out(States, MqttPublish::default())
})
}
/// A state change is sent exactly once, and retained so a dashboard that starts later reads it.
struct Track;
impl<Entry> Handle<Position, (), Outs<(Entry,)>> for Track
where
Entry: OutEntry<States, Wire: Publisher<Options = MqttPublishOptions>>,
{
async fn handle(
&self,
position: &Position,
outs: &Outs<(Entry,)>,
_ctx: &mut Context<'_>,
) -> Result<(), HandlerOutcome> {
let state = State {
open: position.percent > 0,
};
outs.get(States)
.message(&state)
.qos(Qos::ExactlyOnce)
.retain(true)
.publish()
.await
.map_err(|_| HandlerOutcome::retry())
}
}
fn app(broker: MqttBroker) -> EmbeddedApp<impl Mounts> {
EmbeddedApp::new().with_broker(broker, |b| {
b.include(
subscriber("valves/v1/position", Track)
.map_source(|_| MqttTopic::new("valves/v1/position"))
.build(),
)
.out(States, MqttPublish::default())
})
}