/
get.go
72 lines (56 loc) · 1.23 KB
/
get.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
package acl
import (
"sync"
"github.com/uhppoted/uhppote-core/types"
"github.com/uhppoted/uhppote-core/uhppote"
)
func GetACL(u uhppote.IUHPPOTE, devices []uhppote.Device) (ACL, []error) {
acl := sync.Map{}
errors := []error{}
guard := sync.RWMutex{}
for _, device := range devices {
acl.Store(device.DeviceID, map[uint32]types.Card{})
}
var wg sync.WaitGroup
for _, d := range devices {
device := d
wg.Add(1)
go func() {
if cards, err := getACL(u, device.DeviceID); err != nil {
guard.Lock()
errors = append(errors, err)
guard.Unlock()
} else {
acl.Store(device.DeviceID, cards)
}
wg.Done()
}()
}
wg.Wait()
a := make(ACL)
acl.Range(func(k, v interface{}) bool {
a[k.(uint32)] = v.(map[uint32]types.Card)
return true
})
return a, errors
}
func getACL(u uhppote.IUHPPOTE, deviceID uint32) (map[uint32]types.Card, error) {
cards := map[uint32]types.Card{}
N, err := u.GetCards(deviceID)
if err != nil {
return cards, err
}
var index uint32 = 1
for count := 0; count < int(N); {
card, err := u.GetCardByIndex(deviceID, index)
if err != nil {
return nil, err
}
if card != nil {
cards[card.CardNumber] = card.Clone()
count++
}
index++
}
return cards, nil
}