-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcenter.go
155 lines (132 loc) · 2.92 KB
/
center.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package tunes
import (
"context"
"strconv"
"strings"
"sync"
"time"
iapi "github.com/yinyajiang/go-tunes/itunesapi"
)
//NotifyEvent ...
type NotifyEvent struct {
Dev Device
Event string
}
var (
devMutex sync.Mutex
devices map[string]*deviceImpl = make(map[string]*deviceImpl, 2)
extractCancleFuns map[string]context.CancelFunc = make(map[string]context.CancelFunc, 2)
subMutex sync.Mutex
subChans map[chan *NotifyEvent]struct{} = make(map[chan *NotifyEvent]struct{}, 2)
iapiSub bool
)
//SubscriptionDeviceNotify ...
func SubscriptionDeviceNotify() (subChan <-chan *NotifyEvent) {
return subscription(true)
}
//RunEventLoop ...
func RunEventLoop() {
iapi.RunLoopRun()
}
//StopEventLoop ...
func StopEventLoop() {
iapi.RunLoopStop()
}
//OnceForWaitDevice ...
func OnceForWaitDevice(ctx context.Context, id string) (dev Device) {
subscription(false)
dev = waitForDevice(ctx, id)
return
}
//UnsubscriptionDeviceNotify ..
func UnsubscriptionDeviceNotify(ch chan *NotifyEvent) {
if nil == ch {
return
}
subMutex.Lock()
defer subMutex.Unlock()
delete(subChans, ch)
close(ch)
}
//DeviceCount ...
func DeviceCount() int {
return len(devices)
}
func waitForDevice(ctx context.Context, id string) (dev Device) {
for dev == nil {
devMutex.Lock()
if !strings.HasPrefix(id, "0x") {
id = strings.TrimLeft(id, "0")
}
//转成10进制
idInt, err := strconv.ParseInt(id, 0, 64)
if err != nil {
return nil
}
id = strconv.FormatInt(idInt, 10)
devImpl, ok := devices[id]
devMutex.Unlock()
if ok {
dev = devImpl
return
}
select {
case <-ctx.Done():
return
case <-time.After(time.Second):
}
}
return
}
//IsExistDevice ...
func IsExistDevice(id string) (b bool) {
devMutex.Lock()
defer devMutex.Unlock()
_, b = devices[id]
return
}
func subscription(retChan bool) (subChan <-chan *NotifyEvent) {
if retChan {
ch := make(chan *NotifyEvent, 5)
subChan = ch
subMutex.Lock()
subChans[ch] = struct{}{}
subMutex.Unlock()
}
if !iapiSub {
iapi.AMRestorableDeviceRegisterForNotificationsForDevices(deviceEvent)
iapiSub = true
}
return
}
func deviceEvent(even, mode string, modeDevice, restorableDevice uintptr) {
var dev *deviceImpl
devMutex.Lock()
if even == "insert" {
extractCtx, cancleFun := context.WithCancel(context.Background())
dev = newDeviceImpl(extractCtx, mode, modeDevice, restorableDevice)
devices[dev.ID()] = dev
extractCancleFuns[dev.ID()] = cancleFun
} else {
dev = newDeviceImpl(nil, mode, modeDevice, restorableDevice)
delDev, ok := devices[dev.ID()]
if ok {
extractCancleFuns[delDev.ID()]()
delete(devices, delDev.ID())
delete(extractCancleFuns, delDev.ID())
}
}
devMutex.Unlock()
subMutex.Lock()
tempSubChans := subChans
subMutex.Unlock()
for c := range tempSubChans {
select {
case c <- &NotifyEvent{
Event: even,
Dev: dev,
}:
case <-time.After(time.Second * 2):
}
}
}