Skip to content

Commit

Permalink
feat: init device with optional serial, port, mjpeg port (#14)
Browse files Browse the repository at this point in the history
* feat: init device with optional serial, port, mjpeg port

* fix: solve some feedbacks
  • Loading branch information
debugtalk authored Aug 24, 2022
1 parent 07f4517 commit 3df5b5a
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
64 changes: 62 additions & 2 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import (
giDevice "github.com/electricbubble/gidevice"
)

const (
defaultPort = 8100
defaultMjpegPort = 9100
)

type Device struct {
deviceID int
serialNumber string
Expand All @@ -15,6 +20,61 @@ type Device struct {
d giDevice.Device
}

type DeviceOption func(d *Device)

func WithSerialNumber(serialNumber string) DeviceOption {
return func(d *Device) {
d.serialNumber = serialNumber
}
}

func WithPort(port int) DeviceOption {
return func(d *Device) {
d.Port = port
}
}

func WithMjpegPort(port int) DeviceOption {
return func(d *Device) {
d.MjpegPort = port
}
}

func NewDevice(options ...DeviceOption) (device *Device, err error) {
var usbmux giDevice.Usbmux
if usbmux, err = giDevice.NewUsbmux(); err != nil {
return nil, fmt.Errorf("init usbmux failed: %v", err)
}

var deviceList []giDevice.Device
if deviceList, err = usbmux.Devices(); err != nil {
return nil, fmt.Errorf("get attached devices failed: %v", err)
}

device = &Device{
Port: defaultPort,
MjpegPort: defaultMjpegPort,
}
for _, option := range options {
option(device)
}

serialNumber := device.serialNumber
for _, d := range deviceList {
// find device by serial number if specified
if serialNumber != "" && d.Properties().SerialNumber != serialNumber {
continue
}

device.deviceID = d.Properties().DeviceID
device.serialNumber = d.Properties().SerialNumber
device.d = d
return device, nil
}

return nil, fmt.Errorf("device %s not found", device.serialNumber)
}

func DeviceList() (devices []Device, err error) {
var usbmux giDevice.Usbmux
if usbmux, err = giDevice.NewUsbmux(); err != nil {
Expand All @@ -31,8 +91,8 @@ func DeviceList() (devices []Device, err error) {
for i := range devices {
devices[i].deviceID = deviceList[i].Properties().DeviceID
devices[i].serialNumber = deviceList[i].Properties().SerialNumber
devices[i].Port = 8100
devices[i].MjpegPort = 9100
devices[i].Port = defaultPort
devices[i].MjpegPort = defaultMjpegPort
devices[i].d = deviceList[i]
}

Expand Down
2 changes: 1 addition & 1 deletion driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
// NewDriver creates new remote client, this will also start a new session.
func NewDriver(capabilities Capabilities, urlPrefix string, mjpegPort ...int) (driver WebDriver, err error) {
if len(mjpegPort) == 0 {
mjpegPort = []int{9100}
mjpegPort = []int{defaultMjpegPort}
}
wd := new(remoteWD)
if wd.urlPrefix, err = url.Parse(urlPrefix); err != nil {
Expand Down
22 changes: 22 additions & 0 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ func TestViaUSB(t *testing.T) {
}
}

func TestNewDevice(t *testing.T) {
device, _ := NewDevice()
if device != nil {
t.Log(device)
}

device, _ = NewDevice(WithSerialNumber("xxxx"))
if device != nil {
t.Log(device)
}

device, _ = NewDevice(WithPort(8700), WithMjpegPort(8800))
if device != nil {
t.Log(device)
}

device, _ = NewDevice(WithSerialNumber("xxxx"), WithPort(8700), WithMjpegPort(8800))
if device != nil {
t.Log(device)
}
}

func TestNewDriver(t *testing.T) {
var err error
driver, err = NewDriver(nil, urlPrefix)
Expand Down

0 comments on commit 3df5b5a

Please sign in to comment.