Files
4566704 3a44cb9e6a feat: 初始化小米 IoT (MIoT) 智能家居 Go 库
- 实现 MIoT 客户端核心功能(MQTT 连接、设备管理、属性读写)
- 支持云端 API 调用与局域网设备发现(mDNS)
- 集成国际化(i18n)多语言支持
- 添加 MIoT 设备规约解析器(spec_parser)
- 包含单元测试与使用示例
2026-06-28 22:05:49 +08:00

2.6 KiB
Raw Permalink Blame History

第三批移植任务提示词 (P2 LAN 控制)

源文件在 D:\miot\py-miot\,子文档在 D:\miot\go-miot\migration\

任务 3.1 — miot_mdns.py → miot_mdns.go

源文件:D:\miot\py-miot\miot_mdns.py
目标文件:D:\miot\go-miot\miot_mdns.go(新建)
详细说明:D:\miot\go-miot\migration\13-miot-mdns.md

实现 mDNS 服务发现,用于发现内网 MIoT 中央 hub 网关:

1. MipsServiceState 枚举:ADDED=1, REMOVED=2, UPDATED=3

2. MipsServiceData 结构体
   - 字段:name, addresses []string, port int, type, server string
   - did, groupID, role, suiteMQTT(从 profile 二进制解析)
   - 解析规则(profile_bin 字节布局):
     - did: bytes[1:9] big-endian
     - groupID: bytes[9:17] 反向 + hex
     - role: bytes[20] >> 4
     - suiteMQTT: (bytes[22] >> 1) & 0x01 == 0x01

3. MipsService 结构体
   - Init() — 启动 mDNS 浏览器,监听 _miot-central._tcp.local.
   - Deinit() — 停止浏览器
   - GetServices(groupID string) map[string]*MipsServiceData
   - SubServiceChange(key, groupID string, handler)
   - UnsubServiceChange(key string)

4. 状态变化处理
   - ADDED → 新增服务
   - UPDATED → did/address/port 之一变化时更新
   - REMOVED → 忽略(让连接自己关闭)
   - 仅接受 role==1 且 suiteMQTT==true 的服务

### 依赖
- mDNS 库:github.com/hashicorp/mdns
- miot_error.go
- 注意:mDNS 广播只在同一子网有效

任务 3.2 — miot_lan.py → miot_lan.go

源文件:D:\miot\py-miot\miot_lan.py
目标文件:D:\miot\go-miot\miot_lan.go(新建)
详细说明:D:\miot\go-miot\migration\12-miot-lan.md

实现 MIoT LAN 设备控制(仅支持 SPEC-v2 WiFi 设备):

1. 加密层
   - AES-128-CBC 加密/解密
   - 密钥派生:aesKey = MD5(token), aesIV = MD5(aesKey + token)
   - 消息格式:0x2131 header(32字节) + 加密载荷

2. 核心类
   - LanDevice 结构体:did, token, cipher, ip, online, state
   - LanDeviceState 枚举:FRESH, PING1, PING2, PING3, DEAD
   - 保活机制(KA_INTERVAL 10~50s)
   - 在线/离线检测

3. MIoTLan 管理器
   - 设备注册/注销
   - UDP 广播发现(端口 54321)
   - 订阅管理(设备数据订阅、广播注册)
   - 网络接口变化监听(依赖 MIoTNetwork)

4. 消息处理
   - 请求/回复匹配(msg_id)
   - 通配符订阅支持
   - 离线历史记录

### 注意事项
- 依赖 miot_network.go(网络接口变化监听)
- 依赖 miot_mdns.go(mDNS 发现网关地址)
- 加密用 crypto/aes 标准库
- UDP 通信用 net 标准库
- 需要 token(设备密钥)才能加密通信