go.mod 模块名为 xiaomihome,将所有内部 import 从 "miot" 更新为 "xiaomihome/miot",涉及所有测试文件和示例文件。新增 README.md 项目 介绍文档(架构概览、模块说明、移植进度等)。新增 ARCH_PLAN.md 架构 设计文档。
55 lines
1.5 KiB
Go
55 lines
1.5 KiB
Go
// E5 — mDNS service discovery example.
|
|
// Demonstrates: create MipsService → init → discover _miot-central._tcp.local. → print results.
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"xiaomihome/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("示例结束")
|
|
}
|