mirror of
https://github.com/taigrr/adb.git
synced 2026-04-02 02:58:42 -07:00
in-progress of json export feature
This commit is contained in:
1
adb.go
1
adb.go
@@ -42,6 +42,7 @@ type ConnOptions struct {
|
|||||||
//
|
//
|
||||||
// This will return a Device struct, which can be used to call other methods.
|
// This will return a Device struct, which can be used to call other methods.
|
||||||
// If the connection fails or cannot complete on time, Connect will return an error.
|
// If the connection fails or cannot complete on time, Connect will return an error.
|
||||||
|
// TODO
|
||||||
func Connect(ctx context.Context, opts ConnOptions) (Device, error) {
|
func Connect(ctx context.Context, opts ConnOptions) (Device, error) {
|
||||||
if opts.Port == 0 {
|
if opts.Port == 0 {
|
||||||
opts.Port = 5555
|
opts.Port = 5555
|
||||||
|
|||||||
30
capture.go
30
capture.go
@@ -17,8 +17,34 @@ import (
|
|||||||
// Instead of using Shell, please consider submitting a PR with the functionality
|
// Instead of using Shell, please consider submitting a PR with the functionality
|
||||||
// you require.
|
// you require.
|
||||||
|
|
||||||
|
type SeqType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
SeqSwipe SeqType = iota
|
||||||
|
SeqTap
|
||||||
|
SeqSleep
|
||||||
|
)
|
||||||
|
|
||||||
|
type TapSequenceImporter struct {
|
||||||
|
Events []SequenceImporter
|
||||||
|
Resolution Resolution
|
||||||
|
}
|
||||||
|
|
||||||
|
type SequenceImporter struct {
|
||||||
|
Duration time.Duration
|
||||||
|
Type SeqType
|
||||||
|
X int
|
||||||
|
Y int
|
||||||
|
X1 int
|
||||||
|
Y1 int
|
||||||
|
X2 int
|
||||||
|
Y2 int
|
||||||
|
Start time.Time
|
||||||
|
End time.Time
|
||||||
|
}
|
||||||
type SequenceSleep struct {
|
type SequenceSleep struct {
|
||||||
Duration time.Duration
|
Duration time.Duration
|
||||||
|
Type SeqType
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s SequenceSleep) Play(d Device, ctx context.Context) error {
|
func (s SequenceSleep) Play(d Device, ctx context.Context) error {
|
||||||
@@ -44,6 +70,7 @@ type SequenceTap struct {
|
|||||||
Y int
|
Y int
|
||||||
Start time.Time
|
Start time.Time
|
||||||
End time.Time
|
End time.Time
|
||||||
|
Type SeqType
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s SequenceTap) Play(d Device, ctx context.Context) error {
|
func (s SequenceTap) Play(d Device, ctx context.Context) error {
|
||||||
@@ -69,6 +96,7 @@ type SequenceSwipe struct {
|
|||||||
Y2 int
|
Y2 int
|
||||||
Start time.Time
|
Start time.Time
|
||||||
End time.Time
|
End time.Time
|
||||||
|
Type SeqType
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s SequenceSwipe) Play(d Device, ctx context.Context) error {
|
func (s SequenceSwipe) Play(d Device, ctx context.Context) error {
|
||||||
@@ -248,6 +276,7 @@ func insertSleeps(inputs []Input) []Input {
|
|||||||
curr := input.EndTime()
|
curr := input.EndTime()
|
||||||
var sleep SequenceSleep
|
var sleep SequenceSleep
|
||||||
sleep.Duration = curr.Sub(prev)
|
sleep.Duration = curr.Sub(prev)
|
||||||
|
sleep.Type = SeqSleep
|
||||||
sleepingInputs = append(sleepingInputs, sleep)
|
sleepingInputs = append(sleepingInputs, sleep)
|
||||||
}
|
}
|
||||||
sleepingInputs = append(sleepingInputs, input)
|
sleepingInputs = append(sleepingInputs, input)
|
||||||
@@ -320,6 +349,7 @@ func (e eventSet) ToInput() (Input, error) {
|
|||||||
swipe.Y2 = int(endy)
|
swipe.Y2 = int(endy)
|
||||||
swipe.Start = e[0].TimeStamp
|
swipe.Start = e[0].TimeStamp
|
||||||
swipe.End = e[len(e)-1].TimeStamp
|
swipe.End = e[len(e)-1].TimeStamp
|
||||||
|
swipe.Type = SeqSwipe
|
||||||
return swipe, err
|
return swipe, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,9 @@
|
|||||||
module github.com/taigrr/adb/examples/tapRecorder
|
module github.com/taigrr/adb/examples/tapRecorder
|
||||||
|
|
||||||
go 1.19
|
go 1.19
|
||||||
|
|
||||||
|
replace github.com/taigrr/adb => ../..
|
||||||
|
|
||||||
|
require github.com/taigrr/adb v0.0.0-20220803063720-bd9524431495
|
||||||
|
|
||||||
|
require github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
|
||||||
|
|||||||
2
examples/tapRecorder/go.sum
Normal file
2
examples/tapRecorder/go.sum
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
|
||||||
|
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
|
||||||
@@ -1,23 +1,34 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
"github.com/taigrr/adb"
|
"github.com/taigrr/adb"
|
||||||
)
|
)
|
||||||
|
|
||||||
var command string
|
var (
|
||||||
|
command string
|
||||||
func init() {
|
file string
|
||||||
// TODO allow for any input to be used as the command
|
)
|
||||||
command = "ls"
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
flag.StringVar(&command, "command", "rec", "rec or play")
|
||||||
defer cancel()
|
flag.StringVar(&file, "file", "taps.json", "Name of the file to save taps to or to play from")
|
||||||
|
flag.Parse()
|
||||||
|
sigChan := make(chan os.Signal)
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
go func() {
|
||||||
|
<-sigChan
|
||||||
|
cancel()
|
||||||
|
}()
|
||||||
|
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
|
||||||
devs, err := adb.Devices(ctx)
|
devs, err := adb.Devices(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error enumerating devices: %v\n", err)
|
fmt.Printf("Error enumerating devices: %v\n", err)
|
||||||
@@ -33,13 +44,79 @@ func main() {
|
|||||||
// // handle error here
|
// // handle error here
|
||||||
// fmt.Printf("Error: %v\n", err)
|
// fmt.Printf("Error: %v\n", err)
|
||||||
//}
|
//}
|
||||||
fmt.Printf("Begin tapping on device %s now...\n", dev.SerialNo)
|
switch command {
|
||||||
t, err := dev.CaptureSequence(ctx)
|
case "rec":
|
||||||
if err != nil {
|
fmt.Println("Recording taps now. Hit ctrl+c to stop.")
|
||||||
fmt.Printf("Error capturing sequence: %v\n", err)
|
t, err := dev.CaptureSequence(ctx)
|
||||||
return
|
if err != nil {
|
||||||
|
fmt.Printf("Error capturing sequence: %v\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
b, _ := json.Marshal(t)
|
||||||
|
f, err := os.Create(file)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error creating tap file %s: %v", file, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
f.Write(b)
|
||||||
|
case "play":
|
||||||
|
fmt.Println("Replaying taps now. Hit ctrl+c to stop.")
|
||||||
|
f, err := os.Open(file)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error opening tap file %s: %v", file, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
var j map[string]interface{}
|
||||||
|
var t adb.TapSequence
|
||||||
|
var b bytes.Buffer
|
||||||
|
b.ReadFrom(f)
|
||||||
|
err = json.Unmarshal(b.Bytes(), &j)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error parsing tap file %s: %v", file, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if events, ok := j["Events"]; ok {
|
||||||
|
if sliceEvent, ok := events.([]interface{}); ok {
|
||||||
|
for _, e := range sliceEvent {
|
||||||
|
if mapEvent, ok := e.(map[string]interface{}); ok {
|
||||||
|
if eventType, ok := mapEvent["Type"]; ok {
|
||||||
|
if et, ok := eventType.(float64); ok {
|
||||||
|
switch int(et) {
|
||||||
|
case int(adb.SeqSleep):
|
||||||
|
t.Events = append(t.Events, adb.SequenceSleep{})
|
||||||
|
case int(adb.SeqSwipe):
|
||||||
|
t.Events = append(t.Events, adb.SequenceSwipe{})
|
||||||
|
case int(adb.SeqTap):
|
||||||
|
t.Events = append(t.Events, adb.SequenceTap{})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Could not parse %v (%T) into JSON! 1\n", eventType, eventType)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Println("Could not parse JSON! 2")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Println("Could not parse JSON! 3")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Println("Could not parse JSON! 4")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Println("Could not parse JSON! 5")
|
||||||
|
}
|
||||||
|
dev.ReplayTapSequence(ctx, t)
|
||||||
|
err = json.Unmarshal(b.Bytes(), &t)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("struct: %v\n",t)
|
||||||
|
fmt.Printf("bytes: %v\n",b.String())
|
||||||
|
fmt.Printf("Error parsing tap file %s: %v", file, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
}
|
}
|
||||||
fmt.Println("Sequence captured, replaying now...")
|
|
||||||
dev.ReplayTapSequence(context.TODO(), t)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user