Apollo 10.0
自动驾驶开放平台
vehicle_manager.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 The Apollo Authors. All Rights Reserved.
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
18
19#include <cstdlib>
20
21#include "absl/strings/str_cat.h"
22
23#include "cyber/common/file.h"
24#include "cyber/common/log.h"
27
28namespace apollo {
29namespace dreamview {
30
32
33VehicleManager::VehicleManager() {
34 ACHECK(GetProtoFromFile(FLAGS_vehicle_data_config_filename, &vehicle_data_))
35 << "Unable to parse VehicleData config file "
36 << FLAGS_vehicle_data_config_filename;
37}
38
39const std::string &VehicleManager::GetVehicleDataPath() const {
40 return vehicle_data_path_;
41}
42
43bool VehicleManager::UseVehicle(const std::string &vehicle_data_path) {
44 if (FLAGS_vehicle_changed_use_copy_mode) {
45 return UseVehicleUseCopyMode(vehicle_data_path);
46 } else {
47 return UseVehicleUseSymlinkMode(vehicle_data_path);
48 }
49}
50
51bool VehicleManager::UseVehicleUseCopyMode(
52 const std::string &vehicle_data_path) {
53 if (!cyber::common::DirectoryExists(vehicle_data_path)) {
54 AERROR << "Cannot find vehicle data: " << vehicle_data_path;
55 return false;
56 }
57 vehicle_data_path_ = vehicle_data_path;
58
59 for (const auto &data_file : vehicle_data_.data_files()) {
60 const auto source_path =
61 absl::StrCat(vehicle_data_path, "/", data_file.source_path());
62 const auto &dest_path = data_file.dest_path();
63
64 const bool ret = cyber::common::Copy(source_path, dest_path);
65 AINFO_IF(ret) << "Copied " << source_path << " to " << dest_path;
66 }
67 AINFO << "Use vehice [" << vehicle_data_path << "] copy config successfully.";
68
69 // Reload vehicle config for current process.
71
72 return true;
73}
74
75bool VehicleManager::UseVehicleUseSymlinkMode(
76 const std::string &vehicle_data_path) {
77 std::string vehicle_type = vehicle_data_path;
78 auto pos = vehicle_data_path.find_last_of('/');
79 if (pos != std::string::npos) {
80 vehicle_type = vehicle_data_path.substr(pos + 1);
81 }
82 std::string cmd = "aem profile use " + vehicle_type;
83 AINFO << "Use vehicle by cmd: " << cmd;
84 const int ret = std::system(cmd.data());
85 if (ret == 0) {
86 AINFO << "SUCCESS: use vehicle " << vehicle_type;
87
88 // Reload vehicle config for current process.
90 return true;
91 } else {
92 AERROR << "FAILED: use vehicle " << vehicle_type;
93 return false;
94 }
95}
96
97} // namespace dreamview
98} // namespace apollo
static void Init()
Initialize vehicle configurations with default configuration file pointed by gflags FLAGS_vehicle_con...
bool UseVehicle(const std::string &vehicle_data_path)
const std::string & GetVehicleDataPath() const
#define ACHECK(cond)
Definition log.h:80
#define AERROR
Definition log.h:44
#define AINFO_IF(cond)
Definition log.h:72
#define AINFO
Definition log.h:42
bool GetProtoFromFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as a representation of protobufs,...
Definition file.cc:132
bool Copy(const std::string &from, const std::string &to)
Copy a file or directory.
Definition file.cc:295
bool DirectoryExists(const std::string &directory_path)
Check if the directory specified by directory_path exists and is indeed a directory.
Definition file.cc:207
class register implement
Definition arena_queue.h:37