|
| 1 | +//==============================================================================// |
| 2 | +// // |
| 3 | +// RDB Diplomaterv Monitor // |
| 4 | +// A monitor program for the RDB Diplomaterv project // |
| 5 | +// Copyright (C) 2018 András Gergő Kocsis // |
| 6 | +// // |
| 7 | +// This program is free software: you can redistribute it and/or modify // |
| 8 | +// it under the terms of the GNU General Public License as published by // |
| 9 | +// the Free Software Foundation, either version 3 of the License, or // |
| 10 | +// (at your option) any later version. // |
| 11 | +// // |
| 12 | +// This program is distributed in the hope that it will be useful, // |
| 13 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of // |
| 14 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
| 15 | +// GNU General Public License for more details. // |
| 16 | +// // |
| 17 | +// You should have received a copy of the GNU General Public License // |
| 18 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. // |
| 19 | +// // |
| 20 | +//==============================================================================// |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +#include "continous_measurement_data_protocol.hpp" |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +ContinousMeasurementDataProtocol::ContinousMeasurementDataProtocol() |
| 29 | +{ |
| 30 | + state = Constants::States::WaitingForTransmission; |
| 31 | +} |
| 32 | + |
| 33 | +std::string ContinousMeasurementDataProtocol::GetProtocolName(void) |
| 34 | +{ |
| 35 | + return std::string(Constants::protocol_name); |
| 36 | +} |
| 37 | + |
| 38 | +std::vector<DiagramSpecialized> ContinousMeasurementDataProtocol::ProcessData(std::istream& input_data) |
| 39 | +{ |
| 40 | + std::vector<DiagramSpecialized> assembled_diagrams; |
| 41 | + std::string received_data; |
| 42 | + std::string actual_line; |
| 43 | + |
| 44 | + /* |
| 45 | + while(std::getline(input_data, actual_line)) |
| 46 | + { |
| 47 | + std::smatch match_results; |
| 48 | +
|
| 49 | + // Removing the whitespaces from the actual line |
| 50 | + actual_line.erase(std::remove_if(actual_line.begin(), actual_line.end(), isspace), actual_line.end()); |
| 51 | +
|
| 52 | + try |
| 53 | + { |
| 54 | + switch(state) |
| 55 | + { |
| 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))) |
| 59 | + { |
| 60 | + state = Constants::States::ProcessingTitleLine; |
| 61 | + } |
| 62 | + 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))) |
| 68 | + { |
| 69 | + // Then we create a diagram object with the title |
| 70 | + actual_diagram = DiagramSpecialized(match_results[1]); |
| 71 | + // Switching to the next state with a break --> a new line will be fetched |
| 72 | + break; |
| 73 | + } |
| 74 | + else |
| 75 | + { |
| 76 | + // No title was found, we will generate a title from the current date and time and create a diagram object with it |
| 77 | + auto current_date_and_time = std::time(nullptr); |
| 78 | + std::string current_date_and_time_string = ctime(¤t_date_and_time); |
| 79 | + // The ctime adds an extra newline to the string, this needs to be removed |
| 80 | + current_date_and_time_string.pop_back(); |
| 81 | + actual_diagram = DiagramSpecialized(current_date_and_time_string); |
| 82 | + // Switching to the next state without a break --> a new line will NOT be fetched, because this line is the headline |
| 83 | + } |
| 84 | +
|
| 85 | + // 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)))) |
| 92 | + { |
| 93 | + std::string headline = actual_line; |
| 94 | + DataIndexType column_index = 0; |
| 95 | +
|
| 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 | + } |
| 107 | +
|
| 108 | + ++column_index; |
| 109 | + headline = match_results.suffix().str(); |
| 110 | + } |
| 111 | +
|
| 112 | + state = Constants::States::ProcessingDataLines; |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + state = Constants::States::WaitingForStartLine; |
| 117 | + } |
| 118 | + break; |
| 119 | + case Constants::States::ProcessingDataLines: |
| 120 | + if(std::regex_match(actual_line, std::regex(Constants::Regex::data_line))) |
| 121 | + { |
| 122 | + std::string data_line = actual_line; |
| 123 | + DataIndexType column_index = 0; |
| 124 | + DataPointType data_point_x_value = 0; |
| 125 | +
|
| 126 | + // Collecting the data from the dataline |
| 127 | + while(std::regex_search(data_line, match_results, std::regex(Constants::Regex::data_line_analyzer))) |
| 128 | + { |
| 129 | + if(0 == column_index) |
| 130 | + { |
| 131 | + std::stringstream stringstream(match_results[1]); |
| 132 | + stringstream >> data_point_x_value; |
| 133 | + } |
| 134 | + else |
| 135 | + { |
| 136 | + if((column_index - 1) < actual_diagram.GetTheNumberOfDataLines()) |
| 137 | + { |
| 138 | + std::stringstream stringstream(match_results[1]); |
| 139 | + DataPointType data_point_y_value; |
| 140 | + stringstream >> data_point_y_value; |
| 141 | + actual_diagram.AddNewDataPoint((column_index - 1), DataPointSpecialized(data_point_x_value, data_point_y_value)); |
| 142 | + } |
| 143 | + else |
| 144 | + { |
| 145 | + state = Constants::States::WaitingForStartLine; |
| 146 | + break; |
| 147 | + } |
| 148 | + } |
| 149 | +
|
| 150 | + ++column_index; |
| 151 | + data_line = match_results.suffix().str(); |
| 152 | + } |
| 153 | + if((column_index - 1) != actual_diagram.GetTheNumberOfDataLines()) |
| 154 | + { |
| 155 | + state = Constants::States::WaitingForStartLine; |
| 156 | + } |
| 157 | + } |
| 158 | + else |
| 159 | + { |
| 160 | + if(std::regex_match(actual_line, std::regex(Constants::Regex::end_line))) |
| 161 | + { |
| 162 | + assembled_diagrams.push_back(actual_diagram); |
| 163 | + } |
| 164 | + state = Constants::States::WaitingForStartLine; |
| 165 | + } |
| 166 | + break; |
| 167 | + default: |
| 168 | + state = Constants::States::WaitingForStartLine; |
| 169 | + throw("The DataProcessor::ProcessData's statemachine switched to an unexpected state: " + std::to_string(static_cast<std::underlying_type<Constants::States>::type>(state))); |
| 170 | + break; |
| 171 | + } |
| 172 | + } |
| 173 | + catch(const std::regex_error& exception) |
| 174 | + { |
| 175 | + throw("A regex exception was caught: " + std::to_string(exception.code()) + ": " + exception.what()); |
| 176 | + } |
| 177 | + } |
| 178 | + */ |
| 179 | + |
| 180 | + return assembled_diagrams; |
| 181 | +} |
| 182 | + |
| 183 | +bool ContinousMeasurementDataProtocol::CanThisFileBeProcessed(const std::string path_to_file) |
| 184 | +{ |
| 185 | + (void) path_to_file; |
| 186 | + |
| 187 | + // The CMDP can not process files |
| 188 | + return false; |
| 189 | +} |
0 commit comments