Skip to content

Commit ca2a5c9

Browse files
authored
RSDK-9842 - add get_machine_status implementation (#366)
1 parent 0373dd8 commit ca2a5c9

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

src/viam/sdk/robot/client.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,5 +355,43 @@ void RobotClient::stop_all(const std::unordered_map<Name, ProtoStruct>& extra) {
355355
}
356356
}
357357

358+
std::ostream& operator<<(std::ostream& os, const RobotClient::status& v) {
359+
std::string status;
360+
switch (v) {
361+
case RobotClient::status::k_running:
362+
status = "running";
363+
break;
364+
case RobotClient::status::k_initializing:
365+
status = "initializing";
366+
break;
367+
case RobotClient::status::k_unspecified:
368+
default:
369+
status = "unspecified";
370+
}
371+
os << status;
372+
return os;
373+
}
374+
375+
RobotClient::status RobotClient::get_machine_status() const {
376+
const robot::v1::GetMachineStatusRequest req;
377+
robot::v1::GetMachineStatusResponse resp;
378+
ClientContext ctx;
379+
380+
const grpc::Status response = impl_->stub_->GetMachineStatus(ctx, req, &resp);
381+
if (is_error_response(response)) {
382+
BOOST_LOG_TRIVIAL(error) << "Error getting machine status: " << response.error_message()
383+
<< response.error_details();
384+
}
385+
switch (resp.state()) {
386+
case robot::v1::GetMachineStatusResponse_State_STATE_INITIALIZING:
387+
return RobotClient::status::k_initializing;
388+
case robot::v1::GetMachineStatusResponse_State_STATE_RUNNING:
389+
return RobotClient::status::k_running;
390+
case robot::v1::GetMachineStatusResponse_State_STATE_UNSPECIFIED:
391+
default:
392+
return RobotClient::status::k_unspecified;
393+
}
394+
}
395+
358396
} // namespace sdk
359397
} // namespace viam

src/viam/sdk/robot/client.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ namespace sdk {
3434
/// `with_channel` require a user call to `close()`.
3535
class RobotClient {
3636
public:
37+
/// @enum status
38+
/// @brief the current status of the robot
39+
/// @ingroup Robot
40+
enum class status : uint8_t {
41+
k_initializing,
42+
k_running,
43+
k_unspecified,
44+
};
45+
46+
friend std::ostream& operator<<(std::ostream& os, const status& v);
47+
3748
struct frame_system_config {
3849
WorldState::transform frame;
3950
ProtoStruct kinematics;
@@ -132,6 +143,9 @@ class RobotClient {
132143
/// @param id The ID of the operation to cancel.
133144
void cancel_operation(std::string id);
134145

146+
/// @brief gets the current status of the machine
147+
status get_machine_status() const;
148+
135149
private:
136150
std::vector<std::shared_ptr<std::thread>> threads_;
137151
std::atomic<bool> should_refresh_;

src/viam/sdk/tests/mocks/mock_robot.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,21 @@ ::grpc::Status MockRobotService::TransformPose(::grpc::ServerContext* context,
322322
return ::grpc::Status();
323323
}
324324

325+
::grpc::Status MockRobotService::GetMachineStatus(
326+
::grpc::ServerContext* context,
327+
const ::viam::robot::v1::GetMachineStatusRequest*,
328+
::viam::robot::v1::GetMachineStatusResponse* response) {
329+
auto client_md = context->client_metadata();
330+
if (auto client_info = client_md.find("viam_client"); client_info == client_md.end()) {
331+
return ::grpc::Status(::grpc::StatusCode::FAILED_PRECONDITION,
332+
"viam_client info not properly set in metadata");
333+
}
334+
335+
response->set_state(::viam::robot::v1::GetMachineStatusResponse_State_STATE_RUNNING);
336+
337+
return ::grpc::Status();
338+
}
339+
325340
::grpc::Status MockRobotService::GetOperations(::grpc::ServerContext* context,
326341
const ::viam::robot::v1::GetOperationsRequest*,
327342
::viam::robot::v1::GetOperationsResponse* response) {

src/viam/sdk/tests/mocks/mock_robot.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ class MockRobotService : public ResourceServer, public viam::robot::v1::RobotSer
4646
const ::viam::robot::v1::TransformPoseRequest* request,
4747
::viam::robot::v1::TransformPoseResponse* response) override;
4848

49+
::grpc::Status GetMachineStatus(::grpc::ServerContext* context,
50+
const ::viam::robot::v1::GetMachineStatusRequest* request,
51+
::viam::robot::v1::GetMachineStatusResponse* response) override;
52+
4953
::grpc::Status GetOperations(::grpc::ServerContext* context,
5054
const ::viam::robot::v1::GetOperationsRequest* request,
5155
::viam::robot::v1::GetOperationsResponse* response) override;

src/viam/sdk/tests/test_robot.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,15 @@ BOOST_AUTO_TEST_CASE(test_transform_pose) {
190190
});
191191
}
192192

193+
BOOST_AUTO_TEST_CASE(test_get_machine_status) {
194+
robot_client_to_mocks_pipeline(
195+
[](std::shared_ptr<RobotClient> client, MockRobotService& service) -> void {
196+
auto status = client->get_machine_status();
197+
198+
BOOST_CHECK_EQUAL(status, RobotClient::status::k_running);
199+
});
200+
}
201+
193202
BOOST_AUTO_TEST_CASE(test_stop_all) {
194203
robot_client_to_mocks_pipeline(
195204
[](std::shared_ptr<RobotClient> client, MockRobotService& service) -> void {

0 commit comments

Comments
 (0)