设备类型开发
本文讲解 NeoMind 的设备数据模型、数据接入协议(MQTT / Webhook)、自动发现流程,以及如何用 CLI 管理设备。读完你能把任何传感器或执行器接入 NeoMind。
仓库:设备类型定义提交到 camthink-ai/NeoMind-DeviceTypes。运行时设备管理在主仓库
neomind-devicescrate 中。
设备数据模型
DeviceConfig(设备实例)
每个接入 NeoMind 的设备都是一个 DeviceConfig 实例,存储在 data/device_registry.redb 中:
pub struct DeviceConfig {
pub device_id: String, // 全局唯一 ID(如 "living-room-sensor")
pub name: String, // 显示名(如 "客厅温湿度")
pub device_type: String, // 引用 DeviceTypeTemplate(如 "dht22_sensor")
pub adapter_type: String, // 适配器类型:mqtt / webhook / hass
pub connection_config: ConnectionConfig, // 协议相关配置
pub adapter_id: Option<String>, // 管理此设备的适配器 ID
pub last_seen: i64, // 最后活跃时间戳(0 = 从未连接)
}
ConnectionConfig(连接配置)
连接配置是协议无关的结构,通过 Option 字段支持多种协议:
pub struct ConnectionConfig {
// MQTT 相关
pub telemetry_topic: Option<String>, // 上行数据主题
pub command_topic: Option<String>, // 下行命令主题
pub json_path: Option<String>, // JSON 取值路径
// Home Assistant 相关
pub entity_id: Option<String>,
// 其他协议特定参数(扁平化扩展)
#[serde(flatten)]
pub extra: HashMap<String, serde_json::Value>,
}
DeviceTypeTemplate(设备类型模板)
设备类型模板定义了一类设备的指标和命令,可被多个设备实例引用:
pub struct DeviceTypeTemplate {
pub device_type: String, // 唯一标识(如 "dht22_sensor")
pub name: String, // 显示名
pub description: String,
pub categories: Vec<String>, // 分组(如 ["sensor", "environment"])
pub mode: DeviceTypeMode, // Simple 或 Full
pub metrics: Vec<MetricDefinition>, // 提供的指标
pub uplink_samples: Vec<Value>, // Simple 模式的样本数据
pub commands: Vec<CommandDefinition>, // 支持的命令
}
MetricDefinition(指标定义)
pub struct MetricDefinition {
pub name: String, // 指标名(支持点号嵌套,如 "values.temperature")
pub display_name: String, // 显示名
pub data_type: MetricDataType, // Float / Integer / Boolean / String / Binary / Enum
pub unit: String, // 单位(如 "°C"、"%"、"hPa")
pub min: Option<f64>, // 数值范围(可选)
pub max: Option<f64>,
}
DataSourceId 格式为
device:{device_id}:{metric_name},仪表板和规则引擎通过此 ID 引用设备指标。
数据接入协议
MQTT(推荐)
NeoMind 内嵌 MQTT 3.1.1 Broker(端口 1883),设备直接连接即可。
主题格式:
| 方向 | 主题 | 说明 |
|---|---|---|
| 上行(遥测) | device/{device_type}/{device_id}/uplink | 设备发送数据 |
| 下行(命令) | device/{device_type}/{device_id}/downlink | NeoMind 发送命令 |
Payload 格式(JSON):
{
"temperature": 23.5,
"humidity": 65.2,
"battery": 3.7
}
如果设备 payload 有嵌套结构,可在 ConnectionConfig 的 json_path 中指定取值路径(如 data.values)。
Webhook
适用于只有 HTTP 能力的设备(如 ESP32-CAM、树莓派)。
端点:POST http://<服务器IP>:9375/api/devices/{device_id}/webhook
Payload:
{
"timestamp": 1718300000,
"quality": 1.0,
"data": {
"temperature": 25.3,
"humidity": 60
}
}
timestamp 可省略(自动用当前时间),data 对象的 key 必须与设备类型的 metric name 对应。
自动发现流程
NeoMind 默认开启自动发现。任何向 MQTT Broker 发送数据的未知设备都会被捕获为 Draft(草稿),等待用户审批。
设备发布数据到任意 MQTT 主题
│
▼
NeoMind 匹配已知设备类型?─── 是 ──→ 直接创建设备(自动注册)
│ 否
▼
创建 Draft(收集 5 条样本数据)
│
▼
用户在 Web UI 或 CLI 审批 ─── approve ──→ 注册为正式设备
│ │
│ reject │ 指标开始写入 telemetry.redb
▼ │
删除 Draft ▼
仪表板 / 规则 / Agent 可用
CLI 管理 Draft:
# 查看待审批设备
neomind device drafts list
# 查看详情(含样本数据)
neomind device drafts get <DRAFT_ID>
# 审批通过(指定名称和类型)
neomind device drafts approve <DRAFT_ID> --name "客厅传感器" --type dht22_sensor
# 拒绝
neomind device drafts reject <DRAFT_ID>