Skip to content

Commit

Permalink
add get-serialno
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Jan 4, 2019
1 parent 65dedf1 commit 98863c2
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 67 deletions.
77 changes: 13 additions & 64 deletions adb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
84 changes: 84 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
22 changes: 19 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
},
Expand Down

0 comments on commit 98863c2

Please sign in to comment.