go.mod 模块名为 xiaomihome,将所有内部 import 从 "miot" 更新为 "xiaomihome/miot",涉及所有测试文件和示例文件。新增 README.md 项目 介绍文档(架构概览、模块说明、移植进度等)。新增 ARCH_PLAN.md 架构 设计文档。
93 lines
2.4 KiB
Go
93 lines
2.4 KiB
Go
// E7 — Storage and certificate management example.
|
|
// Demonstrates: save/load data → user config → certificate operations → manufacturer lookup.
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"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
|
|
}
|
|
|
|
// 2. Create storage
|
|
storage := miot.NewMIoTStorage("./data")
|
|
fmt.Println("Storage 已创建 (root=./data)")
|
|
|
|
// 3. Save and load data
|
|
fmt.Println("\n=== 读写操作 ===")
|
|
if err := storage.Save("test", "hello", "world"); err != nil {
|
|
fmt.Printf("Save error: %v\n", err)
|
|
} else {
|
|
fmt.Println("Save: test/hello → world (OK)")
|
|
}
|
|
|
|
val, err := storage.Load("test", "hello", "str")
|
|
if err != nil {
|
|
fmt.Printf("Load error: %v\n", err)
|
|
} else {
|
|
fmt.Printf("Load: test/hello = %v\n", val)
|
|
}
|
|
|
|
// 4. User config operations
|
|
fmt.Println("\n=== 用户配置 ===")
|
|
configData := map[string]interface{}{"language": "zh-Hans", "theme": "dark"}
|
|
if uid := cfg.UID; uid != "" {
|
|
err := storage.UpdateUserConfig(uid, cfg.CloudServer, configData, false)
|
|
if err != nil {
|
|
fmt.Printf("UpdateUserConfig error: %v\n", err)
|
|
} else {
|
|
fmt.Println("UpdateUserConfig: OK")
|
|
}
|
|
|
|
loaded := storage.LoadUserConfig(uid, cfg.CloudServer, nil)
|
|
fmt.Printf("LoadUserConfig: %v\n", loaded)
|
|
} else {
|
|
fmt.Println("跳过 (UID 为空)")
|
|
}
|
|
|
|
// 5. Certificate operations
|
|
fmt.Println("\n=== 证书操作 ===")
|
|
cert, err := miot.NewMIoTCert(storage, cfg.UID, cfg.CloudServer)
|
|
if err != nil {
|
|
fmt.Printf("NewMIoTCert error: %v\n", err)
|
|
} else {
|
|
ok, err := cert.VerifyCACert()
|
|
if err != nil {
|
|
fmt.Printf("VerifyCACert error: %v\n", err)
|
|
} else if ok {
|
|
fmt.Println("CA 证书验证: 通过")
|
|
} else {
|
|
fmt.Println("CA 证书验证: 失败 (hash 不匹配)")
|
|
}
|
|
|
|
fmt.Printf("CA 文件: %s\n", cert.CAFile())
|
|
fmt.Printf("Key 文件: %s\n", cert.KeyFile())
|
|
fmt.Printf("Cert 文件: %s\n", cert.CertFile())
|
|
}
|
|
|
|
// 6. Device manufacturer lookup
|
|
fmt.Println("\n=== 厂商信息 ===")
|
|
mfg := miot.NewDeviceManufacturer(storage)
|
|
mfg.Init()
|
|
|
|
manufacturers := []string{"xiaomi", "yeelink", "lumi", "chuangmi"}
|
|
for _, m := range manufacturers {
|
|
name := mfg.GetName(m)
|
|
if name != m {
|
|
fmt.Printf(" %s → %s\n", m, name)
|
|
} else {
|
|
fmt.Printf(" %s → (未缓存)\n", m)
|
|
}
|
|
}
|
|
|
|
fmt.Println("\n示例结束")
|
|
}
|