主要变更: **类型系统重构** - HomeInfo.ID/RoomInfo.ID/HomeID 从 string 改为 int64,json tag 使用 ,string - GetHome/GetRooms/GetRoomDevices 接口同步修改 - 新增 int64FromStr 工具函数,更新 parseHomeInfo 解析逻辑 **新增 Speaker 设备** - 新增 KindSpeaker 类型 - classify 映射覆盖 speaker/bluetooth-speaker/alarm-clock/media-player/playback/music **设备驱动重构** - AC 驱动彻底重写:从硬编码常量改为基于 SPEC ValueList 的动态 valMapper - 所有设备(Fan/Humidifier/Light/OccupancySensor/Switch/TempHumiditySensor/Thermostat/Vacuum/WaterHeater/Cover)新增 GetState() 和 FetchState(ctx) 接口 - base.go 新增 specResolver.format() 方法 **MIoT 模块增强** - MIoTClient 新增 homeIDs 字段,支持按家庭过滤设备 - RefreshDevices 支持 homeIDs 参数 - SpecStdLib 引入 logger.Logger,log.Printf 全部迁移为结构化日志 **其他** - Config 新增 RedirectURL 和 HomeIDs 字段 - AuthInfo.ExpiresIn 类型从 int 改为 int32 - UUID 生成简化为 hex.EncodeToString - 新增 github.com/sirupsen/logrus 依赖 - .gitignore 新增 examples/*/data/ 忽略规则 - 删除已提交的 config.json 敏感配置
126 lines
3.3 KiB
Go
126 lines
3.3 KiB
Go
// E9 — Full MIoT client integration example.
|
|
// Demonstrates: create client → set storage/http/mips → init → refresh devices → subscribe → monitor.
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
"time"
|
|
|
|
"xiaomihome/miot"
|
|
"xiaomihome/miot/examples/config"
|
|
)
|
|
|
|
func main() {
|
|
// 1. Load config
|
|
cfg, err := config.LoadConfig(config.DefaultPath())
|
|
if err != nil {
|
|
fmt.Printf("Load config error: %v\n", err)
|
|
return
|
|
}
|
|
if cfg.AuthInfo.AccessToken == "" {
|
|
fmt.Println("请先运行 E1 cloud_oauth 获取 token")
|
|
os.Exit(1)
|
|
}
|
|
|
|
// 2. Create entry data (Python: entry_data dict)
|
|
entryData := cfg.EntryData()
|
|
|
|
// 3. Create the full client
|
|
client := miot.NewMIoTClient("demo", entryData, cfg.UID, cfg.CloudServer, cfg.GetCtrlMode())
|
|
|
|
// 4. Set up storage
|
|
storage := miot.NewMIoTStorage("./data")
|
|
client.SetStorage(storage)
|
|
fmt.Println("Storage 已配置")
|
|
|
|
// 5. Set up HTTP client
|
|
http, err := miot.NewMIoTHttpClient(cfg.CloudServer, miot.OAUTH2_CLIENT_ID, cfg.AuthInfo.AccessToken)
|
|
if err != nil {
|
|
fmt.Printf("Create HTTP client error: %v\n", err)
|
|
return
|
|
}
|
|
client.SetHTTPClient(http)
|
|
fmt.Println("HTTP Client 已配置")
|
|
|
|
// 6. Set up MIPS cloud client
|
|
mips := miot.NewMipsCloudClient(
|
|
"ssl://cn-ha.mqtt.io.mi.com:8883",
|
|
"ha."+cfg.UUID,
|
|
miot.OAUTH2_CLIENT_ID,
|
|
cfg.AuthInfo.AccessToken,
|
|
)
|
|
client.SetMipsCloud(mips)
|
|
fmt.Println("MIPS Cloud 已配置")
|
|
|
|
// 7. Initialize client
|
|
if err := client.Init(); err != nil {
|
|
fmt.Printf("Init error: %v\n", err)
|
|
return
|
|
}
|
|
fmt.Println("Client 已初始化")
|
|
|
|
// 8. Start background services
|
|
network := miot.NewMIoTNetwork(nil, nil, 30*time.Second)
|
|
network.Init()
|
|
defer network.Deinit()
|
|
|
|
svc := miot.NewMipsService()
|
|
svc.Init()
|
|
defer svc.Deinit()
|
|
|
|
lan, err := miot.NewMIoTLan(nil, network, svc, true, 0)
|
|
if err != nil {
|
|
fmt.Printf("LAN init warning: %v\n", err)
|
|
} else {
|
|
defer lan.Deinit()
|
|
fmt.Println("LAN 控制器已启动")
|
|
}
|
|
|
|
// 9. Connect MIPS
|
|
fmt.Println("正在连接 MIPS...")
|
|
if err := mips.Connect(); err != nil {
|
|
fmt.Printf("MIPS connect warning: %v\n", err)
|
|
} else {
|
|
defer mips.Disconnect()
|
|
fmt.Println("MIPS 已连接")
|
|
}
|
|
|
|
// 10. Refresh device list
|
|
fmt.Println("\n刷新设备列表...")
|
|
if err := client.RefreshDevices(nil); err != nil {
|
|
fmt.Printf("RefreshDevices warning: %v\n", err)
|
|
} else {
|
|
devices := client.DeviceList()
|
|
fmt.Printf("设备数量: %d\n", len(devices))
|
|
for did, info := range devices {
|
|
fmt.Printf(" %s: %v (online=%v)\n", did, info["name"], info["online"])
|
|
}
|
|
|
|
// Subscribe to first device's state
|
|
for did := range devices {
|
|
client.SubDeviceState(did, func(did string, state miot.MIoTDeviceState, ctx interface{}) {
|
|
fmt.Printf("[设备状态] DID=%s, State=%s\n", did, state)
|
|
})
|
|
// Subscribe to all properties for the first device
|
|
client.SubProp(did, func(params map[string]interface{}, ctx interface{}) {
|
|
fmt.Printf("[属性] did=%v, siid=%v, piid=%v, value=%v\n",
|
|
params["did"], params["siid"], params["piid"], params["value"])
|
|
}, 0, 0)
|
|
break // only first device for demo
|
|
}
|
|
}
|
|
|
|
// 11. Wait for Ctrl+C
|
|
fmt.Println("\n等待 Ctrl+C 退出...")
|
|
sigCh := make(chan os.Signal, 1)
|
|
signal.Notify(sigCh, os.Interrupt)
|
|
<-sigCh
|
|
|
|
// 12. Cleanup
|
|
fmt.Println("\n正在清理...")
|
|
client.Deinit()
|
|
fmt.Println("示例结束")
|
|
}
|