Files
xiaomihome/miot/examples/network_monitor/main.go
T
4566704 35efed7d15 chore: 修复 import 路径为 xiaomihome/miot,补充 README 项目文档
go.mod 模块名为 xiaomihome,将所有内部 import 从 "miot" 更新为
"xiaomihome/miot",涉及所有测试文件和示例文件。新增 README.md 项目
介绍文档(架构概览、模块说明、移植进度等)。新增 ARCH_PLAN.md 架构
设计文档。
2026-06-28 23:21:18 +08:00

54 lines
1.4 KiB
Go

// E6 — Network status monitor example.
// Demonstrates: init network → print status/interface info → subscribe to changes.
package main
import (
"fmt"
"time"
"xiaomihome/miot"
)
func main() {
// 1. Create network monitor (use defaults)
network := miot.NewMIoTNetwork(nil, nil, 30*time.Second)
network.Init()
defer network.Deinit()
// 2. Print current network status
fmt.Println("=== 网络状态 ===")
if network.GetNetworkStatus() {
fmt.Println("状态: 已连接")
} else {
fmt.Println("状态: 未连接")
}
// 3. Print each network interface
fmt.Println("\n=== 网络接口 ===")
for name, info := range network.GetNetworkInfo() {
fmt.Printf(" 接口: %s\n", name)
fmt.Printf(" IP: %s\n", info.IP)
fmt.Printf(" Netmask: %s\n", info.Netmask)
fmt.Printf(" NetSeg: %s\n", info.NetSeg)
}
// 4. Subscribe to network status changes
network.SubNetworkStatus("demo", func(status bool) {
if status {
fmt.Println("[网络状态] 已连接")
} else {
fmt.Println("[网络状态] 已断开")
}
})
// 5. Subscribe to interface changes
network.SubNetworkInfo("demo", func(status miot.InterfaceStatus, info *miot.NetworkInfo) {
fmt.Printf("[接口变化] %s: %s (%s/%s)\n", status, info.Name, info.IP, info.Netmask)
})
// 6. Wait for network events
fmt.Println("\n监听网络变化 (30秒)...")
time.Sleep(30 * time.Second)
fmt.Println("示例结束")
}