chore: 提交高层 xiaomi 封装库及示例

- 新增 xiaomi/examples/ 下 8 个示例程序(用户/家庭查询、设备列表、开关/灯光/空调控制、属性订阅、高级过滤分类、SPEC 解析)
- miot_client_sub.go: 重构 SubProp/SubEvent,使用 buildPropTopic/buildEventTopic 支持通配符订阅(siid/piid=0 → +),并修复锁顺序问题(将 RequestRefreshProp 移到 Lock 外)
- spec_parser.go: 新增 downloadSpecFile 方法,本地 SPEC 文件缺失时自动从 miot-spec.org 下载
- ARCH_PLAN.md: 架构设计从 Proposed 更新为 Accepted(v1.0→v1.1),补充设备分类/工厂/SPEC 映射等模块设计
- xiaomi/: 新增 miot 上层强类型封装模块,包含 Client 主入口、用户/家庭/设备 API、属性读写、动作调用、订阅通知,以及 devices/ 设备控制抽象(Switch/Light/AirConditioner/Fan/Cover/Humidifier/Vacuum/WaterHeater/Thermostat)和 specs/ SPEC 查询辅助
- 更多 xiaomi 示例(风扇/窗帘/传感器控制)
This commit is contained in:
2026-06-29 08:51:21 +08:00
parent 35efed7d15
commit a3d94c4b9f
93 changed files with 7582 additions and 157 deletions
+32 -17
View File
@@ -19,23 +19,17 @@ import (
// Returns: subscription ID for unsubscribing.
func (c *MIoTClient) SubProp(did string, handler func(map[string]interface{}, interface{}), siid, piid int) string {
c.mu.Lock()
defer c.mu.Unlock()
// 1. Build topic
topic := fmt.Sprintf("%s/p/%d/%d", did, siid, piid)
// 2. Generate subscription ID
// Build topic with wildcard for 0 values
topic := buildPropTopic(did, siid, piid)
subID := fmt.Sprintf("%p", handler)
// 3. Store handler in subTree
c.subTree.Sub(topic, handler, nil)
// 4. Check if this is the first subscription for this device
// TODO: Implement proper tracking
fmt.Printf("[MIoTClient] Subscribed to property: %s (topic=%s, subID=%s)\n", did, topic, subID)
// 5. Request refresh property from cloud
c.mu.Unlock()
// RequestRefreshProp must be outside the lock (it also acquires c.mu)
c.RequestRefreshProp(did, siid, piid)
return subID
@@ -67,13 +61,8 @@ func (c *MIoTClient) SubEvent(did string, handler func(map[string]interface{}, i
c.mu.Lock()
defer c.mu.Unlock()
// 1. Build topic
topic := fmt.Sprintf("%s/e/%d/%d", did, siid, eiid)
// 2. Generate subscription ID
topic := buildEventTopic(did, siid, eiid)
subID := fmt.Sprintf("%p", handler)
// 3. Store handler in subTree
c.subTree.Sub(topic, handler, nil)
fmt.Printf("[MIoTClient] Subscribed to event: %s (topic=%s, subID=%s)\n", did, topic, subID)
@@ -477,3 +466,29 @@ func (c *MIoTClient) checkDeviceState(did string) bool {
return false
}
// buildPropTopic builds a subscription topic, converting 0 to + wildcard.
// siid=0 → match any siid; piid=0 → match any piid.
func buildPropTopic(did string, siid, piid int) string {
s := "+"
p := "+"
if siid != 0 {
s = fmt.Sprintf("%d", siid)
}
if piid != 0 {
p = fmt.Sprintf("%d", piid)
}
return fmt.Sprintf("%s/p/%s/%s", did, s, p)
}
func buildEventTopic(did string, siid, eiid int) string {
s := "+"
e := "+"
if siid != 0 {
s = fmt.Sprintf("%d", siid)
}
if eiid != 0 {
e = fmt.Sprintf("%d", eiid)
}
return fmt.Sprintf("%s/e/%s/%s", did, s, e)
}