RTC Toolkit 6.0.0
Loading...
Searching...
No Matches
ddtPublisherImage2d.hpp
Go to the documentation of this file.
1
11
12#ifndef DDT_PUBLISHER_IMAGE2D_HPP
13#define DDT_PUBLISHER_IMAGE2D_HPP
14
19
20#include <ddt/ddtDataPublisher.hpp>
21#include <ddt/ddtEncDecImage2D.hpp>
22#include <ddt/ddtLogger.hpp>
23
24#include <boost/endian.hpp>
25#include <fmt/format.h>
26
27namespace rtctk::ddtServer {
28
36template <typename SI>
38public:
39 using StreamInfo = SI;
40
42
43 static_assert(SI::ID.find_first_not_of("abcdefghijklmnopqrstuvwxyz_0123456789") ==
44 std::string_view::npos,
45 "DDT Publisher ID contains illegal characters!");
46
47 DdtPublisherImage2d(const std::string& db_prefix, ServiceContainer& services)
48 : m_logger(componentFramework::GetLogger("rtctk"))
49 , m_rtr(services.Get<RuntimeRepoIf>())
50 , m_rtr_prefix_static(GetStaticRtrPrefix(db_prefix))
51 , m_rtr_prefix_dynamic(GetDynamicRtrPrefix(db_prefix))
52 , m_enabled(true)
53 , m_broker_endpoint("zpb.rr://127.0.0.1:5001") {
54 LOG4CPLUS_INFO(m_logger, fmt::format("Creating DdtPulisherImage2d '{}'", SI::ID));
55
56 // publisher configuration
57
58 auto broker_endpoint_path = DataPointPath(m_rtr_prefix_static + "/broker_endpoint");
59 if (m_rtr.DataPointExists(broker_endpoint_path)) {
60 m_broker_endpoint = m_rtr.GetDataPoint<std::string>(broker_endpoint_path);
61 }
62
63 auto enabled_path = DataPointPath(m_rtr_prefix_dynamic + "/enabled");
64 if (m_rtr.DataPointExists(enabled_path)) {
65 m_enabled = m_rtr.GetDataPoint<bool>(enabled_path);
66 }
67
68 // metadata preparation
69
70 m_meta_data.meta_data_base.bytes_per_pixel = sizeof(typename SI::PixelType);
71 m_meta_data.meta_data_base.number_dimensions = 1;
72 m_meta_data.meta_data_base.complete_flag = true;
73 m_meta_data.meta_data_base.last_segment = true;
74 /*
75 * Important note: Endiannes of the source is assumed to be the same as the one of the
76 * host architecture. This has been set by design, that the low-level devices has to
77 * translate (if necessary) to the RTCTK's endiannes. RTCTK assumes the data comes in the
78 * same endiannes as the machine running this process.
79 */
80 m_meta_data.meta_data_base.byte_order_little_endian =
81 static_cast<bool>(boost::endian::order::native);
82
83 m_meta_data.meta_data_base.description = SI::ID;
84
85 if (typeid(typename SI::PixelType) == typeid(uint8_t)) {
86 m_meta_data.meta_data_base.data_type = ddt::UINT8;
87 } else if (typeid(typename SI::PixelType) == typeid(int16_t)) {
88 m_meta_data.meta_data_base.data_type = ddt::SINT16;
89 } else if (typeid(typename SI::PixelType) == typeid(int32_t)) {
90 m_meta_data.meta_data_base.data_type = ddt::SINT32;
91 } else if (typeid(typename SI::PixelType) == typeid(float)) {
92 m_meta_data.meta_data_base.data_type = ddt::FLOAT32;
93 } else if (typeid(typename SI::PixelType) == typeid(double)) {
94 m_meta_data.meta_data_base.data_type = ddt::FLOAT64;
95 } else if (typeid(typename SI::PixelType) == typeid(uint16_t)) {
96 m_meta_data.meta_data_base.data_type = ddt::UINT16;
97 } else if (typeid(typename SI::PixelType) == typeid(uint32_t)) {
98 m_meta_data.meta_data_base.data_type = ddt::UINT32;
99 } else {
100 CII_THROW(UnsupportedTypeException, typeid(typename SI::PixelType));
101 }
102
103 m_meta_data.binning_factor_x = 1;
104 m_meta_data.binning_factor_y = 1;
105 m_meta_data.number_pixels_x = SI::WIDTH;
106 m_meta_data.number_pixels_y = SI::HEIGHT;
107 m_meta_data.first_pixel_x = 0;
108 m_meta_data.first_pixel_y = 0;
109 m_meta_data.number_chunks_x = 1;
110 m_meta_data.number_chunks_y = 1;
111
112 // publisher instantiation and registration
113
114 m_ddt_publisher = std::make_unique<ddt::DdtDataPublisher>(m_logger);
115 m_ddt_publisher->set_topic_id(m_enc_dec.get_topic_id());
116 int bytes_per_sample = SI::HEIGHT * SI::WIDTH * sizeof(typename SI::PixelType);
117 int samples_in_buffer = 10;
118 m_ddt_publisher->SetBufferSize(bytes_per_sample, samples_in_buffer);
119
120 auto ret =
121 m_ddt_publisher->RegisterPublisher(m_broker_endpoint, std::string(SI::ID), false);
122 if (ret < 0) {
123 CII_THROW(RtctkException,
124 fmt::format("Failed to register DDT Publisher: {} against DDT Broker: {} "
125 "Check Broker URI, and ensure that Broker process is running.",
126 SI::ID,
127 m_broker_endpoint));
128 }
129 }
130
131 void Update() {
132 LOG4CPLUS_INFO(m_logger, fmt::format("Updating DdtPulisherImage2d::{}", SI::ID));
133
134 auto enabled_path = DataPointPath(m_rtr_prefix_dynamic + "/enabled");
135 if (m_rtr.DataPointExists(enabled_path)) {
136 m_enabled = m_rtr.GetDataPoint<bool>(enabled_path);
137 }
138 }
139
140 bool IsEnabled() {
141 return m_enabled;
142 }
143
144 void Publish(uint32_t sample_id, const uint8_t* data_ptr, uint32_t elements) {
145 m_meta_data.image_id = sample_id;
146 m_meta_data.meta_data_base.utc_timestamp = m_enc_dec.get_utc_timestamp();
147 m_enc_dec.Encode(m_meta_data);
148 std::vector<uint8_t> metadata = m_enc_dec.get_meta_data();
149
150 auto data_bytes = elements * sizeof(typename SI::PixelType);
151 m_ddt_publisher->WriteData(
152 sample_id, data_ptr, data_bytes, metadata.data(), metadata.size());
153 m_ddt_publisher->PublishData();
154 }
155
156private:
157 std::string GetStaticRtrPrefix(const std::string& db_prefix) {
158 std::string temp = db_prefix;
159 temp.insert(temp.find("/"), "/static");
160 return "/" + temp + "/" + std::string(SI::ID);
161 }
162
163 std::string GetDynamicRtrPrefix(const std::string& db_prefix) {
164 std::string temp = db_prefix;
165 temp.insert(temp.find("/"), "/dynamic");
166 return "/" + temp + "/" + std::string(SI::ID);
167 }
168
169 log4cplus::Logger& m_logger;
170
171 RuntimeRepoIf& m_rtr;
172
173 std::string m_rtr_prefix_static;
174 std::string m_rtr_prefix_dynamic;
175
179 bool m_enabled;
180
184 std::string m_broker_endpoint;
185
189 MetaDataElementsImage2D m_meta_data;
190
194 DdtEncDecImage2D m_enc_dec;
195
199 std::unique_ptr<ddt::DdtDataPublisher> m_ddt_publisher;
200};
201
202} // namespace rtctk::ddtServer
203
204#endif // DDT_PUBLISHER_IMAGE2D_HPP
This class provides a wrapper for a data point path.
Definition dataPointPath.hpp:76
The RtctkException class is the base class for all Rtctk exceptions.
Definition exceptions.hpp:220
Base interface for all Runtime Configuration Repository adapters.
Definition runtimeRepoIf.hpp:26
Container class that holds services of any type.
Definition serviceContainer.hpp:38
The UnsupportedTypeException is thrown whenever an attempt is made to use an unsupported type in the ...
Definition exceptions.hpp:248
SI StreamInfo
Definition ddtPublisherImage2d.hpp:39
componentFramework::DataPointPath DataPointPath
Definition ddtPublisherImage2d.hpp:41
bool IsEnabled()
Definition ddtPublisherImage2d.hpp:140
void Publish(uint32_t sample_id, const uint8_t *data_ptr, uint32_t elements)
Definition ddtPublisherImage2d.hpp:144
DdtPublisherImage2d(const std::string &db_prefix, ServiceContainer &services)
Definition ddtPublisherImage2d.hpp:47
void Update()
Definition ddtPublisherImage2d.hpp:131
Provides macros and utilities for exception handling.
log4cplus::Logger & GetLogger(const std::string &name="app")
Get handle to a specific logger.
Definition logger.cpp:191
Logging Support Library based on log4cplus.
Definition commandReplier.cpp:21
Definition businessLogic.cpp:23
Header file for RuntimeRepoIf, which defines the API for RuntimeRepoAdapters.
A container that can hold any type of service.