go.mod 模块名为 xiaomihome,将所有内部 import 从 "miot" 更新为 "xiaomihome/miot",涉及所有测试文件和示例文件。新增 README.md 项目 介绍文档(架构概览、模块说明、移植进度等)。新增 ARCH_PLAN.md 架构 设计文档。
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(); 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("示例结束")
|
|
}
|