IoT 连接器
Savitri IoT 连接器使物联网设备能够将传感器数据提交到链上。它提供设备管理、数据接入、边缘处理、批量优化和企业级安全性。
架构
IoT Devices (sensors, actuators, gateways)
│
▼
Protocol Handlers (MQTT, CoAP)
│ parse, validate, authenticate
▼
Edge Processing Engine
│ rules engine, anomaly detection, aggregation
▼
Batch Processor
│ compress, aggregate, schedule
▼
Blockchain Interface (savitri_sendRawTransaction)
│ submit batch as IoT TX (0.00005 SAVT fee)
▼
On-chain Oracle Feed
设备管理
设备类型
| 类型 | 描述 | 示例 |
|---|---|---|
Sensor | 只读数据生产者 | 温度传感器 |
Actuator | 可控设备 | 智能阀门 |
Gateway | 边缘聚合点 | 树莓派集线器 |
EdgeNode | 本地处理单元 | 边缘 ML 服务器 |
Mobile | 移动设备 | 智能手机 |
Industrial | 工业设备 | PLC/SCADA |
设备注册
pub struct Device {
pub id: DeviceId,
pub owner: [u8; 32], // owner address
pub public_key: [u8; 32], // device signing key
pub device_type: DeviceType,
pub capabilities: Vec<String>,
pub status: DeviceStatus,
pub metadata: HashMap<String, String>,
}
设备通过其公钥在链上注册以进行身份验证。设备组可以集体管理。
设备状态
| 状态 | 描述 |
|---|---|
Active | 正常运行 |
Inactive | 临时离线 |
Suspended | 被所有者/治理暂停 |
Banned | 永久撤销 |
数据接入
协议支持
| 协议 | 处理器 | 使用场景 |
|---|---|---|
| MQTT | MQTTHandler | 低功耗传感器、发布/订阅 |
| CoAP | CoAPHandler | 受限设备、UDP |
两者均实现 ProtocolHandler 特征:
pub trait ProtocolHandler: Send + Sync {
async fn handle_connection(&self, stream: TcpStream) -> Result<()>;
fn parse_data(&self, raw: &[u8]) -> Result<IoTData>;
fn validate_device(&self, device_id: &str, signature: &[u8]) -> Result<bool>;
}
IoT 数据格式
pub struct IoTData {
pub device_id: String,
pub timestamp: u64,
pub data_type: String, // "temperature", "humidity", etc.
pub value: DataValue,
pub unit: String, // "celsius", "percent", etc.
pub quality: f64, // 0.0-1.0 (data quality score)
pub metadata: HashMap<String, String>,
}
pub enum DataValue {
Float(f64),
Integer(i64),
Boolean(bool),
String(String),
Binary(Vec<u8>),
Struct(HashMap<String, DataValue>),
Array(Vec<DataValue>),
}
边缘处理
边缘处理引擎在提交前对数据进行过滤、转换和聚合。
规则引擎
// Example rule: alert if temperature > 40
Rule {
condition: Condition::GT("temperature", 40.0),
action: ProcessingAction::CriticalAlert,
}
// Logical operators
Condition::AND(vec![
Condition::GT("temperature", 35.0),
Condition::LT("humidity", 20.0),
])
运算符:EQ、NEQ、GT、LT、GTE、LTE、CONTAINS、IN、AND、OR。
异常检测
边缘处理器对传入数据进行异常检测:
- 统计离群值检测
- 模式识别
- ML 模型推断(本地模型)
异常情况无论批次计划如何都会触发自动提交。
数据聚合
| 策略 | 描述 |
|---|---|
Average | 窗口内值的平均值 |
Sum | 值的总和 |
Min / Max | 极值 |
Median | 中间值 |
Percentile(p) | 第 P 百分位数 |
批量处理
单个传感器读数被批量处理以降低交易成本:
pub struct BatchProcessor {
pub batch_size: usize, // max readings per batch
pub max_delay_ms: u64, // max time before forced submit
pub compression: bool, // compress batch payload
pub aggregation: bool, // aggregate before submit
}
批量触发条件
| 触发条件 | 描述 |
|---|---|
| 达到大小 | 累积了 batch_size 个读数 |
| 最大延迟 | 自第一次读数起经过了 max_delay_ms |
| 高优先级 | 检测到异常或关键警报 |
与逐次读数提交相比,批量处理可将区块链交易减少高达 90%。
安全性
身份验证
三种身份验证方法,可组合使用:
| 方法 | 描述 |
|---|---|
| 证书 | X.509 证书链验证 |
| 令牌 | 带有效期的 API 令牌 |
| 生物特征 | 设备生物特征验证 |
数据加密
- AES-256 加密用于传输中和静止时的数据
- 完整性哈希用于篡改检测
- 证书吊销检查
连接器配置
pub struct ConnectorConfig {
pub max_requests_per_second: u32,
pub timeout_ms: u64,
pub retry_attempts: u32,
}
链上集成
IoT 数据以 IoT 费率的交易形式提交:
let tx = TransactionBuilder::new()
.oracle_call(
"iot_oracle_address",
"submit_sensor_data",
&batch_payload,
)
.nonce(nonce)
.fee(50_000_000_000_000) // 0.00005 SAVT (IoT rate)
.build_and_sign(&device_wallet)?;
预言机馈送集成
提交的 IoT 数据可作为预言机馈送使用:
// Request latest sensor reading
let tx = oracle.request_data(
&iot_oracle_address,
"temperature",
b"sensor_001",
).await?;
连接器指标
pub struct ConnectorMetrics {
pub requests_served: u64,
pub errors_count: u64,
pub average_response_time: f64,
}