Skip to content

Commit b7d481b

Browse files
committed
Further implementation of the ContinousMeasurementDataProtocol
Details: - Implementing the regex patterns - Adding the methods required by the interfaces - Starting to implement the statemachine of the ProcessData()
1 parent dfa2fed commit b7d481b

File tree

2 files changed

+96
-59
lines changed

2 files changed

+96
-59
lines changed

application/sources/continous_measurement_data_protocol.cpp

Lines changed: 68 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
ContinousMeasurementDataProtocol::ContinousMeasurementDataProtocol()
2929
{
30-
state = Constants::States::WaitingForTransmission;
30+
state = Constants::States::WaitingForHeaderMessageStart;
3131
}
3232

3333
std::string ContinousMeasurementDataProtocol::GetProtocolName(void)
@@ -41,7 +41,6 @@ std::vector<DiagramSpecialized> ContinousMeasurementDataProtocol::ProcessData(st
4141
std::string received_data;
4242
std::string actual_line;
4343

44-
/*
4544
while(std::getline(input_data, actual_line))
4645
{
4746
std::smatch match_results;
@@ -53,18 +52,17 @@ std::vector<DiagramSpecialized> ContinousMeasurementDataProtocol::ProcessData(st
5352
{
5453
switch(state)
5554
{
56-
case Constants::States::WaitingForStartLine:
57-
// If a start line was found...
58-
if(std::regex_match(actual_line, std::regex(Constants::Regex::start_line)))
55+
case Constants::States::WaitingForHeaderMessageStart:
56+
// If a header message start line was found
57+
if(std::regex_match(actual_line, std::regex(Constants::Regex::header_start)))
5958
{
60-
state = Constants::States::ProcessingTitleLine;
59+
state = Constants::States::ProcessingHeaderDiagramTitle;
6160
}
6261
break;
63-
case Constants::States::ProcessingTitleLine:
64-
// In any case, we will switch to the next state
65-
state = Constants::States::ProcessingHeadline;
66-
// If this is a diagram title line
67-
if(std::regex_search(actual_line, match_results, std::regex(Constants::Regex::title_line)))
62+
case Constants::States::ProcessingHeaderDiagramTitle:
63+
state = Constants::States::ProcessingHeaderDataLines;
64+
// If a header message diagram title line was found
65+
if(std::regex_search(actual_line, match_results, std::regex(Constants::Regex::header_diagram_title)))
6866
{
6967
// Then we create a diagram object with the title
7068
actual_diagram = DiagramSpecialized(match_results[1]);
@@ -81,50 +79,63 @@ std::vector<DiagramSpecialized> ContinousMeasurementDataProtocol::ProcessData(st
8179
actual_diagram = DiagramSpecialized(current_date_and_time_string);
8280
// Switching to the next state without a break --> a new line will NOT be fetched, because this line is the headline
8381
}
84-
8582
// The falltrough is not an error in this case, this behaviour needed because there was no diagram title found, the actual_line contains the headline
86-
[[fallthrough]];
87-
case Constants::States::ProcessingHeadline:
88-
// If this is a headline but not a dataline
89-
// (this is needed because with regex it is difficult to define the differences between the data and headlines)
90-
if((std::regex_match(actual_line, std::regex(Constants::Regex::headline))) &&
91-
(!std::regex_match(actual_line, std::regex(Constants::Regex::data_line))))
83+
[[fallthrough]];
84+
case Constants::States::ProcessingHeaderDataLines:
85+
if(std::regex_match(actual_line, std::regex(Constants::Regex::header_datalines)))
9286
{
93-
std::string headline = actual_line;
94-
DataIndexType column_index = 0;
87+
std::string header_data_lines = actual_line;
9588

96-
// Collecting the labels from the headline
97-
while(std::regex_search(headline, match_results, std::regex(Constants::Regex::headline_analyzer)))
98-
{
99-
if(0 == column_index)
100-
{
101-
actual_diagram.SetAxisXTitle(match_results[1]);
102-
}
103-
else
104-
{
105-
actual_diagram.AddNewDataLine(match_results[1]);
106-
}
89+
// Extracting the title of the X axis
90+
std::regex_search(header_data_lines, match_results, std::regex(Constants::Regex::header_dataline_x));
91+
actual_diagram.SetAxisXTitle(match_results[1]);
92+
header_data_lines = match_results.suffix().str();
10793

108-
++column_index;
109-
headline = match_results.suffix().str();
94+
// Extracting the title of the Y axis
95+
while(std::regex_search(header_data_lines, match_results, std::regex(Constants::Regex::header_dataline_y)))
96+
{
97+
actual_diagram.AddNewDataLine(match_results[1], match_results[2]);
98+
header_data_lines = match_results.suffix().str();
11099
}
111-
112-
state = Constants::States::ProcessingDataLines;
100+
state = Constants::States::WaitingForHeaderMessageEnd;
113101
}
114102
else
115103
{
116-
state = Constants::States::WaitingForStartLine;
104+
state = Constants::States::WaitingForHeaderMessageStart;
117105
}
118106
break;
119-
case Constants::States::ProcessingDataLines:
120-
if(std::regex_match(actual_line, std::regex(Constants::Regex::data_line)))
107+
case Constants::States::WaitingForHeaderMessageEnd:
108+
// If a header message end line was found
109+
if(std::regex_match(actual_line, std::regex(Constants::Regex::header_end)))
110+
{
111+
state = Constants::States::WaitingForDataMessageStart;
112+
}
113+
else
114+
{
115+
state = Constants::States::WaitingForHeaderMessageStart;
116+
}
117+
break;
118+
case Constants::States::WaitingForDataMessageStart:
119+
// If a header message end line was found
120+
if(std::regex_match(actual_line, std::regex(Constants::Regex::data_start)))
121+
{
122+
state = Constants::States::ProcessingDataMessageContent;
123+
}
124+
else if(std::regex_match(actual_line, std::regex(Constants::Regex::tail)))
125+
{
126+
assembled_diagrams.push_back(actual_diagram);
127+
state = Constants::States::WaitingForHeaderMessageStart;
128+
}
129+
break;
130+
case Constants::States::ProcessingDataMessageContent:
131+
if(std::regex_match(actual_line, std::regex(Constants::Regex::data_datalines)))
121132
{
122133
std::string data_line = actual_line;
123134
DataIndexType column_index = 0;
124135
DataPointType data_point_x_value = 0;
125136

126137
// Collecting the data from the dataline
127-
while(std::regex_search(data_line, match_results, std::regex(Constants::Regex::data_line_analyzer)))
138+
while(std::regex_search(data_line, match_results, std::regex(Constants::Regex::data_datalines)))
128139
{
129140
if(0 == column_index)
130141
{
@@ -142,7 +153,7 @@ std::vector<DiagramSpecialized> ContinousMeasurementDataProtocol::ProcessData(st
142153
}
143154
else
144155
{
145-
state = Constants::States::WaitingForStartLine;
156+
state = Constants::States::WaitingForHeaderMessageStart;
146157
break;
147158
}
148159
}
@@ -152,20 +163,23 @@ std::vector<DiagramSpecialized> ContinousMeasurementDataProtocol::ProcessData(st
152163
}
153164
if((column_index - 1) != actual_diagram.GetTheNumberOfDataLines())
154165
{
155-
state = Constants::States::WaitingForStartLine;
166+
state = Constants::States::WaitingForHeaderMessageStart;
156167
}
157168
}
158169
else
159170
{
160-
if(std::regex_match(actual_line, std::regex(Constants::Regex::end_line)))
171+
if(std::regex_match(actual_line, std::regex(Constants::Regex::data_end)))
172+
{
173+
state = Constants::States::WaitingForDataMessageStart;
174+
}
175+
else
161176
{
162-
assembled_diagrams.push_back(actual_diagram);
177+
state = Constants::States::WaitingForHeaderMessageStart;
163178
}
164-
state = Constants::States::WaitingForStartLine;
165179
}
166180
break;
167181
default:
168-
state = Constants::States::WaitingForStartLine;
182+
state = Constants::States::WaitingForHeaderMessageStart;
169183
throw("The DataProcessor::ProcessData's statemachine switched to an unexpected state: " + std::to_string(static_cast<std::underlying_type<Constants::States>::type>(state)));
170184
break;
171185
}
@@ -175,15 +189,20 @@ std::vector<DiagramSpecialized> ContinousMeasurementDataProtocol::ProcessData(st
175189
throw("A regex exception was caught: " + std::to_string(exception.code()) + ": " + exception.what());
176190
}
177191
}
178-
*/
179192

180193
return assembled_diagrams;
181194
}
182195

