|
| 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 |
0 commit comments