-
Notifications
You must be signed in to change notification settings - Fork 2
/
node.go
110 lines (89 loc) · 2.34 KB
/
node.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
package zda
import (
"github.com/shimmeringbee/zda/rules"
"github.com/shimmeringbee/zigbee"
"golang.org/x/sync/semaphore"
"math"
"sync"
)
type productData struct {
manufacturer string
product string
version string
serial string
}
type endpointDetails struct {
description zigbee.EndpointDescription
productInformation productData
rulesOutput rules.Output
}
type inventory struct {
description *zigbee.NodeDescription
endpoints map[zigbee.Endpoint]endpointDetails
}
func (i inventory) toRulesInput() rules.Input {
ri := rules.Input{
Node: rules.InputNode{
ManufacturerCode: int(i.description.ManufacturerCode),
Type: i.description.LogicalType.String(),
},
Product: make(map[int]rules.InputProductData),
Endpoint: make(map[int]rules.InputEndpoint),
}
for id, details := range i.endpoints {
ri.Product[int(id)] = rules.InputProductData{
Name: details.productInformation.product,
Manufacturer: details.productInformation.manufacturer,
Version: details.productInformation.version,
Serial: details.productInformation.serial,
}
var inClusters []int
var outClusters []int
for _, cid := range details.description.InClusterList {
inClusters = append(inClusters, int(cid))
}
for _, cid := range details.description.OutClusterList {
outClusters = append(outClusters, int(cid))
}
ri.Endpoint[int(id)] = rules.InputEndpoint{
ID: int(id),
ProfileID: int(details.description.ProfileID),
DeviceID: int(details.description.DeviceID),
InClusters: inClusters,
OutClusters: outClusters,
}
}
return ri
}
type node struct {
// Immutable data.
address zigbee.IEEEAddress
m *sync.RWMutex
// Thread safe data.
sequence chan uint8
enumerationSem *semaphore.Weighted
enumerationState bool
// Mutable data, obtain lock first.
device map[uint8]*device
useAPSAck bool
}
func makeTransactionSequence() chan uint8 {
ch := make(chan uint8, math.MaxUint8)
for i := range uint8(math.MaxUint8) {
ch <- i
}
return ch
}
func (n *node) nextTransactionSequence() uint8 {
nextSeq := <-n.sequence
n.sequence <- nextSeq
return nextSeq
}
func (n *node) _nextDeviceSubIdentifier() uint8 {
for i := uint8(0); i < math.MaxUint8; i++ {
if _, found := n.device[i]; !found {
return i
}
}
return math.MaxUint8
}