SRS-control 0.1.4
 
Loading...
Searching...
No Matches
StructToProtoConverter.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <print>
4#include <srs/data/message.pb.h>
6
7namespace srs::process
8{
9 class Struct2ProtoConverter : public DataConverterBase<const StructData*, const proto::Data*>
10 {
11 public:
12 explicit Struct2ProtoConverter(asio::thread_pool& thread_pool)
13 : DataConverterBase{ generate_coro(thread_pool.get_executor()) }
14 {
15 }
16
17 static constexpr auto ConverterOption = std::array{ proto, proto_frame };
18
19 [[nodiscard]] auto data() const -> const auto& { return output_data_; }
20
21 private:
22 proto::Data output_data_;
23
24 void reset() { output_data_.Clear(); }
25
26 auto convert(const StructData& struct_data) -> const proto::Data&
27 {
28 set_header(struct_data);
29 set_marker_data(struct_data);
30 set_hit_data(struct_data);
31 return output_data_;
32 }
33
34 // NOLINTNEXTLINE(readability-static-accessed-through-instance)
35 auto generate_coro(asio::any_io_executor /*unused*/) -> CoroType
36 {
37 InputType temp_data{};
38 while (true)
39 {
40 if (temp_data != nullptr)
41 {
42 reset();
43 convert(*temp_data);
44 }
45 auto data = co_yield (&output_data_);
46 if (data.has_value())
47 {
48 temp_data = data.value();
49 }
50 else
51 {
52 spdlog::debug("Shutting down StructToProto converter.");
53 co_return;
54 }
55 }
56 }
57
58 void set_header(const StructData& struct_data)
59 {
60 const auto& input_header = struct_data.header;
61 auto* header = output_data_.mutable_header();
62 if (header == nullptr)
63 {
64 throw std::runtime_error("header is nullptr!");
65 }
66 header->set_frame_counter(input_header.frame_counter);
67 header->set_fec_id(input_header.fec_id);
68 header->set_udp_timestamp(input_header.udp_timestamp);
69 header->set_overflow(input_header.overflow);
70 }
71
72 void set_marker_data(const StructData& struct_data)
73 {
74 const auto& input_marker_data = struct_data.marker_data;
75 for (const auto& input_data : input_marker_data)
76 {
77 auto* marker_data = output_data_.add_marker_data();
78 marker_data->set_vmm_id(input_data.vmm_id);
79 marker_data->set_srs_timestamp(input_data.srs_timestamp);
80 }
81 }
82
83 void set_hit_data(const StructData& struct_data)
84 {
85 const auto& input_hit_data = struct_data.hit_data;
86 for (const auto& input_data : input_hit_data)
87 {
88 auto* hit_data = output_data_.add_hit_data();
89 hit_data->set_is_over_threshold(input_data.is_over_threshold);
90 hit_data->set_channel_num(input_data.channel_num);
91 hit_data->set_tdc(input_data.tdc);
92 hit_data->set_offset(input_data.offset);
93 hit_data->set_vmm_id(input_data.vmm_id);
94 hit_data->set_adc(input_data.adc);
95 hit_data->set_bc_id(input_data.bc_id);
96 }
97 }
98 };
99} // namespace srs
asio::experimental::coro< OutputType(std::optional< InputType >)> CoroType
void set_header(const StructData &struct_data)
auto convert(const StructData &struct_data) -> const proto::Data &
void set_hit_data(const StructData &struct_data)
void set_marker_data(const StructData &struct_data)
auto generate_coro(asio::any_io_executor) -> CoroType
Struct2ProtoConverter(asio::thread_pool &thread_pool)
ReceiveDataHeader header
Header data.
std::vector< MarkerData > marker_data
Marker data.
std::vector< HitData > hit_data
Hit data.