Skip to content

Commit

Permalink
backend: update proto and add camera
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasVautherin committed May 24, 2018
1 parent 1ae7eb1 commit e2184d9
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 11 deletions.
2 changes: 1 addition & 1 deletion backend/proto
Submodule proto updated from 72834b to f2e8b2
3 changes: 2 additions & 1 deletion backend/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.1)

set(COMPONENTS_LIST core action mission telemetry)
set(COMPONENTS_LIST core action camera mission telemetry)

include(cmake/compile_proto.cmake)

Expand Down Expand Up @@ -37,6 +37,7 @@ endif()
target_link_libraries(backend
dronecore
dronecore_action
dronecore_camera
dronecore_mission
dronecore_telemetry
gRPC::grpc++
Expand Down
1 change: 1 addition & 0 deletions backend/src/grpc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ void GRPCServer::run()

builder.RegisterService(&_core);
builder.RegisterService(&_action_service);
builder.RegisterService(&_camera_service);
builder.RegisterService(&_mission_service);
builder.RegisterService(&_telemetry_service);

Expand Down
6 changes: 6 additions & 0 deletions backend/src/grpc_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "action/action.h"
#include "action/action_service_impl.h"
#include "camera/camera.h"
#include "camera/camera_service_impl.h"
#include "core/core_service_impl.h"
#include "dronecore.h"
#include "mission/mission.h"
Expand All @@ -22,6 +24,8 @@ class GRPCServer
_core(_dc),
_action(_dc.system()),
_action_service(_action),
_camera(_dc.system()),
_camera_service(_camera),
_mission(_dc.system()),
_mission_service(_mission),
_telemetry(_dc.system()),
Expand All @@ -41,6 +45,8 @@ class GRPCServer
CoreServiceImpl<> _core;
Action _action;
ActionServiceImpl<> _action_service;
Camera _camera;
CameraServiceImpl<> _camera_service;
Mission _mission;
MissionServiceImpl<> _mission_service;
Telemetry _telemetry;
Expand Down
6 changes: 3 additions & 3 deletions backend/src/plugins/action/action_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ class ActionServiceImpl final : public rpc::action::ActionService::Service
return grpc::Status::OK;
}

grpc::Status TransitionToFixedWings(grpc::ServerContext * /* context */,
const rpc::action::TransitionToFixedWingsRequest * /* request */,
rpc::action::TransitionToFixedWingsResponse *response) override
grpc::Status TransitionToFixedWing(grpc::ServerContext * /* context */,
const rpc::action::TransitionToFixedWingRequest * /* request */,
rpc::action::TransitionToFixedWingResponse *response) override
{
auto action_result = _action.transition_to_fixedwing();

Expand Down
44 changes: 44 additions & 0 deletions backend/src/plugins/camera/camera_service_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "camera/camera.h"
#include "camera/camera.grpc.pb.h"

namespace dronecore {
namespace backend {

template <typename Camera = Camera>
class CameraServiceImpl final : public rpc::camera::CameraService::Service
{
public:
CameraServiceImpl(Camera &camera)
: _camera(camera) {}

grpc::Status TakePhoto(grpc::ServerContext * /* context */,
const rpc::camera::TakePhotoRequest * /* request */,
rpc::camera::TakePhotoResponse *response) override
{
auto camera_result = _camera.take_photo();

if (response != nullptr) {
fillResponseWithResult(response, camera_result);
}

return grpc::Status::OK;
}

template <typename ResponseType>
void fillResponseWithResult(ResponseType *response, dronecore::Camera::Result &camera_result) const
{
auto rpc_result = static_cast<rpc::camera::CameraResult::Result>(camera_result);

auto *rpc_camera_result = new rpc::camera::CameraResult();
rpc_camera_result->set_result(rpc_result);
rpc_camera_result->set_result_str(dronecore::Camera::result_str(camera_result));

response->set_allocated_camera_result(rpc_camera_result);
}

private:
Camera &_camera;
};

} // namespace backend
} // namespace dronecore
1 change: 1 addition & 0 deletions backend/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.1)
add_executable(unit_tests_backend
action_service_impl_test.cpp
backend_main.cpp
camera_service_impl_test.cpp
connection_initiator_test.cpp
core_service_impl_test.cpp
mission_service_impl_test.cpp
Expand Down
6 changes: 3 additions & 3 deletions backend/test/action_service_impl_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,9 @@ std::string transitionToFWAndGetTranslatedResult(const dronecore::ActionResult
ON_CALL(action, transition_to_fixedwing())
.WillByDefault(Return(transition_to_fw_result));
ActionServiceImpl actionService(action);
dronecore::rpc::action::TransitionToFixedWingsResponse response;
dronecore::rpc::action::TransitionToFixedWingResponse response;

actionService.TransitionToFixedWings(nullptr, nullptr, &response);
actionService.TransitionToFixedWing(nullptr, nullptr, &response);

return ActionResult::Result_Name(response.action_result().result());
}
Expand All @@ -236,7 +236,7 @@ TEST_F(ActionServiceImplTest, transitions2fwEvenWhenArgsAreNull)
EXPECT_CALL(action, transition_to_fixedwing())
.Times(1);

actionService.TransitionToFixedWings(nullptr, nullptr, nullptr);
actionService.TransitionToFixedWing(nullptr, nullptr, nullptr);
}

