npx skills add https://github.com/zhanghandong/rust-skills --skill domain-iot第 3 层:领域约束
| 领域规则 | 设计约束 | Rust 实现含义 |
|---|---|---|
| 不可靠网络 | 离线优先 | 本地缓冲 |
| 电源约束 | 高效代码 | 休眠模式,最少分配 |
| 资源限制 | 小内存占用 | 在需要时使用 no_std |
| 安全性 | 加密通信 | TLS,签名固件 |
| 可靠性 | 自我恢复 | 看门狗,错误处理 |
| OTA 更新 | 安全升级 | 回滚能力 |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
RULE: 网络可能随时中断
WHY: 无线,远程位置
RUST: 本地队列,带退避的重试
RULE: 最小化功耗
WHY: 电池寿命,能源成本
RUST: 休眠模式,高效算法
RULE: 所有通信加密
WHY: 可能物理接触
RUST: TLS,签名消息
从约束到设计(第 2 层):
"需要离线优先设计"
↓ m12-lifecycle: 带持久化的本地缓冲区
↓ m13-domain-error: 带退避的重试
"需要电源效率"
↓ domain-embedded: no_std 模式
↓ m10-performance: 最少内存分配
"需要可靠的消息传递"
↓ m07-concurrency: 带超时的异步
↓ MQTT: QoS 级别
| 环境 | 技术栈 | 库 |
|---|---|---|
| Linux 网关 | tokio + std | rumqttc, reqwest |
| MCU 设备 | embassy + no_std | embedded-hal |
| 混合 | 拆分工作负载 | 两者 |
| 用途 | 库 |
|---|---|
| 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 |
每周安装量
424
代码仓库
GitHub 星标数
920
首次出现
2026年1月20日
安全审计
安装于
opencode384
codex371
gemini-cli363
github-copilot355
amp315
kimi-cli311
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
424
Repository
GitHub Stars
920
First Seen
Jan 20, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykWarn
Installed on
opencode384
codex371
gemini-cli363
github-copilot355
amp315
kimi-cli311
平台战略框架指南:24位行业领袖教你设计、执行与扩展平台业务
670 周安装
SaaS财务指标速查表:收入、CAC、LTV等关键公式与基准快速查阅
414 周安装
iOS WidgetKit 开发指南:构建主屏幕、锁屏、灵动岛小组件与实时活动
414 周安装
web-haptics:为Web应用添加触觉反馈的JavaScript库,支持React、Vue、Svelte框架
414 周安装
PyTorch深度学习开发专家 | Transformer、扩散模型、LLM开发指南与最佳实践
414 周安装
OMX:OpenAI Codex CLI 多智能体编排工具,实现AI驱动开发工作流自动化
414 周安装
MAESTRO:AI驱动的精英软件架构治理框架 - 提升代码质量与项目连续性
414 周安装