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:
@@ -0,0 +1,62 @@
|
||||
# 清理修复方案
|
||||
|
||||
> 2026-06-29
|
||||
|
||||
## 设计原则
|
||||
|
||||
- 与 `miot/examples` 共用同一份 `config.json`,不重复造轮子
|
||||
- 用户先跑 `miot/examples/cloud_oauth` 获取 token,token 自动写入 `miot/examples/config.json`
|
||||
- xiaomi 示例直接读这份已有的配置文件
|
||||
|
||||
---
|
||||
|
||||
## 修改清单
|
||||
|
||||
### A. 删除 `xiaomi/examples/config/config.go`
|
||||
|
||||
整个文件删掉。复用 `miot/examples/config` 包。
|
||||
|
||||
### B. 9 个示例文件 — 统一替换
|
||||
|
||||
**旧代码**(每个文件都要删):
|
||||
```go
|
||||
import "xiaomihome/xiaomi/examples/config"
|
||||
|
||||
cfg, err := config.LoadFromEnv()
|
||||
entryData := map[string]interface{}{
|
||||
"country_code": cfg.CountryCode,
|
||||
"cloud_server": cfg.CloudServer,
|
||||
"client_id": cfg.ClientID,
|
||||
"client_secret": cfg.ClientSecret,
|
||||
"access_token": cfg.AccessToken,
|
||||
}
|
||||
miotClient := miot.NewMIoTClient("phone", entryData, "", miot.DefaultCloudServer, miot.CtrlModeAuto)
|
||||
miotClient.Init()
|
||||
defer miotClient.Deinit()
|
||||
httpClient, _ := miot.NewMIoTHttpClient(cfg.CloudServer, cfg.ClientID, cfg.AccessToken)
|
||||
client := xiaomi.NewClient(miotClient, httpClient)
|
||||
```
|
||||
|
||||
**替换为**:
|
||||
```go
|
||||
import miotconfig "xiaomihome/miot/examples/config"
|
||||
|
||||
cfg, err := miotconfig.LoadConfig("miot/examples/config.json")
|
||||
if err != nil {
|
||||
log.Fatal("请先运行 miot/examples/cloud_oauth 获取 token:", err)
|
||||
}
|
||||
|
||||
client, err := xiaomi.NewClient(ctx, xiaomi.Config{
|
||||
ClientID: miot.OAUTH2_CLIENT_ID,
|
||||
ClientSecret: "",
|
||||
AccessToken: cfg.AuthInfo.AccessToken,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer client.Close()
|
||||
```
|
||||
|
||||
### C. `miot/miot_client.go` — 去无用字段
|
||||
|
||||
`NewMIoTClient` 中 entryData 如有 `"country_code"`,删掉该行。
|
||||
Reference in New Issue
Block a user