// 支持原始数据log文件导入并计算定位结果,并将结果按原始数据日期写入数据库 // 曾敏果 #include "load_raw.h" #include #include extern config_file config; void load_raw::listDir(const char *path, vector& vec_file_path) { DIR *pDir; struct dirent *ent; int i = 0; char childpath[512]; pDir = opendir(path); memset(childpath, 0, sizeof(childpath)); while ((ent = readdir(pDir)) != NULL) { if (ent->d_type & DT_DIR) { if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) continue; sprintf(childpath, "%s/%s", path, ent->d_name); listDir(childpath, vec_file_path); } else { char file_full_path[512]; memset(file_full_path, 0, sizeof(file_full_path)); sprintf(file_full_path, "%s/%s", path, ent->d_name); vec_file_path.push_back(file_full_path); } } } bool load_raw::load_folder(string folder_path) { vector vec_file_path; listDir(folder_path.c_str(), vec_file_path); for (int i = 0; i < vec_file_path.size(); i++) { load_file(vec_file_path[i]); } } bool load_raw::load_file(std::string file_path) { FILE *filePointer; int i = 0; char buffer[FILEBUFFER_LENGTH]; char *temp; raw_length = 0; m_index = -1; m_vec_date.clear(); memset(buffer, '\0', FILEBUFFER_LENGTH * sizeof(char)); strcpy(buffer, EMPTY_STR); memset(raw, '\0', FILEBUFFER_LENGTH * sizeof(char)); strcpy(raw, EMPTY_STR); if (!(filePointer = fopen(file_path.c_str(), "rb"))) { return 0; } while (!feof(filePointer)) { memset(buffer, '\0', FILEBUFFER_LENGTH * sizeof(char)); strcpy(buffer, EMPTY_STR); if (!fgets(buffer, FILEBUFFER_LENGTH, filePointer)) { return 0; } if (0 == strcmp(buffer, "\n")) { if (raw_length != 0) { logn_bin(1, "load raw: ", raw, raw_length);//输出二进制日志 int n_delay_times = 0; m_vec_date.push_back(raw_time); //连接本机的采集监听端口,模拟向其发送原始数据,这样定位算法部分就不需要单独为文件做特定的处理逻辑,简化问题。 write(c_fd, raw, raw_length); raw_length = 0; memset(raw, '\0', FILEBUFFER_LENGTH * sizeof(char)); strcpy(raw, EMPTY_STR); } } else if (0 != strcmp(buffer, EMPTY_STR)) { deal_with_time(buffer); deal_with_raw(buffer); } } if (0 != fclose(filePointer)) { return 0; } return 1; } bool load_raw::init() { m_port = config.get("service.port", 4000); struct sockaddr_in c_addr; memset(&c_addr, 0, sizeof(struct sockaddr_in)); //1.socket int socket(int domain, int type, int protocol); c_fd = socket(AF_INET, SOCK_STREAM, 0); if (c_fd == -1) { return false; } //2.connect int connect(int sockfd, const struct sockaddr *addr,socklen_t addrlen); c_addr.sin_family = AF_INET; c_addr.sin_port = htons(m_port); inet_aton("127.0.0.1", &c_addr.sin_addr); if (connect(c_fd, (struct sockaddr *)&c_addr, sizeof(struct sockaddr_in)) == -1) { return false; } return true; } load_raw::load_raw() { m_port = 0; init(); } std::vector load_raw::m_vec_date; std::string load_raw::m_date; bool load_raw::m_is_history = false; int load_raw::m_index = -1; bool load_raw::deal_with_time(std::string text_line) { int pos = -1; if (text_line.find('[')!=-1) { if ((pos = text_line.find('.')) != -1) { raw_time = text_line.substr(0, pos); } } } bool load_raw::deal_with_raw(std::string text_line) { //将原始数据文本转成二级制数组 if (text_line.find('[') == -1 && text_line != EMPTY_STR)//非时间、非空行,即为原始数据 { int pos = -1; int hex = 0; while ((pos = text_line.find(' ', pos + 1)) != -1) { if (pos - 2 >= 0) { string hex_string = "0x"; hex_string += text_line.substr(pos - 2, 2); sscanf(hex_string.c_str(),"%x", &hex); raw[raw_length++] = hex; } } } }