Skip to content

Commit

Permalink
Add clean directory functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Dave committed Nov 15, 2024
1 parent 2d24677 commit d1560c9
Show file tree
Hide file tree
Showing 17 changed files with 1,097 additions and 326 deletions.
88 changes: 87 additions & 1 deletion doc/motionplus_config.html
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ <h2><a name="Configuration_OptionsTopic"></a> Configuration Options-Listed by T
<td bgcolor="#edf4f9" ><a href="#pause" >pause</a> </td>
</tr>
<td bgcolor="#edf4f9" ><a href="#schedule_params" >schedule_params</a> </td>
<td bgcolor="#edf4f9" ><a href="#cleandir_params" >cleandir_params</a> </td>
<td bgcolor="#edf4f9" ><a href="#watchdog_tmo" >watchdog_tmo</a> </td>
<td bgcolor="#edf4f9" ><a href="#watchdog_kill" >watchdog_kill</a> </td>
<tr>
</tr>
<td bgcolor="#edf4f9" ><a href="#target_dir" >target_dir</a> </td>
<td bgcolor="#edf4f9" ><a href="#camera" >camera</a> </td>
<td bgcolor="#edf4f9" ><a href="#config_dir" >config_dir</a> </td>
<tr>
Expand Down Expand Up @@ -895,6 +895,92 @@ <h3><a name="schedule_params"></a>schedule_params</h3>
</ul>
<p></p>

<h3><a name="cleandir_params"></a>cleandir_params</h3>
<ul>
<li> Values: String | Default: Not Defined</li>
Comma separated list of configuration parameters. Format is: option=value,option2=value2
</ul>
<ul>
<p></p>
This parameter specifies the information used for purging older recording and pictures created by Motionplus. This option
is only applicable if a database has been specified. The default for this parameter is an empty string which means that
no clean directory actions will be performed.
<p></p>

<div>
<i><h4>action</h4></i>
String specifying either <code><small>delete</small></code> or <code><small>script</small></code>. If the option
of <code><small>script</small></code> is specified, then the provided script will be executed at the specified
interval. If the option of <code><small>delete</small></code> is specified, then all files that were created
by Motionplus will be deleted from the system according to the schedule. The default for this
parameter is <code><small>delete</small></code>
</div>
<p></p>

<div>
<i><h4>freq</h4></i>
This parameter specifies the frequency that the clean directory will be run. Valid options for this parameter
are <code><small>hourly</small></code>, <code><small>daily</small></code> or <code><small>weekly</small></code>.
The default value for this parameter is <code><small>weekly</small></code>.
</div>
<p></p>

<div>
<i><h4>runtime</h4></i>
This parameter specifies the timing for running the clean directory actions. The format of this value
varies depending upon the frequency specified.
<ul>
<li><code><small>hourly</small></code> | Specify the minute of the hour using the format mm. e.g. 05 for 5 minutes past each hour</li>
<li><code><small>daily</small></code> | Specify the hour and minute using the format hhmm. e.g. 1420 for 2:20pm</li>
<li><code><small>weekly</small></code> | Specify the day, hour and minute using the format dow-hhmm. e.g. tue-1420 for Tuesdays at 2:20pm</li>
</ul>
</div>
<p></p>

<div>
<i><h4>dur_unit</h4></i>
This parameter specifies the unit of duration for the criteria used to select files. Valid options for this parameter
are <code><small>m</small></code>, <code><small>h</small></code>,<code><small>d</small></code> or <code><small>w</small></code>. For
minutes, hours, days or weeks. The default value for this parameter is <code><small>d</small></code>. As an
example, if the <code><small>dur_unit=d</small></code>
and the <code><small>dur_val=7</small></code> then all files older than 7 days would be deleted.
</div>
<p></p>