TEST_P(ActionServiceImplTest, transition2mcResultIsTranslatedCorrectly)
Expand Down
70 changes: 70 additions & 0 deletions backend/test/camera_service_impl_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <gmock/gmock.h>

#include "camera/camera_service_impl.h"
#include "camera/mocks/camera_mock.h"

namespace {

using testing::NiceMock;
using testing::Return;

using MockCamera = NiceMock<dronecore::testing::MockCamera>;
using CameraServiceImpl = dronecore::backend::CameraServiceImpl<MockCamera>;

using CameraResult = dronecore::rpc::camera::CameraResult;
using InputPair = std::pair<std::string, dronecore::Camera::Result>;

std::vector<InputPair> generateInputPairs();

class CameraServiceImplTest : public ::testing::TestWithParam<InputPair>
{
protected:
CameraServiceImplTest()
: _camera_service(_camera) {}

MockCamera _camera;
CameraServiceImpl _camera_service;
};

TEST_P(CameraServiceImplTest, takePhotoResultIsTranslatedCorrectly)
{
ON_CALL(_camera, take_photo())
.WillByDefault(Return(GetParam().second));
dronecore::rpc::camera::TakePhotoResponse response;

_camera_service.TakePhoto(nullptr, nullptr, &response);

EXPECT_EQ(GetParam().first, CameraResult::Result_Name(response.camera_result().result()));
}

TEST_F(CameraServiceImplTest, takesPhotoEvenWhenArgsAreNull)
{
EXPECT_CALL(_camera, take_photo())
.Times(1);

_camera_service.TakePhoto(nullptr, nullptr, nullptr);
}

INSTANTIATE_TEST_CASE_P(CameraResultCorrespondences,
CameraServiceImplTest,
::testing::ValuesIn(generateInputPairs()));

std::vector<InputPair> generateInputPairs()
{
std::vector<InputPair> input_pairs;
input_pairs.push_back(std::make_pair("SUCCESS", dronecore::Camera::Result::SUCCESS));
input_pairs.push_back(std::make_pair("IN_PROGRESS",
dronecore::Camera::Result::IN_PROGRESS));
input_pairs.push_back(std::make_pair("BUSY", dronecore::Camera::Result::BUSY));
input_pairs.push_back(std::make_pair("DENIED", dronecore::Camera::Result::DENIED));
input_pairs.push_back(std::make_pair("ERROR",
dronecore::Camera::Result::ERROR));
input_pairs.push_back(std::make_pair("TIMEOUT", dronecore::Camera::Result::TIMEOUT));
input_pairs.push_back(std::make_pair("WRONG_ARGUMENT",
dronecore::Camera::Result::WRONG_ARGUMENT));
input_pairs.push_back(std::make_pair("UNKNOWN", dronecore::Camera::Result::UNKNOWN));

return input_pairs;
}

} // namespace
6 changes: 3 additions & 3 deletions plugins/camera/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ class Camera : public PluginBase
* @brief Possible results returned for camera commands.
*/
enum class Result {
SUCCESS = 0, /**< @brief Camera command executed successfully. */
UNKNOWN, /**< @brief The result is unknown. */
SUCCESS, /**< @brief Camera command executed successfully. */
IN_PROGRESS, /**< @brief Camera command is in progress. */
BUSY, /**< @brief Camera is busy and rejected command. */
DENIED, /**< @brief Camera has denied the command. */
ERROR, /**< @brief An error has occurred while executing the command. */
TIMEOUT, /**< @brief Camera has not responded in time and the command has timed out. */
WRONG_ARGUMENT, /**< @brief The command has wrong arguments. */
UNKNOWN /**< @brief The result is unknown. */
WRONG_ARGUMENT /**< @brief The command has wrong arguments. */
};

/**
Expand Down
15 changes: 15 additions & 0 deletions plugins/camera/mocks/camera_mock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <gmock/gmock.h>

#include "camera/camera.h"

namespace dronecore {
namespace testing {

class MockCamera
{
public:
MOCK_CONST_METHOD0(take_photo, Camera::Result());
};

} // namespace testing
} // namespace dronecore

0 comments on commit e2184d9

Please sign in to comment.