// 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 "mo_detect.h" #include "logger.h" void detectMoInFile(const string &bufPath, shared_t *share) { detLog("detect_mo_in_file() -- start", share); Mat thumbNail; if (moDetect(bufPath, thumbNail, share)) { share->skipCmd = true; wrOut(bufPath, thumbNail, share); } else if (exists(bufPath)) { remove(bufPath); } detLog("detect_mo_in_file() -- finished", share); } void recLoop(shared_t *share) { while (rdConf(share)) { recLog("rec_loop() -- start", share); enforceMaxLogSize(share->recLogPath, share); enforceMaxLogSize(share->detLogPath, share); initLogFile(share->recLogPath, share->recLogFile); initLogFile(share->detLogPath, share->detLogFile); initLogFrontPages(share); if (!exists("/tmp/mow-lock")) { system("touch /tmp/mow-lock"); genCSS(share); genHTMLul(share->webRoot, string(APP_NAME) + " " + string(APP_VER), share); remove("/tmp/mow-lock"); recLog("webroot page updated: " + cleanDir(share->webRoot) + "/index.html", share); } else { recLog("skipping update of the webroot page, it is busy.", share); } genHTMLul(share->outDir, share->camName, share); recLog("camera specific webroot page updated: " + share->outDir + "/index.html", share); for (auto i = 0; i < share->numOfClips; ++i) { auto bufPath = cleanDir(share->buffDir) + "/" + to_string(i) + "." + share->vidExt; auto cmd = "timeout -k 1 " + to_string(share->clipLen + 2) + " "; cmd += "ffmpeg -hide_banner -i " + share->recordUrl + " -y -vcodec " + share->vidCodec + " -movflags faststart -t " + to_string(share->clipLen) + " " + bufPath; recLog("ffmpeg_run: " + cmd, share); auto retCode = system(cmd.c_str()); recLog("ffmpeg_retcode: " + to_string(retCode), share); if (retCode == 0) { recLog("detect_mo_in_file() -- started in a seperate thread.", share); share->detThreads.push_back(thread(detectMoInFile, bufPath, share)); } else { recLog("ffmpeg returned non zero, indicating failure. please check stderr output.", share); if (exists(bufPath)) { remove(bufPath); } sleep(share->clipLen); } } waitForDetThreads(share); if (!share->skipCmd) { recLog("no motion detected", share); if (share->postCmd.empty()) { recLog("post command not defined, skipping.", share); } else { recLog("running post command: " + share->postCmd, share); system(share->postCmd.c_str()); } } else { recLog("motion detected, skipping the post command.", share); } recLog("rec_loop() -- finished", share); if (share->retCode != 0) { break; } } } int main(int argc, char** argv) { struct shared_t sharedRes; sharedRes.conf = parseForList("-c", argc, argv); if (parseForParam("-h", argc, argv, true) == "true") { cout << "Motion Watch " << APP_VER << endl << endl; cout << "Usage: mow " << endl << endl; cout << "-h : display usage information about this application." << endl; cout << "-c : path to a config file." << endl; cout << "-v : display the current version." << endl << endl; cout << "note: multiple -c config files can be passed, reading from left" << endl; cout << " to right. any conflicting values between the files will" << endl; cout << " have the latest value from the latest file overwrite the" << endl; cout << " the earliest." << endl; } else if (parseForParam("-v", argc, argv, true) == "true") { cout << APP_VER << endl; } else if (sharedRes.conf.empty()) { cerr << "err: no config file(s) were given in -c" << endl; } else { sharedRes.retCode = 0; sharedRes.skipCmd = false; sharedRes.init = true; recLoop(&sharedRes); return sharedRes.retCode; } return EINVAL; }