#include "CheckStorageThread.h" #include "Constant.h" #include "DatabaseManager.h" #include "Log.h" #include "Tool.h" #include #include #include // 磁盘剩余最小容量 #define THRESHOLD 10 * 1024 * 1024 // 检查间隔 #define INTERVAL 10 * 60 * 1000 extern QString curFilename; extern QMutex mutex; extern QWaitCondition condition; extern DatabaseManager* db; CheckStorageThread::CheckStorageThread(QObject* parent) : QThread(parent) { } /** * @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..."); emit diskWillFull(); // 从数据库中取出前两条数据,找到相关的文件删除 QList fileList = db->getTopTwo(); for (auto& file : fileList) { QString filename = file.filename; QString channel = file.channel == DatabaseManager::MainChannel ? Constant::MainChannel : Constant::SecondaryChannel; // 判断文件是否再回放,如果在回放就阻塞等待 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(channel).arg(filename); bool ret = Tool::removeFile(path); if (!ret) { Log::error("remove file {} failed", path.toStdString()); } else { Log::info("remove file {} success", path.toStdString()); } // 从数据库清除这条记录 db->remove(file.channel, file.filename); } } else { emit diskNotFull(); } QThread::msleep(INTERVAL); } }