|
| 1 | +# Copyright 2025 Open Source Robotics Foundation, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import rclpy |
| 16 | +from rclpy.parameter import parameter_value_to_python |
| 17 | +from ros2param.api import call_get_parameters |
| 18 | +from ros2param.api import call_list_parameters |
| 19 | + |
| 20 | +from ros2cli.node.direct import DirectNode |
| 21 | +from ros2cli.node.strategy import NodeStrategy |
| 22 | +from ros2doctor.api import DoctorCheck |
| 23 | +from ros2doctor.api import DoctorReport |
| 24 | +from ros2doctor.api import Report |
| 25 | +from ros2doctor.api import Result |
| 26 | +from ros2doctor.api.format import doctor_warn |
| 27 | + |
| 28 | + |
| 29 | +class ParameterReport(DoctorReport): |
| 30 | + """Report parameter related information.""" |
| 31 | + |
| 32 | + def category(self): |
| 33 | + return 'parameter' |
| 34 | + |
| 35 | + def report(self): |
| 36 | + report = Report('PARAMETER LIST') |
| 37 | + with NodeStrategy(None) as node: |
| 38 | + try: |
| 39 | + node_names_and_namespaces = \ |
| 40 | + node.get_node_names_and_namespaces() |
| 41 | + except Exception: |
| 42 | + node_names_and_namespaces = [] |
| 43 | + if not node_names_and_namespaces: |
| 44 | + report.add_to_report('total nodes checked', 0) |
| 45 | + report.add_to_report('total parameter count', 0) |
| 46 | + report.add_to_report('parameter', 'none') |
| 47 | + return report |
| 48 | + |
| 49 | + param_count = 0 |
| 50 | + nodes_checked = 0 |
| 51 | + |
| 52 | + with DirectNode(None) as param_node: |
| 53 | + for node_name, namespace in sorted(node_names_and_namespaces): |
| 54 | + nodes_checked += 1 |
| 55 | + full_name = f"{namespace.rstrip('/')}/{node_name}" |
| 56 | + try: |
| 57 | + response = call_list_parameters( |
| 58 | + node=param_node.node, node_name=full_name) |
| 59 | + if response is None: |
| 60 | + continue |
| 61 | + elif response.result() is None: |
| 62 | + continue |
| 63 | + |
| 64 | + param_names = response.result().result.names |
| 65 | + if param_names: |
| 66 | + param_count += len(param_names) |
| 67 | + report.add_to_report('node', full_name) |
| 68 | + try: |
| 69 | + param_response = call_get_parameters( |
| 70 | + node=param_node.node, |
| 71 | + node_name=full_name, |
| 72 | + parameter_names=param_names |
| 73 | + ) |
| 74 | + param_values = None |
| 75 | + if param_response: |
| 76 | + param_values = param_response.values |
| 77 | + if param_values and len(param_values) == len( |
| 78 | + param_names |
| 79 | + ): |
| 80 | + params_with_values = sorted( |
| 81 | + zip(param_names, param_values) |
| 82 | + ) |
| 83 | + for name, value_msg in params_with_values: |
| 84 | + value = parameter_value_to_python( |
| 85 | + value_msg |
| 86 | + ) |
| 87 | + report.add_to_report( |
| 88 | + 'parameter', f'{name}: {value}') |
| 89 | + else: |
| 90 | + for param_name in sorted(param_names): |
| 91 | + report.add_to_report( |
| 92 | + 'parameter', param_name |
| 93 | + ) |
| 94 | + except RuntimeError: |
| 95 | + for param_name in sorted(param_names): |
| 96 | + report.add_to_report( |
| 97 | + 'parameter', param_name |
| 98 | + ) |
| 99 | + except RuntimeError: |
| 100 | + pass |
| 101 | + |
| 102 | + report.add_to_report('total nodes checked', nodes_checked) |
| 103 | + report.add_to_report('total parameter count', param_count) |
| 104 | + return report |
0 commit comments