forked from mlnoga/rct
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnection.go
291 lines (243 loc) · 6.06 KB
/
connection.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package rct
import (
"context"
"errors"
"net"
"sync"
"time"
"github.com/cenkalti/backoff/v5"
"github.com/evcc-io/rct/internal"
)
var (
// DialTimeout is the timeout for network connection
DialTimeout = 5 * time.Second
// SuccessTimeout is the timeout for data returned from device
SuccessTimeout = 30 * time.Second
// ErrMustRetry is returned when a query result was not found in the cache.
// It's up to the client application to decide the timing for retrying the query.
ErrMustRetry = errors.New("must retry")
)
// Connection to a RCT device
type Connection struct {
mu sync.Mutex
conn net.Conn
cache *cache
broker *internal.Broker[*Datagram]
errCB func(error)
timeout time.Duration
logger func(format string, a ...any)
}
// WithErrorCallback sets the error callback. It is only invoked after initial connection succeeds.
func WithErrorCallback(cb func(error)) func(*Connection) {
return func(c *Connection) {
c.errCB = cb
}
}
// WithTimeout sets the query timeout
func WithTimeout(timeout time.Duration) func(*Connection) {
return func(c *Connection) {
c.timeout = timeout
}
}
// WithLogger sets the query timeout
func WithLogger(logger func(format string, a ...any)) func(*Connection) {
return func(c *Connection) {
c.logger = logger
}
}
// Creates a new connection to a RCT device at the given address.
// Must not be called concurrently.
func NewConnection(ctx context.Context, host string, opt ...func(*Connection)) (*Connection, error) {
conn := &Connection{
cache: newCache(),
broker: internal.NewBroker[*Datagram](),
}
for _, o := range opt {
o(conn)
}
bufC := make(chan byte, 1024)
errC := make(chan error, 1)
go conn.receive(ctx, net.JoinHostPort(host, "8899"), bufC, errC)
// wait up to SuccessTimeout for first non-error response, i.e. successful read
var lastErr error
t := time.NewTimer(SuccessTimeout)
INIT:
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-t.C:
if lastErr != nil {
return nil, lastErr
}
return nil, errors.New("timeout")
case err := <-errC:
if err != nil {
lastErr = err
continue
}
break INIT
}
}
go func() {
for err := range errC {
if conn.errCB != nil {
conn.errCB(err)
}
}
}()
go conn.broker.Start(ctx)
go ParseStream(ctx, bufC, conn.broker.PublishChan())
go conn.handle(ctx, conn.broker.Subscribe())
if conn.logger != nil {
go func() {
for dg := range conn.broker.Subscribe() {
conn.log("recv", dg)
}
}()
}
return conn, nil
}
// receive streams received bytes from the connection
func (c *Connection) receive(ctx context.Context, addr string, bufC chan<- byte, errC chan<- error) {
buf := make([]byte, 1024)
defer c.close()
bo := backoff.NewExponentialBackOff()
bo.MaxInterval = 10 * time.Second
for {
conn, err := c.connect(ctx, addr)
if err != nil {
errC <- err
time.Sleep(bo.NextBackOff())
continue
}
n, err := conn.Read(buf)
if err != nil {
c.mu.Lock()
c.close()
c.mu.Unlock()
errC <- err
time.Sleep(bo.NextBackOff())
continue
}
// ack data received
errC <- nil
bo.Reset()
// stream received data
for _, b := range buf[:n] {
bufC <- b
}
}
}
// handle is the receiver go routine
func (c *Connection) connect(ctx context.Context, addr string) (net.Conn, error) {
c.mu.Lock()
defer c.mu.Unlock()
if c.conn != nil {
return c.conn, nil
}
ctx, cancel := context.WithTimeout(ctx, DialTimeout)
defer cancel()
var d net.Dialer
conn, err := d.DialContext(ctx, "tcp", addr)
if err == nil {
c.conn = conn
}
return conn, err
}
func (c *Connection) handle(ctx context.Context, dgC <-chan *Datagram) {
for {
select {
case <-ctx.Done():
return
case dg := <-dgC:
if dg.Cmd == Response || dg.Cmd == LongResponse {
c.cache.Put(dg)
}
}
}
}
// log is the logger go routine
func (c *Connection) log(action string, dg *Datagram) {
c.logger(action + ": " + dg.String())
}
// Closes the connection
func (c *Connection) close() {
if c.conn != nil {
c.conn.Close()
c.conn = nil
}
}
func (c *Connection) Send(dg *Datagram) (int, error) {
c.mu.Lock()
defer c.mu.Unlock()
// ensure active connection
if c.conn == nil {
return 0, errors.New("disconnected")
}
c.log("send", dg)
var rdb DatagramBuilder
rdb.Build(dg)
c.conn.SetWriteDeadline(time.Now().Add(DialTimeout))
n, err := c.conn.Write(rdb.Bytes())
if err != nil {
c.close()
}
return n, err
}
func (c *Connection) Subscribe() chan *Datagram {
return c.broker.Subscribe()
}
func (c *Connection) Unsubscribe(ch chan *Datagram) {
c.broker.Unsubscribe(ch)
}
func (c *Connection) Get(id Identifier) (*Datagram, time.Time) {
return c.cache.Get(id)
}
// Queries the given identifier on the RCT device, returning its value as a datagram
func (c *Connection) Query(id Identifier) (*Datagram, error) {
if dg, ts := c.cache.Get(id); dg != nil && time.Since(ts) < c.timeout {
return dg, nil
}
if _, err := c.Send(&Datagram{Read, id, nil}); err != nil {
return nil, err
}
return nil, ErrMustRetry
}
// Queries the given identifier on the RCT device, returning its value as a float32
func (c *Connection) QueryFloat32(id Identifier) (float32, error) {
dg, err := c.Query(id)
if err != nil {
return 0, err
}
return dg.Float32()
}
// Queries the given identifier on the RCT device, returning its value as a uint8
func (c *Connection) QueryInt32(id Identifier) (int32, error) {
dg, err := c.Query(id)
if err != nil {
return 0, err
}
return dg.Int32()
}
// Queries the given identifier on the RCT device, returning its value as a uint16
func (c *Connection) QueryUint16(id Identifier) (uint16, error) {
dg, err := c.Query(id)
if err != nil {
return 0, err
}
return dg.Uint16()
}
// Queries the given identifier on the RCT device, returning its value as a uint8
func (c *Connection) QueryUint8(id Identifier) (uint8, error) {
dg, err := c.Query(id)
if err != nil {
return 0, err
}
return dg.Uint8()
}
// Writes the given identifier with the given value on the RCT device
func (c *Connection) Write(id Identifier, data []byte) error {
_, err := c.Send(&Datagram{Write, id, data})
return err
}