Common
common packages for kyubic
 
Loading...
Searching...
No Matches
topic_status_check_base.hpp
Go to the documentation of this file.
1
10#ifndef _TOPIC_STATUS_CHECK_BASE_HPP
11#define _TOPIC_STATUS_CHECK_BASE_HPP
12
13#include <rclcpp/rclcpp.hpp>
14#include <rclcpp/wait_for_message.hpp>
15#include <string>
17
19{
20
25template <typename MessageT>
27{
28protected:
33 void set_status_id(const std::string id) { status_id_ = id; }
34
35 void set_config(const std::string topic_name, const uint32_t timeout_ms)
36 {
37 topic_name_ = topic_name;
38 timeout_ms_ = timeout_ms;
39 };
40
41 virtual void prepare_check(rclcpp::Node::SharedPtr node) override = 0;
42
48 virtual bool validate([[maybe_unused]] const MessageT & msg) { return true; }
49
50private:
51 std::string topic_name_;
52 uint32_t timeout_ms_ = 0;
53 std::string status_msg_;
54
55 std::string status_id_ = "";
56 std::string get_unique_id() const override
57 {
58 if (status_id_.empty()) {
59 return "";
60 }
61 return "TopicStatusCheckBase::" + topic_name_ + "::" + status_id_;
62 }
63
64 bool check_impl(rclcpp::Node::SharedPtr node) override
65 {
66 if (topic_name_.empty() || timeout_ms_ == 0) {
67 status_msg_ = "Not set topic_name or timeout. Please invoke `set_config()` function.";
68 return false; // FAIL (no received)
69 }
70
71 MessageT msg;
72
73 // Wait message received
74 bool received = rclcpp::wait_for_message<MessageT>(
75 msg, node, topic_name_,
76 std::chrono::duration_cast<std::chrono::nanoseconds>(
77 std::chrono::duration<double>(timeout_ms_ * 1e-3)));
78
79 if (received) {
80 if (validate(msg)) {
81 status_msg_ = "Received & Validated";
82 return true; // PASS
83 } else {
84 status_msg_ = "Received but invalid content";
85 return false; // FAIL (not validate)
86 }
87 } else {
88 status_msg_ = "Timeout";
89 return false; // FAIL (no received)
90 }
91 }
92
93 std::string report_impl([[maybe_unused]] rclcpp::Node::SharedPtr node) override
94 {
95 return "Topic: " + topic_name_ + " | " + status_msg_;
96 }
97};
98
99} // namespace system_health_check::base
100
101#endif
Abstract base class for all system check plugins.
Definition: system_check_base.hpp:24
Template class for checking topic reception.
Definition: topic_status_check_base.hpp:27
void set_config(const std::string topic_name, const uint32_t timeout_ms)
Definition: topic_status_check_base.hpp:35
void set_status_id(const std::string id)
Set the status id for cache check.
Definition: topic_status_check_base.hpp:33
virtual bool validate(const MessageT &msg)
Virtual function to verify message content. Defaults to "OK if received". Override in a derived class...
Definition: topic_status_check_base.hpp:48
virtual void prepare_check(rclcpp::Node::SharedPtr node) override=0
Prepares the check before execution.
Definition: action_server_check_base.hpp:25
Abstruct Class of check process.