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