From cc1674b25577f8105acb96dbf2c2019783624996 Mon Sep 17 00:00:00 2001 From: suguo Date: Fri, 31 Mar 2023 08:32:10 +0800 Subject: [PATCH] init --- .gitignore | 3 +++ go.mod | 10 ++++++++ main.go | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ readme.md | 2 ++ 4 files changed, 87 insertions(+) create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 main.go create mode 100644 readme.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a1b2e31 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +go.sum +totalcontrol-demo +*.exe \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..674b022 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module myschools.me/suguo/totalcontrol-demo + +go 1.19 + +require github.com/tidwall/gjson v1.14.4 + +require ( + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.0 // indirect +) diff --git a/main.go b/main.go new file mode 100644 index 0000000..e98efc3 --- /dev/null +++ b/main.go @@ -0,0 +1,72 @@ +package main + +import ( + "bytes" + "fmt" + "io/ioutil" + "net/http" + "time" + + "github.com/tidwall/gjson" +) + +func main() { + // 查找设备 + resp, err := http.Get("http://localhost:5037/devices") + if err != nil { + panic(err) + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + panic(err) + } + + // 解析设备信息 + serial := gjson.GetBytes(body, "devices.0.serial").String() + + // 连接设备 + resp, err = http.Post(fmt.Sprintf("http://localhost:5037/devices/%s/connect", serial), "", nil) + if err != nil { + panic(err) + } + defer resp.Body.Close() + + // 获取应用程序内容页信息 + resp, err = http.Post(fmt.Sprintf("http://localhost:5037/devices/%s/activities", serial), "application/json", bytes.NewBufferString(`{"package":"com.example.myapp"}`)) + if err != nil { + panic(err) + } + defer resp.Body.Close() + + body, err = ioutil.ReadAll(resp.Body) + if err != nil { + panic(err) + } + + // 解析应用程序内容页信息 + layout := gjson.GetBytes(body, "layout") + + // 查找关闭按钮位置 + var x, y int + layout.ForEach(func(key, value gjson.Result) bool { + if value.Get("class").String() == "android.widget.Button" && value.Get("text").String() == "Close" { + x = int(value.Get("bounds.0").Float()) + y = int(value.Get("bounds.1").Float()) + return false + } + return true + }) + + // 发送模拟点击指令 + cmd := fmt.Sprintf("input tap %d %d", x, y) + resp, err = http.Post(fmt.Sprintf("http://localhost:5037/devices/%s/shell", serial), "application/octet-stream", bytes.NewBufferString(cmd)) + if err != nil { + panic(err) + } + defer resp.Body.Close() + + // 等待点击操作完成 + time.Sleep(time.Second) +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..20e6e3b --- /dev/null +++ b/readme.md @@ -0,0 +1,2 @@ +Total Control 控制演练 +