Reset Mechanism
This commit is contained in:
1 parent
5eb122f422
commit
428f2a1140
8 files changed
+3330
-42
No files matched your search
+53
-13
@@ -8,31 +8,68 @@
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
VideoBatchWriter::VideoBatchWriter(const std::string& shm_dir,
|
||||
const std::string& output_dir,
|
||||
int w, int h, int fps,
|
||||
float delay_sec,
|
||||
float end_delay_sec)
|
||||
float end_delay_sec,
|
||||
int retention_days)
|
||||
: shm_dir_(shm_dir), output_dir_(output_dir),
|
||||
w_(w), h_(h), fps_(fps),
|
||||
delay_sec_(delay_sec), end_delay_sec_(end_delay_sec)
|
||||
delay_sec_(delay_sec), end_delay_sec_(end_delay_sec),
|
||||
retention_days_(retention_days)
|
||||
{
|
||||
ensure_dir(shm_dir_);
|
||||
ensure_dir(output_dir_);
|
||||
|
||||
for (const auto& entry : std::filesystem::directory_iterator(shm_dir_)) {
|
||||
for (const auto& entry : fs::directory_iterator(shm_dir_)) {
|
||||
if (entry.path().extension() == ".mp4") {
|
||||
std::filesystem::remove(entry.path());
|
||||
fs::remove(entry.path());
|
||||
}
|
||||
}
|
||||
|
||||
cleanup_retention();
|
||||
}
|
||||
|
||||
VideoBatchWriter::~VideoBatchWriter() {
|
||||
shutdown();
|
||||
}
|
||||
|
||||
void VideoBatchWriter::cleanup_retention() {
|
||||
auto cutoff = std::chrono::system_clock::now() - std::chrono::hours(retention_days_ * 24);
|
||||
auto cutoff_t = std::chrono::system_clock::to_time_t(cutoff);
|
||||
std::ostringstream cutoff_ss;
|
||||
cutoff_ss << std::put_time(std::gmtime(&cutoff_t), "%Y-%m-%d");
|
||||
std::string cutoff_str = cutoff_ss.str();
|
||||
|
||||
for (const auto& entry : fs::directory_iterator(output_dir_)) {
|
||||
if (!entry.is_directory()) continue;
|
||||
std::string dirname = entry.path().filename().string();
|
||||
if (dirname < cutoff_str) {
|
||||
std::error_code ec;
|
||||
fs::remove_all(entry.path(), ec);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void VideoBatchWriter::ensure_dir(const std::string& dir) {
|
||||
std::filesystem::create_directories(dir);
|
||||
fs::create_directories(dir);
|
||||
}
|
||||
|
||||
static std::string today_str() {
|
||||
auto t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
|
||||
std::ostringstream ss;
|
||||
ss << std::put_time(std::localtime(&t), "%Y-%m-%d");
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
static std::string timestamp_str() {
|
||||
auto t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
|
||||
std::ostringstream ss;
|
||||
ss << std::put_time(std::localtime(&t), "%H%M%S");
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
void VideoBatchWriter::note_motion() {
|
||||
@@ -114,7 +151,7 @@ static void copy_and_clean(const std::string& src, const std::string& dst) {
|
||||
}
|
||||
}
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(src, ec);
|
||||
fs::remove(src, ec);
|
||||
}
|
||||
|
||||
void VideoBatchWriter::stop_and_delete() {
|
||||
@@ -124,7 +161,7 @@ void VideoBatchWriter::stop_and_delete() {
|
||||
if (!current_path_.empty()) {
|
||||
std::cout << "[RECORD] Discarding: " << current_path_ << std::endl;
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(current_path_, ec);
|
||||
fs::remove(current_path_, ec);
|
||||
current_path_.clear();
|
||||
}
|
||||
}
|
||||
@@ -138,11 +175,12 @@ void VideoBatchWriter::stop_and_move(int batch_number) {
|
||||
std::string src = current_path_;
|
||||
current_path_.clear();
|
||||
|
||||
auto t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
|
||||
std::string day_dir = output_dir_ + "/" + today_str();
|
||||
ensure_dir(day_dir);
|
||||
|
||||
std::ostringstream name;
|
||||
name << "batch_" << batch_number << "_"
|
||||
<< std::put_time(std::localtime(&t), "%Y%m%d_%H%M%S") << ".mp4";
|
||||
std::string dst = output_dir_ + "/" + name.str();
|
||||
name << "batch_" << batch_number << "_" << timestamp_str() << ".mp4";
|
||||
std::string dst = day_dir + "/" + name.str();
|
||||
|
||||
pid_t pid = fork();
|
||||
if (pid == 0) {
|
||||
@@ -162,8 +200,10 @@ void VideoBatchWriter::shutdown() {
|
||||
|
||||
if (!current_path_.empty()) {
|
||||
std::string src = current_path_;
|
||||
std::string dst = output_dir_ + "/" +
|
||||
std::filesystem::path(current_path_).filename().string();
|
||||
std::string day_dir = output_dir_ + "/" + today_str();
|
||||
ensure_dir(day_dir);
|
||||
std::string dst = day_dir + "/" +
|
||||
fs::path(current_path_).filename().string();
|
||||
current_path_.clear();
|
||||
|
||||
pid_t pid = fork();
|
||||
|
||||
Reference in new issue
Block a user