-
Notifications
You must be signed in to change notification settings - Fork 110
/
client.go
166 lines (154 loc) · 4.09 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
package navigation
import (
"context"
"github.com/edaniels/golog"
geo "github.com/kellydunn/golang-geo"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson/primitive"
commonpb "go.viam.com/api/common/v1"
pb "go.viam.com/api/service/navigation/v1"
"go.viam.com/utils/protoutils"
"go.viam.com/utils/rpc"
rprotoutils "go.viam.com/rdk/protoutils"
"go.viam.com/rdk/resource"
)
// client implements NavigationServiceClient.
type client struct {
resource.Named
resource.TriviallyReconfigurable
resource.TriviallyCloseable
name string
client pb.NavigationServiceClient
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,
) (Service, error) {
grpcClient := pb.NewNavigationServiceClient(conn)
c := &client{
Named: name.PrependRemote(remoteName).AsNamed(),
name: name.ShortName(),
client: grpcClient,
logger: logger,
}
return c, nil
}
func (c *client) Mode(ctx context.Context, extra map[string]interface{}) (Mode, error) {
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return 0, err
}
resp, err := c.client.GetMode(ctx, &pb.GetModeRequest{Name: c.name, Extra: ext})
if err != nil {
return 0, err
}
pbMode := resp.GetMode()
switch pbMode {
case pb.Mode_MODE_MANUAL:
return ModeManual, nil
case pb.Mode_MODE_WAYPOINT:
return ModeWaypoint, nil
case pb.Mode_MODE_UNSPECIFIED:
fallthrough
default:
return 0, errors.New("mode error")
}
}
func (c *client) SetMode(ctx context.Context, mode Mode, extra map[string]interface{}) error {
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return err
}
var pbMode pb.Mode
switch mode {
case ModeManual:
pbMode = pb.Mode_MODE_MANUAL
case ModeWaypoint:
pbMode = pb.Mode_MODE_WAYPOINT
default:
pbMode = pb.Mode_MODE_UNSPECIFIED
}
_, err = c.client.SetMode(ctx, &pb.SetModeRequest{Name: c.name, Mode: pbMode, Extra: ext})
if err != nil {
return err
}
return nil
}
func (c *client) Location(ctx context.Context, extra map[string]interface{}) (*geo.Point, error) {
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return nil, err
}
resp, err := c.client.GetLocation(ctx, &pb.GetLocationRequest{Name: c.name, Extra: ext})
if err != nil {
return nil, err
}
loc := resp.GetLocation()
result := geo.NewPoint(loc.GetLatitude(), loc.GetLongitude())
return result, nil
}
func (c *client) Waypoints(ctx context.Context, extra map[string]interface{}) ([]Waypoint, error) {
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return nil, err
}
resp, err := c.client.GetWaypoints(ctx, &pb.GetWaypointsRequest{Name: c.name, Extra: ext})
if err != nil {
return nil, err
}
waypoints := resp.GetWaypoints()
result := make([]Waypoint, 0, len(waypoints))
for _, wpt := range waypoints {
id, err := primitive.ObjectIDFromHex(wpt.GetId())
if err != nil {
return nil, err
}
loc := wpt.GetLocation()
result = append(result, Waypoint{
ID: id,
Lat: loc.GetLatitude(),
Long: loc.GetLongitude(),
})
}
return result, nil
}
func (c *client) AddWaypoint(ctx context.Context, point *geo.Point, extra map[string]interface{}) error {
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return err
}
loc := &commonpb.GeoPoint{
Latitude: point.Lat(),
Longitude: point.Lng(),
}
req := &pb.AddWaypointRequest{
Name: c.name,
Location: loc,
Extra: ext,
}
_, err = c.client.AddWaypoint(ctx, req)
if err != nil {
return err
}
return nil
}
func (c *client) RemoveWaypoint(ctx context.Context, id primitive.ObjectID, extra map[string]interface{}) error {
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return err
}
req := &pb.RemoveWaypointRequest{Name: c.name, Id: id.Hex(), Extra: ext}
_, err = c.client.RemoveWaypoint(ctx, req)
if err != nil {
return err
}
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)
}