JustMotion/src/main.cpp

128 lines
4.2 KiB
C++
Raw Normal View History

// This file is part of JustMotion.
2022-04-14 09:45:54 -04:00
// JustMotion 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.
2022-04-14 09:45:54 -04:00
// JustMotion 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.
2022-04-14 09:45:54 -04:00
#include "common.h"
#include "camera.h"
#include "proc_control.h"
void showHelp(const QString etcDir)
{
QTextStream(stdout) << APP_NAME << " " << APP_VERSION << Qt::endl << Qt::endl;
QTextStream(stdout) << "Usage: " << APP_TARGET << " <argument>" << Qt::endl << Qt::endl;
QTextStream(stdout) << "-h : display usage information about this application." << Qt::endl;
QTextStream(stdout) << "-d : all valid config files found in " << etcDir << " will be used to" << Qt::endl;
QTextStream(stdout) << " create camera instances. (this is blocking, meant to run with systemd)" << Qt::endl;
QTextStream(stdout) << "-v : display the current version." << Qt::endl;
QTextStream(stdout) << "-u : uninstall the entire app from your system, including the service." << Qt::endl;
QTextStream(stdout) << "-f : force an action without pausing for user confirmation." << Qt::endl;
QTextStream(stdout) << "-s : view the status of all camera instances." << Qt::endl;
QTextStream(stdout) << "-q : kill all camera instances." << Qt::endl;
QTextStream(stdout) << "-r : same as -d except it is non-blocking by starting all camera instances" << Qt::endl;
QTextStream(stdout) << " via systemd. same as 'systemctl start " << APP_TARGET << "'" << Qt::endl;
}
int main(int argc, char** argv)
{
QCoreApplication app(argc, argv);
QCoreApplication::setApplicationName(APP_NAME);
QCoreApplication::setApplicationVersion(APP_VERSION);
auto args = QCoreApplication::arguments();
auto etcDir = "/etc/" + QString(APP_TARGET);
auto staDir = QDir::tempPath() + "/" + APP_TARGET + "-stats";
auto procFile = QDir::tempPath() + "/" + APP_TARGET + "-proc";
auto ret = 0;
if (!QFileInfo::exists(staDir))
{
mkPath(staDir);
}
if (args.contains("-h"))
{
showHelp(etcDir);
}
else if (args.contains("-v"))
{
QTextStream(stdout) << APP_VERSION << Qt::endl;
v2.0.t1 Completely reformed the internal workings of the application code. I brought back multi-threaded functions so there is now 5 separate threads for different tasks. recLoop() - this function calls ffmpeg to begin recording footage from the defined camera and stores the footage in hls format. It is designed to keep running for as long as the application is running and if it does stop for whatever reason, it will attempt to auto re-start. upkeep() - this function does regular cleanup and enforcement of maxDays maxLogSize and maxEvents without the need to stop recording or detecting motion. detectMo() - this function reads directly from recLoop's hls output and list all footage that has motion in it. motion detection no longer has to wait for the clip to finish recording thanks to the use of .ts containers for the video clips. this makes the motion detection for less cpu intensive now that it will now operate at the camera's fps (slower). eventLoop() - this function reads the motion list from detectMo and copies the footage pointed out by the list to an events folder, also in hls format. schLoop() - this function runs an optional user defined external command every amount of seconds defined in sch_sec. this command temporary stops motion detection without actually terminating the thread. It will also not run the command at the scheduled time if motion was detected. Benefits to this reform: - far less cpu intensive operation - multi-threaded architecture for better asynchronous operation - it has support for live streaming now that hls is being used - a buff_dir is no longer necessary
2023-03-05 16:07:07 -05:00
}
else if (args.contains("-d"))
{
auto confs = lsFilesInDir(etcDir);
auto proc = new ProcControl(procFile, staDir, &app);
if (!proc->init())
{
ret = EACCES;
}
else
{
for (auto &&conf : confs)
{
new Camera(etcDir + "/" + conf, staDir, proc, &app);
}
ret = app.exec();
}
2022-04-14 09:45:54 -04:00
}
else if (args.contains("-u"))
{
if (args.contains("-f"))
{
ret = QProcess::execute("/opt/" + QString(APP_TARGET) + "/uninstall.sh", QStringList());
}
else
{
char ans;
std::cout << "This will completely uninstall " << APP_NAME << " from your system. continue y/n? ";
std::cin >> ans;
if (ans == 'y' || ans == 'Y')
{
ret = QProcess::execute("/opt/" + QString(APP_TARGET) + "/uninstall.sh", QStringList());
}
}
}
else if (args.contains("-s"))
{
auto statFiles = lsFilesInDir(staDir);
for (auto &&statFile: statFiles)
{
QFile file(staDir + "/" + statFile);
file.open(QFile::ReadOnly);
std::cout << file.readAll().data();
file.close();
}
}
else if (args.contains("-q"))
{
QFile::remove(procFile);
QThread::currentThread()->sleep(5);
}
else if (args.contains("-r"))
{
QProcess::execute("systemctl start " + QString(APP_TARGET));
}
2022-04-14 09:45:54 -04:00
else
{
showHelp(etcDir);
2022-04-14 09:45:54 -04:00
}
return ret;
2022-04-14 09:45:54 -04:00
}