Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dummy_diag_publisher): update param setting #9946

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
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,13 @@ class DummyDiagPublisher : public rclcpp::Node
DummyDiagConfig config_;

RequiredDiags required_diags_;
rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr param_callback_handle_;

void loadRequiredDiags();
rcl_interfaces::msg::SetParametersResult onSetParams(
const std::vector<rclcpp::Parameter> & parameters);

std::optional<Status> convertStrToStatus(std::string & status_str);
std::optional<Status> convertStrToStatus(const std::string & status_str);
std::string convertStatusToStr(const Status & status);
diagnostic_msgs::msg::DiagnosticStatus::_level_type convertStatusToLevel(const Status & status);

Expand Down
3 changes: 1 addition & 2 deletions system/dummy_diag_publisher/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
<buildtool_depend>ament_cmake_auto</buildtool_depend>
<buildtool_depend>autoware_cmake</buildtool_depend>

<depend>autoware_universe_utils</depend>
<depend>diagnostic_updater</depend>
<depend>diagnostic_msgs</depend>
<depend>fmt</depend>
<depend>rclcpp</depend>
<depend>rclcpp_components</depend>
Expand Down
63 changes: 62 additions & 1 deletion system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#define FMT_HEADER_ONLY
#include <fmt/format.h>

#include <sstream>

namespace
{
std::vector<std::string> split(const std::string & str, const char delim)
Expand All @@ -37,7 +39,7 @@
} // namespace

std::optional<DummyDiagPublisher::Status> DummyDiagPublisher::convertStrToStatus(
std::string & status_str)
const std::string & status_str)
{
static std::unordered_map<std::string, Status> const table = {
{"OK", Status::OK}, {"Warn", Status::WARN}, {"Error", Status::ERROR}, {"Stale", Status::STALE}};
Expand Down Expand Up @@ -144,6 +146,61 @@
true);
}

rcl_interfaces::msg::SetParametersResult DummyDiagPublisher::onSetParams(

Check warning on line 149 in system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp

View check run for this annotation

Codecov / codecov/patch

system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp#L149

Added line #L149 was not covered by tests
const std::vector<rclcpp::Parameter> & parameters)
{
rcl_interfaces::msg::SetParametersResult result;
result.successful = true;

Check warning on line 153 in system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp

View check run for this annotation

Codecov / codecov/patch

system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp#L152-L153

Added lines #L152 - L153 were not covered by tests

for (const auto & parameter : parameters) {
bool param_found = false;
const auto & param_name = parameter.get_name();

Check warning on line 157 in system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp

View check run for this annotation

Codecov / codecov/patch

system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp#L157

Added line #L157 was not covered by tests

for (auto & diag : required_diags_) {
if (param_name == diag.name + std::string(".status")) {
param_found = true;
auto new_status = convertStrToStatus(parameter.as_string());

Check warning on line 162 in system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp

View check run for this annotation

Codecov / codecov/patch

system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp#L162

Added line #L162 was not covered by tests
if (new_status) {
diag.status = *new_status;
RCLCPP_INFO(

Check warning on line 165 in system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp

View check run for this annotation

Codecov / codecov/patch

system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp#L164-L165

Added lines #L164 - L165 were not covered by tests
this->get_logger(), "Updated %s status to: %s", diag.name.c_str(),
parameter.as_string().c_str());
} else {
result.successful = false;
result.reason = "Invalid status value for: " + parameter.as_string();
RCLCPP_WARN(

Check warning on line 171 in system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp

View check run for this annotation

Codecov / codecov/patch

system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp#L169-L171

Added lines #L169 - L171 were not covered by tests
this->get_logger(), "Invalid status value for %s: %s", diag.name.c_str(),
parameter.as_string().c_str());
}
} else if (param_name == diag.name + std::string(".is_active")) {
param_found = true;
try {
diag.is_active = parameter.as_bool();
RCLCPP_INFO(

Check warning on line 179 in system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp

View check run for this annotation

Codecov / codecov/patch

system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp#L178-L179

Added lines #L178 - L179 were not covered by tests
this->get_logger(), "Updated %s is_active to: %s", diag.name.c_str(),
diag.is_active ? "true" : "false");
} catch (const rclcpp::ParameterTypeException & e) {
result.successful = false;
result.reason = "Invalid is_active value for: " + parameter.as_string();
RCLCPP_WARN(

Check warning on line 185 in system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp

View check run for this annotation

Codecov / codecov/patch

system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp#L182-L185

Added lines #L182 - L185 were not covered by tests
this->get_logger(), "Invalid is_active value for %s: %s", diag.name.c_str(),
parameter.as_string().c_str());
}

Check warning on line 188 in system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp

View check run for this annotation

Codecov / codecov/patch

system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp#L188

Added line #L188 was not covered by tests
}
}

if (!param_found) {
result.successful = false;
result.reason = "Parameter not registered: " + parameter.get_name();
RCLCPP_WARN(

Check warning on line 195 in system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp

View check run for this annotation

Codecov / codecov/patch

system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp#L193-L195

Added lines #L193 - L195 were not covered by tests
this->get_logger(), "Attempted to set unregistered parameter: %s",
parameter.get_name().c_str());
}
}

return result;

Check warning on line 201 in system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp

View check run for this annotation

Codecov / codecov/patch

system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp#L201

Added line #L201 was not covered by tests
}

Check warning on line 202 in system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Complex Method

DummyDiagPublisher::onSetParams has a cyclomatic complexity of 9, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.

Check warning on line 202 in system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Deep, Nested Complexity

DummyDiagPublisher::onSetParams has a nested complexity depth of 4, threshold = 4. This function contains deeply nested logic such as if statements and/or loops. The deeper the nesting, the lower the code health.

DummyDiagPublisher::DummyDiagPublisher(const rclcpp::NodeOptions & options)
: Node("dummy_diag_publisher", override_options(options))

Expand All @@ -164,6 +221,10 @@

// Publisher
pub_ = create_publisher<diagnostic_msgs::msg::DiagnosticArray>("/diagnostics", rclcpp::QoS(1));

// Parameter Callback Handle
param_callback_handle_ = this->add_on_set_parameters_callback(
std::bind(&DummyDiagPublisher::onSetParams, this, std::placeholders::_1));

Check warning on line 227 in system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp

View check run for this annotation

Codecov / codecov/patch

system/dummy_diag_publisher/src/dummy_diag_publisher_core.cpp#L226-L227

Added lines #L226 - L227 were not covered by tests
}

#include <rclcpp_components/register_node_macro.hpp>
Expand Down
Loading