Skip to content

Commit 0cb6b02

Browse files
committed
nav client compiles
1 parent 4783dd6 commit 0cb6b02

File tree

2 files changed

+200
-0
lines changed

2 files changed

+200
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#include <viam/sdk/services/private/navigation_client.hpp>
2+
3+
#include <math.h>
4+
5+
#include <grpcpp/support/status.h>
6+
7+
#include <viam/api/service/navigation/v1/navigation.grpc.pb.h>
8+
#include <viam/api/service/navigation/v1/navigation.pb.h>
9+
10+
#include <viam/sdk/common/client_helper.hpp>
11+
#include <viam/sdk/common/proto_value.hpp>
12+
#include <viam/sdk/common/utils.hpp>
13+
#include <viam/sdk/services/navigation.hpp>
14+
15+
namespace viam {
16+
namespace sdk {
17+
namespace impl {
18+
19+
using namespace viam::service::navigation::v1;
20+
21+
NavigationClient::NavigationClient(std::string name, std::shared_ptr<grpc::Channel> channel)
22+
: Navigation(std::move(name)),
23+
stub_(service::navigation::v1::NavigationService::NewStub(channel)),
24+
channel_(std::move(channel)) {};
25+
26+
Navigation::Mode NavigationClient::get_mode(const std::string name, const ProtoStruct& extra) {
27+
return make_client_helper(this, *stub_, &StubType::GetMode)
28+
.with([&](auto& request) {
29+
*request.mutable_name() = name;
30+
*request.mutable_extra() = map_to_struct(extra);
31+
})
32+
.invoke([](auto& response) { return Navigation::Mode(response.mode()); });
33+
}
34+
35+
void NavigationClient::set_mode(const std::string name,
36+
const Navigation::Mode mode,
37+
const ProtoStruct& extra) {
38+
return make_client_helper(this, *stub_, &StubType::SetMode)
39+
.with([&](auto& request) {
40+
*request.mutable_name() = name;
41+
request.set_mode(viam::service::navigation::v1::Mode(mode));
42+
*request.mutable_extra() = map_to_struct(extra);
43+
})
44+
.invoke([](auto& response) {});
45+
}
46+
47+
Navigation::LocationResponse NavigationClient::get_location(const std::string name,
48+
const ProtoStruct& extra) {
49+
return make_client_helper(this, *stub_, &StubType::GetLocation)
50+
.with([&](auto& request) {
51+
*request.mutable_name() = name;
52+
*request.mutable_extra() = map_to_struct(extra);
53+
})
54+
.invoke([](auto& response) {
55+
return Navigation::LocationResponse{
56+
response.location(),
57+
response.compass_heading(),
58+
};
59+
});
60+
}
61+
62+
// helper; unpacks a repeated pointer to a vector of destination type.
63+
// Dst must have a constructor that takes Src*.
64+
template <typename Src, typename Dst>
65+
std::unique_ptr<std::vector<Dst>> repeatedPtrToVector(
66+
const ::google::protobuf::RepeatedPtrField<Src>& items) {
67+
auto vec = std::make_unique<std::vector<Dst>>();
68+
vec->reserve(items.size());
69+
for (auto& x : items) {
70+
vec->push_back(x);
71+
}
72+
return vec;
73+
}
74+
75+
std::unique_ptr<std::vector<Navigation::Waypoint>> NavigationClient::get_waypoints(
76+
const std::string name, const ProtoStruct& extra) {
77+
return make_client_helper(this, *stub_, &StubType::GetWaypoints)
78+
.with([&](auto& request) {
79+
*request.mutable_name() = name;
80+
*request.mutable_extra() = map_to_struct(extra);
81+
})
82+
.invoke([](auto& response) {
83+
return repeatedPtrToVector<viam::service::navigation::v1::Waypoint,
84+
Navigation::Waypoint>(response.waypoints());
85+
});
86+
}
87+
88+
void NavigationClient::add_waypoint(const std::string name,
89+
const GeoPoint& location,
90+
const ProtoStruct& extra) {
91+
return make_client_helper(this, *stub_, &StubType::AddWaypoint)
92+
.with([&](auto& request) {
93+
*request.mutable_name() = name;
94+
*request.mutable_location() = location;
95+
*request.mutable_extra() = map_to_struct(extra);
96+
})
97+
.invoke([](auto& response) {});
98+
}
99+
100+
void NavigationClient::remove_waypoint(const std::string name,
101+
const std::string id,
102+
const ProtoStruct& extra) {
103+
return make_client_helper(this, *stub_, &StubType::RemoveWaypoint)
104+
.with([&](auto& request) {
105+
*request.mutable_name() = name;
106+
*request.mutable_id() = id;
107+
*request.mutable_extra() = map_to_struct(extra);
108+
})
109+
.invoke([](auto& response) {});
110+
}
111+
112+
std::unique_ptr<std::vector<GeoGeometry>> NavigationClient::get_obstacles(
113+
const std::string name, const ProtoStruct& extra) {
114+
return make_client_helper(this, *stub_, &StubType::GetObstacles)
115+
.with([&](auto& request) {
116+
*request.mutable_name() = name;
117+
*request.mutable_extra() = map_to_struct(extra);
118+
})
119+
.invoke([](auto& response) {
120+
return std::make_unique<std::vector<GeoGeometry>>(response.obstacles().begin(),
121+
response.obstacles().end());
122+
});
123+
}
124+
125+
std::unique_ptr<std::vector<NavigationClient::Path>> NavigationClient::get_paths(
126+
const std::string name, const ProtoStruct& extra) {
127+
return make_client_helper(this, *stub_, &StubType::GetPaths)
128+
.with([&](auto& request) {
129+
*request.mutable_name() = name;
130+
*request.mutable_extra() = map_to_struct(extra);
131+
})
132+
.invoke([](auto& response) {
133+
return std::make_unique<std::vector<Path>>(response.paths().begin(),
134+
response.paths().end());
135+
});
136+
}
137+
138+
NavigationClient::MapType NavigationClient::get_properties(const std::string name) {
139+
return make_client_helper(this, *stub_, &StubType::GetProperties)
140+
.with([&](auto& request) { *request.mutable_name() = name; })
141+
.invoke([](auto& response) { return MapType(response.map_type()); });
142+
}
143+
144+
ProtoStruct NavigationClient::do_command(const ProtoStruct& command) {
145+
return make_client_helper(this, *stub_, &StubType::DoCommand)
146+
.with([&](auto& request) { *request.mutable_command() = map_to_struct(command); })
147+
.invoke([](auto& response) { return struct_to_map(response.result()); });
148+
}
149+
150+
} // namespace impl
151+
} // namespace sdk
152+
} // namespace viam
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/// @file services/navigation/client.hpp
2+
///
3+
/// @brief Implements a gRPC client for the `Navigation` service.
4+
#pragma once
5+
6+
#include <grpcpp/channel.h>
7+
8+
#include <viam/api/service/navigation/v1/navigation.grpc.pb.h>
9+
10+
#include <viam/sdk/services/navigation.hpp>
11+
12+
namespace viam {
13+
namespace sdk {
14+
namespace impl {
15+
16+
/// @class NavigationClient
17+
/// @brief gRPC client implementation of a `Navigation` service.
18+
/// @ingroup Navigation
19+
class NavigationClient : public Navigation {
20+
public:
21+
using interface_type = Navigation;
22+
NavigationClient(std::string name, std::shared_ptr<grpc::Channel> channel);
23+
24+
Mode get_mode(const std::string name, const ProtoStruct& extra) override;
25+
void set_mode(const std::string name, const Mode mode, const ProtoStruct& extra) override;
26+
LocationResponse get_location(const std::string name, const ProtoStruct& extra) override;
27+
std::unique_ptr<std::vector<Waypoint>> get_waypoints(const std::string name, const ProtoStruct& extra) override;
28+
void add_waypoint(const std::string name,
29+
const GeoPoint& location,
30+
const ProtoStruct& extra) override;
31+
void remove_waypoint(const std::string name,
32+
const std::string id,
33+
const ProtoStruct& extra) override;
34+
std::unique_ptr<std::vector<GeoGeometry>> get_obstacles(const std::string name,
35+
const ProtoStruct& extra) override;
36+
std::unique_ptr<std::vector<Path>> get_paths(const std::string name, const ProtoStruct& extra) override;
37+
MapType get_properties(const std::string name) override;
38+
ProtoStruct do_command(const ProtoStruct& command) override;
39+
40+
private:
41+
using StubType = service::navigation::v1::NavigationService::StubInterface;
42+
std::unique_ptr<StubType> stub_;
43+
std::shared_ptr<grpc::Channel> channel_;
44+
};
45+
46+
} // namespace impl
47+
} // namespace sdk
48+
} // namespace viam

0 commit comments

Comments
 (0)