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"
|
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
QString getParam(const QString &key, const QStringList &args)
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
2023-05-15 15:29:47 -04:00
|
|
|
// this can be used by command objects to pick out parameters
|
|
|
|
// from a command line that are pointed by a name identifier
|
|
|
|
// example: -i /etc/some_file, this function should pick out
|
|
|
|
// "/etc/some_file" from args if "-i" is passed into key.
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
QString ret;
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
int pos = args.indexOf(QRegularExpression(key, QRegularExpression::CaseInsensitiveOption));
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
if (pos != -1)
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
2023-05-15 15:29:47 -04:00
|
|
|
// key found.
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
if ((pos + 1) <= (args.size() - 1))
|
2023-03-05 16:07:07 -05:00
|
|
|
{
|
2023-05-15 15:29:47 -04:00
|
|
|
// check ahead to make sure pos + 1 will not go out
|
|
|
|
// of range.
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
if (!args[pos + 1].startsWith("-"))
|
2022-12-11 12:33:56 -05:00
|
|
|
{
|
2023-05-15 15:29:47 -04:00
|
|
|
// the "-" used throughout this application
|
|
|
|
// indicates an argument so the above 'if'
|
|
|
|
// statement will check to make sure it does
|
|
|
|
// not return another argument as a parameter
|
|
|
|
// in case a back-to-back "-arg -arg" is
|
|
|
|
// present.
|
|
|
|
|
|
|
|
ret = args[pos + 1];
|
2022-09-22 20:57:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
return ret;
|
2022-09-22 20:57:46 -04:00
|
|
|
}
|
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
QByteArray genMD5(const QByteArray &bytes)
|
2022-12-04 15:13:39 -05:00
|
|
|
{
|
2023-05-15 15:29:47 -04:00
|
|
|
QCryptographicHash hasher(QCryptographicHash::Md5);
|
2022-12-04 15:13:39 -05:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
hasher.addData(bytes); return hasher.result();
|
2022-12-04 15:13:39 -05:00
|
|
|
}
|
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
QByteArray genMD5(const QString &path)
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
2023-05-15 19:39:29 -04:00
|
|
|
QFile file(path);
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
if (file.open(QFile::ReadOnly))
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
2023-05-15 15:29:47 -04:00
|
|
|
return genMD5(file.readAll());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return genMD5(QByteArray("EMPTY"));
|
2022-12-04 15:13:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
QStringList lsFilesInDir(const QString &path, const QString &ext)
|
2022-12-04 15:13:39 -05:00
|
|
|
{
|
2023-05-15 15:29:47 -04:00
|
|
|
QStringList filters;
|
2022-12-13 21:13:10 -05:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
filters << "*" + ext;
|
2023-03-26 10:32:56 -04:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
QDir dirObj(path);
|
2023-03-10 19:35:44 -05:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
dirObj.setFilter(QDir::Files);
|
|
|
|
dirObj.setNameFilters(filters);
|
|
|
|
dirObj.setSorting(QDir::Name);
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
return dirObj.entryList();
|
|
|
|
}
|
2023-03-05 16:07:07 -05:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
QStringList lsDirsInDir(const QString &path)
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
2023-05-15 15:29:47 -04:00
|
|
|
QDir dirObj(path);
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
dirObj.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
|
|
|
|
dirObj.setSorting(QDir::Name);
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
return dirObj.entryList();
|
|
|
|
}
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
void enforceMaxEvents(shared_t *share)
|
|
|
|
{
|
|
|
|
auto names = lsFilesInDir("events", ".mp4");
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
while (names.size() > share->maxEvents)
|
|
|
|
{
|
|
|
|
auto nameOnly = "events/" + names[0];
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
nameOnly.remove(".mp4");
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
auto mp4File = nameOnly + ".mp4";
|
|
|
|
auto imgFile = nameOnly + ".jpg";
|
|
|
|
auto webFile = nameOnly + ".html";
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
QFile::remove(mp4File);
|
|
|
|
QFile::remove(imgFile);
|
|
|
|
QFile::remove(webFile);
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
names.removeFirst();
|
|
|
|
}
|
2023-04-20 14:52:59 -04:00
|
|
|
}
|
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
void rdLine(const QString ¶m, const QString &line, QString *value)
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
2023-05-15 15:29:47 -04:00
|
|
|
if (line.startsWith(param))
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
2023-05-15 15:29:47 -04:00
|
|
|
*value = line.mid(param.size());
|
2022-09-22 20:57:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
void rdLine(const QString ¶m, const QString &line, int *value)
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
2023-05-15 15:29:47 -04:00
|
|
|
if (line.startsWith(param))
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
2023-05-15 15:29:47 -04:00
|
|
|
*value = line.mid(param.size()).toInt();
|
2022-09-22 20:57:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
bool rdConf(const QString &filePath, shared_t *share)
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
2023-05-15 15:29:47 -04:00
|
|
|
QFile varFile(filePath);
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
if (!varFile.open(QFile::ReadOnly))
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
|
|
|
share->retCode = ENOENT;
|
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
QTextStream(stderr) << "err: config file: " << filePath << " does not exists or lack read permissions." << Qt::endl;
|
2022-09-22 20:57:46 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-05-15 15:29:47 -04:00
|
|
|
QString line;
|
2022-09-22 20:57:46 -04:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2023-05-15 15:29:47 -04:00
|
|
|
line = QString::fromUtf8(varFile.readLine());
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
if (!line.startsWith("#"))
|
2022-09-22 20:57:46 -04:00
|
|
|
{
|
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);
|
2023-04-20 14:52:59 -04:00
|
|
|
rdLine("max_event_secs = ", line, &share->evMaxSecs);
|
|
|
|
rdLine("post_secs = ", line, &share->postSecs);
|
2022-09-22 20:57:46 -04:00
|
|
|
rdLine("post_cmd = ", line, &share->postCmd);
|
2022-12-04 15:13:39 -05:00
|
|
|
rdLine("pix_thresh = ", line, &share->pixThresh);
|
|
|
|
rdLine("img_thresh = ", line, &share->imgThresh);
|
2023-04-20 14:52:59 -04:00
|
|
|
rdLine("frame_gap = ", line, &share->frameGap);
|
2023-03-05 16:07:07 -05:00
|
|
|
rdLine("max_events = ", line, &share->maxEvents);
|
2022-12-13 20:27:32 -05:00
|
|
|
rdLine("max_log_size = ", line, &share->maxLogSize);
|
2022-09-22 20:57:46 -04:00
|
|
|
}
|
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
} while(!line.isEmpty());
|
2022-12-24 13:48:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return share->retCode == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool rdConf(shared_t *share)
|
|
|
|
{
|
2023-03-05 16:07:07 -05:00
|
|
|
if (rdConf(share->conf, share))
|
2023-02-18 21:20:24 -05:00
|
|
|
{
|
2023-05-15 15:29:47 -04:00
|
|
|
if (share->camName.isEmpty())
|
2023-02-05 14:05:56 -05:00
|
|
|
{
|
2023-05-15 15:29:47 -04:00
|
|
|
share->camName = QFileInfo(share->conf).fileName();
|
2023-02-05 14:05:56 -05:00
|
|
|
}
|
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
share->outDir = QDir().cleanPath(share->webRoot) + "/" + share->camName;
|
2022-12-11 15:06:09 -05:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
QDir().mkpath(share->outDir);
|
2023-03-05 16:07:07 -05:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
if (!QDir::setCurrent(share->outDir))
|
2023-03-05 16:07:07 -05:00
|
|
|
{
|
2023-05-15 15:29:47 -04:00
|
|
|
QTextStream(stderr) << "err: failed to change/create the current working directory to camera folder: '" << share->outDir << ".' does it exists?" << Qt::endl;
|
2022-09-22 20:57:46 -04:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
share->retCode = ENOENT;
|
2022-09-22 20:57:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
return share->retCode == 0;
|
2023-03-05 16:07:07 -05:00
|
|
|
}
|