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
+45 -2
View File
@@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"hash/crc32"
"io"
"log"
"math"
"net/http"
@@ -216,8 +217,8 @@ func (p *MIoTSpecParser) Parse(urn string, model string) (*MIoTSpecInstance, err
}
// readSpecFile reads and parses a local JSON spec file from specDir/{urn}.json.
// If the file doesn't exist, it attempts to download it from miot-spec.org first.
func (p *MIoTSpecParser) readSpecFile(urn string) (map[string]interface{}, error) {
// On Windows, colons are not allowed in filenames
fileName := urn
if runtime.GOOS == "windows" {
fileName = strings.ReplaceAll(urn, ":", "_")
@@ -226,7 +227,16 @@ func (p *MIoTSpecParser) readSpecFile(urn string) (map[string]interface{}, error
data, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("read %s: %w", filePath, err)
// Auto-download if file not found
if os.IsNotExist(err) {
if dlErr := p.downloadSpecFile(urn, filePath); dlErr != nil {
return nil, fmt.Errorf("download spec: %w", dlErr)
}
data, err = os.ReadFile(filePath)
}
if err != nil {
return nil, fmt.Errorf("read %s: %w", filePath, err)
}
}
var result map[string]interface{}
@@ -236,6 +246,39 @@ func (p *MIoTSpecParser) readSpecFile(urn string) (map[string]interface{}, error
return result, nil
}
// downloadSpecFile downloads a single device spec from miot-spec.org and saves it locally.
func (p *MIoTSpecParser) downloadSpecFile(urn, filePath string) error {
url := fmt.Sprintf("https://miot-spec.org/miot-spec-v2/instance?type=%s", urn)
httpClient := &http.Client{Timeout: 15 * time.Second}
resp, err := httpClient.Get(url)
if err != nil {
return fmt.Errorf("GET %s: %w", url, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("GET %s: status %d", url, resp.StatusCode)
}
// Ensure specDir exists
if err := os.MkdirAll(filepath.Dir(filePath), 0755); err != nil {
return fmt.Errorf("mkdir: %w", err)
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("read body: %w", err)
}
if err := os.WriteFile(filePath, data, 0644); err != nil {
return fmt.Errorf("write %s: %w", filePath, err)
}
log.Printf("[spec] downloaded spec: %s -> %s", urn, filePath)
return nil
}
// parseSpec parses a raw spec dict into a MIoTSpecInstance tree.
func (p *MIoTSpecParser) parseSpec(urn string, model string, specData map[string]interface{}) (*MIoTSpecInstance, error) {
// Get instance description from std lib