-
Notifications
You must be signed in to change notification settings - Fork 110
/
client.go
116 lines (95 loc) · 3.31 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
package slam
import (
"context"
"github.com/pkg/errors"
"go.opencensus.io/trace"
pb "go.viam.com/api/service/slam/v1"
"go.viam.com/utils/rpc"
"go.viam.com/rdk/logging"
rprotoutils "go.viam.com/rdk/protoutils"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/services/slam/grpchelper"
"go.viam.com/rdk/spatialmath"
)
// client implements SLAMServiceClient.
type client struct {
resource.Named
resource.TriviallyReconfigurable
resource.TriviallyCloseable
name string
client pb.SLAMServiceClient
logger logging.Logger
}
// NewClientFromConn constructs a new Client from the connection passed in.
func NewClientFromConn(
ctx context.Context,
conn rpc.ClientConn,
remoteName string,
name resource.Name,
logger logging.Logger,
) (Service, error) {
grpcClient := pb.NewSLAMServiceClient(conn)
c := &client{
Named: name.PrependRemote(remoteName).AsNamed(),
name: name.ShortName(),
client: grpcClient,
logger: logger,
}
return c, nil
}
// Position creates a request, calls the slam service Position, and parses the response into a Pose with a component reference string.
func (c *client) Position(ctx context.Context) (spatialmath.Pose, string, error) {
ctx, span := trace.StartSpan(ctx, "slam::client::Position")
defer span.End()
req := &pb.GetPositionRequest{
Name: c.name,
}
resp, err := c.client.GetPosition(ctx, req)
if err != nil {
return nil, "", err
}
p := resp.GetPose()
componentReference := resp.GetComponentReference()
return spatialmath.NewPoseFromProtobuf(p), componentReference, nil
}
// PointCloudMap creates a request, calls the slam service PointCloudMap and returns a callback
// function which will return the next chunk of the current pointcloud map when called.
func (c *client) PointCloudMap(ctx context.Context) (func() ([]byte, error), error) {
ctx, span := trace.StartSpan(ctx, "slam::client::PointCloudMap")
defer span.End()
return grpchelper.PointCloudMapCallback(ctx, c.name, c.client)
}
// InternalState creates a request, calls the slam service InternalState and returns a callback
// function which will return the next chunk of the current internal state of the slam algo when called.
func (c *client) InternalState(ctx context.Context) (func() ([]byte, error), error) {
ctx, span := trace.StartSpan(ctx, "slam::client::InternalState")
defer span.End()
return grpchelper.InternalStateCallback(ctx, c.name, c.client)
}
// Properties returns information regarding the current SLAM session, including
// if the session is running in the cloud and what mapping mode it is in.
func (c *client) Properties(ctx context.Context) (Properties, error) {
ctx, span := trace.StartSpan(ctx, "slam::client::GetProperties")
defer span.End()
req := &pb.GetPropertiesRequest{
Name: c.name,
}
resp, err := c.client.GetProperties(ctx, req)
if err != nil {
return Properties{}, errors.Wrapf(err, "failure to get properties")
}
mappingMode, err := protobufToMappingMode(resp.MappingMode)
if err != nil {
return Properties{}, err
}
prop := Properties{
CloudSlam: resp.CloudSlam,
MappingMode: mappingMode,
}
return prop, err
}
func (c *client) DoCommand(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error) {
ctx, span := trace.StartSpan(ctx, "slam::client::DoCommand")
defer span.End()
return rprotoutils.DoFromResourceClient(ctx, c.client, c.name, cmd)
}