<div>
<i><h4>dur_val</h4></i>
This parameter specifies the number associated with the duration units for the criteria used to select files.
The default value for this parameter is <code><small>7</small></code>. As an example, if the <code><small>dur_unit=d</small></code>
and the <code><small>dur_val=7</small></code> then all files older than 7 days would be deleted.
</div>
<p></p>

<div>
<i><h4>removedir</h4></i>
This parameter is a boolean that specifies whether the associated directory (only one level up) should be
removed if there are no longer any files in it after the clean process has run. If there are other files
in the directory that are not created by Motionplus, then the directory will not be removed.
</div>
<p></p>

<div>
<i><h4>script</h4></i>
This parameter specifies the full path to a script to run at the scheduled time.
</div>
<p></p>

<div>
<i><h4> params_file </h4></i>
<ul>
<li> Values: String | Default: Not Defined </li>
Full path and file name for a file containing the params listed one per line.
</ul>
</div>
<p></p>

</ul>
<p></p>

<h3><a name="watchdog_tmo"></a>watchdog_tmo</h3>
<ul>
<li> Values: Integer | Default: 90 </li>
Expand Down
248 changes: 248 additions & 0 deletions src/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ void cls_camera::init_values()
movie_timelapse = nullptr;
movie_extpipe = nullptr;
draw = nullptr;
cleandir = nullptr;

gethostname (hostname, PATH_MAX);
hostname[PATH_MAX-1] = '\0';
Expand Down Expand Up @@ -746,6 +747,7 @@ void cls_camera::cleanup()
mydelete(movie_timelapse);
mydelete(movie_extpipe);
mydelete(draw);
mydelete(cleandir);

if (pipe != -1) {
close(pipe);
Expand All @@ -759,6 +761,248 @@ void cls_camera::cleanup()

}

void cls_camera::init_cleandir_default()
{
if ((cleandir->action != "delete") &&
(cleandir->action != "script")) {
MOTPLS_LOG(ERR, TYPE_ALL, NO_ERRNO
,_("Invalid clean directory action : %s")
,cleandir->action.c_str());
cleandir->action = "";
}
if (cleandir->action == "") {
MOTPLS_LOG(NTC, TYPE_ALL, NO_ERRNO
,_("Setting default clean directory action to delete."));
cleandir->action = "delete";
}

if ((cleandir->freq != "hourly") &&
(cleandir->freq != "daily") &&
(cleandir->freq != "weekly")) {
MOTPLS_LOG(ERR, TYPE_ALL, NO_ERRNO
,_("Invalid clean directory freq : %s")
,cleandir->freq.c_str());
cleandir->freq = "";
}
if (cleandir->freq == "") {
MOTPLS_LOG(NTC, TYPE_ALL, NO_ERRNO
,_("Setting default clean directory frequency to weekly."));
cleandir->freq = "weekly";
}

if (cleandir->runtime == "") {
if (cleandir->freq == "hourly") {
MOTPLS_LOG(NTC, TYPE_ALL, NO_ERRNO
,_("Setting default clean directory runtime to 15."));
cleandir->runtime = "15";
} else if (cleandir->freq == "daily") {
MOTPLS_LOG(NTC, TYPE_ALL, NO_ERRNO
,_("Setting default clean directory runtime to 0115."));
cleandir->runtime = "0115";
} else if (cleandir->freq == "weekly") {
MOTPLS_LOG(NTC, TYPE_ALL, NO_ERRNO
,_("Setting default clean directory runtime to mon-0115."));
cleandir->runtime = "mon-0115";
}
}

if ((cleandir->dur_unit != "m") &&
(cleandir->dur_unit != "h") &&
(cleandir->dur_unit != "d") &&
(cleandir->dur_unit != "w")) {
MOTPLS_LOG(ERR, TYPE_ALL, NO_ERRNO
,_("Invalid clean directory duration unit : %s")
,cleandir->dur_unit.c_str());
cleandir->dur_unit = "";
}
if (cleandir->dur_unit == "") {
MOTPLS_LOG(NTC, TYPE_ALL, NO_ERRNO
,_("Setting default clean directory duration unit to d."));
cleandir->dur_unit = "d";
}

if ((cleandir->dur_val == 0 )) {
MOTPLS_LOG(ERR, TYPE_ALL, NO_ERRNO
,_("Invalid clean directory duration number : %d")
,cleandir->dur_val);
cleandir->dur_val = -1;
}
if (cleandir->dur_val <= 0) {
MOTPLS_LOG(NTC, TYPE_ALL, NO_ERRNO
,_("Setting default clean directory duration value to 7."));
cleandir->dur_val = 7;
}
}

