- 改动:go.mod module 声明与全部内部导入路径补全为完整域名路径,README 同步 - 原因:裸模块名无法被 go get 解析,发版前必须修正为 GOPRIVATE 可拉取路径
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"
|
|
|
|
"git.zeroonesoft.cn/golib/xiaomihome/miot"
|
|
"git.zeroonesoft.cn/golib/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示例结束")
|
|
}
|