Files
xiaomihome/miot/examples/lan_control/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

66 lines
1.8 KiB
Go

// E4 — LAN device control example.
// Demonstrates: init network → create LAN controller → register device → wait for online → subscribe.
package main
import (
"fmt"
"time"
"xiaomihome/miot"
)
func main() {
// 1. Initialize network monitor
network := miot.NewMIoTNetwork(nil, nil, 30*time.Second)
network.Init()
defer network.Deinit()
// 3. Create MIPS service (required by MIoTLan)
svc := miot.NewMipsService()
svc.Init()
defer svc.Deinit()
// 4. Create LAN controller
lan, err := miot.NewMIoTLan(nil, network, svc, true, 0)
if err != nil {
fmt.Printf("Create LAN error: %v\n", err)
return
}
defer lan.Deinit()
// 5. Register devices with hardcoded values
// Replace with your actual device DID and token (32 hex chars)
devices := map[string]map[string]interface{}{
"12345678": {
"did": "12345678",
"token": "00000000000000000000000000000000",
},
}
lan.UpdateDevices(devices)
fmt.Println("已注册设备,等待上线...")
// 6. Subscribe to device state
lan.SubDeviceState("demo", func(did string, state map[string]interface{}, ctx interface{}) {
fmt.Printf("[设备状态] DID=%s, State=%v\n", did, state)
}, nil)
// 7. Subscribe to property changes (all properties)
did := "12345678"
lan.SubProp(did, func(params map[string]interface{}, ctx interface{}) {
fmt.Printf("[属性变化] DID=%s, siid=%v, piid=%v, value=%v\n",
params["did"], params["siid"], params["piid"], params["value"])
}, 0, 0, nil)
// 8. Wait for device to come online
fmt.Println("等待设备上线 (30秒)...")
time.Sleep(30 * time.Second)
// Print current online devices
devList := lan.GetDevList()
fmt.Println("\n=== 在线设备 ===")
for did, state := range devList {
fmt.Printf(" DID: %s, State: %v\n", did, state)
}
fmt.Println("示例结束")
}