183196
bool ContinousMeasurementDataProtocol::CanThisFileBeProcessed(const std::string path_to_file)
184197
{
185-
(void) path_to_file;
198+
bool bResult = false;
199+
200+
std::string file_extension = QFileInfo(QString::fromStdString(path_to_file)).completeSuffix().toStdString();
201+
202+
if(std::string(Constants::native_file_extension) == file_extension)
203+
{
204+
bResult = true;
205+
}
186206

187-
// The CMDP can not process files
188-
return false;
207+
return bResult;
189208
}

application/sources/continous_measurement_data_protocol.hpp

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <iostream>
2525
#include <memory>
2626
#include <string>
27+
#include <vector>
2728
#include <sstream>
2829
#include <algorithm>
2930
#include <functional>
@@ -35,6 +36,7 @@
3536
#include <QFileInfo>
3637

3738
#include "global.hpp"
39+
#include "protocol_interface.hpp"
3840
#include "data_processing_interface.hpp"
3941
#include "diagram.hpp"
4042

@@ -57,10 +59,10 @@ class ContinousMeasurementDataProtocol : public DataProcessingInterface
5759
ContinousMeasurementDataProtocol& operator=(const ContinousMeasurementDataProtocol&) = delete;
5860
ContinousMeasurementDataProtocol& operator=(ContinousMeasurementDataProtocol&&) = delete;
5961

