17
17
#define FMT_HEADER_ONLY
18
18
#include < fmt/format.h>
19
19
20
+ #include < sstream>
21
+ #include < unordered_map>
22
+
20
23
namespace
21
24
{
22
25
std::vector<std::string> split (const std::string & str, const char delim)
@@ -32,7 +35,7 @@ std::vector<std::string> split(const std::string & str, const char delim)
32
35
} // namespace
33
36
34
37
std::optional<DummyDiagPublisher::Status> DummyDiagPublisher::convertStrToStatus (
35
- std::string & status_str)
38
+ const std::string & status_str)
36
39
{
37
40
static std::unordered_map<std::string, Status> const table = {
38
41
{" OK" , Status::OK}, {" Warn" , Status::WARN}, {" Error" , Status::ERROR}, {" Stale" , Status::STALE}};
@@ -139,6 +142,61 @@ rclcpp::NodeOptions override_options(rclcpp::NodeOptions options)
139
142
true );
140
143
}
141
144
145
+ rcl_interfaces::msg::SetParametersResult DummyDiagPublisher::onSetParams (
146
+ const std::vector<rclcpp::Parameter> & parameters)
147
+ {
148
+ rcl_interfaces::msg::SetParametersResult result;
149
+ result.successful = true ;
150
+
151
+ for (const auto & parameter : parameters) {
152
+ bool param_found = false ;
153
+ const auto & param_name = parameter.get_name ();
154
+
155
+ for (auto & diag : required_diags_) {
156
+ if (param_name == diag.name + std::string (" .status" )) {
157
+ param_found = true ;
158
+ auto new_status = convertStrToStatus (parameter.as_string ());
159
+ if (new_status) {
160
+ diag.status = *new_status;
161
+ RCLCPP_INFO (
162
+ this ->get_logger (), " Updated %s status to: %s" , diag.name .c_str (),
163
+ parameter.as_string ().c_str ());
164
+ } else {
165
+ result.successful = false ;
166
+ result.reason = " Invalid status value for: " + parameter.as_string ();
167
+ RCLCPP_WARN (
168
+ this ->get_logger (), " Invalid status value for %s: %s" , diag.name .c_str (),
169
+ parameter.as_string ().c_str ());
170
+ }
171
+ } else if (param_name == diag.name + std::string (" .is_active" )) {
172
+ param_found = true ;
173
+ try {
174
+ diag.is_active = parameter.as_bool ();
175
+ RCLCPP_INFO (
176
+ this ->get_logger (), " Updated %s is_active to: %s" , diag.name .c_str (),
177
+ diag.is_active ? " true" : " false" );
178
+ } catch (const rclcpp::ParameterTypeException & e) {
179
+ result.successful = false ;
180
+ result.reason = " Invalid is_active value for: " + parameter.as_string ();
181
+ RCLCPP_WARN (
182
+ this ->get_logger (), " Invalid is_active value for %s: %s" , diag.name .c_str (),
183
+ parameter.as_string ().c_str ());
184
+ }
185
+ }
186
+ }
187
+
188
+ if (!param_found) {
189
+ result.successful = false ;
190
+ result.reason = " Parameter not registered: " + parameter.get_name ();
191
+ RCLCPP_WARN (
192
+ this ->get_logger (), " Attempted to set unregistered parameter: %s" ,
193
+ parameter.get_name ().c_str ());
194
+ }
195
+ }
196
+
197
+ return result;
198
+ }
199
+
142
200
DummyDiagPublisher::DummyDiagPublisher (const rclcpp::NodeOptions & options)
143
201
: Node(" dummy_diag_publisher" , override_options(options))
144
202
@@ -159,6 +217,10 @@ DummyDiagPublisher::DummyDiagPublisher(const rclcpp::NodeOptions & options)
159
217
160
218
// Publisher
161
219
pub_ = create_publisher<diagnostic_msgs::msg::DiagnosticArray>(" /diagnostics" , rclcpp::QoS (1 ));
220
+
221
+ // Parameter Callback Handle
222
+ param_callback_handle_ = this ->add_on_set_parameters_callback (
223
+ std::bind (&DummyDiagPublisher::onSetParams, this , std::placeholders::_1));
162
224
}
163
225
164
226
#include < rclcpp_components/register_node_macro.hpp>
0 commit comments