Files
xiaomihome/miot/examples/diag_control/main.go
T
4566704 cdc459b5ff fix(xiaomihome): 模块路径改为 git.zeroonesoft.cn/golib/xiaomihome
- 改动:go.mod module 声明与全部内部导入路径补全为完整域名路径,README 同步
- 原因:裸模块名无法被 go get 解析,发版前必须修正为 GOPRIVATE 可拉取路径
2026-10-05 08:53:23 +08:00

91 lines
3.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Command diag_control 直接控制米家设备(纯 HTTP,不初始化 MQTT)。
//
// 控制序列:开空调 → 设温度 → 设模式 → 回读验证。
// 与生产 SDK 同一云端链路(miotspec/prop/set),不占 MQTT client id。
//
// 用法:token/did 改下面常量,在生产环境 go run .(Linux 下 dataDir 填生产路径)。
package main
import (
"fmt"
"os"
"git.zeroonesoft.cn/golib/xiaomihome/miot"
)
// ── 配置:按需修改 ──
const (
accessToken = "V3_1GfW88WpawoKACRqwdYBb86In_I5e8r8stZdRPXI1PR4zZgIEm4cbjNwn0wYa6-ZQtIliJJerf-iI0W9Lx8t6fm7Ln16w0RVAmYO3e9XubJ0dK7D3eM_zBlCcVmQCla6Sx0BKQv_YXI9E2zXXopKaRjIUWyx0lb0YcE7oEopLuvfA0sEFoTohVKt543KnHJH" // 米家 OAuth access_token
targetDID = "2188673403" // 目标设备 did
dataDir = "/home/ubuntu/server/zoSmartUser/data" // spec 缓存目录(生产 Linux)
// ──────────────────────
)
func main() {
if accessToken == "" {
fmt.Fprintln(os.Stderr, "请先填写 accessToken")
os.Exit(1)
}
httpClient, err := miot.NewMIoTHttpClient("cn", miot.OAUTH2_CLIENT_ID, accessToken)
if err != nil {
fmt.Fprintf(os.Stderr, "创建 HTTP 客户端失败: %v\n", err)
os.Exit(1)
}
fmt.Printf("HTTP 客户端 OK(未连接 MQTT),设备 %s\n", targetDID)
// 读取当前状态(控制前)
readAll("控制前状态", httpClient)
// ── 控制序列 ──
set("开空调 siid=4/piid=1=true", 4, 1, true, httpClient)
set("设温度 siid=4/piid=2=26", 4, 2, float64(26), httpClient)
set("设模式 siid=2/piid=2=1(Cool)", 2, 2, float64(1), httpClient)
set("关空调 siid=4/piid=1=false", 4, 1, false, httpClient)
// 回读验证(控制后)
readAll("控制后状态", httpClient)
}
// set 写属性并回读验证。
func set(label string, siid, piid int, value interface{}, c *miot.MIoTHttpClient) {
fmt.Printf("\n=== %s ===\n", label)
rs, err := c.SetProp([]miot.PropParam{{DID: targetDID, Siid: siid, Piid: piid, Value: value}})
if err != nil {
fmt.Printf(" 写失败: %v\n", err)
return
}
for _, r := range rs {
fmt.Printf(" 写返回 code=%d\n", r.Code)
}
// 回读
gr, err := c.GetProps([]miot.PropParam{{DID: targetDID, Siid: siid, Piid: piid}})
if err != nil {
fmt.Printf(" 回读失败: %v\n", err)
return
}
if len(gr) > 0 {
fmt.Printf(" 回读 siid=%d/piid=%d = %v\n", siid, piid, gr[0].Value)
}
}
// readAll 读取关键属性。
func readAll(label string, c *miot.MIoTHttpClient) {
fmt.Printf("\n--- %s ---\n", label)
read := func(siid, piid int, name string) {
rs, err := c.GetProps([]miot.PropParam{{DID: targetDID, Siid: siid, Piid: piid}})
if err != nil {
fmt.Printf(" %-28s 错误: %v\n", name, err)
return
}
if len(rs) > 0 {
fmt.Printf(" %-28s = %v\n", name, rs[0].Value)
}
}
read(4, 1, "空调开关 siid=4/piid=1")
read(4, 2, "空调温度 siid=4/piid=2")
read(2, 2, "模式 siid=2/piid=2")
read(2, 4, "地暖温度 siid=2/piid=4")
read(5, 2, "环境温度 siid=5/piid=2")
}