From 98863c20fc4298f550e3b3f972c16c1fef91c66a Mon Sep 17 00:00:00 2001 From: codeskyblue Date: Fri, 4 Jan 2019 14:40:05 +0800 Subject: [PATCH] add get-serialno --- adb.go | 77 +++++++++----------------------------------------- client.go | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 22 +++++++++++++-- 3 files changed, 116 insertions(+), 67 deletions(-) create mode 100644 client.go diff --git a/adb.go b/adb.go index 80dcd09..8762901 100644 --- a/adb.go +++ b/adb.go @@ -118,75 +118,11 @@ func (conn *AdbConnection) readString() (string, error) { return conn.readN(length) } -type AdbClient struct { - Addr string -} - -func NewAdbClient() *AdbClient { - return &AdbClient{ - Addr: defaultHost + ":" + strconv.Itoa(defaultPort), - } -} - -var DefaultAdbClient = &AdbClient{ - Addr: "127.0.0.1:5037", -} - -func (c *AdbClient) newConnection() (conn *AdbConnection, err error) { - netConn, err := net.Dial("tcp", c.Addr) - if err != nil { - return nil, err - } - return &AdbConnection{netConn}, nil -} - -// Version return 4 size string -func (c *AdbClient) Version() (string, error) { - ver, err := c.rawVersion() - if err == nil { - return ver, nil - } - exec.Command(adbPath(), "start-server").Run() - return c.rawVersion() -} - -func (c *AdbClient) Watch() (C chan string, err error) { - C = make(chan string, 0) - defer close(C) - // c.Version() - conn, err := c.newConnection() - if err != nil { - return - } - conn.WritePacket("host:watchers") - return -} - -// Version returns adb server version -func (c *AdbClient) rawVersion() (string, error) { - conn, err := c.newConnection() - if err != nil { - return "", err - } - defer conn.Close() - if err := conn.WritePacket("host:version"); err != nil { - return "", err - } - return conn.readString() -} - type AdbDevice struct { *AdbClient Serial string } -func (c *AdbClient) DeviceWithSerial(serial string) *AdbDevice { - return &AdbDevice{ - AdbClient: c, - Serial: serial, - } -} - func (c *AdbDevice) OpenShell(cmd string) (rw io.ReadWriteCloser, err error) { conn, err := c.newConnection() if err != nil { @@ -218,3 +154,16 @@ func (c *AdbDevice) RunCommand(args ...string) (exitCode int, err error) { _, err = io.Copy(os.Stdout, reader) return } + +func (c *AdbDevice) SerialNo() (string, error) { + conn, err := c.newConnection() + if err != nil { + return "", err + } + + err = conn.WritePacket("host-serial:" + c.Serial + ":get-serialno") + if err != nil { + return "", err + } + return conn.readString() +} diff --git a/client.go b/client.go new file mode 100644 index 0000000..9198c64 --- /dev/null +++ b/client.go @@ -0,0 +1,84 @@ +package main + +import ( + "net" + "os/exec" + "strconv" + "strings" +) + +type AdbClient struct { + Addr string +} + +func NewAdbClient() *AdbClient { + return &AdbClient{ + Addr: defaultHost + ":" + strconv.Itoa(defaultPort), + } +} + +var DefaultAdbClient = &AdbClient{ + Addr: "127.0.0.1:5037", +} + +func (c *AdbClient) newConnection() (conn *AdbConnection, err error) { + netConn, err := net.Dial("tcp", c.Addr) + if err != nil { + return nil, err + } + return &AdbConnection{netConn}, nil +} + +// Version return 4 size string +func (c *AdbClient) Version() (string, error) { + ver, err := c.rawVersion() + if err == nil { + return ver, nil + } + exec.Command(adbPath(), "start-server").Run() + return c.rawVersion() +} + +func (c *AdbClient) Watch() (C chan string, err error) { + C = make(chan string, 0) + // c.Version() + conn, err := c.newConnection() + if err != nil { + return + } + conn.WritePacket("host:track-devices") + go func() { + defer close(C) + for { + line, err := conn.readString() + if err != nil { + break + } + line = strings.TrimSpace(line) + if line != "" { + C <- line + } + } + }() + return +} + +// Version returns adb server version +func (c *AdbClient) rawVersion() (string, error) { + conn, err := c.newConnection() + if err != nil { + return "", err + } + defer conn.Close() + if err := conn.WritePacket("host:version"); err != nil { + return "", err + } + return conn.readString() +} + +func (c *AdbClient) DeviceWithSerial(serial string) *AdbDevice { + return &AdbDevice{ + AdbClient: c, + Serial: serial, + } +} diff --git a/main.go b/main.go index 29d50d2..7f5e4c5 100644 --- a/main.go +++ b/main.go @@ -375,6 +375,24 @@ func main() { return runCommand("python", args...) }, }, + { + Name: "get-serialno", + Usage: "print serial-number", + Action: func(ctx *cli.Context) error { + serial, err := chooseOne() + if err != nil { + return err + } + client := NewAdbClient() + device := client.DeviceWithSerial(serial) + realSerial, err := device.SerialNo() + if err != nil { + return err + } + println(realSerial) + return nil + }, + }, { Name: "healthcheck", Usage: "check device health status", @@ -392,15 +410,13 @@ func main() { Name: "watch", Usage: "show newest state when device state change", Action: func(ctx *cli.Context) error { - log.Println("Not implemented yet.") - return nil client := NewAdbClient() eventC, err := client.Watch() if err != nil { return err } for ev := range eventC { - log.Println(ev) + println(ev) } return nil },