feat: plugin-sdk同步Device类型 + example-plugin适配新Message变体

This commit is contained in:
showen
2026-03-13 06:58:28 +08:00
parent 2c14f5f04c
commit 48d1eeb7c4
4 changed files with 189 additions and 0 deletions

View File

@@ -2311,3 +2311,23 @@ DevicePlugin 阶段一 Task 4集成测试 ✅
### 下一步
Task 4 已完成DevicePlugin 阶段一基础框架全部就绪。等待 PM 刘建国向 CEO 汇报。
---
**李思琪** (2026-03-13 15:42)
已完成 plugin-sdk 与主库 Device 类型同步任务。
**完成内容**
1. 在 plugin-sdk/src/lib.rs 的 Message enum 中添加了 DeviceCommand、DeviceResponse、DeviceEvent 三个变体
2. 定义了完整的 Device 相关类型PixelFormat、SensorType、TouchAction、DeviceCapability、DeviceCommand、DeviceResponse、DeviceEvent与主库 src/core/message.rs 保持完全一致
3. 所有类型都派生了 Debug, Clone, Serialize, Deserialize, PartialEq
4. 添加了完整的中文文档注释
5. 修复了 example-plugin 中的 match 穷尽性问题
**验证结果**
- `cargo check --workspace --all-targets` 零 warning
- `cargo test --workspace` 全部通过77 个测试)
plugin-sdk 现在已与主库的 Device 消息系统完全同步,第三方插件可以使用这些类型与 DevicePlugin 进行通信。

View File

@@ -131,6 +131,12 @@ pub enum Message {
///
/// 字符串内容通常为就绪插件的名称。
PluginReady(String),
/// 设备命令(业务插件 → DevicePlugin
DeviceCommand(DeviceCommand),
/// 设备响应DevicePlugin → 请求者)。
DeviceResponse(DeviceResponse),
/// 设备事件DevicePlugin → 广播)。
DeviceEvent(DeviceEvent),
/// 自定义业务消息。
///
/// 当标准消息不足以表达插件间协议时,可通过 `kind` 区分消息类型,
@@ -143,6 +149,136 @@ pub enum Message {
},
}
// ── 设备管理类型 ──
/// 像素格式
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum PixelFormat {
/// RGBA 8888 格式(每像素 4 字节)
RGBA8888,
/// RGB 888 格式(每像素 3 字节)
RGB888,
/// RGB 565 格式(每像素 2 字节)
RGB565,
}
/// 传感器类型
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum SensorType {
/// 温度传感器
Temperature,
/// 湿度传感器
Humidity,
/// 光线传感器
Light,
/// 接近传感器
Proximity,
}
/// 触摸动作
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum TouchAction {
/// 按下
Down,
/// 移动
Move,
/// 抬起
Up,
}
/// 设备能力
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum DeviceCapability {
/// 显示屏
Display,
/// 触摸屏
Touch,
/// 按钮
Buttons,
/// 音频
Audio,
/// 电池
Battery,
/// 背光
Backlight,
/// 传感器
Sensors,
/// 帧缓冲
Framebuffer,
/// GPIO
GPIO,
}
/// 设备命令(业务插件 → DevicePlugin
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DeviceCommand {
/// 获取显示信息
GetDisplayInfo,
/// 设置亮度0-100
SetBrightness(u8),
/// 设置背光开关
SetBacklight(bool),
/// 写入帧缓冲
WriteFramebuffer { data: Vec<u8>, format: PixelFormat },
/// 设置防息屏
SetSleepInhibit(bool),
/// 获取电池电量
GetBatteryLevel,
/// 设置音量0-100
SetVolume(u8),
/// 播放音频
PlayAudio { path: String },
/// 获取触摸事件
GetTouchEvents,
/// 获取按钮状态
GetButtonState,
/// 获取传感器数据
GetSensorData(SensorType),
/// 自定义命令
CustomCommand {
subsystem: String,
payload: serde_json::Value,
},
}
/// 设备响应DevicePlugin → 请求者)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DeviceResponse {
/// 显示信息
DisplayInfo {
width: u32,
height: u32,
format: PixelFormat,
},
/// 传感器数据
SensorData { sensor: SensorType, value: f64 },
/// 电池电量0-100
BatteryLevel(u8),
/// 操作成功
Ok,
/// 操作失败
Error(String),
/// 自定义响应
Custom(serde_json::Value),
}
/// 设备事件DevicePlugin → 广播)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DeviceEvent {
/// 触摸事件
TouchEvent { x: i32, y: i32, action: TouchAction },
/// 按钮事件
ButtonEvent { button: u8, pressed: bool },
/// 电池电量低
BatteryLow(u8),
/// 显示器已连接
DisplayConnected,
/// 显示器已断开
DisplayDisconnected,
/// 传感器警报
SensorAlert { sensor: SensorType, value: f64 },
}
// ── FFI 类型(与主程序 plugin_abi.rs 完全对应) ──
/// 插件实例在 FFI 边界上的不透明句柄。

View File

@@ -468,6 +468,15 @@ impl ShowenPlugin for ExamplePlugin {
Message::PluginReady(plugin_name) => {
eprintln!("[ExamplePlugin] observed peer readiness: {plugin_name}");
}
Message::DeviceCommand(_cmd) => {
eprintln!("[ExamplePlugin] device command received (not handled)");
}
Message::DeviceResponse(_resp) => {
eprintln!("[ExamplePlugin] device response received (not handled)");
}
Message::DeviceEvent(_event) => {
eprintln!("[ExamplePlugin] device event received (not handled)");
}
Message::Custom { kind, payload } => {
eprintln!("[ExamplePlugin] custom message: kind={kind}, payload={payload}");
}

View File

@@ -65,3 +65,27 @@
- cargo check --workspace --all-targets 零 warning
- cargo test --workspace 全部通过73 个测试,新增 7 个)
- 测试覆盖了所有核心消息类型的序列化和 MockBackend 的基本行为
## 个人经验 (2026-03-13 SDK 同步)
- 完成 plugin-sdk 与主库 Device 类型同步任务
- 在 plugin-sdk/src/lib.rs 的 Message enum 中添加了三个新变体:
- DeviceCommand(DeviceCommand)
- DeviceResponse(DeviceResponse)
- DeviceEvent(DeviceEvent)
- 在 plugin-sdk 中定义了完整的 Device 相关类型(与主库保持一致):
- PixelFormat enum (RGBA8888, RGB888, RGB565)
- SensorType enum (Temperature, Humidity, Light, Proximity)
- TouchAction enum (Down, Move, Up)
- DeviceCapability enum (Display, Touch, Buttons, Audio, Battery, Backlight, Sensors, Framebuffer, GPIO)
- DeviceCommand enum (12 个变体,包括 GetDisplayInfo, SetBrightness, WriteFramebuffer 等)
- DeviceResponse enum (6 个变体,包括 DisplayInfo, SensorData, BatteryLevel 等)
- DeviceEvent enum (6 个变体,包括 TouchEvent, ButtonEvent, BatteryLow 等)
- 所有类型都派生了 Debug, Clone, Serialize, Deserialize, PartialEq与主库一致
- 添加了完整的中文文档注释
- 修复了 example-plugin 中的 match 穷尽性问题,添加了三个 Device 消息的处理分支
- cargo check --workspace --all-targets 零 warning
- cargo test --workspace 全部通过77 个测试)
- 经验总结:
- SDK 与主库的类型同步需要保持完全一致的派生宏和字段定义
- 添加新的 Message 变体后,需要检查所有现有插件的 match 表达式
- Rust 的穷尽性检查是很好的安全网,确保不会遗漏新增的消息类型