Skip to content

Commit

Permalink
去掉第三方adb库
Browse files Browse the repository at this point in the history
  • Loading branch information
IoTServ committed Feb 24, 2021
1 parent be09388 commit 92364ae
Show file tree
Hide file tree
Showing 277 changed files with 99,052 additions and 25,651 deletions.
7 changes: 5 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package config
import (
"fmt"
"github.com/IoTDevice/phicomm-r1-controler/utils"
adb "github.com/mDNSService/goadb"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
Expand All @@ -21,10 +20,14 @@ var SingleServicePort = 0
var ConfigFileName = "phicomm-r1-controler.yaml"
var ConfigFilePath = fmt.Sprintf("./%s", ConfigFileName)
var ConfigModelVar = &ConfigModel{
ADBConfig: &adb.ServerConfig{PathToAdb: ""},
ADBConfig: &ServerConfig{PathToAdb: ""},
NetworkDevices: []string{"192.168.123.146:5555"},
}

type ServerConfig struct {
PathToAdb string
}

//将配置写入指定的路径的文件
func WriteConfigFile(ConfigMode *ConfigModel, path string) (err error) {
configByte, err := yaml.Marshal(ConfigMode)
Expand Down
60 changes: 52 additions & 8 deletions config/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/OpenIoTHub/service-register/nettool"
"github.com/gin-gonic/gin"
"github.com/grandcat/zeroconf"
adb "github.com/mDNSService/goadb"
"io/ioutil"
"log"
"net"
Expand All @@ -18,13 +17,62 @@ import (
"time"
)

type AdbDeviceInfo struct {
Serial string
}

type ConfigModel struct {
ADBConfig *adb.ServerConfig
ADBConfig *ServerConfig
NetworkDevices []string
}

func (cm *ConfigModel) RunAdbCmd(cmd []string) (string, error) {
var name = "adb"
if cm.ADBConfig.PathToAdb != "" {
name = cm.ADBConfig.PathToAdb
}

cmdOut := &exec.Cmd{
Path: name,
Args: append([]string{name}, cmd...),
}
if filepath.Base(name) == name {
if lp, err := exec.LookPath(name); err != nil {
return "", err
} else {
cmdOut.Path = lp
}
}
outbytes, err := cmdOut.Output()
return string(outbytes), err
}

func (cm *ConfigModel) StartAdbServer() (string, error) {
return cm.RunAdbCmd([]string{"start-server"})
}

func (cm *ConfigModel) ListDevices() (devices []*AdbDeviceInfo, err error) {
out, err := cm.RunAdbCmd([]string{"devices"})
if err != nil {
log.Fatalln(err)
}
//log.Println("ListDevices:")
//log.Println(out)
out = strings.Replace(out, "List of devices attached", "", -1)
//log.Println(out)
outN := strings.Split(strings.Trim(out, "\n"), "\n")
//log.Println(len(outN))
for _, line := range outN {
//log.Println(line)
serialInfo := strings.SplitN(line, " ", 2)
//log.Println(serialInfo[0])
devices = append(devices, &AdbDeviceInfo{Serial: serialInfo[0]})
}

return
}

type AndroidAdbDeviceWithOpenIoTHub struct {
*adb.Device
SerialID string
listener net.Listener
zeroconfServer *zeroconf.Server
Expand Down Expand Up @@ -252,14 +300,10 @@ func (ao *AndroidAdbDeviceWithOpenIoTHub) RunAdbCommand(args []string) (string,
if ConfigModelVar.ADBConfig.PathToAdb != "" {
name = ConfigModelVar.ADBConfig.PathToAdb
}
s, err := ao.Serial()
if err != nil {
return "", err
}

cmdOut := &exec.Cmd{
Path: name,
Args: append([]string{name, "-s", s}, args...),
Args: append([]string{name, "-s", ao.SerialID}, args...),
}
if filepath.Base(name) == name {
if lp, err := exec.LookPath(name); err != nil {
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ go 1.15
require (
github.com/OpenIoTHub/service-register v0.1.11
github.com/gin-gonic/gin v1.6.3
github.com/go-bindata/go-bindata v3.1.2+incompatible // indirect
github.com/grandcat/zeroconf v1.0.0
github.com/mDNSService/goadb v0.0.0-20210203094918-19a1d1baf045
github.com/urfave/cli/v2 v2.2.0
gopkg.in/yaml.v2 v2.2.8
)
Expand Down
25 changes: 2 additions & 23 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/OpenIoTHub/service-register v0.1.10 h1:Ba2opzY/k4Q4rZsd6xXtIhFPUbaSNp7VidZ7WcTG0k8=
github.com/OpenIoTHub/service-register v0.1.10/go.mod h1:9ivJCDbIesbP+MRTCtMdBUlx9R3H6xLtAttPWi2T3V8=
github.com/OpenIoTHub/service-register v0.1.11 h1:5tSmxLVM6p0JaizFlaYMu0oPTqmID5b8e012Rtei/LE=
github.com/OpenIoTHub/service-register v0.1.11/go.mod h1:9ivJCDbIesbP+MRTCtMdBUlx9R3H6xLtAttPWi2T3V8=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20201120081800-1786d5ef83d4/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/cheggaaa/pb v1.0.29/go.mod h1:W40334L7FMC5JKWldsTWbdGjLo0RxUKK73K+TuPxX30=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/go-bindata/go-bindata v3.1.2+incompatible h1:5vjJMVhowQdPzjE1LdxyFF7YFTXg5IgGVW4gBr5IbvE=
github.com/go-bindata/go-bindata v3.1.2+incompatible/go.mod h1:xK8Dsgwmeed+BBsSy2XTopBn/8uK2HWuGSnA11C3Joo=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
Expand All @@ -42,14 +34,8 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/mDNSService/goadb v0.0.0-20210203094918-19a1d1baf045 h1:9itOVg8qrI5aStLSSNPubNITbm6mXj4hyftFfFsJsHw=
github.com/mDNSService/goadb v0.0.0-20210203094918-19a1d1baf045/go.mod h1:O8cMm43h9+3FljOdmbxy7Fq+Meg/+7lI4sVcyYRO6Ew=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/miekg/dns v1.1.27 h1:aEH/kqUzUxGJ/UHcEKdJY+ugH6WEzsEBBSPa8zuy1aM=
github.com/miekg/dns v1.1.27/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
Expand All @@ -68,9 +54,8 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5I
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
Expand All @@ -89,19 +74,15 @@ golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c h1:VwygUrnw9jn88c4u8GD3rZQbqrP/tgas88tPUbBxQrk=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
Expand All @@ -110,5 +91,3 @@ gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
36 changes: 11 additions & 25 deletions services/run.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package services

import (
"fmt"
"github.com/IoTDevice/phicomm-r1-controler/config"
adb "github.com/mDNSService/goadb"
"github.com/urfave/cli/v2"
"log"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
)

Expand All @@ -22,33 +18,33 @@ func Run(c *cli.Context) (err error) {
log.Println(err)
}
//启动adb服务
adbClient, err := adb.NewWithConfig(*config.ConfigModelVar.ADBConfig)
_, err = config.ConfigModelVar.StartAdbServer()
if err != nil {
log.Fatalln(err)
}
if err != nil {
return
}
//连接配置文件的所有网络安卓adb设备
if config.SingleIpPort != "" {
ConnectOneDevice(adbClient, config.SingleIpPort)
ConnectOneDevice(config.SingleIpPort)
} else {
for _, device := range config.ConfigModelVar.NetworkDevices {
ConnectOneDevice(adbClient, device)
ConnectOneDevice(device)
}
}
devList, err := adbClient.ListDevices()
devList, err := config.ConfigModelVar.ListDevices()
if err != nil {
log.Fatal(err)
}
for _, info := range devList {
log.Println("List adb devices:")
log.Printf("%+v", info)
dev := adbClient.Device(adb.DeviceWithSerial(info.Serial))
id := fmt.Sprintf("%s-%s", info.Model, info.Serial)
id := info.Serial
log.Println("id:", id)
AndroidAdbDeviceWithOpenIoTHubMap[id] = &config.AndroidAdbDeviceWithOpenIoTHub{
Device: dev,
SerialID: info.Serial,
}
log.Println(id, dev.String())
}
for _, do := range AndroidAdbDeviceWithOpenIoTHubMap {
go do.Reg()
Expand Down Expand Up @@ -79,19 +75,9 @@ func RunAdbCommand(args []string) (string, error) {
return string(outbytes), err
}

func ConnectOneDevice(adbClient *adb.Adb, device string) (err error) {
log.Println("connecting :", config.SingleIpPort)
var ip string
var port int
if sn := strings.SplitN(device, ":", 2); strings.Contains(device, ":") && len(sn) == 2 {
ip = sn[0]
port, err = strconv.Atoi(sn[1])

} else {
ip = device
port = 5555
}
err = adbClient.Connect(ip, port)
func ConnectOneDevice(device string) (err error) {
log.Println("connecting :", device)
_, err = RunAdbCommand([]string{"connect", device})
if err != nil {
log.Println(err)
}
Expand Down
1 change: 0 additions & 1 deletion vendor/github.com/mDNSService/goadb/.gitignore

This file was deleted.

Loading

0 comments on commit 92364ae

Please sign in to comment.