RecordControlApplication/CheckStorageThread.cpp

59 lines
2.0 KiB
C++
Executable File

#include "CheckStorageThread.h"
#include "Constant.h"
#include "Log.h"
#include "Tool.h"
#include <QFile>
#include <QMutex>
#include <QWaitCondition>
// 磁盘剩余最小容量
#define THRESHOLD 10 * 1024 * 1024
// 检查间隔
#define INTERVAL 10 * 60 * 1000
extern QString curFilename;
extern QMutex mutex;
extern QWaitCondition condition;
CheckStorageThread::CheckStorageThread()
{
}
/**
* @brief 10分钟检查一次外接硬盘的可用空间
*/
void CheckStorageThread::run()
{
while (true) {
int64_t available = Tool::getAvailableStorage(Constant::MountedPath);
if (available < THRESHOLD) {
Log::info("there are not enough storage, then remove some files...");
// 获取文件列表
QStringList fileList = Tool::getFileList(QString("%1/%2").arg(Constant::VideoPath).arg(Constant::MainChannel));
if (!fileList.isEmpty()) {
QString filename = fileList.first();
// 判断文件是否再回放,如果在回放就阻塞等待
mutex.lock();
if (filename == curFilename) {
Log::info("{} is playing, wait for play end and remove the files...", filename.toStdString());
condition.wait(&mutex);
Log::info("check thread end wait...");
}
mutex.unlock();
// 删除文件
QString path = QString("%1/%2/%3").arg(Constant::VideoPath).arg(Constant::MainChannel).arg(filename);
bool ret = Tool::removeFile(path);
if (!ret) {
Log::error("remove file {} failed", path.toStdString());
}
path = QString("%1/%2/%3").arg(Constant::VideoPath).arg(Constant::SecondaryChannel).arg(filename);
ret = Tool::removeFile(path);
if (!ret) {
Log::error("remove file {} failed", path.toStdString());
}
}
}
QThread::msleep(INTERVAL);
}
}