2026-04-25 19:01:54 +08:00
|
|
|
|
#ifndef DEBUG_DATA_SAVER_H
|
|
|
|
|
|
#define DEBUG_DATA_SAVER_H
|
|
|
|
|
|
|
2026-05-25 00:28:08 +08:00
|
|
|
|
#include <cstdint>
|
2026-04-25 19:01:54 +08:00
|
|
|
|
#include <string>
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
#include <mutex>
|
|
|
|
|
|
#include <condition_variable>
|
|
|
|
|
|
#include <queue>
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
|
#include "VZNL_Types.h"
|
|
|
|
|
|
|
|
|
|
|
|
class DebugDataSaver
|
|
|
|
|
|
{
|
|
|
|
|
|
public:
|
|
|
|
|
|
explicit DebugDataSaver(int maxThreads = 4);
|
|
|
|
|
|
~DebugDataSaver();
|
|
|
|
|
|
|
|
|
|
|
|
// 异步保存点云数据(内部深拷贝数据,不阻塞调用线程)
|
|
|
|
|
|
// timestamp: 检测时刻的时间戳,格式 YYYYMMDD_HHmmss
|
|
|
|
|
|
// 调用方需在持有数据锁时调用,DeepCopyData 会在调用线程完成深拷贝
|
|
|
|
|
|
void SaveAsync(const std::string& basePath,
|
|
|
|
|
|
const std::string& timestamp,
|
|
|
|
|
|
const std::vector<std::pair<EVzResultDataType, SVzLaserLineData>>& data,
|
|
|
|
|
|
const std::string& deviceName = "");
|
|
|
|
|
|
|
|
|
|
|
|
// 停止所有工作线程(等待队列中任务完成后退出)
|
|
|
|
|
|
void Stop();
|
|
|
|
|
|
|
2026-05-25 00:28:08 +08:00
|
|
|
|
// 设置 PointCloud 目录最大总大小(字节)。超过后保存前会循环删除最早的文件。默认 10GB
|
|
|
|
|
|
void SetMaxStorageSize(int64_t bytes) { m_maxStorageSize = bytes; }
|
|
|
|
|
|
|
2026-04-25 19:01:54 +08:00
|
|
|
|
private:
|
|
|
|
|
|
struct SaveTask {
|
|
|
|
|
|
std::string filePath;
|
|
|
|
|
|
std::string deviceName;
|
|
|
|
|
|
std::vector<std::pair<EVzResultDataType, SVzLaserLineData>> data;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void WorkerThread();
|
|
|
|
|
|
|
|
|
|
|
|
// 深拷贝检测数据(含原始指针数组的完整拷贝)
|
|
|
|
|
|
static std::vector<std::pair<EVzResultDataType, SVzLaserLineData>> DeepCopyData(
|
|
|
|
|
|
const std::vector<std::pair<EVzResultDataType, SVzLaserLineData>>& src);
|
|
|
|
|
|
|
|
|
|
|
|
// 释放深拷贝的数据
|
|
|
|
|
|
static void FreeData(std::vector<std::pair<EVzResultDataType, SVzLaserLineData>>& data);
|
|
|
|
|
|
|
|
|
|
|
|
// 生成带日期目录的完整保存路径: basePath/YYYYMMDD/pointcloud[_deviceName]_YYYYMMDD_HHmmss_NNN.txt
|
|
|
|
|
|
// timestamp: 检测时刻的时间戳,格式 YYYYMMDD_HHmmss
|
|
|
|
|
|
// deviceName: 设备名称(为空时不包含在文件名中)
|
|
|
|
|
|
std::string GenerateSavePath(const std::string& basePath, const std::string& timestamp, const std::string& deviceName = "");
|
|
|
|
|
|
|
2026-05-25 00:28:08 +08:00
|
|
|
|
// 确保 basePath 总大小不超过 m_maxStorageSize,超过时循环删除最早的文件
|
|
|
|
|
|
// basePath: SaveAsync 传入的根目录(不含日期子目录)
|
|
|
|
|
|
void EnsureStorageLimit(const std::string& basePath);
|
|
|
|
|
|
|
|
|
|
|
|
// 统计 rootPath 下(含子目录)所有 .txt 文件的总大小(字节)
|
|
|
|
|
|
static int64_t ComputeDirSize(const std::string& rootPath);
|
|
|
|
|
|
|
|
|
|
|
|
// 查找 rootPath 下(含子目录)按修改时间排序最早的 .txt 文件,找不到返回空串
|
|
|
|
|
|
static std::string FindOldestFile(const std::string& rootPath);
|
|
|
|
|
|
|
2026-04-25 19:01:54 +08:00
|
|
|
|
int m_maxThreads;
|
|
|
|
|
|
std::vector<std::thread> m_workers;
|
|
|
|
|
|
std::queue<SaveTask> m_taskQueue;
|
|
|
|
|
|
std::mutex m_queueMutex;
|
|
|
|
|
|
std::condition_variable m_queueCondition;
|
|
|
|
|
|
std::atomic<bool> m_running;
|
2026-05-25 00:28:08 +08:00
|
|
|
|
|
|
|
|
|
|
// 磁盘空间维护:多个 worker 共享,必须串行
|
|
|
|
|
|
std::mutex m_diskSpaceMutex;
|
|
|
|
|
|
std::atomic<int64_t> m_maxStorageSize{10LL * 1024 * 1024 * 1024}; // 默认 10GB
|
2026-04-25 19:01:54 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // DEBUG_DATA_SAVER_H
|