-
Notifications
You must be signed in to change notification settings - Fork 8
/
zstack.go
121 lines (93 loc) · 2.75 KB
/
zstack.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
package zstack
import (
"context"
"github.com/shimmeringbee/logwrap"
"github.com/shimmeringbee/logwrap/impl/golog"
"github.com/shimmeringbee/persistence"
"github.com/shimmeringbee/unpi/broker"
"github.com/shimmeringbee/unpi/library"
"github.com/shimmeringbee/zigbee"
"golang.org/x/sync/semaphore"
"io"
"log"
"os"
"time"
)
type RequestResponder interface {
RequestResponse(ctx context.Context, req interface{}, resp interface{}) error
}
type Awaiter interface {
Await(ctx context.Context, resp interface{}) error
}
type Subscriber interface {
Subscribe(message interface{}, callback func(v interface{})) (error, func())
}
type ZStack struct {
requestResponder RequestResponder
awaiter Awaiter
subscriber Subscriber
NetworkProperties NetworkProperties
events chan interface{}
networkManagerStop chan bool
networkManagerIncoming chan interface{}
messageReceiverStop func()
nodeTable *nodeTable
transactionIdStore chan uint8
persistence persistence.Section
sem *semaphore.Weighted
logger logwrap.Logger
}
var _ zigbee.Provider = (*ZStack)(nil)
type JoinState uint8
const (
Off JoinState = 0x00
OnCoordinator JoinState = 0x01
OnAllRouters JoinState = 0x02
)
type NetworkProperties struct {
NetworkAddress zigbee.NetworkAddress
IEEEAddress zigbee.IEEEAddress
PANID zigbee.PANID
ExtendedPANID zigbee.ExtendedPANID
NetworkKey zigbee.NetworkKey
Channel uint8
JoinState JoinState
}
const DefaultZStackTimeout = 5 * time.Second
const DefaultResolveIEEETimeout = 500 * time.Millisecond
const DefaultZStackRetries = 3
const DefaultInflightEvents = 50
const DefaultInflightTransactions = 20
func New(uart io.ReadWriter, p persistence.Section) *ZStack {
ml := library.NewLibrary()
registerMessages(ml)
znp := broker.NewBroker(uart, uart, ml)
znp.Start()
transactionIDs := make(chan uint8, DefaultInflightTransactions)
for i := range DefaultInflightTransactions {
transactionIDs <- uint8(i)
}
zstack := &ZStack{
requestResponder: znp,
awaiter: znp,
subscriber: znp,
events: make(chan interface{}, DefaultInflightEvents),
networkManagerStop: make(chan bool, 1),
networkManagerIncoming: make(chan interface{}, DefaultInflightEvents),
nodeTable: newNodeTable(p.Section("Nodes")),
transactionIdStore: transactionIDs,
persistence: p,
}
zstack.WithGoLogger(log.New(os.Stderr, "", log.LstdFlags))
return zstack
}
func (z *ZStack) Stop() {
z.stopNetworkManager()
z.stopMessageReceiver()
}
func (z *ZStack) WithGoLogger(parentLogger *log.Logger) {
z.logger = logwrap.New(golog.Wrap(parentLogger))
}
func (z *ZStack) WithLogWrapLogger(parentLogger logwrap.Logger) {
z.logger = parentLogger
}