Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/viam/sdk/robot/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,5 +355,43 @@ void RobotClient::stop_all(const std::unordered_map<Name, ProtoStruct>& extra) {
}
}

std::ostream& operator<<(std::ostream& os, const RobotClient::status& v) {
std::string status;
switch (v) {
case RobotClient::status::k_running:
status = "running";
break;
case RobotClient::status::k_initializing:
status = "initializing";
break;
case RobotClient::status::k_unspecified:
default:
status = "unspecified";
}
os << status;
return os;
}

RobotClient::status RobotClient::get_machine_status() const {
const robot::v1::GetMachineStatusRequest req;
robot::v1::GetMachineStatusResponse resp;
ClientContext ctx;

const grpc::Status response = impl_->stub_->GetMachineStatus(ctx, req, &resp);
if (is_error_response(response)) {
BOOST_LOG_TRIVIAL(error) << "Error getting machine status: " << response.error_message()
<< response.error_details();
}
switch (resp.state()) {
case robot::v1::GetMachineStatusResponse_State_STATE_INITIALIZING:
return RobotClient::status::k_initializing;
case robot::v1::GetMachineStatusResponse_State_STATE_RUNNING:
return RobotClient::status::k_running;
case robot::v1::GetMachineStatusResponse_State_STATE_UNSPECIFIED:
default:
return RobotClient::status::k_unspecified;
}
}

} // namespace sdk
} // namespace viam
14 changes: 14 additions & 0 deletions src/viam/sdk/robot/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ namespace sdk {
/// `with_channel` require a user call to `close()`.
class RobotClient {
public:
/// @enum status
/// @brief the current status of the robot
/// @ingroup Robot
enum class status : uint8_t {
k_initializing,
k_running,
k_unspecified,
};

friend std::ostream& operator<<(std::ostream& os, const status& v);

struct frame_system_config {
WorldState::transform frame;
ProtoStruct kinematics;
Expand Down Expand Up @@ -132,6 +143,9 @@ class RobotClient {
/// @param id The ID of the operation to cancel.
void cancel_operation(std::string id);

/// @brief gets the current status of the machine
status get_machine_status() const;

private:
std::vector<std::shared_ptr<std::thread>> threads_;
std::atomic<bool> should_refresh_;
Expand Down
15 changes: 15 additions & 0 deletions src/viam/sdk/tests/mocks/mock_robot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,21 @@ ::grpc::Status MockRobotService::TransformPose(::grpc::ServerContext* context,
return ::grpc::Status();
}

::grpc::Status MockRobotService::GetMachineStatus(
::grpc::ServerContext* context,
const ::viam::robot::v1::GetMachineStatusRequest*,
::viam::robot::v1::GetMachineStatusResponse* response) {
auto client_md = context->client_metadata();
if (auto client_info = client_md.find("viam_client"); client_info == client_md.end()) {
return ::grpc::Status(::grpc::StatusCode::FAILED_PRECONDITION,
"viam_client info not properly set in metadata");
}

response->set_state(::viam::robot::v1::GetMachineStatusResponse_State_STATE_RUNNING);

return ::grpc::Status();
}

::grpc::Status MockRobotService::GetOperations(::grpc::ServerContext* context,
const ::viam::robot::v1::GetOperationsRequest*,
::viam::robot::v1::GetOperationsResponse* response) {
Expand Down
4 changes: 4 additions & 0 deletions src/viam/sdk/tests/mocks/mock_robot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class MockRobotService : public ResourceServer, public viam::robot::v1::RobotSer
const ::viam::robot::v1::TransformPoseRequest* request,
::viam::robot::v1::TransformPoseResponse* response) override;

::grpc::Status GetMachineStatus(::grpc::ServerContext* context,
const ::viam::robot::v1::GetMachineStatusRequest* request,
::viam::robot::v1::GetMachineStatusResponse* response) override;

::grpc::Status GetOperations(::grpc::ServerContext* context,
const ::viam::robot::v1::GetOperationsRequest* request,
::viam::robot::v1::GetOperationsResponse* response) override;
Expand Down
9 changes: 9 additions & 0 deletions src/viam/sdk/tests/test_robot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,15 @@ BOOST_AUTO_TEST_CASE(test_transform_pose) {
});
}

BOOST_AUTO_TEST_CASE(test_get_machine_status) {
robot_client_to_mocks_pipeline(
[](std::shared_ptr<RobotClient> client, MockRobotService& service) -> void {
auto status = client->get_machine_status();

BOOST_CHECK_EQUAL(status, RobotClient::status::k_running);
});
}

BOOST_AUTO_TEST_CASE(test_stop_all) {
robot_client_to_mocks_pipeline(
[](std::shared_ptr<RobotClient> client, MockRobotService& service) -> void {
Expand Down