feat: 初始化小米 IoT (MIoT) 智能家居 Go 库

- 实现 MIoT 客户端核心功能(MQTT 连接、设备管理、属性读写)
- 支持云端 API 调用与局域网设备发现(mDNS)
- 集成国际化(i18n)多语言支持
- 添加 MIoT 设备规约解析器(spec_parser)
- 包含单元测试与使用示例
This commit is contained in:
2026-06-28 22:05:49 +08:00
parent ac442812b4
commit 3a44cb9e6a
118 changed files with 31630 additions and 0 deletions
+204
View File
@@ -0,0 +1,204 @@
# 示例任务提示词
> 目标目录:`go-miot/examples/`
> 公共配置:`examples/config/config.go`
> **约束:禁用命令行参数 + 禁用环境变量,仅用 config.json,其余变量直接硬编码**
---
## E0 — config(先做)
```
目标目录:D:\miot\go-miot\examples\config
目标文件:D:\miot\go-miot\examples\config\config.go
创建公共配置包,所有示例共享。
1. Config 结构体:
CloudServer, ClientID, AccessToken, RefreshToken string
UID, UUID, RedirectURL, Language string
CertFile, KeyFile, CAFile string
TokenPath string // 本地 config.json 路径
2. LoadConfig(path string) (*Config, error)
- 若 path 非空,读取 JSON 文件
- 若 path 为空,返回 SampleConfig() 默认值
- **禁用环境变量,禁用命令行参数**
3. SaveConfig(path string, cfg *Config) error
- 写回 JSON 文件(用于 OAuth 示例保存 token)
4. SampleConfig() *Config
- 返回带默认值的配置(CloudServer="cn", ClientID=OAUTH2常量等)
```
---
## E1 — cloud_oauth
```
目标文件:D:\miot\go-miot\examples\cloud_oauth\main.go
详细说明:D:\miot\go-miot\EXAMPLES_PLAN.md
package main,import miot + examples/config
流程:
1. cfg := config.LoadConfig("config.json")
2. oauth := miot.NewMIoTOauthClient(cfg.ClientID, cfg.RedirectURL, cfg.CloudServer, cfg.UUID)
3. url := oauth.GenAuthURL("", "", nil, false)
4. fmt.Println("打开此URL授权:", url)
5. fmt.Print("输入回调code: "); var code string; fmt.Scanln(&code)
6. token, err := oauth.GetAccessToken(code)
7. cfg.AccessToken = token["access_token"], cfg.RefreshToken = token["refresh_token"]
8. config.SaveConfig("config.json", cfg)
9. 演示 refresh: newToken, err := oauth.RefreshAccessToken(cfg.RefreshToken)
```
---
## E2 — cloud_devices
```
目标文件:D:\miot\go-miot\examples\cloud_devices\main.go
流程:
1. cfg := config.LoadConfig("config.json")
2. http, err := miot.NewMIoTHttpClient(cfg.CloudServer, cfg.ClientID, cfg.AccessToken)
3. user, _ := http.GetUserInfo()
4. homes, _ := http.GetHomeInfos()
5. devices, _ := http.GetDevices(nil)
6. 选第一个设备 did,调用 http.GetProp(did, siid, piid)
7. 调用 http.SetProp(did, siid, piid, value)
8. 调用 http.DoAction(did, siid, aiid, inList)
9. 所有结果 fmt.Printf 打印
```
---
## E3 — mips_subscribe
```
目标文件:D:\miot\go-miot\examples\mips_subscribe\main.go
流程:
1. cfg := config.LoadConfig("config.json")
2. storage := miot.NewMIoTStorage("./data")
3. cert, _ := miot.NewMIoTCert(storage, cfg.UID, cfg.CloudServer)
4. mips := miot.NewMipsCloudClient("ssl://cn-ha.mqtt.io.mi.com:8883", cfg.ClientID, cfg.UID, cfg.AccessToken)
5. mips.Connect()
6. mips.SubProp(did, handler) / mips.SubEvent(did, handler)
7. time.Sleep(60 * time.Second)
8. mips.Disconnect()
```
---
## E4 — lan_control
```
目标文件:D:\miot\go-miot\examples\lan_control\main.go
流程:
1. cfg := config.LoadConfig("config.json")
2. network := miot.NewMIoTNetwork(nil, nil, 30*time.Second)
3. network.Init()
4. lan, err := miot.NewMIoTLan(nil, network, nil, true, 0)
5. lan.UpdateDevices([]miot.LanDeviceInfo{{Did: "device_did", Token: "device_hex_token"}})
6. lan.SubProp(did, handler)
7. 等待设备上线,打印属性
8. time.Sleep(30 * time.Second)
```
---
## E5 — mdns_discovery
```
目标文件:D:\miot\go-miot\examples\mdns_discovery\main.go
流程:
1. svc := miot.NewMipsService()
2. svc.Init()
3. svc.SubServiceChange("demo", "*", handler)
4. services := svc.GetServices("")
5. 打印每个服务的 Name, Addresses, Port, DID, GroupID
6. time.Sleep(30 * time.Second)
7. svc.Deinit()
```
---
## E6 — network_monitor
```
目标文件:D:\miot\go-miot\examples\network_monitor\main.go
流程:
1. network := miot.NewMIoTNetwork(nil, nil, 30*time.Second)
2. network.Init()
3. 打印 network.GetNetworkStatus()
4. 打印 network.GetNetworkInfo() 每个网卡
5. network.SubNetworkStatus("demo", handler)
6. network.SubNetworkInfo("demo", handler)
7. time.Sleep(30 * time.Second)
```
---
## E7 — storage_manage
```
目标文件:D:\miot\go-miot\examples\storage_manage\main.go
流程:
1. cfg := config.LoadConfig("config.json")
2. storage := miot.NewMIoTStorage("./data")
3. storage.Save("test", "hello", "world") / Load("test", "hello", "str")
4. storage.UpdateUserConfig(cfg.UID, cfg.CloudServer, map, false)
5. cert, _ := miot.NewMIoTCert(storage, cfg.UID, cfg.CloudServer)
6. cert.VerifyCACert()
7. mfg := miot.NewDeviceManufacturer(storage)
8. mfg.Init() / mfg.GetName("xiaomi")
```
---
## E8 — spec_parser
```
目标文件:D:\miot\go-miot\examples\spec_parser\main.go
流程:
1. storage := miot.NewMIoTStorage("./data")
2. parser := miot.NewMIoTSpecParser("zh-Hans", storage, "specs")
3. instance, err := parser.Parse("urn:miot-spec-v2:device:light:0000A001:yeelink-light1:1", "")
4. 遍历 instance.Services:
对每个 MIoTSpecService:
打印 Type, Description
遍历 Properties: 打印 IID/Format/Access/Writable/ValueRange
演示 ValueList.Names() / GetValueByDescription()
遍历 Actions: 打印 IID/In/Out
演示 ValuePrecision / ValueFormat
```
---
## E9 — full_client
```
目标文件:D:\miot\go-miot\examples\full_client\main.go
流程:
1. cfg := config.LoadConfig("config.json")
2. client := miot.NewMIoTClient("demo", entryData, cfg.UID, cfg.CloudServer, miot.CtrlModeCloud)
3. client.SetStorage(storage)
4. client.SetHTTPClient(http)
5. client.SetMipsCloud(mips)
6. client.Init()
7. client.RefreshDevices()
8. client.SubDeviceState(did, handler)
9. client.SubProp(did, handler, siid, piid)
10. 启动 LAN/mDNS/Network
11. signal 捕获 Ctrl+C
12. client.Deinit()
```