ros2_canopen  master
C++ ROS CANopen Library
configuration_manager.hpp
Go to the documentation of this file.
1 // Copyright 2022 Harshavadan Deshpande
2 // Christoph Hellmann Santos
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef CONFIGURATION_MANAGER_HPP
17 #define CONFIGURATION_MANAGER_HPP
18 
19 #include <iostream>
20 #include <map>
21 #include <optional>
22 #include <rclcpp/rclcpp.hpp>
23 #include <string>
24 #include <vector>
25 #include "yaml-cpp/yaml.h"
26 
27 namespace ros2_canopen
28 {
39 {
40 private:
41  std::string file_;
42  YAML::Node root_;
43  std::map<std::string, YAML::Node> devices_;
44 
45 public:
46  ConfigurationManager(std::string & file) : file_(file) { root_ = YAML::LoadFile(file_.c_str()); }
47 
56  template <typename T>
57  std::optional<T> get_entry(std::string device_name, std::string entry_name)
58  {
59  try
60  {
61  auto config = devices_.at(device_name);
62  return std::optional<T>(config[entry_name.c_str()].as<T>());
63  }
64  catch (const std::exception & e)
65  {
66  RCLCPP_INFO(
67  rclcpp::get_logger("yaml-cpp"), "Failed to load entry \"%s\" for device \"%s\" ",
68  entry_name.c_str(), device_name.c_str());
69  }
70 
71  return std::nullopt;
72  }
73 
80  std::string dump_device(std::string device_name)
81  {
82  std::string result;
83  try
84  {
85  auto config = devices_.at(device_name);
86  result = YAML::Dump(config);
87  }
88  catch (const std::exception & e)
89  {
90  std::cerr << e.what() << '\n';
91  }
92  return result;
93  }
94 
99  void init_config();
100 
107  uint32_t get_all_devices(std::vector<std::string> & devices);
108 };
109 } // namespace ros2_canopen
110 
111 #endif
Manager for Bus Configuration.
Definition: configuration_manager.hpp:39
ConfigurationManager(std::string &file)
Definition: configuration_manager.hpp:46
void init_config()
Initialises the configuration.
std::string dump_device(std::string device_name)
Dump device string.
Definition: configuration_manager.hpp:80
uint32_t get_all_devices(std::vector< std::string > &devices)
Returns all device names.
std::optional< T > get_entry(std::string device_name, std::string entry_name)
Gets a configuration entry for a specific device.
Definition: configuration_manager.hpp:57
Definition: configuration_manager.hpp:28