Ниже показан английский оригинал.
MqttTopic
MqttTopic subscribes to one topic a publisher can name, and answers for its own retry copies.
A topic carries no wildcard, so it is a name a publisher can use.
A subscription on a topic is reached again by publishing to that topic, which is why the descriptor can say where the copy of a retried delivery goes.
A value with a + or a # is refused when the app starts, and the message names MqttFilter as the descriptor that takes it.
The topic must not be empty and must be text an MQTT string carries: no control character and no Unicode non-character.
qos sets the quality of service the subscription asks for, and shared makes it a shared subscription.
A definition written #[subscriber(MqttTopic)] takes its topic from the name given at the mount site.
Example
A valve listens on its own topic.
- Macros
- Manual
/// The valve listens on its own topic.
#[subscriber(MqttTopic::new("valves/v1/set"))]
async fn set(command: &Command) -> HandlerOutcome {
let _ = command.open;
HandlerOutcome::ack()
}
#[ruststream::app(runtime = embassy)]
fn app(broker: MqttBroker) -> EmbeddedApp<impl Mounts> {
EmbeddedApp::new().with_broker(broker, |b| b.include(set))
}
/// The valve listens on its own topic.
struct Set;
impl Handle<Command> for Set {
fn handle(
&self,
_command: &Command,
_outs: &(),
_ctx: &mut Context<'_>,
) -> impl Future<Output = Result<(), HandlerOutcome>> {
ready(Ok(()))
}
}
fn app(broker: MqttBroker) -> EmbeddedApp<impl Mounts> {
EmbeddedApp::new().with_broker(broker, |b| {
b.include(
subscriber("valves/v1/set", Set)
.map_source(|_| MqttTopic::new("valves/v1/set"))
.build(),
)
})
}