英文版本为准
本页译自英文,由模型翻译。若与英文原文存在差异,以英文原文为准。
在 compio 主机上入门
在 compio 主机上,服务只需要处理器和应用。
MqttBroker::compio 在连接 Broker 的核心上启动 runner,使用 compio 的 TCP 流,因此不需要手动启动任何任务。
堆变体
主机有堆,本 crate 的堆变体会使用它:即 alloc 特性,compio 会开启它。
Broker 运行在 rust-mqtt 客户端之上,设置持有自有字符串,处理器用框架的编解码器解码载荷,json 是其中之一。
Broker 不需要 MqttState,因为客户端自己分配收到的内容。
带有堆的设备同样使用这个变体,开启 alloc 和 embassy 即可。
它用 MqttBroker::new(config) 构建 Broker,并把 runner 作为任务启动,方式与在设备上入门相同,处理器使用 serde 类型。
没有堆的设备使用设备变体。
所需的 crate
cargo add ruststream --no-default-features --features compio,macros,json
cargo add ruststream-rust-mqtt --features compio
服务
- 宏
- 手写
/// Takes every reading the sensors publish.
#[subscriber(MqttTopic::new("sensors/temp"))]
async fn store(reading: &Reading) -> HandlerOutcome {
let _ = reading.celsius;
HandlerOutcome::ack()
}
#[ruststream::app(runtime = compio, cores = 1)]
fn app() -> impl App {
let config = MqttConfig::new("mqtt://localhost:1883").client_id("collector");
RustStream::new(AppInfo::new("collector", "0.1.0"))
.with_broker(MqttBroker::compio(config), |b| b.include(store))
}
/// Takes every reading the sensors publish.
struct Store;
impl Handle<Reading> for Store {
fn handle(
&self,
_reading: &Reading,
_outs: &(),
_ctx: &mut Context<'_>,
) -> impl Future<Output = Result<(), HandlerOutcome>> {
ready(Ok(()))
}
}
fn app() -> impl App {
let config = MqttConfig::new("mqtt://localhost:1883").client_id("collector");
RustStream::new(AppInfo::new("collector", "0.1.0")).with_broker(
MqttBroker::compio(config),
|b| {
b.include(
subscriber("sensors/temp", Store)
.map_source(|_| MqttTopic::new("sensors/temp"))
.build(),
)
},
)
}
#[ruststream::app(runtime = compio, cores = 1)] 生成二进制程序的 main,它理解命令 run。
cargo run -- run 针对配置中指定的服务器启动服务。
订阅、确认和发布的工作方式与设备上相同,只有网络、运行时和内存不同。
测试 compio 服务展示这个应用的测试。
带有堆的设备
Runner 是开发板的一个任务,Broker 只接收配置。
- 宏
- 手写
/// The runner owns the connection, and the app starts only while it runs.
#[embassy_executor::task]
async fn mqtt(runner: MqttRunner, stack: Stack<'static>) -> ! {
let mut rx = [0; 1024];
let mut tx = [0; 1024];
runner
.run(EmbassyNetwork::new(stack, &mut rx, &mut tx))
.await
}
/// What the board's `main` does once its network stack is up.
fn start(spawner: Spawner, stack: Stack<'static>) {
let config = MqttConfig::new("192.168.1.10:1883").client_id("sensor-7");
let (broker, runner) = MqttBroker::new(config);
spawner.spawn(mqtt(runner, stack).expect("the pool holds the runner's task"));
spawner.spawn(app::task(broker).expect("the pool holds the app's task"));
}
/// The runner owns the connection, and the app starts only while it runs.
#[embassy_executor::task]
async fn mqtt(runner: MqttRunner, stack: Stack<'static>) -> ! {
let mut rx = [0; 1024];
let mut tx = [0; 1024];
runner
.run(EmbassyNetwork::new(stack, &mut rx, &mut tx))
.await
}
/// The app's control: `stop` asks it to shut down, and `health` reads where it is.
static CONTROL: Control = Control::new();
/// Runs the app until the control stops it.
#[embassy_executor::task]
async fn service(broker: MqttBroker) {
let _ = app(broker).run(&CONTROL).await;
}
/// What the board's `main` does once its network stack is up.
fn start(spawner: Spawner, stack: Stack<'static>) {
let config = MqttConfig::new("192.168.1.10:1883").client_id("sensor-7");
let (broker, runner) = MqttBroker::new(config);
spawner.spawn(mqtt(runner, stack).expect("the pool holds the runner's task"));
spawner.spawn(service(broker).expect("the pool holds the app's task"));
}