15 #ifndef CONCEALER__SERVICE_WITH_VALIDATION_HPP_
16 #define CONCEALER__SERVICE_WITH_VALIDATION_HPP_
18 #include <autoware_adapi_v1_msgs/msg/response_status.hpp>
21 #include <rclcpp/rclcpp.hpp>
23 #include <tier4_external_api_msgs/msg/response_status.hpp>
24 #include <tier4_rtc_msgs/srv/auto_mode_with_module.hpp>
25 #include <type_traits>
27 template <
typename T,
typename =
void>
34 :
public std::true_type
41 template <
typename T,
typename =
void>
48 :
public std::true_type
63 const std::chrono::nanoseconds validation_interval = std::chrono::seconds(1))
64 : service_name(service_name),
65 logger(autoware.get_logger()),
66 client(autoware.create_client<T>(service_name, rmw_qos_profile_default)),
67 validation_rate(validation_interval)
74 template <
typename... Ts>
80 auto operator()(
const typename T::Request::SharedPtr & request, std::size_t attempts_count = 1)
83 validateAvailability();
84 for (std::size_t attempt = 0; attempt < attempts_count; ++attempt, validation_rate.sleep()) {
85 if (
const auto & service_call_result = callWithTimeoutValidation(request)) {
86 if constexpr (has_data_member_status_v<typename T::Response>) {
87 if constexpr (std::is_same_v<
88 tier4_external_api_msgs::msg::ResponseStatus,
89 decltype(T::Response::status)>) {
90 if (
const auto & service_call_status = service_call_result->get()->status;
93 logger, service_name <<
" service request has been accepted"
94 << (service_call_status.message.empty()
96 :
" (" + service_call_status.message +
")."));
101 <<
" service request was accepted, but ResponseStatus is FAILURE"
102 << (service_call_status.message.empty()
104 :
" (" + service_call_status.message +
")"));
106 }
else if constexpr (std::is_same_v<
107 autoware_adapi_v1_msgs::msg::ResponseStatus,
108 decltype(T::Response::status)>) {
109 if (
const auto & service_call_status = service_call_result->get()->status;
110 service_call_status.success) {
112 logger, service_name <<
" service request has been accepted"
113 << (service_call_status.message.empty()
115 :
" (" + service_call_status.message +
")."));
120 <<
" service request was accepted, but ResponseStatus::success is false "
121 << (service_call_status.message.empty()
123 :
" (" + service_call_status.message +
")"));
126 RCLCPP_INFO_STREAM(logger, service_name <<
" service request has been accepted.");
129 }
else if constexpr (has_data_member_success_v<typename T::Response>) {
130 if constexpr (std::is_same_v<
bool, decltype(T::Response::success)>) {
131 if (service_call_result->get()->success) {
132 RCLCPP_INFO_STREAM(logger, service_name <<
" service request has been accepted.");
137 <<
" service request has been accepted, but Response::success is false.");
140 RCLCPP_INFO_STREAM(logger, service_name <<
" service request has been accepted.");
144 RCLCPP_INFO_STREAM(logger, service_name <<
" service request has been accepted.");
150 "Requested the service ", std::quoted(service_name),
" ", attempts_count,
151 " times, but was not successful.");
155 auto validateAvailability() ->
void
157 while (!client->service_is_ready()) {
158 RCLCPP_INFO_STREAM(logger, service_name <<
" service is not ready.");
159 validation_rate.sleep();
163 auto callWithTimeoutValidation(
const typename T::Request::SharedPtr & request)
164 -> std::optional<typename rclcpp::Client<T>::SharedFuture>
166 if (
auto future = client->async_send_request(request);
167 future.wait_for(validation_rate.period()) == std::future_status::ready) {
170 RCLCPP_ERROR_STREAM(logger, service_name <<
" service request has timed out.");
177 rclcpp::Logger logger;
179 typename rclcpp::Client<T>::SharedPtr client;
181 rclcpp::WallRate validation_rate;
Definition: field_operator_application.hpp:60
Definition: service_with_validation.hpp:72
TimeoutError(Ts &&... xs)
Definition: service_with_validation.hpp:75
Definition: service_with_validation.hpp:59
ServiceWithValidation(const std::string &service_name, FieldOperatorApplication &autoware, const std::chrono::nanoseconds validation_interval=std::chrono::seconds(1))
Definition: service_with_validation.hpp:61
auto operator()(const typename T::Request::SharedPtr &request, std::size_t attempts_count=1) -> void
Definition: service_with_validation.hpp:80
Definition: concatenate.hpp:24
Definition: autoware.hpp:30
void void_t
Definition: void_t.hpp:25
constexpr int SUCCESS
Definition: constants.hpp:22
Definition: junit5.hpp:25
std::string string
Definition: junit5.hpp:26
constexpr auto has_data_member_status_v
Definition: service_with_validation.hpp:39
constexpr auto has_data_member_success_v
Definition: service_with_validation.hpp:53
Definition: exception.hpp:27
Definition: service_with_validation.hpp:29
Definition: service_with_validation.hpp:43