|
|
|
|
@ -25,7 +25,6 @@
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <mutex>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <ctime>
|
|
|
|
|
|
|
|
|
|
static std::mutex g_ngImgSave_mutex;
|
|
|
|
|
static std::vector<std::future<void>> g_ngImgSave_futures;
|
|
|
|
|
@ -1240,52 +1239,43 @@ int CameraCheckAnalysisy::ngImgSave() {
|
|
|
|
|
auto result_copy = m_pCheck_Result;
|
|
|
|
|
std::string save_path_root = "/home/aidlux/BOE/FOG/cloud/";
|
|
|
|
|
|
|
|
|
|
// 生成当天日期字符串,用于按天组织存储目录
|
|
|
|
|
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]() {
|
|
|
|
|
auto task = std::async(std::launch::async, [result_copy, save_path_root]() {
|
|
|
|
|
try {
|
|
|
|
|
if (!result_copy) return;
|
|
|
|
|
|
|
|
|
|
// ---------- 目录清理(加锁防止并发删除)----------
|
|
|
|
|
// 只保留最近 2 天的数据(今天 + 昨天),删除更早的日期目录
|
|
|
|
|
{
|
|
|
|
|
static std::mutex 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());
|
|
|
|
|
if (dir) {
|
|
|
|
|
std::vector<std::pair<std::string, time_t>> dirs;
|
|
|
|
|
struct dirent* entry;
|
|
|
|
|
while ((entry = readdir(dir)) != nullptr) {
|
|
|
|
|
std::string name = entry->d_name;
|
|
|
|
|
if (name == "." || name == "..") continue;
|
|
|
|
|
// 检查是否为日期格式目录 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);
|
|
|
|
|
}
|
|
|
|
|
struct stat lst;
|
|
|
|
|
if (lstat(full_path.c_str(), &lst) == 0 && !S_ISLNK(lst.st_mode)) {
|
|
|
|
|
dirs.emplace_back(full_path, st.st_mtime);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -1314,8 +1304,8 @@ int CameraCheckAnalysisy::ngImgSave() {
|
|
|
|
|
!m_CheckResult_shareP->resultimg.empty() &&
|
|
|
|
|
!m_CheckResult_shareP->cutSrcimg.empty()) {
|
|
|
|
|
|
|
|
|
|
// 构建保存目录(使用净化后的组件,包含日期子目录)
|
|
|
|
|
std::string save_dir = save_path_root + date_str + "/" + safe_product + "/" + safe_camera + "/";
|
|
|
|
|
// 构建保存目录(使用净化后的组件)
|
|
|
|
|
std::string save_dir = save_path_root + "/" + safe_product + "/" + safe_camera + "/";
|
|
|
|
|
if (CheckUtil::CreateDir(save_dir) != 0) {
|
|
|
|
|
cerr << ("Failed to create directory: " + save_dir);
|
|
|
|
|
continue;
|
|
|
|
|
@ -1330,7 +1320,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) {
|
|
|
|
|
|