domain-iot by actionbook/rust-skills
npx skills add https://github.com/actionbook/rust-skills --skill domain-iot第 3 层:领域约束
| 领域规则 | 设计约束 | Rust 实现含义 |
|---|---|---|
| 不可靠网络 | 离线优先 | 本地缓冲 |
| 电源约束 | 高效代码 | 休眠模式,最少内存分配 |
| 资源限制 | 小体积 | 在需要时使用 no_std |
| 安全性 | 加密通信 | TLS,签名固件 |
| 可靠性 | 自我恢复 | 看门狗,错误处理 |
| OTA 更新 | 安全升级 | 回滚能力 |
RULE: 网络可能随时中断
WHY: 无线,远程位置
RUST: 本地队列,带退避的重试机制
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
RULE: 最小化功耗
WHY: 电池寿命,能源成本
RUST: 休眠模式,高效算法
RULE: 所有通信必须加密
WHY: 可能物理接触设备
RUST: TLS,签名消息
从约束到设计(第 2 层):
"需要离线优先设计"
↓ m12-lifecycle: 带持久化的本地缓冲区
↓ m13-domain-error: 带退避的重试机制
"需要电源效率"
↓ domain-embedded: no_std 模式
↓ m10-performance: 最少内存分配
"需要可靠的消息传递"
↓ m07-concurrency: 带超时的异步处理
↓ MQTT: QoS 级别
| 环境 | 技术栈 | Crate |
|---|---|---|
| Linux 网关 | tokio + std | rumqttc, reqwest |
| MCU 设备 | embassy + no_std | embedded-hal |
| 混合环境 | 拆分工作负载 | 两者 |
| 用途 | Crate |
|---|---|
| MQTT (std) | rumqttc, paho-mqtt |
| 嵌入式 | embedded-hal, embassy |
| 异步 (std) | tokio |
| 异步 (no_std) | embassy |
| 日志记录 (no_std) | defmt |
| 日志记录 (std) | tracing |
| 模式 | 目的 | 实现 |
|---|---|---|
| 发布/订阅 | 设备通信 | MQTT 主题 |
| 边缘计算 | 本地处理 | 上传前过滤 |
| OTA 更新 | 固件升级 | 签名 + 回滚 |
| 电源管理 | 电池寿命 | 休眠 + 唤醒事件 |
| 存储转发 | 网络可靠性 | 本地队列 |
use rumqttc::{AsyncClient, MqttOptions, QoS};
async fn run_mqtt() -> anyhow::Result<()> {
let mut options = MqttOptions::new("device-1", "broker.example.com", 1883);
options.set_keep_alive(Duration::from_secs(30));
let (client, mut eventloop) = AsyncClient::new(options, 10);
// 订阅命令
client.subscribe("devices/device-1/commands", QoS::AtLeastOnce).await?;
// 发布遥测数据
tokio::spawn(async move {
loop {
let data = read_sensor().await;
client.publish("devices/device-1/telemetry", QoS::AtLeastOnce, false, data).await.ok();
tokio::time::sleep(Duration::from_secs(60)).await;
}
});
// 处理事件
loop {
match eventloop.poll().await {
Ok(event) => handle_event(event).await,
Err(e) => {
tracing::error!("MQTT error: {}", e);
tokio::time::sleep(Duration::from_secs(5)).await;
}
}
}
}
| 错误 | 违反的领域规则 | 修复方法 |
|---|---|---|
| 无重试逻辑 | 数据丢失 | 指数退避 |
| 无线电常开 | 电池耗尽 | 发送间隔休眠 |
| 未加密的 MQTT | 安全风险 | TLS |
| 无本地缓冲区 | 网络中断 = 数据丢失 | 本地持久化存储 |
| 约束 | 第 2 层模式 | 第 1 层实现 |
|---|---|---|
| 离线优先 | 存储转发 | 本地队列 + 刷新 |
| 电源效率 | 休眠模式 | 基于定时器的唤醒 |
| 网络可靠性 | 重试 | tokio-retry, backoff |
| 安全性 | TLS | rustls, native-tls |
| 何时需要 | 参见 |
|---|---|
| 嵌入式模式 | domain-embedded |
| 异步模式 | m07-concurrency |
| 错误恢复 | m13-domain-error |
| 性能 | m10-performance |
每周安装量
34
代码仓库
GitHub 星标数
828
首次出现
Jan 23, 2026
安全审计
安装于
gemini-cli31
opencode31
codex29
claude-code26
cursor25
github-copilot25
Layer 3: Domain Constraints
| Domain Rule | Design Constraint | Rust Implication |
|---|---|---|
| Unreliable network | Offline-first | Local buffering |
| Power constraints | Efficient code | Sleep modes, minimal alloc |
| Resource limits | Small footprint | no_std where needed |
| Security | Encrypted comms | TLS, signed firmware |
| Reliability | Self-recovery | Watchdog, error handling |
| OTA updates | Safe upgrades | Rollback capability |
RULE: Network can fail at any time
WHY: Wireless, remote locations
RUST: Local queue, retry with backoff
RULE: Minimize power consumption
WHY: Battery life, energy costs
RUST: Sleep modes, efficient algorithms
RULE: All communication encrypted
WHY: Physical access possible
RUST: TLS, signed messages
From constraints to design (Layer 2):
"Need offline-first design"
↓ m12-lifecycle: Local buffer with persistence
↓ m13-domain-error: Retry with backoff
"Need power efficiency"
↓ domain-embedded: no_std patterns
↓ m10-performance: Minimal allocations
"Need reliable messaging"
↓ m07-concurrency: Async with timeout
↓ MQTT: QoS levels
| Environment | Stack | Crates |
|---|---|---|
| Linux gateway | tokio + std | rumqttc, reqwest |
| MCU device | embassy + no_std | embedded-hal |
| Hybrid | Split workloads | Both |
| Purpose | Crate |
|---|---|
| MQTT (std) | rumqttc, paho-mqtt |
| Embedded | embedded-hal, embassy |
| Async (std) | tokio |
| Async (no_std) | embassy |
| Logging (no_std) | defmt |
| Logging (std) | tracing |
| Pattern | Purpose | Implementation |
|---|---|---|
| Pub/Sub | Device comms | MQTT topics |
| Edge compute | Local processing | Filter before upload |
| OTA updates | Firmware upgrade | Signed + rollback |
| Power mgmt | Battery life | Sleep + wake events |
| Store & forward | Network reliability | Local queue |
use rumqttc::{AsyncClient, MqttOptions, QoS};
async fn run_mqtt() -> anyhow::Result<()> {
let mut options = MqttOptions::new("device-1", "broker.example.com", 1883);
options.set_keep_alive(Duration::from_secs(30));
let (client, mut eventloop) = AsyncClient::new(options, 10);
// Subscribe to commands
client.subscribe("devices/device-1/commands", QoS::AtLeastOnce).await?;
// Publish telemetry
tokio::spawn(async move {
loop {
let data = read_sensor().await;
client.publish("devices/device-1/telemetry", QoS::AtLeastOnce, false, data).await.ok();
tokio::time::sleep(Duration::from_secs(60)).await;
}
});
// Process events
loop {
match eventloop.poll().await {
Ok(event) => handle_event(event).await,
Err(e) => {
tracing::error!("MQTT error: {}", e);
tokio::time::sleep(Duration::from_secs(5)).await;
}
}
}
}
| Mistake | Domain Violation | Fix |
|---|---|---|
| No retry logic | Lost data | Exponential backoff |
| Always-on radio | Battery drain | Sleep between sends |
| Unencrypted MQTT | Security risk | TLS |
| No local buffer | Network outage = data loss | Persist locally |
| Constraint | Layer 2 Pattern | Layer 1 Implementation |
|---|---|---|
| Offline-first | Store & forward | Local queue + flush |
| Power efficiency | Sleep patterns | Timer-based wake |
| Network reliability | Retry | tokio-retry, backoff |
| Security | TLS | rustls, native-tls |
| When | See |
|---|---|
| Embedded patterns | domain-embedded |
| Async patterns | m07-concurrency |
| Error recovery | m13-domain-error |
| Performance | m10-performance |
Weekly Installs
34
Repository
GitHub Stars
828
First Seen
Jan 23, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykWarn
Installed on
gemini-cli31
opencode31
codex29
claude-code26
cursor25
github-copilot25
Kotlin Ktor 服务器模式指南:构建健壮 HTTP API 的架构与最佳实践
1,100 周安装