- 实现 MIoT 客户端核心功能(MQTT 连接、设备管理、属性读写) - 支持云端 API 调用与局域网设备发现(mDNS) - 集成国际化(i18n)多语言支持 - 添加 MIoT 设备规约解析器(spec_parser) - 包含单元测试与使用示例
54 lines
1.4 KiB
Go
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"
|
|
|
|
"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("示例结束")
|
|
}
|