forked from electricbubble/gwda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.go
112 lines (89 loc) · 2.28 KB
/
device.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package gwda
import (
"fmt"
giDevice "github.com/electricbubble/gidevice"
)
const (
defaultPort = 8100
defaultMjpegPort = 9100
)
type Device struct {
deviceID int
serialNumber string
Port int
MjpegPort int
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 {
return nil, fmt.Errorf("usbmuxd: %w", err)
}
var deviceList []giDevice.Device
if deviceList, err = usbmux.Devices(); err != nil {
return nil, fmt.Errorf("device list: %w", err)
}
devices = make([]Device, len(deviceList))
for i := range devices {
devices[i].deviceID = deviceList[i].Properties().DeviceID
devices[i].serialNumber = deviceList[i].Properties().SerialNumber
devices[i].Port = defaultPort
devices[i].MjpegPort = defaultMjpegPort
devices[i].d = deviceList[i]
}
return
}
func (d Device) DeviceID() int {
return d.deviceID
}
func (d Device) SerialNumber() string {
return d.serialNumber
}
func (d Device) GIDevice() giDevice.Device {
return d.d
}