Apollo 10.0
自动驾驶开放平台
py_record.h
浏览该文件的文档.
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
38namespace apollo {
39namespace cyber {
40namespace record {
41
42struct 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_
std::string GetProtoDesc(const std::string &channel_name)
Definition py_record.h:83
std::string GetMessageType(const std::string &channel_name)
Definition py_record.h:79
PyRecordReader(const std::string &file)
Definition py_record.h:52
std::set< std::string > GetChannelList() const
Definition py_record.h:95
BagMessage ReadMessage(uint64_t begin_time=0, uint64_t end_time=std::numeric_limits< uint64_t >::max())
Definition py_record.h:56
uint64_t GetMessageNumber(const std::string &channel_name)
Definition py_record.h:75
bool WriteChannel(const std::string &channel_str, const std::string &type, const std::string &proto_desc)
Definition py_record.h:109
const std::string & GetProtoDesc(const std::string &channel_name) const
Definition py_record.h:138
bool WriteMessage(const std::string &channel_name, const std::string &rawmessage, uint64_t time, const std::string &proto_desc="")
Definition py_record.h:114
uint64_t GetMessageNumber(const std::string &channel_name) const
Definition py_record.h:130
bool Open(const std::string &path)
Definition py_record.h:105
bool SetSizeOfFileSegmentation(uint64_t size_kilobytes)
Definition py_record.h:122
const std::string & GetMessageType(const std::string &channel_name) const
Definition py_record.h:134
bool SetIntervalOfFileSegmentation(uint64_t time_sec)
Definition py_record.h:126
uint64_t GetMessageNumber(const std::string &channel_name) const override
Get message number by channel name.
const std::string & GetProtoDesc(const std::string &channel_name) const override
Get proto descriptor string by channel name.
bool SetIntervalOfFileSegmentation(uint64_t time_sec)
Set max interval (Second) to segment record file.
bool SetSizeOfFileSegmentation(uint64_t size_kilobytes)
Set max size (KB) to segment record file
bool Open(const std::string &file)
Open a record to write.
bool WriteMessage(const std::string &channel_name, const MessageT &message, const uint64_t time_nanosec, const std::string &proto_desc="")
Write a message to record.
const std::string & GetMessageType(const std::string &channel_name) const override
Get message type by channel name.
bool WriteChannel(const std::string &channel_name, const std::string &message_type, const std::string &proto_desc)
Write a channel to record.
class register implement
Definition arena_queue.h:37
Basic data struct of record message.
std::string content
The content of the message.
uint64_t time
The time (nanosecond) of the message.
std::string channel_name
The channel name of the message.