feat: 初始化小米 IoT (MIoT) 智能家居 Go 库
- 实现 MIoT 客户端核心功能(MQTT 连接、设备管理、属性读写) - 支持云端 API 调用与局域网设备发现(mDNS) - 集成国际化(i18n)多语言支持 - 添加 MIoT 设备规约解析器(spec_parser) - 包含单元测试与使用示例
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
// E5 — mDNS service discovery example.
|
||||
// Demonstrates: create MipsService → init → discover _miot-central._tcp.local. → print results.
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"miot"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 1. Create MIPS service browser
|
||||
svc := miot.NewMipsService()
|
||||
|
||||
// 2. Initialize mDNS browser
|
||||
svc.Init()
|
||||
defer svc.Deinit()
|
||||
fmt.Println("mDNS 服务浏览器已启动")
|
||||
|
||||
// 3. Subscribe to service changes
|
||||
svc.SubServiceChange("demo", "*", func(groupID string, state miot.MipsServiceState, data map[string]interface{}) {
|
||||
fmt.Printf("\n[服务变化] groupID=%s, state=%s\n", groupID, state)
|
||||
for k, v := range data {
|
||||
fmt.Printf(" %s: %v\n", k, v)
|
||||
}
|
||||
})
|
||||
|
||||
// 4. Wait a moment for initial discovery
|
||||
fmt.Println("正在搜索 MIoT 中央网关 (5秒)...")
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
// 5. Get and print all discovered services
|
||||
services := svc.GetServices("")
|
||||
fmt.Println("\n=== 已发现的 MIoT 服务 ===")
|
||||
if len(services) == 0 {
|
||||
fmt.Println(" 未发现任何服务 (可能需要等待更长时间)")
|
||||
} else {
|
||||
for groupID, s := range services {
|
||||
fmt.Printf(" GroupID: %s\n", groupID)
|
||||
fmt.Printf(" Name: %s\n", s.Name)
|
||||
fmt.Printf(" Addresses: %v\n", s.Addresses)
|
||||
fmt.Printf(" Port: %d\n", s.Port)
|
||||
fmt.Printf(" DID: %s\n", s.DID)
|
||||
fmt.Printf(" Server: %s\n", s.Server)
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
|
||||
// 6. Keep browser running for more discoveries
|
||||
fmt.Println("继续监听服务变化 (30秒)...")
|
||||
time.Sleep(30 * time.Second)
|
||||
fmt.Println("示例结束")
|
||||
}
|
||||
Reference in New Issue
Block a user