16#ifndef EASYFLEET_MISSION_MANAGER__RUN_CAPABILITY_HPP_
17#define EASYFLEET_MISSION_MANAGER__RUN_CAPABILITY_HPP_
25#include "easyfleet_core/capability_client.hpp"
27#include "easyfleet_mission_manager/ansi.hpp"
28#include "easyfleet_mission_manager/output.hpp"
30namespace easyfleet_mission_manager
33inline std::string outcome_to_string(uint8_t outcome_value)
36 switch (outcome_value) {
37 case 0:
return "SUCCEEDED";
38 case 1:
return "ABORTED";
39 case 2:
return "CANCELED";
40 case 3:
return "REJECTED";
41 case 4:
return "TIMEOUT";
42 case 5:
return "SERVER_UNAVAILABLE";
43 default:
return "UNKNOWN";
52template<
typename ActionT>
53typename easyfleet_core::CapabilityClient<ActionT>::Response run_capability(
55 const std::string & label,
56 const typename ActionT::Goal & goal,
57 std::chrono::seconds timeout,
58 std::function<
void(
const typename ActionT::Feedback &)> on_feedback)
60 using Client = easyfleet_core::CapabilityClient<ActionT>;
61 using Feedback =
typename ActionT::Feedback;
63 auto response_promise = std::make_shared<std::promise<typename Client::Response>>();
64 auto response_future = response_promise->get_future();
67 std::ostringstream out;
68 out << ansi::bold << ansi::cyan <<
"[" << label <<
"] " << ansi::reset
69 <<
"sending goal (will run for up to " << timeout.count() <<
"s)...";
70 safe_print(out.str());
75 [response_promise](
const typename Client::Response & response) {
76 response_promise->set_value(response);
78 [on_feedback, label](std::shared_ptr<const Feedback> feedback) {
80 on_feedback(*feedback);
84 typename Client::Response response;
85 if (response_future.wait_for(timeout) == std::future_status::timeout) {
86 std::ostringstream out;
87 out << ansi::yellow <<
"[" << label <<
"] " << ansi::reset
88 << timeout.count() <<
"s elapsed, stopping it...";
89 safe_print(out.str());
91 response = response_future.get();
93 response = response_future.get();
96 std::ostringstream out;
97 out << ansi::bold <<
"[" << label <<
"] " << ansi::reset
98 <<
"finished with outcome " << ansi::magenta
99 << outcome_to_string(
static_cast<uint8_t
>(response.outcome)) << ansi::reset;
100 safe_print(out.str());
void cancel()
Asks the capability to stop whatever it is currently doing (e.g.
Definition capability_client_impl.hpp:88
std::shared_ptr< CapabilityClient< ActionT > > SharedPtr
Shared pointer to a CapabilityClient<ActionT>.
Definition capability_client.hpp:59
void request(const Goal &goal, ResponseCallback on_response, FeedbackCallback on_feedback=nullptr)
Asks the capability to do something.
Definition capability_client_impl.hpp:62