Advertisement
Queen4

Parsing logs GLGSV 1.1

Feb 9th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <unordered_set>
  5. #include <map>
  6.  
  7. // Переменные для поиска времени
  8. struct MainTime {
  9.     int hour = 0;
  10.     int nexthour = 0;
  11.     int minute = 0;
  12.     int nextminute = 0;
  13.     float sec = 0.0;
  14.     float nextsec = 0.0;
  15.     float sum = 0.0;
  16.     float SNRs = 0.0;
  17. };
  18.  
  19. // Cкладывает по принципу XOR
  20. int XOR(const char* s) {
  21.     int c = 0;
  22.  
  23.     while (*s)
  24.         c ^= *s++;
  25.  
  26.     return c;
  27. }
  28.  
  29. int main() {
  30.     int comma, key; // Переменная для подсчета запятых и проверки контрольной суммы
  31.     //Структуры обработки времени
  32.     MainTime time;
  33.  
  34.     // Переменные для работы со строками
  35.     char linec_h[220];
  36.     std::string line, key_s;
  37.     // Работа с сетом номеров спутника
  38.     std::unordered_set<std::string> Set; //сет номеров спутника
  39.     std::string SatelliteNumber;
  40.     // Работа со среднем значением времени к каждому SNR
  41.     int SNRnumber;
  42.     std::map <int, float[2]> Map; // Map с SNR, временем и количеством времени
  43.  
  44.     std::ifstream logs_("C:/Users/Ivan/Desktop/resetLog_2.log"); // Откуда берем данные
  45.     std::ofstream pout("C:/Users/Ivan/Desktop/oldSNR2.txt"); // Куда загружаем данные о спутниках
  46.     std::ofstream tout("C:/Users/Ivan/Desktop/oldAvarage Time2.txt"); // Куда загружаем данные о среднем времени
  47.  
  48.     if (logs_.is_open()) {
  49.         std::cout << "Please wait." << std::endl;
  50.         while (getline(logs_, line)) {
  51.  
  52.             if (line.substr(0, 25) == "%off%set,lock/glo/fcn,off"/*"RE005%off%"*/) {
  53.                 pout << "End of cycle" << std::endl;
  54.             }
  55.             else if (line.substr(0, 25) == "%%set,lock/galileo/sat,on"/*"RE004%on%"*/) { // Если строка с включением, то
  56.                 pout << "Time of work: " << time.sum << " sec" << std::endl; // Выводим время работы цикла
  57.                 // Обнуляем переменные времени
  58.                 time.hour = 0;
  59.                 time.minute = 0;
  60.                 time.sec = 0.0;
  61.                 time.nexthour = 0;
  62.                 time.nextminute = 0;
  63.                 time.nextsec = 0.0;
  64.                 // Очищаем сет
  65.                 Set.clear();
  66.             }
  67.             else {
  68.                 //Проверяем контрольную сумму
  69.                 key_s = line.substr(line.length() - 2, 2);
  70.                 key = strtol(key_s.c_str(), nullptr, 16);
  71.                 line = line.substr(1, line.length() - 4);              
  72.                 strcpy_s(linec_h, line.c_str());
  73.                 if (line != /*"E00"*/"%"
  74.                     && line != "%set,lock/beidou/sat,"
  75.                     && line != "%set,lock/galileo/sat,"
  76.                     && line != "on%set,lock/glo/fcn"
  77.                     && line != "%set,lock/beidou/sat"
  78.                     && key != XOR(linec_h)) std::cout << "Broken line:" << std::endl << line << std::endl && pout << "Line is corrupted!" << std::endl;
  79.                 else { //Если выполняется, то
  80.                     comma = 0;
  81.                     // Находим время, переводим в секунды, находим отношение первого значения и всех следующих
  82.                     if (line.substr(0, 5) == "GPGGA") {
  83.                         if (time.hour == 0 && time.minute == 0 && time.sec == 0.0) {
  84.                             time.hour = stoi(line.substr(6, 2)) * 3600;
  85.                             time.minute = stoi(line.substr(8, 2)) * 60;
  86.                             time.sec = stof(line.substr(10, 4));
  87.                         }
  88.                         else {
  89.                             time.nexthour = stoi(line.substr(6, 2)) * 3600 - time.hour;
  90.                             time.nextminute = stoi(line.substr(8, 2)) * 60 - time.minute;
  91.                             time.nextsec = stof(line.substr(10, 4)) - time.sec;
  92.                         }
  93.                         time.sum = time.nexthour + time.nextminute + time.nextsec;
  94.                     }
  95.                     // Если строка GPGSV/GLGSV
  96.                     else if (line.substr(0, 5) == "GLGSV") {
  97.                        
  98.                         // Проходимся по строке
  99.                         for (size_t i = 0, N = 4, SNR = 7; i < line.size(); i++) {
  100.                             // Считаем количество запятых
  101.                             if (line[i] == ',') comma++;
  102.                             // При нахождении нужного количества
  103.                             if (comma == N) {
  104.                                 SatelliteNumber = line.substr(i - 1, 2);
  105.                                 ;
  106.                             }
  107.                             else if (comma == SNR) {
  108.                                 if (Set.find(SatelliteNumber) == Set.end()) { // Если значение встречается в первый раз, то
  109.                                     SNRnumber = stoi(line.substr(i + 1, 2)); // Присваиваем соответсвующий ему номер SNR
  110.                                    
  111.                                         if (Map.find(SNRnumber) == Map.end()) { // Если номера нет в map
  112.                                             Map[SNRnumber][0] += time.sum;      // в 0 хранится текущее среднее
  113.                                             Map[SNRnumber][1]++;                // в 1 хранится кол-во повторений SNR                                                                      
  114.                                         }
  115.                                         else {  // Иначе считаем среднее время
  116.                                             Map[SNRnumber][0] *= Map[SNRnumber][1];
  117.                                             Map[SNRnumber][0] += time.sum;
  118.                                             Map[SNRnumber][1]++;
  119.                                             Map[SNRnumber][0] /= Map[SNRnumber][1];
  120.                                         }
  121.                                         // Выводим значения
  122.                                         pout << "Satellite name: " << SatelliteNumber << "  " << "SNR: " << SNRnumber << "  Elapsed time : " << time.sum << " sec" << std::endl;
  123.                                         Set.insert(SatelliteNumber); // Добавляем его в сет                                
  124.                                 }
  125.                                 // Идем дальше по строке
  126.                                 N += 4;
  127.                                 SNR += 4;
  128.                             }
  129.                         }
  130.                     }
  131.                 }
  132.                
  133.             }
  134.         }
  135.         logs_.close();
  136.         // Выводим содержимое map
  137.         for (auto & it : Map) {
  138.             tout << "SNR:  " << it.first << "  Average time: " << it.second[0] << " sec   Number of iterations: " << it.second[1] <<  std::endl;
  139.         }
  140.         std::cout << "Success" << std::endl;
  141.     }
  142.     else std::cout << "File is not open" << std::endl;
  143.     pout.close();
  144.     tout.close();
  145.  
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement