in-progress of json export feature

This commit is contained in:
2022-08-03 12:06:23 -07:00
parent bd95244314
commit 08c76ed2c8
5 changed files with 132 additions and 16 deletions

View File

@@ -1,23 +1,34 @@
package main
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"time"
"os"
"os/signal"
"syscall"
"github.com/taigrr/adb"
)
var command string
func init() {
// TODO allow for any input to be used as the command
command = "ls"
}
var (
command string
file string
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
flag.StringVar(&command, "command", "rec", "rec or play")
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)
if err != nil {
fmt.Printf("Error enumerating devices: %v\n", err)
@@ -33,13 +44,79 @@ func main() {
// // handle error here
// fmt.Printf("Error: %v\n", err)
//}
fmt.Printf("Begin tapping on device %s now...\n", dev.SerialNo)
t, err := dev.CaptureSequence(ctx)
if err != nil {
fmt.Printf("Error capturing sequence: %v\n", err)
return
switch command {
case "rec":
fmt.Println("Recording taps now. Hit ctrl+c to stop.")
t, err := dev.CaptureSequence(ctx)
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)
}
}