load_raw.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // 支持原始数据log文件导入并计算定位结果,并将结果按原始数据日期写入数据库
  2. // 曾敏果
  3. #include "load_raw.h"
  4. #include <config_file.h>
  5. #include<thread>
  6. extern config_file config;
  7. void load_raw::listDir(const char *path, vector<string>& vec_file_path)
  8. {
  9. DIR *pDir;
  10. struct dirent *ent;
  11. int i = 0;
  12. char childpath[512];
  13. pDir = opendir(path);
  14. memset(childpath, 0, sizeof(childpath));
  15. while ((ent = readdir(pDir)) != NULL)
  16. {
  17. if (ent->d_type & DT_DIR)
  18. {
  19. if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
  20. continue;
  21. sprintf(childpath, "%s/%s", path, ent->d_name);
  22. listDir(childpath, vec_file_path);
  23. }
  24. else
  25. {
  26. char file_full_path[512];
  27. memset(file_full_path, 0, sizeof(file_full_path));
  28. sprintf(file_full_path, "%s/%s", path, ent->d_name);
  29. vec_file_path.push_back(file_full_path);
  30. }
  31. }
  32. }
  33. bool load_raw::load_folder(string folder_path)
  34. {
  35. vector<string> vec_file_path;
  36. listDir(folder_path.c_str(), vec_file_path);
  37. for (int i = 0; i < vec_file_path.size(); i++)
  38. {
  39. load_file(vec_file_path[i]);
  40. }
  41. }
  42. bool load_raw::load_file(std::string file_path)
  43. {
  44. FILE *filePointer;
  45. int i = 0;
  46. char buffer[FILEBUFFER_LENGTH];
  47. char *temp;
  48. raw_length = 0;
  49. m_index = -1;
  50. m_vec_date.clear();
  51. memset(buffer, '\0', FILEBUFFER_LENGTH * sizeof(char));
  52. strcpy(buffer, EMPTY_STR);
  53. memset(raw, '\0', FILEBUFFER_LENGTH * sizeof(char));
  54. strcpy(raw, EMPTY_STR);
  55. if (!(filePointer = fopen(file_path.c_str(), "rb")))
  56. {
  57. return 0;
  58. }
  59. while (!feof(filePointer))
  60. {
  61. memset(buffer, '\0', FILEBUFFER_LENGTH * sizeof(char));
  62. strcpy(buffer, EMPTY_STR);
  63. if (!fgets(buffer, FILEBUFFER_LENGTH, filePointer))
  64. {
  65. return 0;
  66. }
  67. if (0 == strcmp(buffer, "\n"))
  68. {
  69. if (raw_length != 0)
  70. {
  71. logn_bin(1, "load raw: ", raw, raw_length);//输出二进制日志
  72. int n_delay_times = 0;
  73. m_vec_date.push_back(raw_time);
  74. //连接本机的采集监听端口,模拟向其发送原始数据,这样定位算法部分就不需要单独为文件做特定的处理逻辑,简化问题。
  75. write(c_fd, raw, raw_length);
  76. raw_length = 0;
  77. memset(raw, '\0', FILEBUFFER_LENGTH * sizeof(char));
  78. strcpy(raw, EMPTY_STR);
  79. }
  80. }
  81. else if (0 != strcmp(buffer, EMPTY_STR))
  82. {
  83. deal_with_time(buffer);
  84. deal_with_raw(buffer);
  85. }
  86. }
  87. if (0 != fclose(filePointer))
  88. {
  89. return 0;
  90. }
  91. return 1;
  92. }
  93. bool load_raw::init()
  94. {
  95. m_port = config.get("service.port", 4000);
  96. struct sockaddr_in c_addr;
  97. memset(&c_addr, 0, sizeof(struct sockaddr_in));
  98. //1.socket int socket(int domain, int type, int protocol);
  99. c_fd = socket(AF_INET, SOCK_STREAM, 0);
  100. if (c_fd == -1) {
  101. return false;
  102. }
  103. //2.connect int connect(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
  104. c_addr.sin_family = AF_INET;
  105. c_addr.sin_port = htons(m_port);
  106. inet_aton("127.0.0.1", &c_addr.sin_addr);
  107. if (connect(c_fd, (struct sockaddr *)&c_addr, sizeof(struct sockaddr_in)) == -1) {
  108. return false;
  109. }
  110. return true;
  111. }
  112. load_raw::load_raw()
  113. {
  114. m_port = 0;
  115. init();
  116. }
  117. std::vector<std::string> load_raw::m_vec_date;
  118. std::string load_raw::m_date;
  119. bool load_raw::m_is_history = false;
  120. int load_raw::m_index = -1;
  121. bool load_raw::deal_with_time(std::string text_line)
  122. {
  123. int pos = -1;
  124. if (text_line.find('[')!=-1)
  125. {
  126. if ((pos = text_line.find('.')) != -1)
  127. {
  128. raw_time = text_line.substr(0, pos);
  129. }
  130. }
  131. }
  132. bool load_raw::deal_with_raw(std::string text_line)
  133. {
  134. //将原始数据文本转成二级制数组
  135. if (text_line.find('[') == -1 && text_line != EMPTY_STR)//非时间、非空行,即为原始数据
  136. {
  137. int pos = -1;
  138. int hex = 0;
  139. while ((pos = text_line.find(' ', pos + 1)) != -1)
  140. {
  141. if (pos - 2 >= 0)
  142. {
  143. string hex_string = "0x";
  144. hex_string += text_line.substr(pos - 2, 2);
  145. sscanf(hex_string.c_str(),"%x", &hex);
  146. raw[raw_length++] = hex;
  147. }
  148. }
  149. }
  150. }