Train_RFID_Linux/engine/DelExpiredEngine/DelExpiredEngine.cpp

199 lines
5.6 KiB
C++
Raw Normal View History

#include "DelExpiredEngine.h"
using namespace ai_matrix;
DelExpiredEngine::DelExpiredEngine() {}
DelExpiredEngine::~DelExpiredEngine() {}
APP_ERROR DelExpiredEngine::Init()
{
strPort0_ = engineName_ + "_" + std::to_string(engineId_) + "_0";
this->baseConfig_ = Config::GetIns()->getBaseConfig();
LogInfo << "DelExpiredEngine Init ok";
return APP_ERR_OK;
}
APP_ERROR DelExpiredEngine::DeInit()
{
LogInfo << "DelExpiredEngine DeInit ok";
return APP_ERR_OK;
}
APP_ERROR DelExpiredEngine::Process()
{
int iRet = APP_ERR_OK;
while (!isStop_)
{
//pop端口0 车厢信息
std::shared_ptr<void> pVoidData0 = nullptr;
iRet = inputQueMap_[strPort0_]->pop(pVoidData0);
if (nullptr == pVoidData0)
{
usleep(1000);
continue;
}
std::shared_ptr<TrainInfo> pTrain = std::static_pointer_cast<TrainInfo>(pVoidData0);
std::string strTrainDate_temp = TimeUtil::getins()->getDate();
DeletePreviousFolder(this->baseConfig_.strRfidPath, strTrainDate_temp, this->baseConfig_.iResultSaveDays);
DeletePreviousFolder(this->baseConfig_.strResultPath, strTrainDate_temp, this->baseConfig_.iResultSaveDays);
usleep(1000*1000*3600*24); //每二十四小时执行一次
}
return APP_ERR_OK;
}
void DelExpiredEngine::getSubfolderNames(std::string &directory, std::vector<Date> &folder_names)
{
if (directory.back() != '/')
directory += "/";
DIR *dir;
struct dirent *ent;
if ((dir = opendir(directory.c_str())) != nullptr)
{
while ((ent = readdir(dir)) != nullptr)
{
// 排除"."和".."
if (ent->d_type == DT_DIR && ent->d_name[0] != '.' && ent->d_name == "best")
{
folder_names.push_back(StrToDate(ent->d_name));
}
}
closedir(dir);
}
else
{
LogError << "Unable to open directory: " << directory;
}
}
void DelExpiredEngine::DeletePreviousFolder(std::string path, const std::string &date, int n_days)
{
// 1 computer date
std::string previous_date = getDateBeforeNDays(date, n_days);
if (!previous_date.empty())
std::cout << "Date before " << n_days << " days from " << date << " is: " << previous_date << std::endl;
// 2
std::vector<Date> subfolders;
getSubfolderNames(path, subfolders);
// for (const auto &it : subfolders)
// std::cout << it.year << "." << it.month << "." << it.day << std::endl;
// 3 delete
if (path.back() != '/')
path += "/";
Date reference_date = StrToDate(previous_date); // 给定的参考日期
DeleteEarlierDatesFolder(path, subfolders, reference_date);
}
// 获取某月有多少天
int DelExpiredEngine::DaysInMonth(int year, int month)
{
int max_days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
{
max_days[2] = 29; // 闰年2月有29天
}
return max_days[month];
}
// 解析字符串为日期结构体
Date DelExpiredEngine::StrToDate(const std::string &date_str)
{
std::istringstream iss(date_str);
int year, month, day;
char dash;
if (!(iss >> year >> dash && dash == '-' &&
iss >> month >> dash && dash == '-' &&
iss >> day))
{
LogError << ("Invalid date format") << ":" << date_str;
}
return {year, month, day};
}
// 减去指定天数
void DelExpiredEngine::SubtractDays(Date &date, int n_days)
{
while (n_days > 0)
{
date.day--;
n_days--;
if (date.day == 0)
{
if (--date.month == 0)
{
--date.year;
date.month = 12;
}
int max_days = DaysInMonth(date.year, date.month);
date.day = max_days;
}
}
}
// 格式化日期结构体为字符串
std::string DelExpiredEngine::DateToStr(const Date &date)
{
std::ostringstream oss;
oss << date.year << "-" << std::setfill('0') << std::setw(2) << date.month << "-" << std::setw(2) << date.day;
return oss.str();
}
// 主要功能函数接收一个日期字符串和一个整数n返回n天前的日期字符串
std::string DelExpiredEngine::getDateBeforeNDays(const std::string &input_date, int n_days)
{
try
{
Date date = StrToDate(input_date);
SubtractDays(date, n_days);
return DateToStr(date);
}
catch (const std::exception &e)
{
LogError << "Error: " << e.what();
return "";
}
}
void DelExpiredEngine::DeleteFolder(const std::string directory)
{
std::string command = "rm -rf " + directory;
int result = system(command.c_str());
if (result != 0)
std::cout << "Failed to remove directory recursively: " << directory << std::endl;
else
std::cout << "delete folder successfully : " << directory << std::endl;
}
// 删除向量中小于指定日期的所有元素
void DelExpiredEngine::DeleteEarlierDatesFolder(std::string &path, std::vector<Date> &subfolders, const Date &reference_date)
{
if (path.back() != '/')
path += "/";
for (const Date &cur : subfolders)
{
// bool flag = false;
if (cur.year < reference_date.year)
{
DeleteFolder(path + DateToStr(cur));
}
else if (cur.year == reference_date.year && cur.month < reference_date.month)
{
DeleteFolder(path + DateToStr(cur));
}
else if (cur.year == reference_date.year && cur.month == reference_date.month && cur.day < reference_date.day)
{
DeleteFolder(path + DateToStr(cur));
}
}
}