Motorcortex Core  version: 2.7.6
cmdline_config.h
1 /*
2  * Developer : Alexey Zakharov (alexey.zakharov@vectioneer.com)
3  * All rights reserved. Copyright (c) 2020 VECTIONEER.
4  */
5 
6 #ifndef MOTORCORTEX_CORE_CMDLINE_CONFIG_H
7 #define MOTORCORTEX_CORE_CMDLINE_CONFIG_H
8 
9 #include "cm_conndata.h"
10 #include "cmdline_field.h"
11 #include "cmdline_helper.h"
12 #include "cmdline_realtime.h"
13 #include "cmdline_systemmode.h"
14 #include "cmdline_task.h"
15 #include "license.h"
16 #include "utl_log.h"
17 #include <string>
18 #include <vector>
19 
20 namespace mcx::cmd_line {
21 
22 struct Component {
23  std::string name;
24  std::string version;
25 };
26 
28 
29 class Config {
30  const static std::map<std::string, FieldDesc> FIELDS;
31 
32 public:
33  Config() = default;
34 
35  explicit Config(std::vector<Component> components);
36 
37  Config(std::vector<Component> components, SystemMode mode);
38 
39  ~Config() = default;
40 
41  bool load(const std::string& file);
42 
43  std::string version() const;
44 
45  std::string toString() const;
46 
47  license::License license() const;
48 
49  template <typename T>
50  T get(const std::string& name) const {
51  return mcx::cmd_line::get<T>(config_, {name, Field::Required, Container::Any, DataType::Any}, path_);
52  }
53 
54  std::string path(std::string path) const;
55 
56  Task task(std::string path) const;
57 
58  Server server(std::string path) const;
59 
60  SystemMode mode(std::string path = {}) const;
61 
62  SystemMode systemMode(std::string path = {}) const;
63 
64  Realtime realtime(std::string path = {}) const;
65 
66  nlohmann::json find(const std::string& path) const;
67 
68 private:
69  std::shared_ptr<license::License> license_;
70 
71  static container::TaskSched getTaskSched(std::string task_sched);
72 
73  static std::tuple<std::string, std::string, std::string, std::string> parseUrl(std::string url);
74 
75  static inline std::string makePath(const std::string& field_name, const std::string& path) {
76  if (!path.empty() && path[0] != '/') {
77  return fmt::format("{}/{}", field_name, path);
78  }
79  return field_name;
80  }
81 
82  std::string path_;
83  nlohmann::json config_;
84  std::vector<Component> components_;
85  SystemMode cmd_mode_{SystemMode::PRODUCTION};
86 };
87 
88 std::string path(const std::string&);
89 
90 std::string filename(const std::string& path);
91 
92 Config parse(int argc, char** argv, std::vector<Component> component = {});
93 
94 } // namespace mcx::cmd_line
95 
96 #endif // MOTORCORTEX_CORE_CMDLINE_CONFIG_H
mcx::comm::ConnectionData
Definition: cm_conndata.h:18
mcx::cmd_line::Realtime
Definition: cmdline_realtime.h:14
mcx::cmd_line::Task
Definition: cmdline_task.h:16
mcx::cmd_line::Config
Definition: cmdline_config.h:29
mcx::cmd_line::Component
Definition: cmdline_config.h:22
mcx::license::License
Definition: license.h:28