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
@@ -0,0 +1,21 @@
-----BEGIN CERTIFICATE-----
MIIBazCCAQ+gAwIBAgIEA/UKYDAMBggqhkjOPQQDAgUAMCIxEzARBgNVBAoTCk1p
amlhIFJvb3QxCzAJBgNVBAYTAkNOMCAXDTE2MTEyMzAxMzk0NVoYDzIwNjYxMTEx
MDEzOTQ1WjAiMRMwEQYDVQQKEwpNaWppYSBSb290MQswCQYDVQQGEwJDTjBZMBMG
ByqGSM49AgEGCCqGSM49AwEHA0IABL71iwLa4//4VBqgRI+6xE23xpovqPCxtv96
2VHbZij61/Ag6jmi7oZ/3Xg/3C+whglcwoUEE6KALGJ9vccV9PmjLzAtMAwGA1Ud
EwQFMAMBAf8wHQYDVR0OBBYEFJa3onw5sblmM6n40QmyAGDI5sURMAwGCCqGSM49
BAMCBQADSAAwRQIgchciK9h6tZmfrP8Ka6KziQ4Lv3hKfrHtAZXMHPda4IYCIQCG
az93ggFcbrG9u2wixjx1HKW4DUA5NXZG0wWQTpJTbQ==
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
MIIBjzCCATWgAwIBAgIBATAKBggqhkjOPQQDAjAiMRMwEQYDVQQKEwpNaWppYSBS
b290MQswCQYDVQQGEwJDTjAgFw0yMjA2MDkxNDE0MThaGA8yMDcyMDUyNzE0MTQx
OFowLDELMAkGA1UEBhMCQ04xHTAbBgNVBAoMFE1JT1QgQ0VOVFJBTCBHQVRFV0FZ
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEdYrzbnp/0x/cZLZnuEDXTFf8mhj4
CVpZPwgj9e9Ve5r3K7zvu8Jjj7JF1JjQYvEC6yhp1SzBgglnK4L8xQzdiqNQME4w
HQYDVR0OBBYEFCf9+YBU7pXDs6K6CAQPRhlGJ+cuMB8GA1UdIwQYMBaAFJa3onw5
sblmM6n40QmyAGDI5sURMAwGA1UdEwQFMAMBAf8wCgYIKoZIzj0EAwIDSAAwRQIh
AKUv+c8v98vypkGMTzMwckGjjVqTef8xodsy6PhcSCq+AiA/n9mDs62hAo5zXyJy
Bs1s7mqXPf1XgieoxIvs1MqyiA==
-----END CERTIFICATE-----
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1 @@
{"type":"urn:miot-spec-v2:device:fan:0000A005:jipin-f003:1:0000D062","description":"jipin fan f003","services":[{"iid":1,"type":"urn:miot-spec-v2:service:device-information:00007801:jipin-f003:1:0000D062","description":"Device Information","properties":[{"iid":1,"type":"urn:miot-spec-v2:property:manufacturer:00000001:jipin-f003:1:0000D062","description":"Device Manufacturer","format":"string","access":["read"],"source":4},{"iid":2,"type":"urn:miot-spec-v2:property:model:00000002:jipin-f003:1:0000D062","description":"Device Model","format":"string","access":["read"],"source":4},{"iid":3,"type":"urn:miot-spec-v2:property:serial-number:00000003:jipin-f003:1:0000D062","description":"Device ID","format":"string","access":["read"],"source":4},{"iid":4,"type":"urn:miot-spec-v2:property:firmware-revision:00000005:jipin-f003:1:0000D062","description":"Current Firmware Version","format":"string","access":["read"],"source":4},{"iid":5,"type":"urn:miot-spec-v2:property:serial-no:000000FF:jipin-f003:1:0000D062","description":"Serial Number","format":"string","access":["read"],"source":4}]},{"iid":2,"type":"urn:miot-spec-v2:service:fan:00007808:jipin-f003:1:0000D062","description":"fan","properties":[{"iid":1,"type":"urn:miot-spec-v2:property:on:00000006:jipin-f003:1:0000D062","description":"switch status","format":"bool","access":["read","write","notify"],"source":1},{"iid":3,"type":"urn:miot-spec-v2:property:mode:00000008:jipin-f003:1:0000D062","description":"mode","format":"uint8","access":["read","write","notify"],"value-list":[{"value":0,"description":"None"},{"value":1,"description":"Sleep"}],"source":1,"gatt-access":[]},{"iid":4,"type":"urn:miot-spec-v2:property:fan-level:00000016:jipin-f003:1:0000D062","description":"Gear Fan Level","format":"uint8","access":["read","write","notify"],"value-range":[1,8,1],"source":1,"gatt-access":[]},{"iid":2,"type":"urn:miot-spec-v2:property:fault:00000009:jipin-f003:1:0000D062","description":"Device Fault","format":"uint8","access":["read","notify"],"value-list":[{"value":0,"description":"No Faults"}],"source":1,"gatt-access":[]},{"iid":6,"type":"urn:miot-spec-v2:property:horizontal-swing:00000017:jipin-f003:1:0000D062","description":"Horizontal Swing","format":"bool","access":["read","write","notify"],"source":1},{"iid":8,"type":"urn:miot-spec-v2:property:vertical-swing:00000018:jipin-f003:1:0000D062","description":"Vertical Swing","format":"bool","access":["read","write","notify"],"source":1},{"iid":14,"type":"urn:miot-spec-v2:property:anion:00000025:jipin-f003:1","description":"Anion","format":"bool","access":["read","write","notify"]}],"actions":[{"iid":3,"type":"urn:miot-spec-v2:action:toggle:00002811:jipin-f003:1:0000D062","description":"toggle","in":[],"out":[]}]},{"iid":5,"type":"urn:miot-spec-v2:service:indicator-light:00007803:jipin-f003:1:0000D062","description":"Indicator-light","properties":[{"iid":1,"type":"urn:miot-spec-v2:property:on:00000006:jipin-f003:1:0000D062","description":"Switch Status","format":"bool","access":["read","write","notify"],"source":1}]},{"iid":7,"type":"urn:miot-spec-v2:service:alarm:00007804:jipin-f003:1:0000D062","description":"Alarm","properties":[{"iid":1,"type":"urn:miot-spec-v2:property:alarm:00007804:jipin-f003:1:0000D062","description":"Alarm","format":"bool","access":["read","write","notify"],"source":1}]},{"iid":11,"type":"urn:miot-spec-v2:service:countdown:0000782D:jipin-f003:1","description":"Countdown","properties":[{"iid":1,"type":"urn:miot-spec-v2:property:countdown-time:00000055:jipin-f003:1","description":"Countdown Time","format":"uint16","access":["read","write","notify"],"unit":"minutes","value-range":[0,720,1],"gatt-access":[]}]},{"iid":12,"type":"urn:miot-spec-v2:service:night-light:00007883:jipin-f003:1","description":"Night Light","properties":[{"iid":1,"type":"urn:miot-spec-v2:property:on:00000006:jipin-f003:1","description":"Switch Status","format":"bool","access":["read","write","notify"]}]}]}
+82
View File
@@ -0,0 +1,82 @@
// Package main demonstrates fan device control.
// 运行前提:已配置环境变量 XIAOMI_CLIENT_ID / XIAOMI_CLIENT_SECRET / XIAOMI_ACCESS_TOKEN。
package main
import (
"context"
"log"
"time"
"xiaomihome/xiaomi"
"xiaomihome/xiaomi/devices"
miotconfig "xiaomihome/miot/examples/config"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
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{
ClientSecret: "",
AccessToken: cfg.AuthInfo.AccessToken,
UID: cfg.UID,
UUID: cfg.UUID,
})
if err != nil {
log.Fatal(err)
}
defer client.Close()
log.Println("=== 风扇控制 ===")
const targetDID = "820891929"
info, err := client.GetDevice(ctx, targetDID)
if err != nil {
log.Fatalf("获取设备 %s 失败: %v", targetDID, err)
}
log.Printf("设备: %s (DID: %s, 型号: %s)", info.Name, info.DID, info.Model)
fan, err := devices.NewFan(client, info)
if err != nil {
log.Fatalf("创建 Fan 控制失败: %v", err)
}
log.Println(" 开启风扇...")
fan.TurnOn(ctx)
log.Println(" 设置 2 档风...")
if err := fan.SetFanLevel(ctx, 2); err != nil {
log.Printf(" 调速失败(可能不支持): %v", err)
} else {
level, _ := fan.GetFanLevel(ctx)
log.Printf(" 当前档位: %d", level)
}
log.Println(" 开启摇头...")
if err := fan.SetOscillation(ctx, true); err != nil {
log.Printf(" 摇头失败(可能不支持): %v", err)
} else {
osc, _ := fan.IsOscillating(ctx)
log.Printf(" 摇头状态: %v", osc)
}
log.Println(" 开启自然风...")
if err := fan.SetNaturalWind(ctx, true); err != nil {
log.Printf(" 自然风失败(可能不支持): %v", err)
} else {
nw, _ := fan.IsNaturalWind(ctx)
log.Printf(" 自然风状态: %v", nw)
}
on, _ := fan.IsOn(ctx)
level, _ := fan.GetFanLevel(ctx)
osc, _ := fan.IsOscillating(ctx)
nw, _ := fan.IsNaturalWind(ctx)
log.Printf(" 风扇状态汇总: on=%v level=%d oscillation=%v naturalWind=%v", on, level, osc, nw)
log.Println("风扇演示完毕")
}