18#include "modules/dreamview/backend/common/sim_control_manager/proto/dynamic_model_conf.pb.h"
30using apollo::cyber::class_loader::LibraryAlreadyLoadedException;
31using apollo::cyber::class_loader::LibraryLoadException;
33using apollo::cyber::class_loader::SymbolNotFoundException;
35using ::std::unordered_map;
47DynamicModelFactory::DynamicModelFactory() : dynamic_model_local_path_(
"") {
49 dynamic_model_local_path_ = home_path_ + FLAGS_resource_dynamic_model_path;
51 RegisterSimPerfectControl();
54DynamicModelFactory::~DynamicModelFactory() {
57 delete iter->second.dynamic_model_ptr;
58 iter->second.dynamic_model_ptr =
nullptr;
62void DynamicModelFactory::RegisterSimPerfectControl() {
68 s_dynamic_model_map_[FLAGS_sim_perfect_control].dynamic_model_name =
69 FLAGS_sim_perfect_control;
70 s_dynamic_model_map_[FLAGS_sim_perfect_control].dynamic_model_ptr =
71 new SimPerfectControl(
new MapService());
75bool DynamicModelFactory::RegisterDynamicModel(
const std::string &dm_dir_name) {
76 std::string dynamic_model_conf_json_path;
77 GetDynamicModelPath(dm_dir_name, &dynamic_model_conf_json_path,
true);
79 AERROR <<
"Failed to load Dynamic Model: " << dm_dir_name
80 <<
". conf file is not exists!";
85 &dynamic_model_conf)) {
86 AERROR <<
"Unable to parse Dynamic model conf from file "
87 << dynamic_model_conf_json_path;
92 if (!dynamic_model_conf.has_library_name() ||
93 !dynamic_model_conf.has_dynamic_model_name() ||
94 !dynamic_model_conf.has_depend_model_package()) {
95 AERROR <<
"Missing required field!";
101 AERROR <<
"This dynamic model: " << dynamic_model_name
102 <<
" is already registered!";
105 std::string dm_library_name = dynamic_model_conf.
library_name();
107 std::replace(depend_model_package.begin(), depend_model_package.end(),
'-',
'_');
109 std::string dynamic_model_package_library_path;
111 FLAGS_dynamic_model_package_library_path,
"APOLLO_LIB_PATH",
112 &dynamic_model_package_library_path)) {
113 AERROR <<
"Failed to get dynamic model package library path: "
114 << FLAGS_dynamic_model_package_library_path;
117 dynamic_model_package_library_path +=
118 depend_model_package +
"/" + dm_library_name;
120 AINFO <<
"dm_library_name: " << dm_library_name;
121 AINFO <<
"dynamic_model_package_library_path: " << dynamic_model_package_library_path;
128 reinterpret_cast<create_t *
>(shared_library->GetSymbol(
"create"));
130 create_dynamic_model(dm_dir_name, home_path_);
131 if (!dynamic_model_ptr) {
148 }
catch (
const LibraryLoadException &e) {
149 AERROR <<
"LibraryLoadException: " << e.what();
151 }
catch (
const LibraryAlreadyLoadedException &e) {
152 AERROR <<
"LibraryAlreadyLoadedException: " << e.what();
154 }
catch (
const SymbolNotFoundException &e) {
155 AERROR <<
"SymbolNotFoundException: " << e.what();
161void DynamicModelFactory::GetDynamicModelPath(
162 const std::string &dynamic_model_name, std::string *path,
163 bool get_conf_json) {
165 *path = dynamic_model_local_path_ + dynamic_model_name;
167 *path = *path +
"/dynamic_model.json";
172nlohmann::json DynamicModelFactory::RegisterDynamicModels() {
173 nlohmann::json result = {};
174 result[
"result"] =
true;
175 result[
"loaded_dynamic_models"] = {};
177 AERROR <<
"Failed to find DynamicModel!No dynamic model locally,Or do not "
178 "place it in correct location.";
180 DIR *directory = opendir(dynamic_model_local_path_.c_str());
181 if (directory ==
nullptr) {
182 AERROR <<
"Cannot open directory " << dynamic_model_local_path_;
185 std::string dynamic_model_dir_name;
187 while ((file = readdir(directory)) !=
nullptr) {
189 if (!strcmp(file->d_name,
".") || !strcmp(file->d_name,
"..")) {
192 if (file->d_type != DT_DIR) {
195 dynamic_model_dir_name = file->d_name;
197 RegisterDynamicModel(dynamic_model_dir_name);
206 result[
"loaded_dynamic_models"] = {FLAGS_sim_perfect_control};
209 if (iter->first != FLAGS_sim_perfect_control) {
210 result[
"loaded_dynamic_models"].push_back(iter->first);
217 std::string dynamic_model_name) {
220 AERROR <<
"Failed to get " << dynamic_model_name <<
" related pointer.";
223 return iter->second.dynamic_model_ptr;
226bool DynamicModelFactory::UnregisterDynamicModel(
227 const std::string &dynamic_model_name) {
230 AERROR <<
"Failed to get " << dynamic_model_name <<
" related pointer.";
233 std::string library_name = iter->second.library_name;
235 std::string dynamic_model_dir;
236 GetDynamicModelPath(dynamic_model_name, &dynamic_model_dir,
false);
237 std::string command =
"rm -fr " + dynamic_model_dir;
240 if (std::system(command.data()) != 0) {
241 AERROR <<
"Failed to delete dynamic model directory for: "
242 << std::strerror(errno);
247 std::string depend_model_package = iter->second.depend_model_package;
248 if (count_iter->second == 1) {
249 std::string lib_path =
250 home_path_ + FLAGS_dynamic_model_library_path + library_name;
263 auto dynamic_model_ptr = iter->second.dynamic_model_ptr;
264 delete dynamic_model_ptr;
Interface of simulated control algorithm
bool DeleteFile(const string &filename)
bool PathExists(const std::string &path)
Check if the path exists.
bool GetFilePathWithEnv(const std::string &path, const std::string &env_var, std::string *file_path)
get file path, judgement priority:
bool GetProtoFromJsonFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the json file specified by the file_name as ascii representation of protobufs,...
std::string GetEnv(const std::string &var_name, const std::string &default_value="")
unordered_map< string, DynamicModelInfo > s_dynamic_model_map_
unordered_map< string, int > s_dm_lib_count_
std::shared_ptr< SharedLibrary > SharedLibraryPtr
SimControlBase * create_t(std::string dynamic_name, std::string home_path)
Get SimControl class
optional string depend_model_package
optional string library_name
optional string dynamic_model_name
SimControlBase * dynamic_model_ptr
std::string dynamic_model_name
std::string depend_model_package