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
|
|
|
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-26 16:12:53 -04:00
|
|
|
QStringList listFacingFiles(const QString &path, const QString &ext, const QDateTime &stamp, int secs, char dir)
|
|
|
|
{
|
|
|
|
QStringList ret;
|
|
|
|
|
|
|
|
for (auto i = 0; i < secs; ++i)
|
|
|
|
{
|
|
|
|
QString filePath;
|
|
|
|
|
|
|
|
if (dir == '-') filePath = path + "/" + stamp.addSecs(-i).toString(DATETIME_FMT) + ext;
|
|
|
|
if (dir == '+') filePath = path + "/" + stamp.addSecs(i).toString(DATETIME_FMT) + ext;
|
|
|
|
|
|
|
|
if (QFile::exists(filePath))
|
|
|
|
{
|
|
|
|
if (dir == '-') ret.insert(0, filePath);
|
|
|
|
if (dir == '+') ret.append(filePath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList backwardFacingFiles(const QString &path, const QString &ext, const QDateTime &stamp, int secs)
|
|
|
|
{
|
|
|
|
return listFacingFiles(path, ext, stamp, secs, '-');
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList forwardFacingFiles(const QString &path, const QString &ext, const QDateTime &stamp, int secs)
|
|
|
|
{
|
|
|
|
return listFacingFiles(path, ext, stamp, secs, '+');
|
|
|
|
}
|
|
|
|
|
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-26 16:12:53 -04:00
|
|
|
void enforceMaxImages()
|
|
|
|
{
|
|
|
|
auto names = lsFilesInDir("img", ".bmp");
|
|
|
|
|
|
|
|
while (names.size() > MAX_IMAGES)
|
|
|
|
{
|
|
|
|
QFile::remove("img/" + names[0]);
|
|
|
|
|
|
|
|
names.removeFirst();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-09 16:24:32 -04:00
|
|
|
void enforceMaxVids()
|
|
|
|
{
|
|
|
|
auto names = lsFilesInDir("live", ".ts");
|
|
|
|
|
|
|
|
while (names.size() > MAX_VIDEOS)
|
|
|
|
{
|
|
|
|
QFile::remove("live/" + names[0]);
|
|
|
|
|
|
|
|
names.removeFirst();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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("img_thresh = ", line, &share->imgThresh);
|
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-17 15:06:58 -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
|
|
|
}
|
2023-05-17 15:06:58 -04:00
|
|
|
|
|
|
|
MultiInstance::MultiInstance(QObject *parent) : QObject(parent) {}
|
|
|
|
|
|
|
|
void MultiInstance::instStdout()
|
|
|
|
{
|
|
|
|
for (auto &&proc : procList)
|
|
|
|
{
|
|
|
|
QTextStream(stdout) << proc->readAllStandardOutput();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiInstance::instStderr()
|
|
|
|
{
|
|
|
|
for (auto &&proc : procList)
|
|
|
|
{
|
|
|
|
QTextStream(stderr) << proc->readAllStandardError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-17 15:40:09 -04:00
|
|
|
void MultiInstance::procChanged(QProcess::ProcessState newState)
|
2023-05-17 15:06:58 -04:00
|
|
|
{
|
2023-05-17 15:40:09 -04:00
|
|
|
Q_UNUSED(newState)
|
2023-05-17 15:06:58 -04:00
|
|
|
|
|
|
|
for (auto &&proc : procList)
|
|
|
|
{
|
|
|
|
if (proc->state() == QProcess::Running)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QCoreApplication::quit();
|
|
|
|
}
|
|
|
|
|
|
|
|
int MultiInstance::start(const QStringList &args)
|
|
|
|
{
|
|
|
|
auto ret = ENOENT;
|
2023-05-17 17:10:39 -04:00
|
|
|
auto path = QDir().cleanPath(getParam("-d", args));
|
2023-05-17 15:06:58 -04:00
|
|
|
auto files = lsFilesInDir(path);
|
|
|
|
|
|
|
|
if (!QDir(path).exists())
|
|
|
|
{
|
|
|
|
QTextStream(stderr) << "err: the supplied directory in -d '" << path << "' does not exists or is not a directory.";
|
|
|
|
}
|
|
|
|
else if (files.isEmpty())
|
|
|
|
{
|
|
|
|
QTextStream(stderr) << "err: no config files found in '" << path << "'";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-05-17 17:10:39 -04:00
|
|
|
ret = 0;
|
|
|
|
|
2023-05-17 15:06:58 -04:00
|
|
|
for (auto &&conf : files)
|
|
|
|
{
|
|
|
|
auto proc = new QProcess(this);
|
|
|
|
|
|
|
|
QStringList subArgs;
|
|
|
|
|
2023-05-17 17:10:39 -04:00
|
|
|
subArgs << "-c" << path + "/" + conf;
|
2023-05-17 15:06:58 -04:00
|
|
|
|
|
|
|
connect(proc, &QProcess::readyReadStandardOutput, this, &MultiInstance::instStdout);
|
|
|
|
connect(proc, &QProcess::readyReadStandardError, this, &MultiInstance::instStderr);
|
2023-05-17 15:40:09 -04:00
|
|
|
connect(proc, &QProcess::stateChanged, this, &MultiInstance::procChanged);
|
2023-05-17 15:06:58 -04:00
|
|
|
|
|
|
|
connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, proc, &QProcess::terminate);
|
|
|
|
|
|
|
|
proc->setProgram(APP_BIN);
|
|
|
|
proc->setArguments(subArgs);
|
|
|
|
proc->start();
|
|
|
|
|
|
|
|
procList.append(proc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|