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

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"
"git.zeroonesoft.cn/golib/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("示例结束")
}