-
Notifications
You must be signed in to change notification settings - Fork 109
/
client.go
62 lines (54 loc) · 1.48 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
// Package sensor contains a gRPC based sensor client.
package sensor
import (
"context"
commonpb "go.viam.com/api/common/v1"
pb "go.viam.com/api/component/sensor/v1"
"go.viam.com/utils/rpc"
"google.golang.org/protobuf/types/known/structpb"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/protoutils"
"go.viam.com/rdk/resource"
)
// client implements SensorServiceClient.
type client struct {
resource.Named
resource.TriviallyReconfigurable
resource.TriviallyCloseable
name string
client pb.SensorServiceClient
logger logging.Logger
}
// 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,
) (Sensor, error) {
c := pb.NewSensorServiceClient(conn)
return &client{
Named: name.PrependRemote(remoteName).AsNamed(),
name: name.ShortName(),
client: c,
logger: logger,
}, nil
}
func (c *client) Readings(ctx context.Context, extra map[string]interface{}) (map[string]interface{}, error) {
ext, err := structpb.NewStruct(extra)
if err != nil {
return nil, err
}
resp, err := c.client.GetReadings(ctx, &commonpb.GetReadingsRequest{
Name: c.name,
Extra: ext,
})
if err != nil {
return nil, err
}
return protoutils.ReadingProtoToGo(resp.Readings)
}
func (c *client) DoCommand(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error) {
return protoutils.DoFromResourceClient(ctx, c.client, c.name, cmd)
}