-
Notifications
You must be signed in to change notification settings - Fork 110
/
client.go
345 lines (309 loc) · 8.67 KB
/
client.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Package board contains a gRPC based board client.
package board
import (
"context"
"math"
"slices"
"sync"
"time"
"github.com/pkg/errors"
pb "go.viam.com/api/component/board/v1"
"go.viam.com/utils/protoutils"
"go.viam.com/utils/rpc"
"google.golang.org/protobuf/types/known/durationpb"
"go.viam.com/rdk/logging"
rprotoutils "go.viam.com/rdk/protoutils"
"go.viam.com/rdk/resource"
)
// errUnimplemented is used for any unimplemented methods that should
// eventually be implemented server side or faked client side.
var errUnimplemented = errors.New("unimplemented")
// client implements BoardServiceClient.
type client struct {
resource.Named
resource.TriviallyReconfigurable
resource.TriviallyCloseable
client pb.BoardServiceClient
logger logging.Logger
info boardInfo
interruptStreams []*interruptStream
mu sync.Mutex
}
type boardInfo struct {
name string
analogNames []string
digitalInterruptNames []string
}
// NewClientFromConn constructs a new Client from connection passed in.
func NewClientFromConn(
ctx context.Context,
conn rpc.ClientConn,
remoteName string,
name resource.Name,
logger logging.Logger,
) (Board, error) {
info := boardInfo{
name: name.ShortName(),
analogNames: []string{},
digitalInterruptNames: []string{},
}
bClient := pb.NewBoardServiceClient(conn)
c := &client{
Named: name.PrependRemote(remoteName).AsNamed(),
client: bClient,
logger: logger,
info: info,
}
return c, nil
}
func (c *client) AnalogByName(name string) (Analog, error) {
if !slices.Contains(c.info.analogNames, name) {
c.info.analogNames = append(c.info.analogNames, name)
}
return &analogClient{
client: c,
boardName: c.info.name,
analogName: name,
}, nil
}
func (c *client) DigitalInterruptByName(name string) (DigitalInterrupt, error) {
if !slices.Contains(c.info.digitalInterruptNames, name) {
c.info.digitalInterruptNames = append(c.info.digitalInterruptNames, name)
}
return &digitalInterruptClient{
client: c,
boardName: c.info.name,
digitalInterruptName: name,
}, nil
}
func (c *client) GPIOPinByName(name string) (GPIOPin, error) {
return &gpioPinClient{
client: c,
boardName: c.info.name,
pinName: name,
}, nil
}
func (c *client) AnalogNames() []string {
if len(c.info.analogNames) == 0 {
c.logger.Debugw("no cached analog readers")
return []string{}
}
return copyStringSlice(c.info.analogNames)
}
func (c *client) DigitalInterruptNames() []string {
if len(c.info.digitalInterruptNames) == 0 {
c.logger.Debugw("no cached digital interrupts")
return []string{}
}
return copyStringSlice(c.info.digitalInterruptNames)
}
func (c *client) SetPowerMode(ctx context.Context, mode pb.PowerMode, duration *time.Duration) error {
var dur *durationpb.Duration
if duration != nil {
dur = durationpb.New(*duration)
}
_, err := c.client.SetPowerMode(ctx, &pb.SetPowerModeRequest{Name: c.info.name, PowerMode: mode, Duration: dur})
return err
}
func (c *client) DoCommand(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error) {
return rprotoutils.DoFromResourceClient(ctx, c.client, c.info.name, cmd)
}
// analogClient satisfies a gRPC based board.AnalogReader. Refer to the interface
// for descriptions of its methods.
type analogClient struct {
*client
boardName string
analogName string
}
func (ac *analogClient) Read(ctx context.Context, extra map[string]interface{}) (int, error) {
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return 0, err
}
// the api method is named ReadAnalogReader, it is named differently than
// the board interface functions.
resp, err := ac.client.client.ReadAnalogReader(ctx, &pb.ReadAnalogReaderRequest{
BoardName: ac.boardName,
AnalogReaderName: ac.analogName,
Extra: ext,
})
if err != nil {
return 0, err
}
return int(resp.Value), nil
}
func (ac *analogClient) Write(ctx context.Context, value int, extra map[string]interface{}) error {
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return err
}
_, err = ac.client.client.WriteAnalog(ctx, &pb.WriteAnalogRequest{
Name: ac.boardName,
Pin: ac.analogName,
Value: int32(value),
Extra: ext,
})
if err != nil {
return err
}
return nil
}
// digitalInterruptClient satisfies a gRPC based board.DigitalInterrupt. Refer to the
// interface for descriptions of its methods.
type digitalInterruptClient struct {
*client
boardName string
digitalInterruptName string
}
func (dic *digitalInterruptClient) Value(ctx context.Context, extra map[string]interface{}) (int64, error) {
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return 0, err
}
resp, err := dic.client.client.GetDigitalInterruptValue(ctx, &pb.GetDigitalInterruptValueRequest{
BoardName: dic.boardName,
DigitalInterruptName: dic.digitalInterruptName,
Extra: ext,
})
if err != nil {
return 0, err
}
return resp.Value, nil
}
func (dic *digitalInterruptClient) Tick(ctx context.Context, high bool, nanoseconds uint64) error {
panic(errUnimplemented)
}
func (dic *digitalInterruptClient) Name() string {
return dic.digitalInterruptName
}
func (c *client) StreamTicks(ctx context.Context, interrupts []DigitalInterrupt, ch chan Tick,
extra map[string]interface{},
) error {
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return err
}
stream := &interruptStream{
extra: ext,
client: c,
}
c.mu.Lock()
c.interruptStreams = append(c.interruptStreams, stream)
c.mu.Unlock()
err = stream.startStream(ctx, interrupts, ch)
if err != nil {
return err
}
return nil
}
func (c *client) removeStream(s *interruptStream) {
c.mu.Lock()
defer c.mu.Unlock()
for i, stream := range s.interruptStreams {
if stream == s {
// To remove this item, we replace it with the last item in the list, then truncate the
// list by 1.
s.client.interruptStreams[i] = s.client.interruptStreams[len(s.client.interruptStreams)-1]
s.client.interruptStreams = s.client.interruptStreams[:len(s.client.interruptStreams)-1]
break
}
}
}
// gpioPinClient satisfies a gRPC based board.GPIOPin. Refer to the interface
// for descriptions of its methods.
type gpioPinClient struct {
*client
boardName string
pinName string
}
func (gpc *gpioPinClient) Set(ctx context.Context, high bool, extra map[string]interface{}) error {
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return err
}
_, err = gpc.client.client.SetGPIO(ctx, &pb.SetGPIORequest{
Name: gpc.boardName,
Pin: gpc.pinName,
High: high,
Extra: ext,
})
return err
}
func (gpc *gpioPinClient) Get(ctx context.Context, extra map[string]interface{}) (bool, error) {
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return false, err
}
resp, err := gpc.client.client.GetGPIO(ctx, &pb.GetGPIORequest{
Name: gpc.boardName,
Pin: gpc.pinName,
Extra: ext,
})
if err != nil {
return false, err
}
return resp.High, nil
}
func (gpc *gpioPinClient) PWM(ctx context.Context, extra map[string]interface{}) (float64, error) {
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return math.NaN(), err
}
resp, err := gpc.client.client.PWM(ctx, &pb.PWMRequest{
Name: gpc.boardName,
Pin: gpc.pinName,
Extra: ext,
})
if err != nil {
return math.NaN(), err
}
return resp.DutyCyclePct, nil
}
func (gpc *gpioPinClient) SetPWM(ctx context.Context, dutyCyclePct float64, extra map[string]interface{}) error {
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return err
}
_, err = gpc.client.client.SetPWM(ctx, &pb.SetPWMRequest{
Name: gpc.boardName,
Pin: gpc.pinName,
DutyCyclePct: dutyCyclePct,
Extra: ext,
})
return err
}
func (gpc *gpioPinClient) PWMFreq(ctx context.Context, extra map[string]interface{}) (uint, error) {
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return 0, err
}
resp, err := gpc.client.client.PWMFrequency(ctx, &pb.PWMFrequencyRequest{
Name: gpc.boardName,
Pin: gpc.pinName,
Extra: ext,
})
if err != nil {
return 0, err
}
return uint(resp.FrequencyHz), nil
}
func (gpc *gpioPinClient) SetPWMFreq(ctx context.Context, freqHz uint, extra map[string]interface{}) error {
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return err
}
_, err = gpc.client.client.SetPWMFrequency(ctx, &pb.SetPWMFrequencyRequest{
Name: gpc.boardName,
Pin: gpc.pinName,
FrequencyHz: uint64(freqHz),
Extra: ext,
})
return err
}
// copyStringSlice is a helper to simply copy a string slice
// so that no one mutates it.
func copyStringSlice(src []string) []string {
out := make([]string, len(src))
copy(out, src)
return out
}