IRSOL
C++ code implementing socket server for interacting with Baumer camera.
utils.cpp
Go to the documentation of this file.
1#include "irsol/utils.hpp"
2
3#include "irsol/assert.hpp"
4#include "irsol/logging.hpp"
5
6#include <iomanip>
7#include <neoapi/neoapi.hpp>
8#include <random>
9#include <sstream>
10#include <tabulate/table.hpp>
11#include <thread>
12
13namespace irsol {
14namespace utils {
15std::string
17{
18 static std::random_device rd;
19 static std::mt19937 gen(rd());
20 std::uniform_int_distribution<> dis(0, 15);
21 std::uniform_int_distribution<> dis2(8, 11);
22
23 std::stringstream ss;
24 ss << std::hex;
25
26 for(int i = 0; i < 8; i++) {
27 ss << dis(gen);
28 }
29 ss << "-";
30 for(int i = 0; i < 4; i++) {
31 ss << dis(gen);
32 }
33 ss << "-4"; // Version 4
34 for(int i = 0; i < 3; i++) {
35 ss << dis(gen);
36 }
37 ss << "-";
38 ss << dis2(gen);
39 for(int i = 0; i < 3; i++) {
40 ss << dis(gen);
41 }
42 ss << "-";
43 for(int i = 0; i < 12; i++) {
44 ss << dis(gen);
45 }
46 return ss.str();
47}
48
49std::vector<std::string>
50split(const std::string& s, char delimiter)
51{
52 std::vector<std::string> tokens;
53 std::string token;
54 for(char c : s) {
55 if(c == delimiter) {
56 if(!token.empty()) {
57 tokens.push_back(token);
58 token.clear();
59 }
60 } else {
61 token += c;
62 }
63 }
64 if(!token.empty()) {
65 tokens.push_back(token);
66 }
67 return tokens;
68}
69
70std::string
71strip(const std::string& s, const std::string& delimiters)
72{
73 size_t start = 0;
74 size_t end = s.size();
75 IRSOL_LOG_TRACE("Stripping delimiters '{0:s}' from string '{1:s}'", delimiters, s);
76 while((start < end) && (delimiters.find(s[start]) != std::string::npos)) {
77 start++;
78 }
79 while((start < end) && (delimiters.find(s[end - 1]) != std::string::npos)) {
80 end--;
81 }
82
83 auto result = s.substr(start, end - start);
84 IRSOL_LOG_TRACE("Stripped string '{0:s}' is '{1:s}'", s, result);
85 return result;
86}
87
88std::string
89stripString(const std::string& s, const std::string& strippedString)
90{
91 // Check if the stripped string is present in the input string at the beginning
92 std::string result{s};
93 IRSOL_LOG_TRACE("Stripping string '{0:s}' from string '{1:s}'", strippedString, s);
94
95 if(result.find(strippedString) == 0) {
96 result = result.substr(strippedString.length());
97 }
98
99 // Check if the stripped string is present in the input string at the end
100 if(result.rfind(strippedString) == result.size() - strippedString.size()) {
101 result = result.substr(0, result.size() - strippedString.size());
102 }
103
105 "Stripped string '{0:s}' from string '{1:s}' is '{2:s}'", s, strippedString, result);
106
107 return result;
108}
109
110std::string
112{
113 using system_clock_t = std::chrono::system_clock;
114 auto now_sys = std::chrono::time_point_cast<system_clock_t::duration>(
115 tp - irsol::types::clock_t::now() + system_clock_t::now());
116
117 // Extract microseconds from time since epoch
118 auto us = std::chrono::duration_cast<std::chrono::microseconds>(now_sys.time_since_epoch()) %
119 std::chrono::seconds(1);
120
121 // Convert to time_t for formatting
122 std::time_t t_c = system_clock_t::to_time_t(now_sys);
123 std::tm tm = *std::localtime(&t_c);
124
125 std::stringstream ss;
126 ss << std::put_time(&tm, "%F %T"); // Date and time to seconds
127 ss << '.' << std::setfill('0') << std::setw(6) << us.count(); // Append microseconds
128
129 return ss.str();
130}
131
132std::string
134{
135 std::stringstream ss;
136 if(dr.count() == 0) {
137 return "0ns";
138 }
139
140 if(dr >= std::chrono::hours(1)) {
141 auto hours = std::chrono::duration_cast<std::chrono::hours>(dr);
142 ss << std::to_string(hours.count());
143 ss << "hours ";
144 dr -= hours;
145 }
146 if(dr >= std::chrono::minutes(1)) {
147 auto minutes = std::chrono::duration_cast<std::chrono::minutes>(dr);
148 ss << std::setw(2) << std::setfill('0') << std::to_string(minutes.count());
149 ss << "minutes ";
150 dr -= minutes;
151 }
152 if(dr >= std::chrono::seconds(1)) {
153 auto seconds = std::chrono::duration_cast<std::chrono::seconds>(dr);
154 ss << std::setw(2) << std::setfill('0') << std::to_string(seconds.count());
155 ss << "s ";
156 dr -= seconds;
157 }
158 if(dr >= std::chrono::milliseconds(1)) {
159 auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(dr);
160 ss << std::setw(3) << std::setfill('0') << std::to_string(milliseconds.count());
161 ss << "ms ";
162 dr -= milliseconds;
163 }
164 if(dr >= std::chrono::microseconds(1)) {
165 auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(dr);
166 ss << std::setw(3) << std::setfill('0') << std::to_string(microseconds.count());
167 ss << "us ";
168 dr -= microseconds;
169 }
170 if(dr >= std::chrono::nanoseconds(1)) {
171 auto nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(dr);
172 ss << std::setw(3) << std::setfill('0') << std::to_string(nanoseconds.count());
173 ss << "ns ";
174 dr -= nanoseconds;
175 }
176
177 auto result = ss.str();
178 return strip(result, " 0");
179}
180
181std::vector<irsol::types::byte_t>
182stringToBytes(const std::string& s)
183{
184 const auto* data = reinterpret_cast<const irsol::types::byte_t*>(s.data());
185 return {data, data + s.size()};
186}
187
188std::string
189bytesToString(const std::vector<irsol::types::byte_t>& input)
190{
191 const auto* data = reinterpret_cast<const char*>(input.data());
192 return std::string(data, data + input.size());
193}
194
195NeoAPI::Cam
197{
198 IRSOL_LOG_DEBUG("Loading default camera");
199 NeoAPI::Cam cam = NeoAPI::Cam();
200
201 const auto cameraSerialNumber = internal::defaultCameraSerialNumber();
202 IRSOL_LOG_TRACE("Trying to connect to default camera with SN '{0:s}'.", cameraSerialNumber);
203 try {
204 cam.Connect(cameraSerialNumber);
205 } catch(NeoAPI::NotConnectedException& e) {
206 IRSOL_ASSERT_FATAL(false, "Camera connection failed: %s", e.GetDescription());
207 throw e;
208 }
209 IRSOL_ASSERT_FATAL(cam.IsConnected(), "Camera is not connected");
210 IRSOL_LOG_DEBUG("Camera connection successful");
211
212 return cam;
213}
214
215NeoAPI::CamInfoList&
217{
218 IRSOL_LOG_TRACE("Discovering cameras");
219 NeoAPI::CamInfoList& infoList = NeoAPI::CamInfoList::Get();
220 IRSOL_LOG_TRACE("Refreshing camera list");
221 infoList.Refresh();
222
223 return infoList;
224}
225namespace internal {
226constexpr const char*
228{
229 return "700011810487";
230}
231} // namespace internal
232
233} // namespace utils
234} // namespace irsol
Assertion macros and utilities based on the PPK_ASSERT library.
#define IRSOL_ASSERT_FATAL
Fatal-level assertion macro.
Definition assert.hpp:135
#define IRSOL_LOG_DEBUG(...)
Logs a debug-level message using the default logger.
Definition logging.hpp:91
#define IRSOL_LOG_TRACE(...)
Logs a trace-level message using the default logger.
Definition logging.hpp:90
Logging utilities and configuration for the irsol library.
clock_t::duration duration_t
Alias for a duration of time as defined by clock_t.
Definition types.hpp:128
std::byte byte_t
Alias for a single byte used in serialization or binary data handling.
Definition types.hpp:165
clock_t::time_point timepoint_t
Alias for a point in time as defined by clock_t.
Definition types.hpp:120
constexpr const char * defaultCameraSerialNumber()
Default serial number for selecting the system camera.
Definition utils.cpp:227
NeoAPI::Cam loadDefaultCamera()
Loads the default camera device.
Definition utils.cpp:196
std::string bytesToString(const std::vector< irsol::types::byte_t > &input)
Converts a std::vector of irsol::types::byte_t to a std::string.
Definition utils.cpp:189
std::string strip(const std::string &s, const std::string &delimiters=" \t\r\n")
Removes leading and trailing characters from a string.
Definition utils.cpp:71
NeoAPI::CamInfoList & discoverCameras()
Discovers all cameras connected to the system.
Definition utils.cpp:216
std::string stripString(const std::string &s, const std::string &strippedString)
Removes all occurrences of a specific substring from the start and end of a string.
Definition utils.cpp:89
std::string uuid()
Generates a new UUID string.
Definition utils.cpp:16
std::vector< std::string > split(const std::string &s, char delimiter)
Splits a string into tokens based on a delimiter.
Definition utils.cpp:50
std::vector< irsol::types::byte_t > stringToBytes(const std::string &s)
Converts a std::string to a std::vector of irsol::types::byte_t.
Definition utils.cpp:182
std::string timestampToString(irsol::types::timepoint_t tp)
Converts a steady_clock time point to a human-readable string.
Definition utils.cpp:111
std::string durationToString(irsol::types::duration_t dr)
Converts a duration to a human-readable string.
Definition utils.cpp:133
auto result
std::string input
General utility functions used throughout the irsol library.