Compare commits

..

2 Commits

@ -25,6 +25,7 @@
#include <vector> #include <vector>
#include <mutex> #include <mutex>
#include <algorithm> #include <algorithm>
#include <ctime>
static std::mutex g_ngImgSave_mutex; static std::mutex g_ngImgSave_mutex;
static std::vector<std::future<void>> g_ngImgSave_futures; static std::vector<std::future<void>> g_ngImgSave_futures;
@ -1239,43 +1240,52 @@ int CameraCheckAnalysisy::ngImgSave() {
auto result_copy = m_pCheck_Result; auto result_copy = m_pCheck_Result;
std::string save_path_root = "/home/aidlux/BOE/FOG/cloud/"; 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 { try {
if (!result_copy) return; if (!result_copy) return;
// ---------- 目录清理(加锁防止并发删除)---------- // ---------- 目录清理(加锁防止并发删除)----------
// 只保留最近 2 天的数据(今天 + 昨天),删除更早的日期目录
{ {
static std::mutex cleanup_mutex; // 静态锁,所有实例共享 static std::mutex cleanup_mutex; // 静态锁,所有实例共享
std::lock_guard<std::mutex> lock(cleanup_mutex); std::lock_guard<std::mutex> 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()); DIR* dir = opendir(save_path_root.c_str());
if (dir) { if (dir) {
std::vector<std::pair<std::string, time_t>> dirs;
struct dirent* entry; struct dirent* entry;
while ((entry = readdir(dir)) != nullptr) { while ((entry = readdir(dir)) != nullptr) {
std::string name = entry->d_name; std::string name = entry->d_name;
if (name == "." || name == "..") continue; if (name == "." || name == "..") continue;
std::string full_path = save_path_root + name; // 检查是否为日期格式目录 YYYY-MM-DD
struct stat st; if (name.length() == 10 && name[4] == '-' && name[7] == '-') {
if (stat(full_path.c_str(), &st) == 0 && S_ISDIR(st.st_mode)) { // ISO 日期格式可按字典序直接比较
struct stat lst; if (name < cutoff_str) {
if (lstat(full_path.c_str(), &lst) == 0 && !S_ISLNK(lst.st_mode)) { std::string full_path = save_path_root + name;
dirs.emplace_back(full_path, st.st_mtime); struct stat st;
if (stat(full_path.c_str(), &st) == 0 && S_ISDIR(st.st_mode)) {
CheckUtil::DeleteDir(full_path);
}
} }
} }
} }
closedir(dir); 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->resultimg.empty() &&
!m_CheckResult_shareP->cutSrcimg.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) { if (CheckUtil::CreateDir(save_dir) != 0) {
cerr << ("Failed to create directory: " + save_dir); cerr << ("Failed to create directory: " + save_dir);
continue; continue;
@ -1320,7 +1330,7 @@ int CameraCheckAnalysisy::ngImgSave() {
// cv::imwrite(save_dir + safe_channel + "_AIMask.png", mask_img); // 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 + "_Resultimg.png", result_img);
// cv::imwrite(save_dir + safe_channel + "_CutImg.png", cut_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) { } catch (const cv::Exception& e) {
cerr << ("OpenCV exception: " + std::string(e.what())); cerr << ("OpenCV exception: " + std::string(e.what()));
} catch (const std::exception& e) { } catch (const std::exception& e) {

Loading…
Cancel
Save