SRS-control 0.1.4
 
Loading...
Searching...
No Matches
JsonWriter.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <fstream>
4#include <map>
5
6#include <glaze/glaze.hpp>
7
9
10namespace srs::writer
11{
13 {
15 std::size_t marker_size{};
16 std::size_t hit_size{};
17 std ::map<std::string, std::vector<uint64_t>> marker_data{ { "vmm_id", {} }, { "srs_timestamp", {} } };
18 std ::map<std::string, std::vector<uint16_t>> hit_data{ { "is_over_threshold", {} },
19 { "channel_num", {} },
20 { "tdc", {} },
21 { "offset", {} },
22 { "vmm_id", {} },
23 { "adc", {} },
24 { "bc_id", {} } };
25 // HitDataVec hit_data;
26
27 void set_value(const StructData& data_struct)
28 {
29 header = data_struct.header;
30 fill_hit_data(data_struct.hit_data);
31 fill_marker_data(data_struct.marker_data);
32 }
33
34 private:
35 void fill_hit_data(const std::vector<HitData>& hits)
36 {
37 hit_size = hits.size();
38 for (auto& [key, value] : hit_data)
39 {
40 value.clear();
41 value.reserve(hit_size);
42 }
43 for (const auto& hit : hits)
44 {
45 hit_data.at("is_over_threshold").push_back(static_cast<uint16_t>(hit.is_over_threshold));
46 hit_data.at("channel_num").push_back(hit.channel_num);
47 hit_data.at("tdc").push_back(hit.tdc);
48 hit_data.at("offset").push_back(hit.offset);
49 hit_data.at("vmm_id").push_back(hit.vmm_id);
50 hit_data.at("adc").push_back(hit.adc);
51 hit_data.at("bc_id").push_back(hit.bc_id);
52 }
53 }
54
55 void fill_marker_data(const std::vector<MarkerData>& markers)
56 {
57 marker_size = markers.size();
58 for (auto& [key, value] : marker_data)
59 {
60 value.clear();
61 value.reserve(marker_size);
62 }
63 for (const auto& marker : markers)
64 {
65 marker_data.at("vmm_id").push_back(marker.vmm_id);
66 marker_data.at("srs_timestamp").push_back(marker.srs_timestamp);
67 }
68 }
69 };
70
71 class Json
72 {
73 public:
74 using InputType = const StructData*;
75 using OutputType = int;
76 using CoroType = asio::experimental::coro<OutputType(std::optional<InputType>)>;
77 using InputFuture = boost::shared_future<std::optional<InputType>>;
78 using OutputFuture = boost::unique_future<std::optional<OutputType>>;
79 static constexpr auto IsStructType = true;
80
81 explicit Json(asio::thread_pool& thread_pool, const std::string& filename)
82 : filename_{ filename }
83 , file_stream_{ filename, std::ios::out | std::ios::trunc }
84 {
85 if (not file_stream_.is_open())
86 {
87 spdlog::critical("JsonWriter: cannot open the file with filename {:?}", filename);
88 throw std::runtime_error("Error occured with JsonWriter");
89 }
90 file_stream_ << "[\n";
91 coro_ = generate_coro(thread_pool.get_executor());
92 common::coro_sync_start(coro_, std::optional<InputType>{}, asio::use_awaitable);
93 }
94
95 [[nodiscard]] static auto get_convert_mode() -> process::DataConvertOptions
96 {
98 }
99 auto write(auto pre_future) -> boost::unique_future<std::optional<int>>
100 {
101 return common::create_coro_future(coro_, pre_future);
102 }
103
104 private:
105 bool is_first_item = true;
106 std::string filename_;
107 std::fstream file_stream_;
109 std::string string_buffer_;
111
112 // NOLINTNEXTLINE(readability-static-accessed-through-instance)
113 auto generate_coro(asio::any_io_executor /*unused*/) -> CoroType
114 {
115 InputType data_struct{};
116 auto res = 0;
117 while (true)
118 {
119 res = 0;
120 if (data_struct != nullptr)
121 {
122 write_json(*data_struct);
123 res = 1;
124 }
125 auto data_temp = co_yield res;
126 if (data_temp.has_value())
127 {
128 data_struct = data_temp.value();
129 }
130 else
131 {
132 file_stream_ << "]\n";
133 file_stream_.close();
134 spdlog::info("JSON file {} is closed successfully", filename_);
135 co_return;
136 }
137 }
138 }
139
140 void write_json(const StructData& data_struct)
141 {
142 if (not is_first_item)
143 {
144 file_stream_ << ", ";
145 }
146 else
147 {
148 is_first_item = false;
149 }
150
151 data_buffer_.set_value(data_struct);
152 auto error_code =
153 glz::write<glz::opts{ .prettify = 1, .new_lines_in_arrays = 0 }>(data_buffer_, string_buffer_);
154 if (error_code)
155 {
156 spdlog::critical("JsonWriter: cannot interpret data struct to json. Error: {}",
157 error_code.custom_error_message);
158 throw std::runtime_error("Error occured with JsonWriter");
159 }
161 string_buffer_.clear();
162 }
163 };
164
165} // namespace srs::writer
std::string string_buffer_
asio::experimental::coro< OutputType(std::optional< InputType >)> CoroType
static auto get_convert_mode() -> process::DataConvertOptions
CompactExportData data_buffer_
Json(asio::thread_pool &thread_pool, const std::string &filename)
auto write(auto pre_future) -> boost::unique_future< std::optional< int > >
static constexpr auto IsStructType
boost::shared_future< std::optional< InputType > > InputFuture
const StructData * InputType
auto generate_coro(asio::any_io_executor) -> CoroType
std::fstream file_stream_
std::string filename_
boost::unique_future< std::optional< OutputType > > OutputFuture
void write_json(const StructData &data_struct)
auto create_coro_future(auto &coro, auto &&pre_fut)
void coro_sync_start(auto &coro, auto &&... args)
ReceiveDataHeader header
Header data.
std::vector< MarkerData > marker_data
Marker data.
std::vector< HitData > hit_data
Hit data.
void fill_hit_data(const std::vector< HitData > &hits)
std ::map< std::string, std::vector< uint64_t > > marker_data
std ::map< std::string, std::vector< uint16_t > > hit_data
void fill_marker_data(const std::vector< MarkerData > &markers)
void set_value(const StructData &data_struct)