-
Notifications
You must be signed in to change notification settings - Fork 110
/
client.go
98 lines (87 loc) · 2.23 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
// Package gripper contains a gRPC based gripper client.
package gripper
import (
"context"
"github.com/edaniels/golog"
pb "go.viam.com/api/component/gripper/v1"
"go.viam.com/utils/protoutils"
"go.viam.com/utils/rpc"
rprotoutils "go.viam.com/rdk/protoutils"
"go.viam.com/rdk/referenceframe"
"go.viam.com/rdk/resource"
)
// client implements GripperServiceClient.
type client struct {
resource.Named
resource.TriviallyReconfigurable
resource.TriviallyCloseable
name string
client pb.GripperServiceClient
logger golog.Logger
}
// NewClientFromConn constructs a new Client from connection passed in.
func NewClientFromConn(
ctx context.Context,
conn rpc.ClientConn,
remoteName string,
name resource.Name,
logger golog.Logger,
) (Gripper, error) {
c := pb.NewGripperServiceClient(conn)
return &client{
Named: name.PrependRemote(remoteName).AsNamed(),
name: name.ShortName(),
client: c,
logger: logger,
}, nil
}
func (c *client) Open(ctx context.Context, extra map[string]interface{}) error {
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return err
}
_, err = c.client.Open(ctx, &pb.OpenRequest{
Name: c.name,
Extra: ext,
})
return err
}
func (c *client) Grab(ctx context.Context, extra map[string]interface{}) (bool, error) {
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return false, err
}
resp, err := c.client.Grab(ctx, &pb.GrabRequest{
Name: c.name,
Extra: ext,
})
if err != nil {
return false, err
}
return resp.Success, nil
}
func (c *client) Stop(ctx context.Context, extra map[string]interface{}) error {
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return err
}
_, err = c.client.Stop(ctx, &pb.StopRequest{
Name: c.name,
Extra: ext,
})
return err
}
func (c *client) ModelFrame() referenceframe.Model {
// TODO(erh): this feels wrong
return nil
}
func (c *client) DoCommand(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error) {
return rprotoutils.DoFromResourceClient(ctx, c.client, c.name, cmd)
}
func (c *client) IsMoving(ctx context.Context) (bool, error) {
resp, err := c.client.IsMoving(ctx, &pb.IsMovingRequest{Name: c.name})
if err != nil {
return false, err
}
return resp.IsMoving, nil
}