Apollo 10.0
自动驾驶开放平台
preprocess_monitor.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2020 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 "absl/strings/str_cat.h"
20#include "gflags/gflags.h"
21#include "google/protobuf/descriptor.h"
22#include "google/protobuf/message.h"
23#include "google/protobuf/util/json_util.h"
24
25#include "cyber/common/file.h"
31
32namespace apollo {
33namespace dreamview {
34
37using google::protobuf::util::MessageToJsonString;
38
39using Json = nlohmann::json;
40
42 : FuelMonitor(FLAGS_preprocess_monitor_name),
43 node_(cyber::CreateNode("progress_monitor")) {
44 InitReaders();
45 LoadConfiguration();
46}
47
48PreprocessMonitor::PreprocessMonitor(const std::string& task_name)
49 : FuelMonitor(FLAGS_preprocess_monitor_name),
50 task_name_(task_name),
51 node_(cyber::CreateNode(task_name + "_progress_monitor")) {
52 InitReaders();
53 LoadConfiguration();
54}
55
57
58void PreprocessMonitor::InitReaders() {
59 node_->CreateReader<Progress>(
60 FLAGS_progress_topic, [this](const std::shared_ptr<Progress>& progress) {
61 this->OnProgress(progress);
62 });
63}
64
65void PreprocessMonitor::LoadConfiguration() {
66 if (!task_name_.empty()) {
67 const std::string& vehicle_dir =
68 VehicleManager::Instance()->GetVehicleDataPath();
69 std::string config_path = absl::StrCat(
70 vehicle_dir, "dreamview_conf/", task_name_, "_preprocess_table.pb.txt");
71 if (!PathExists(config_path)) {
72 AWARN << "No corresponding preprocess table file found in " << vehicle_dir
73 << ". Using default one instead.";
74 config_path = absl::StrCat("modules/dreamview/conf/", task_name_,
75 "_preprocess_table.pb.txt");
76 }
77
78 ACHECK(cyber::common::GetProtoFromFile(config_path, &preprocess_table_))
79 << "Unable to parse preprocess configuration from file " << config_path;
80 }
81 auto* progress = preprocess_table_.mutable_progress();
82 progress->set_percentage(0.0);
83 progress->set_log_string("Click on the button to start preprocessing");
84 progress->set_status(Status::UNKNOWN);
85
86 std::string json_string;
87 MessageToJsonString(preprocess_table_, &json_string);
88 current_status_json_ = Json::parse(json_string);
89
90 ADEBUG << "Configuration loaded.";
91}
92
94 if (!enabled_) {
95 current_status_json_.clear();
96 LoadConfiguration();
97 }
98 enabled_ = true;
99}
100
102
103void PreprocessMonitor::OnProgress(const std::shared_ptr<Progress>& progress) {
104 if (!enabled_) {
105 return;
106 }
107
108 std::string json_string;
109 MessageToJsonString(*progress, &json_string);
110 {
111 boost::unique_lock<boost::shared_mutex> writer_lock(mutex_);
112 current_status_json_["progress"] = Json::parse(json_string);
113 }
114}
115
117 boost::unique_lock<boost::shared_mutex> reader_lock(mutex_);
118 return current_status_json_;
119}
120
121} // namespace dreamview
122} // namespace apollo
@Brief This is a helper class that can load vehicle configurations.
A base class that monitor progress for Fuel client
nlohmann::json GetProgressAsJson() override
return preprocess progress as json
void Stop() override
stop monitoring preprocess progress
PreprocessMonitor()
Constructor of PreprocessMonitor.
void Start() override
start monitoring preprocess progress
#define ACHECK(cond)
Definition log.h:80
#define ADEBUG
Definition log.h:41
#define AWARN
Definition log.h:43
nlohmann::json Json
bool PathExists(const std::string &path)
Check if the path exists.
Definition file.cc:195
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
class register implement
Definition arena_queue.h:37