2022-09-22 20:57:46 -04:00
|
|
|
// This file is part of Motion Watch.
|
|
|
|
|
|
|
|
// Motion Watch is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Motion Watch is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
string cleanDir(const string &path)
|
|
|
|
{
|
|
|
|
if (path[path.size() - 1] == '/')
|
|
|
|
{
|
|
|
|
return path.substr(0, path.size() - 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool createDir(const string &dir)
|
|
|
|
{
|
|
|
|
auto ret = mkdir(dir.c_str(), 0777);
|
|
|
|
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
return errno == EEXIST;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool createDirTree(const string &full_path)
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
auto ret = true;
|
|
|
|
|
|
|
|
while (ret == true && pos != string::npos)
|
|
|
|
{
|
|
|
|
pos = full_path.find('/', pos + 1);
|
|
|
|
ret = createDir(full_path.substr(0, pos));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool fileExists(const string& name)
|
|
|
|
{
|
|
|
|
return access(name.c_str(), F_OK) != -1;
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:13:39 -05:00
|
|
|
string replaceAll(string str, const string &from, const string &to)
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
2022-12-04 15:13:39 -05:00
|
|
|
if (from.empty()) return str;
|
2022-09-22 20:57:46 -04:00
|
|
|
|
|
|
|
size_t startPos = 0;
|
|
|
|
|
2022-12-04 15:13:39 -05:00
|
|
|
while ((startPos = str.find(from, startPos)) != string::npos)
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
|
|
|
str.replace(startPos, from.length(), to);
|
|
|
|
|
|
|
|
startPos += to.length();
|
|
|
|
}
|
2022-12-04 15:13:39 -05:00
|
|
|
|
|
|
|
return str;
|
2022-09-22 20:57:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
vector<string> lsFilesInDir(const string &path, const string &ext)
|
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *ent;
|
|
|
|
vector<string> names;
|
|
|
|
|
|
|
|
if ((dir = opendir(path.c_str())) != NULL)
|
|
|
|
{
|
|
|
|
while ((ent = readdir(dir)) != NULL)
|
|
|
|
{
|
2022-12-04 15:13:39 -05:00
|
|
|
if (ent->d_type & DT_REG)
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
2022-12-04 15:13:39 -05:00
|
|
|
auto name = string(ent->d_name);
|
|
|
|
|
|
|
|
if (name.ends_with(ext.c_str()) || ext.empty())
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
|
|
|
names.push_back(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
sort(names.begin(), names.end());
|
|
|
|
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:13:39 -05:00
|
|
|
vector<string> lsDirsInDir(const string &path)
|
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *ent;
|
|
|
|
vector<string> names;
|
|
|
|
|
|
|
|
if ((dir = opendir(path.c_str())) != NULL)
|
|
|
|
{
|
|
|
|
while ((ent = readdir(dir)) != NULL)
|
|
|
|
{
|
|
|
|
if (ent->d_type & DT_DIR)
|
|
|
|
{
|
2022-12-11 10:25:22 -05:00
|
|
|
auto name = string(ent->d_name);
|
|
|
|
|
|
|
|
if ((name != "..") || (name != "."))
|
|
|
|
{
|
|
|
|
names.push_back(name);
|
|
|
|
}
|
2022-12-04 15:13:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
sort(names.begin(), names.end());
|
|
|
|
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
|
|
|
void enforceMaxDays(const string &dirPath, shared_t *share)
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
2022-12-04 15:13:39 -05:00
|
|
|
auto names = lsDirsInDir(dirPath);
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2022-12-04 15:13:39 -05:00
|
|
|
while (names.size() > (share->maxDays - 1))
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
2022-12-04 15:13:39 -05:00
|
|
|
remove_all(string(cleanDir(dirPath) + "/" + names[0]).c_str());
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2022-12-04 15:13:39 -05:00
|
|
|
names.erase(names.begin());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void enforceMaxClips(const string &dirPath, shared_t *share)
|
|
|
|
{
|
|
|
|
auto names = lsDirsInDir(dirPath);
|
|
|
|
|
|
|
|
while ((names.size() * 3) > ((share->maxClips - 1) * 3))
|
|
|
|
{
|
|
|
|
remove(string(cleanDir(dirPath) + "/" + names[0]).c_str());
|
|
|
|
remove(string(cleanDir(dirPath) + "/" + names[1]).c_str());
|
|
|
|
remove(string(cleanDir(dirPath) + "/" + names[2]).c_str());
|
2022-09-22 20:57:46 -04:00
|
|
|
|
|
|
|
names.erase(names.begin());
|
2022-12-04 15:13:39 -05:00
|
|
|
names.erase(names.begin());
|
|
|
|
names.erase(names.begin());
|
2022-09-22 20:57:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
string genTimeStr(const char *fmt)
|
|
|
|
{
|
|
|
|
time_t rawtime;
|
|
|
|
|
|
|
|
time(&rawtime);
|
|
|
|
|
|
|
|
auto timeinfo = localtime(&rawtime);
|
|
|
|
|
|
|
|
char ret[50];
|
|
|
|
|
|
|
|
strftime(ret, 50, fmt, timeinfo);
|
|
|
|
|
|
|
|
return string(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
string genDstFile(const string &dirOut, const char *fmt, const string &ext)
|
|
|
|
{
|
|
|
|
createDirTree(cleanDir(dirOut));
|
|
|
|
|
|
|
|
return cleanDir(dirOut) + string("/") + genTimeStr(fmt) + ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rdLine(const string ¶m, const string &line, string *value)
|
|
|
|
{
|
|
|
|
if (line.rfind(param.c_str(), 0) == 0)
|
|
|
|
{
|
|
|
|
*value = line.substr(param.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void rdLine(const string ¶m, const string &line, int *value)
|
|
|
|
{
|
|
|
|
if (line.rfind(param.c_str(), 0) == 0)
|
|
|
|
{
|
|
|
|
*value = strtol(line.substr(param.size()).c_str(), NULL, 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool rdConf(shared_t *share)
|
|
|
|
{
|
|
|
|
ifstream varFile(share->conf.c_str());
|
|
|
|
|
|
|
|
if (!varFile.is_open())
|
|
|
|
{
|
|
|
|
share->retCode = ENOENT;
|
|
|
|
|
|
|
|
cerr << "err: Failed to open the config file: " << share->conf << " for reading. please check file permissions or if it exists." << endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
string line;
|
|
|
|
|
|
|
|
share->recordUrl.clear();
|
|
|
|
share->postCmd.clear();
|
|
|
|
share->buffDir.clear();
|
|
|
|
|
2022-12-11 10:25:22 -05:00
|
|
|
share->retCode = 0;
|
2022-12-04 15:13:39 -05:00
|
|
|
share->frameGap = 10;
|
|
|
|
share->pixThresh = 30;
|
|
|
|
share->imgThresh = 512;
|
|
|
|
share->secs = 60;
|
|
|
|
share->maxDays = 15;
|
|
|
|
share->maxClips = 30;
|
2022-12-11 10:25:22 -05:00
|
|
|
share->maxLogLines = 1000;
|
2022-12-04 15:13:39 -05:00
|
|
|
share->camName = path(share->conf.c_str()).filename();
|
2022-12-11 10:25:22 -05:00
|
|
|
share->webRoot = "/var/www/html";
|
2022-12-04 15:13:39 -05:00
|
|
|
share->vidExt = "mp4";
|
|
|
|
share->recLoopWait = false;
|
|
|
|
share->skipCmd = false;
|
2022-12-11 10:25:22 -05:00
|
|
|
share->webBg = "#485564";
|
|
|
|
share->webTxt = "#dee5ee";
|
|
|
|
share->webFont = "courier";
|
2022-09-22 20:57:46 -04:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
getline(varFile, line);
|
|
|
|
|
|
|
|
if (line.rfind("#", 0) != 0)
|
|
|
|
{
|
2022-12-04 15:13:39 -05:00
|
|
|
rdLine("cam_name = ", line, &share->camName);
|
2022-09-22 20:57:46 -04:00
|
|
|
rdLine("recording_stream = ", line, &share->recordUrl);
|
2022-12-11 10:25:22 -05:00
|
|
|
rdLine("web_root = ", line, &share->webRoot);
|
|
|
|
rdLine("web_text = ", line, &share->webTxt);
|
|
|
|
rdLine("web_bg = ", line, &share->webBg);
|
|
|
|
rdLine("web_font = ", line, &share->webFont);
|
2022-09-22 20:57:46 -04:00
|
|
|
rdLine("post_cmd = ", line, &share->postCmd);
|
|
|
|
rdLine("duration = ", line, &share->secs);
|
|
|
|
rdLine("buff_dir = ", line, &share->buffDir);
|
2022-12-04 15:13:39 -05:00
|
|
|
rdLine("frame_gap = ", line, &share->frameGap);
|
|
|
|
rdLine("pix_thresh = ", line, &share->pixThresh);
|
|
|
|
rdLine("img_thresh = ", line, &share->imgThresh);
|
2022-09-22 20:57:46 -04:00
|
|
|
rdLine("max_days = ", line, &share->maxDays);
|
2022-12-04 15:13:39 -05:00
|
|
|
rdLine("max_clips = ", line, &share->maxClips);
|
2022-12-11 10:25:22 -05:00
|
|
|
rdLine("max_log_lines = ", line, &share->maxLogLines);
|
2022-09-22 20:57:46 -04:00
|
|
|
rdLine("vid_container = ", line, &share->vidExt);
|
|
|
|
}
|
|
|
|
|
|
|
|
} while(!line.empty());
|
|
|
|
|
2022-12-11 10:25:22 -05:00
|
|
|
share->outDir = cleanDir(share->webRoot) + "/" + share->camName;
|
|
|
|
|
|
|
|
createDirTree(cleanDir(share->buffDir));
|
|
|
|
createDirTree(share->outDir);
|
2022-09-30 16:18:39 -04:00
|
|
|
|
2022-09-22 20:57:46 -04:00
|
|
|
if (share->init)
|
|
|
|
{
|
|
|
|
remove_all(share->buffDir.c_str());
|
|
|
|
|
|
|
|
share->init = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
varFile.close();
|
|
|
|
|
|
|
|
return share->retCode == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
string parseForParam(const string &arg, int argc, char** argv, bool argOnly)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < argc; ++i)
|
|
|
|
{
|
|
|
|
auto argInParams = string(argv[i]);
|
|
|
|
|
|
|
|
if (arg.compare(argInParams) == 0)
|
|
|
|
{
|
|
|
|
if (!argOnly)
|
|
|
|
{
|
|
|
|
// check ahead, make sure i + 1 won't cause out-of-range exception
|
|
|
|
if ((i + 1) <= (argc - 1))
|
|
|
|
{
|
|
|
|
return string(argv[i + 1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return string("true");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return string();
|
|
|
|
}
|