2024-03-04 16:22:40 +08:00
|
|
|
#include "CheckStorageThread.h"
|
|
|
|
#include "Constant.h"
|
2024-08-12 11:26:42 +08:00
|
|
|
#include "DatabaseManager.h"
|
2024-03-04 16:22:40 +08:00
|
|
|
#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;
|
2024-08-12 11:26:42 +08:00
|
|
|
extern DatabaseManager* db;
|
2024-03-04 16:22:40 +08:00
|
|
|
|
2024-12-24 03:51:48 +08:00
|
|
|
CheckStorageThread::CheckStorageThread(QObject* parent)
|
|
|
|
: QThread(parent)
|
2024-03-04 16:22:40 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-03-05 13:47:32 +08:00
|
|
|
* @brief 10分钟检查一次外接硬盘的可用空间
|
2024-03-04 16:22:40 +08:00
|
|
|
*/
|
|
|
|
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...");
|
2024-05-07 15:06:36 +08:00
|
|
|
emit diskWillFull();
|
2024-08-12 11:26:42 +08:00
|
|
|
// 从数据库中取出前两条数据,找到相关的文件删除
|
|
|
|
QList<DatabaseManager::File> fileList = db->getTopTwo();
|
|
|
|
for (auto& file : fileList) {
|
2024-08-23 13:34:58 +08:00
|
|
|
QString filename = file.filename;
|
2024-08-12 11:26:42 +08:00
|
|
|
QString channel = file.channel == DatabaseManager::MainChannel
|
|
|
|
? Constant::MainChannel
|
|
|
|
: Constant::SecondaryChannel;
|
2024-03-05 13:47:32 +08:00
|
|
|
// 判断文件是否再回放,如果在回放就阻塞等待
|
2024-03-04 16:22:40 +08:00
|
|
|
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();
|
2024-03-05 13:47:32 +08:00
|
|
|
// 删除文件
|
2024-08-12 11:26:42 +08:00
|
|
|
QString path = QString("%1/%2/%3").arg(Constant::VideoPath).arg(channel).arg(filename);
|
2024-03-04 16:22:40 +08:00
|
|
|
bool ret = Tool::removeFile(path);
|
|
|
|
if (!ret) {
|
|
|
|
Log::error("remove file {} failed", path.toStdString());
|
2024-05-11 15:40:07 +08:00
|
|
|
} else {
|
|
|
|
Log::info("remove file {} success", path.toStdString());
|
2024-03-04 16:22:40 +08:00
|
|
|
}
|
2024-08-12 11:26:42 +08:00
|
|
|
// 从数据库清除这条记录
|
2024-08-23 13:34:58 +08:00
|
|
|
db->remove(file.channel, file.filename);
|
2024-03-04 16:22:40 +08:00
|
|
|
}
|
2024-05-07 15:06:36 +08:00
|
|
|
} else {
|
|
|
|
emit diskNotFull();
|
2024-03-04 16:22:40 +08:00
|
|
|
}
|
|
|
|
QThread::msleep(INTERVAL);
|
|
|
|
}
|
|
|
|
}
|