void cls_camera::init_cleandir_runtime()
{
std::string ptst,punit,premove;
struct tm c_tm;
struct timespec curr_ts;
int p_min, p_hr, p_dow;

clock_gettime(CLOCK_REALTIME, &curr_ts);
localtime_r(&curr_ts.tv_sec, &c_tm);

cleandir->next_ts.tv_nsec = 0;
cleandir->next_ts.tv_sec = curr_ts.tv_sec;

if (cleandir->freq == "hourly") {
punit = "hours";
if (cleandir->runtime.length() < 2) {
p_min = -1;
} else {
p_min = mtoi(cleandir->runtime);
}
if ((p_min > 59) || (p_min < 0)) {
MOTPLS_LOG(ERR, TYPE_ALL, NO_ERRNO
,_("Invalid clean directory hourly runtime : %s")
,cleandir->runtime.c_str());
cleandir->runtime = "";
p_min = 15;
}
cleandir->next_ts.tv_sec += ((p_min - c_tm.tm_min) * 60);
if (cleandir->next_ts.tv_sec < curr_ts.tv_sec) {
cleandir->next_ts.tv_sec += 3600;
}

} else if (cleandir->freq == "daily") {
punit = "days";
if (cleandir->runtime.length() < 4) {
p_min = -1;
} else {
p_hr = mtoi(cleandir->runtime.substr(0,2));
p_min = mtoi(cleandir->runtime.substr(2,2));
}
if ((p_min > 59) || (p_min < 0) ||
(p_hr > 23) || (p_hr < 0)) {
MOTPLS_LOG(ERR, TYPE_ALL, NO_ERRNO
,_("Invalid clean directory daily runtime : %s")
,cleandir->runtime.c_str());
cleandir->runtime = "";
p_min = 15;
p_hr = 1;
}
cleandir->next_ts.tv_sec += ((p_min - c_tm.tm_min) * 60);
cleandir->next_ts.tv_sec += ((p_hr - c_tm.tm_hour) * 3600);
if (cleandir->next_ts.tv_sec < curr_ts.tv_sec) {
cleandir->next_ts.tv_sec += (60 * 60 * 24);
}

} else if (cleandir->freq == "weekly") {
punit = "weeks";
if (cleandir->runtime.length() < 8) {
p_min = -1;
} else {
ptst = cleandir->runtime.substr(0,3); /*"mon-0115"*/
if (ptst == "sun") { p_dow = 0;
} else if (ptst == "mon") { p_dow = 1;
} else if (ptst == "tue") { p_dow = 2;
} else if (ptst == "wed") { p_dow = 3;
} else if (ptst == "thu") { p_dow = 4;
} else if (ptst == "fri") { p_dow = 5;
} else if (ptst == "sat") { p_dow = 6;
} else { p_dow= -1;
}
p_hr = mtoi(cleandir->runtime.substr(4,2));
p_min = mtoi(cleandir->runtime.substr(6,2));
}
if ((p_min > 59) || (p_min < 0) ||
(p_hr > 23) || (p_hr < 0) ||
(p_dow == -1)) {
MOTPLS_LOG(ERR, TYPE_ALL, NO_ERRNO
,_("Invalid clean directory weekly runtime : %s")
,cleandir->runtime.c_str());
cleandir->runtime = "";
p_min = 15;
p_hr = 1;
p_dow = 1;
}
cleandir->next_ts.tv_sec += ((p_min - c_tm.tm_min) * 60);
cleandir->next_ts.tv_sec += ((p_hr - c_tm.tm_hour) * 3600);
cleandir->next_ts.tv_sec += ((p_dow - c_tm.tm_wday) * 86400);
if (cleandir->next_ts.tv_sec < curr_ts.tv_sec) {
cleandir->next_ts.tv_sec += (60 * 60 * 24 * 7);
}
}

localtime_r(&cleandir->next_ts.tv_sec, &c_tm);
cleandir->next_ts.tv_sec -= c_tm.tm_sec;

if (cleandir->action == "delete") {
MOTPLS_LOG(INF, TYPE_ALL, NO_ERRNO
, _("Cleandir next run:%04d-%02d-%02d %02d:%02d Criteria:%d%s RemoveDir:%s")
,c_tm.tm_year+1900,c_tm.tm_mon+1,c_tm.tm_mday
,c_tm.tm_hour,c_tm.tm_min
,cleandir->dur_val
,cleandir->dur_unit.c_str()
,cleandir->removedir ? "Y":"N");
} else {
MOTPLS_LOG(INF, TYPE_ALL, NO_ERRNO
, _("Clean directory set to run script at %04d-%02d-%02d %02d:%02d")
,c_tm.tm_year+1900,c_tm.tm_mon+1,c_tm.tm_mday
,c_tm.tm_hour,c_tm.tm_min);
}

}

