JustMotion/src/main.cpp
zii daad0dffa2 v3.5.t2
-re-added recordloop as a thread within the app.
-the app no longer use mutiple services and will instead fully
 operate in a single master service.
-build/install.py will now install the app as a single service.
-added/updated -s, -r and -q options to manage the single master
 service.
2024-04-21 08:03:30 -04:00

128 lines
4.2 KiB
C++

// This file is part of JustMotion.
// 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.
// 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.
#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;
}
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();
}
}
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));
}
else
{
showHelp(etcDir);
}
return ret;
}