60-
std::string GetProtocolName(void) override;
61-
std::vector<DiagramSpecialized> ProcessData(std::istream& input_data) override;
62-
bool CanThisFileBeProcessed(const std::string path_to_file) override;
63-
std::string GetSupportedFileType(void) override {return Constants::native_file_extension;}
62+
virtual std::string GetProtocolName(void) override;
63+
virtual std::vector<DiagramSpecialized> ProcessData(std::istream& input_data) override;
64+
virtual bool CanThisFileBeProcessed(const std::string path_to_file) override;
65+
virtual std::string GetSupportedFileType(void) override {return Constants::native_file_extension;}
6466

6567
private:
6668
struct Constants
@@ -71,12 +73,28 @@ class ContinousMeasurementDataProtocol : public DataProcessingInterface
7173
enum class States : uint8_t
7274
{
7375
INVALID = 0,
74-
WaitingForTransmission,
75-
HeaderStartReceived,
76-
HeaderContentReceived,
77-
HeaderEndReceived,
78-
DataStartReceived,
79-
DataEndReceived
76+
WaitingForHeaderMessageStart,
77+
ProcessingHeaderDiagramTitle,
78+
ProcessingHeaderDataLines,
79+
WaitingForHeaderMessageEnd,
80+
WaitingForDataMessageStart,
81+
ProcessingDataMessageContent,
82+
};
83+
84+
struct Regex
85+
{
86+
// REGEX strings to search the input data for valid measurement session
87+
static constexpr char header_start[] = R"(^<CMDP_H>$)";
88+
static constexpr char header_diagram_title[] = R"(^<.*>$)";
89+
static constexpr char header_datalines[] = R"(^X:[^,]*,(Y\d+:[^,]*,)+$)";
90+
static constexpr char header_dataline_x[] = R"(^X:([^,]*),)";
91+
static constexpr char header_dataline_y[] = R"(^Y(\d+):([^,]*),)";
92+
static constexpr char header_end[] = R"(^>CMDP_H<$)";
93+
static constexpr char data_start[] = R"(^<CMDP_D>$)";
94+
static constexpr char data_datalines[] = R"(^X:\"(\d+)\",(Y(\d):\"(\d+)\")+$)";
95+
static constexpr char data_end[] = R"(^>CMDP_D<$)";
96+
static constexpr char tail[] = R"(^<CMDP_T>$)";
97+
static constexpr char reset[] = R"(^<CMDP_R>$)";
8098
};
8199
};
82100

0 commit comments

Comments
 (0)