From 5ab50433cf95c46e35d825da50724fe0a03a018f Mon Sep 17 00:00:00 2001 From: Maurice ONeal Date: Sun, 11 Dec 2022 12:33:56 -0500 Subject: [PATCH] v1.5.t4 The error checking with ffmpeg is not working. Learned that it doesn't always return 0 on success. Decided to remove the error checking altogether. Instead ffmpeg failures should be checked manually using stderr. Dirent includes .. and . so I decided to switch to the filesystem entry listing that should hopefully exclude those special directories. The camera webroot was not generating .index files. those files would only get generated if motion was detected. Copied the code that does that onto recLoop() to execute regardless of motion. --- src/common.cpp | 53 +++++++++----------------------------------------- src/common.h | 4 +--- src/logger.cpp | 1 - src/main.cpp | 26 +++++++------------------ src/web.cpp | 16 +++++++-------- 5 files changed, 25 insertions(+), 75 deletions(-) diff --git a/src/common.cpp b/src/common.cpp index f590acd..a556974 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -57,44 +57,21 @@ bool fileExists(const string& name) return access(name.c_str(), F_OK) != -1; } -string replaceAll(string str, const string &from, const string &to) -{ - if (from.empty()) return str; - - size_t startPos = 0; - - while ((startPos = str.find(from, startPos)) != string::npos) - { - str.replace(startPos, from.length(), to); - - startPos += to.length(); - } - - return str; -} - vector lsFilesInDir(const string &path, const string &ext) { - DIR *dir; - struct dirent *ent; vector names; - if ((dir = opendir(path.c_str())) != NULL) + for (auto &entry : directory_iterator(path)) { - while ((ent = readdir(dir)) != NULL) + if (entry.is_regular_file()) { - if (ent->d_type & DT_REG) - { - auto name = string(ent->d_name); + auto name = entry.path().filename().string(); - if (name.ends_with(ext.c_str()) || ext.empty()) - { - names.push_back(name); - } + if (ext.empty() || name.ends_with(ext)) + { + names.push_back(name); } } - - closedir(dir); } sort(names.begin(), names.end()); @@ -104,26 +81,14 @@ vector lsFilesInDir(const string &path, const string &ext) vector lsDirsInDir(const string &path) { - DIR *dir; - struct dirent *ent; vector names; - if ((dir = opendir(path.c_str())) != NULL) + for (auto &entry : directory_iterator(path)) { - while ((ent = readdir(dir)) != NULL) + if (entry.is_directory()) { - if (ent->d_type & DT_DIR) - { - auto name = string(ent->d_name); - - if ((name != "..") || (name != ".")) - { - names.push_back(name); - } - } + names.push_back(entry.path().filename().string()); } - - closedir(dir); } sort(names.begin(), names.end()); diff --git a/src/common.h b/src/common.h index 729dab6..2fc40de 100644 --- a/src/common.h +++ b/src/common.h @@ -22,7 +22,6 @@ #include #include #include -#include #include #include #include @@ -36,7 +35,7 @@ using namespace cv; using namespace std; using namespace std::filesystem; -#define APP_VER "1.5.t3" +#define APP_VER "1.5.t4" #define APP_NAME "Motion Watch" struct shared_t @@ -72,7 +71,6 @@ string genDstFile(const string &dirOut, const char *fmt, const string &e string genTimeStr(const char *fmt); string cleanDir(const string &path); string parseForParam(const string &arg, int argc, char** argv, bool argOnly); -string replaceAll(string str, const string &from, const string &to); bool createDir(const string &dir); bool createDirTree(const string &full_path); bool fileExists(const string& name); diff --git a/src/logger.cpp b/src/logger.cpp index 152c898..00a556b 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -46,7 +46,6 @@ void logLoop(shared_t *share) htmlText += "\n"; htmlText += "\n"; htmlText += "\n"; - htmlText += ""; htmlText += "\n"; htmlText += "\n"; htmlText += "

\n"; diff --git a/src/main.cpp b/src/main.cpp index f5965a8..6c67a10 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -52,7 +52,7 @@ void detectLoop(shared_t *share) sleep(1); } } - while (!bufFiles.empty()); + while (!bufFiles.empty() && !share->recLoopWait); detLog("detectLoop() -- finished", share); } @@ -77,6 +77,9 @@ void recLoop(shared_t *share) recLog("/tmp/mow-lock pesent, skipping update of the webroot page.", share); } + genHTMLul(share->outDir, share->camName, share); + recLog("camera specific webroot page updated. page = " + share->outDir + "/index.html", share); + auto bufPath = cleanDir(share->buffDir) + "/%03d." + share->vidExt; auto secs = to_string(share->secs); auto limSecs = to_string(share->secs + 3); @@ -87,26 +90,11 @@ void recLoop(shared_t *share) recLog("detect_loop started in a seperate thread.", share); recLog("ffmpeg = " + cmd, share); - auto ret = system(cmd.c_str()); + system(cmd.c_str()); - if (ret == 0) - { - recLog("ffmpeg_return_code = " + to_string(ret), share); + share->recLoopWait = true; - share->recLoopWait = true; - - th2.join(); - } - else - { - recLog("ffmpeg failed, cooling down for " + to_string(share->secs) + "secs.", share); - recLog("ffmpeg_return_code = " + to_string(ret), share); - - // simulate that ffmpeg is running even after it has failed. - sleep(share->secs); - - share->recLoopWait = true; - } + th2.join(); if (!share->skipCmd) { diff --git a/src/web.cpp b/src/web.cpp index f6c165e..ec83075 100644 --- a/src/web.cpp +++ b/src/web.cpp @@ -14,8 +14,6 @@ void genHTMLul(const string &outputDir, const string &title, shared_t *share) { - DIR *dir; - struct dirent *ent; vector logNames; vector regNames = lsFilesInDir(outputDir); vector dirNames = lsDirsInDir(outputDir); @@ -82,24 +80,23 @@ void genHTMLul(const string &outputDir, const string &title, shared_t *share) void genHTMLvid(const string &outputVid, shared_t *share) { - auto filename = path(outputVid).filename().string(); + auto vidName = path(outputVid).filename().string(); auto filePath = path(outputVid).parent_path().string(); + auto fileName = vidName.substr(0, vidName.size() - (share->vidExt.size() + 1)); string htmlText = "\n"; - filename = replaceAll(filename, share->vidExt, "html"); - htmlText += "\n"; htmlText += "\n"; htmlText += "\n"; htmlText += "\n"; htmlText += "\n"; htmlText += "\n"; htmlText += "\n"; htmlText += ""; - ofstream file(string(cleanDir(filePath) + "/" + filename + ".html").c_str()); + ofstream file(string(filePath + "/" + fileName + ".html").c_str()); file << htmlText << endl; @@ -113,7 +110,10 @@ void genCSS(shared_t *share) cssText += " background-color: " + share->webBg + ";\n"; cssText += " color: " + share->webTxt + ";\n"; cssText += " font-family: " + share->webFont + ";\n"; - cssText += "}"; + cssText += "}\n"; + cssText += "a {\n"; + cssText += " color: " + share->webTxt + ";\n"; + cssText += "}\n"; ofstream file(string(cleanDir(share->webRoot) + "/theme.css").c_str());