Apollo  6.0
Open source self driving car software
py_record.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 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 
17 #ifndef PYTHON_WRAPPER_PY_RECORD_H_
18 #define PYTHON_WRAPPER_PY_RECORD_H_
19 
20 #include <unistd.h>
21 
22 #include <iostream>
23 #include <limits>
24 #include <memory>
25 #include <mutex>
26 #include <set>
27 #include <string>
28 #include <thread>
29 
33 #include "cyber/proto/record.pb.h"
37 
38 namespace apollo {
39 namespace cyber {
40 namespace record {
41 
42 struct BagMessage {
43  uint64_t timestamp = 0;
44  std::string channel_name = "";
45  std::string data = "";
46  std::string data_type = "";
47  bool end = true;
48 };
49 
51  public:
52  explicit PyRecordReader(const std::string& file) {
53  record_reader_.reset(new RecordReader(file));
54  }
55 
57  uint64_t begin_time = 0,
58  uint64_t end_time = std::numeric_limits<uint64_t>::max()) {
59  BagMessage ret_msg;
60  RecordMessage record_message;
61  if (!record_reader_->ReadMessage(&record_message, begin_time, end_time)) {
62  ret_msg.end = true;
63  return ret_msg;
64  }
65 
66  ret_msg.end = false;
67  ret_msg.channel_name = record_message.channel_name;
68  ret_msg.data = record_message.content;
69  ret_msg.timestamp = record_message.time;
70  ret_msg.data_type =
71  record_reader_->GetMessageType(record_message.channel_name);
72  return ret_msg;
73  }
74 
75  uint64_t GetMessageNumber(const std::string& channel_name) {
76  return record_reader_->GetMessageNumber(channel_name);
77  }
78 
79  std::string GetMessageType(const std::string& channel_name) {
80  return record_reader_->GetMessageType(channel_name);
81  }
82 
83  std::string GetProtoDesc(const std::string& channel_name) {
84  return record_reader_->GetProtoDesc(channel_name);
85  }
86 
87  std::string GetHeaderString() {
88  std::string org_data;
89  record_reader_->GetHeader().SerializeToString(&org_data);
90  return org_data;
91  }
92 
93  void Reset() { record_reader_->Reset(); }
94 
95  std::set<std::string> GetChannelList() const {
96  return record_reader_->GetChannelList();
97  }
98 
99  private:
100  std::unique_ptr<RecordReader> record_reader_;
101 };
102 
104  public:
105  bool Open(const std::string& path) { return record_writer_.Open(path); }
106 
107  void Close() { record_writer_.Close(); }
108 
109  bool WriteChannel(const std::string& channel_str, const std::string& type,
110  const std::string& proto_desc) {
111  return record_writer_.WriteChannel(channel_str, type, proto_desc);
112  }
113 
114  bool WriteMessage(const std::string& channel_name,
115  const std::string& rawmessage, uint64_t time,
116  const std::string& proto_desc = "") {
117  return record_writer_.WriteMessage(
118  channel_name, std::make_shared<message::RawMessage>(rawmessage), time,
119  proto_desc);
120  }
121 
122  bool SetSizeOfFileSegmentation(uint64_t size_kilobytes) {
123  return record_writer_.SetSizeOfFileSegmentation(size_kilobytes);
124  }
125 
126  bool SetIntervalOfFileSegmentation(uint64_t time_sec) {
127  return record_writer_.SetIntervalOfFileSegmentation(time_sec);
128  }
129 
130  uint64_t GetMessageNumber(const std::string& channel_name) const {
131  return record_writer_.GetMessageNumber(channel_name);
132  }
133 
134  const std::string& GetMessageType(const std::string& channel_name) const {
135  return record_writer_.GetMessageType(channel_name);
136  }
137 
138  const std::string& GetProtoDesc(const std::string& channel_name) const {
139  return record_writer_.GetProtoDesc(channel_name);
140  }
141 
142  private:
143  RecordWriter record_writer_;
144 };
145 
146 } // namespace record
147 } // namespace cyber
148 } // namespace apollo
149 
150 #endif // PYTHON_WRAPPER_PY_RECORD_H_
Definition: py_record.h:103
uint64_t time
The time (nanosecond) of the message.
Definition: record_message.h:64
Basic data struct of record message.
Definition: record_message.h:34
const std::string & GetProtoDesc(const std::string &channel_name) const
Definition: py_record.h:138
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
std::string content
The content of the message.
Definition: record_message.h:59
The record writer.
Definition: record_writer.h:44
bool Open(const std::string &path)
Definition: py_record.h:105
uint64_t timestamp
Definition: py_record.h:43
std::string channel_name
Definition: py_record.h:44
bool WriteChannel(const std::string &channel_str, const std::string &type, const std::string &proto_desc)
Definition: py_record.h:109
bool SetIntervalOfFileSegmentation(uint64_t time_sec)
Definition: py_record.h:126
Definition: py_record.h:42
PyRecordReader(const std::string &file)
Definition: py_record.h:52
The record reader.
Definition: record_reader.h:39
std::string channel_name
The channel name of the message.
Definition: record_message.h:54
std::string data_type
Definition: py_record.h:46
uint64_t GetMessageNumber(const std::string &channel_name)
Definition: py_record.h:75
uint64_t GetMessageNumber(const std::string &channel_name) const
Definition: py_record.h:130
void Reset()
Definition: py_record.h:93
bool end
Definition: py_record.h:47
bool WriteMessage(const std::string &channel_name, const std::string &rawmessage, uint64_t time, const std::string &proto_desc="")
Definition: py_record.h:114
const std::string & GetMessageType(const std::string &channel_name) const
Definition: py_record.h:134
bool SetSizeOfFileSegmentation(uint64_t size_kilobytes)
Definition: py_record.h:122
std::string GetMessageType(const std::string &channel_name)
Definition: py_record.h:79
void Close()
Definition: py_record.h:107
std::string GetProtoDesc(const std::string &channel_name)
Definition: py_record.h:83
std::set< std::string > GetChannelList() const
Definition: py_record.h:95
std::string data
Definition: py_record.h:45
BagMessage ReadMessage(uint64_t begin_time=0, uint64_t end_time=std::numeric_limits< uint64_t >::max())
Definition: py_record.h:56
Definition: py_record.h:50
std::string GetHeaderString()
Definition: py_record.h:87