diff --git a/AlgorithmModule/src/CameraCheckAnalysisy.cpp b/AlgorithmModule/src/CameraCheckAnalysisy.cpp index e93f052..0ddf6e5 100644 --- a/AlgorithmModule/src/CameraCheckAnalysisy.cpp +++ b/AlgorithmModule/src/CameraCheckAnalysisy.cpp @@ -25,6 +25,7 @@ #include #include #include +#include static std::mutex g_ngImgSave_mutex; static std::vector> g_ngImgSave_futures; @@ -1239,43 +1240,52 @@ int CameraCheckAnalysisy::ngImgSave() { auto result_copy = m_pCheck_Result; std::string save_path_root = "/home/aidlux/BOE/FOG/cloud/"; - auto task = std::async(std::launch::async, [result_copy, save_path_root]() { + // 生成当天日期字符串,用于按天组织存储目录 + time_t now = time(nullptr); + struct tm tm_now; + localtime_r(&now, &tm_now); + char date_buf[16]; + strftime(date_buf, sizeof(date_buf), "%Y-%m-%d", &tm_now); + std::string date_str(date_buf); + + auto task = std::async(std::launch::async, [result_copy, save_path_root, date_str]() { try { if (!result_copy) return; // ---------- 目录清理(加锁防止并发删除)---------- + // 只保留最近 2 天的数据(今天 + 昨天),删除更早的日期目录 { static std::mutex cleanup_mutex; // 静态锁,所有实例共享 std::lock_guard lock(cleanup_mutex); + // 计算 1 天前的日期作为清理截止线(保留今天和昨天共 2 天) + time_t now_cleanup = time(nullptr); + time_t cutoff_time = now_cleanup - 1 * 24 * 3600; + struct tm tm_cutoff; + localtime_r(&cutoff_time, &tm_cutoff); + char cutoff_buf[16]; + strftime(cutoff_buf, sizeof(cutoff_buf), "%Y-%m-%d", &tm_cutoff); + std::string cutoff_str(cutoff_buf); + DIR* dir = opendir(save_path_root.c_str()); if (dir) { - std::vector> dirs; struct dirent* entry; while ((entry = readdir(dir)) != nullptr) { std::string name = entry->d_name; if (name == "." || name == "..") continue; - std::string full_path = save_path_root + name; - struct stat st; - if (stat(full_path.c_str(), &st) == 0 && S_ISDIR(st.st_mode)) { - struct stat lst; - if (lstat(full_path.c_str(), &lst) == 0 && !S_ISLNK(lst.st_mode)) { - dirs.emplace_back(full_path, st.st_mtime); + // 检查是否为日期格式目录 YYYY-MM-DD + if (name.length() == 10 && name[4] == '-' && name[7] == '-') { + // ISO 日期格式可按字典序直接比较 + if (name < cutoff_str) { + std::string full_path = save_path_root + name; + struct stat st; + if (stat(full_path.c_str(), &st) == 0 && S_ISDIR(st.st_mode)) { + CheckUtil::DeleteDir(full_path); + } } } } closedir(dir); - - if (dirs.size() > 100) { - std::sort(dirs.begin(), dirs.end(), - [](auto& a, auto& b) { return a.second < b.second; }); - const std::string& oldest_dir = dirs.front().first; - if (!oldest_dir.empty() && oldest_dir != save_path_root) { - CheckUtil::DeleteDir(oldest_dir); - } - } - } else { - cerr << ("ERROR: cannot open root directory for cleanup"); } } @@ -1304,8 +1314,8 @@ int CameraCheckAnalysisy::ngImgSave() { !m_CheckResult_shareP->resultimg.empty() && !m_CheckResult_shareP->cutSrcimg.empty()) { - // 构建保存目录(使用净化后的组件) - std::string save_dir = save_path_root + "/" + safe_product + "/" + safe_camera + "/"; + // 构建保存目录(使用净化后的组件,包含日期子目录) + std::string save_dir = save_path_root + date_str + "/" + safe_product + "/" + safe_camera + "/"; if (CheckUtil::CreateDir(save_dir) != 0) { cerr << ("Failed to create directory: " + save_dir); continue; @@ -1320,7 +1330,7 @@ int CameraCheckAnalysisy::ngImgSave() { // cv::imwrite(save_dir + safe_channel + "_AIMask.png", mask_img); // cv::imwrite(save_dir + safe_channel + "_Resultimg.png", result_img); // cv::imwrite(save_dir + safe_channel + "_CutImg.png", cut_img); - writeLog(save_dir+ safe_channel + "log", m_CheckResult_shareP->det_LogList); + writeLog(save_dir+ safe_channel + "_log", m_CheckResult_shareP->det_LogList); } catch (const cv::Exception& e) { cerr << ("OpenCV exception: " + std::string(e.what())); } catch (const std::exception& e) { diff --git a/AlgorithmModule/src/ImgCheckAnalysisy.cpp b/AlgorithmModule/src/ImgCheckAnalysisy.cpp index 7b72934..83351d4 100644 --- a/AlgorithmModule/src/ImgCheckAnalysisy.cpp +++ b/AlgorithmModule/src/ImgCheckAnalysisy.cpp @@ -170,7 +170,7 @@ int ImgCheckAnalysisy::GetStatus() std::string ImgCheckAnalysisy::GetVersion() { - return std::string("BOE_1.2.3_" + std::string(__DATE__) + "_" + std::string(__TIME__)); + return std::string("BOE_1.2.4_" + std::string(__DATE__) + "_" + std::string(__TIME__)); } std::string ImgCheckAnalysisy::GetErrorInfo()