void cls_camera::init_cleandir()
{
int indx;
ctx_params *params;
std::string pnm, pvl;

if (cfg->cleandir_params == "") {
return;
}

params = new ctx_params;
util_parms_parse(params, "cleandir_params", cfg->cleandir_params);

if (params->params_cnt == 0) {
mydelete(params);
return;
}

cleandir = new ctx_cleandir;
cleandir->action = "delete";
cleandir->freq = "weekly";
cleandir->script = "";
cleandir->runtime = "mon-0115";
cleandir->removedir = false;
cleandir->dur_unit = "w";
cleandir->dur_val = 2;

for (indx=0; indx<params->params_cnt; indx++) {
pnm = params->params_array[indx].param_name;
pvl = params->params_array[indx].param_value;
if (pnm == "runtime") {
cleandir->runtime = pvl;
}
if (pnm == "freq") {
cleandir->freq = pvl;
}
if (pnm == "action") {
cleandir->action = pvl;
}
if (pnm == "script") {
cleandir->script = pvl;
}
if (pnm == "dur_val") {
cleandir->dur_val =mtoi(pvl);
}
if (pnm == "dur_unit") {
cleandir->dur_unit = pvl;
}
if (pnm == "removedir") {
cleandir->removedir = mtob(pvl);
}
}
init_cleandir_default();
init_cleandir_runtime();

}

void cls_camera::init_schedule()
{
int indx, indx1;
Expand All @@ -776,6 +1020,7 @@ void cls_camera::init_schedule()
util_parms_parse(params, "schedule_params", cfg->schedule_params);

if (params->params_cnt == 0) {
mydelete(params);
return;
}

Expand Down Expand Up @@ -938,6 +1183,8 @@ void cls_camera::init()
movie_timelapse = new cls_movie(this, "timelapse");
movie_extpipe = new cls_movie(this, "extpipe");

init_cleandir();

init_schedule();

init_areadetect();
Expand Down Expand Up @@ -1767,6 +2014,7 @@ cls_camera::cls_camera(cls_motapp *p_app)
user_pause = false;
missing_frame_counter = -1;
schedule.clear();
cleandir = nullptr;

info_diff_tot = 0;
info_diff_cnt = 0;
Expand Down
Loading

0 comments on commit d1560c9

Please sign in to comment.