ClassFlowImage.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "ClassFlowImage.h"
  2. #include <string>
  3. #include <string.h>
  4. #include <sys/stat.h>
  5. #include <dirent.h>
  6. #include "time_sntp.h"
  7. #include "ClassLogFile.h"
  8. ClassFlowImage::ClassFlowImage(const char* logTag)
  9. {
  10. this->logTag = logTag;
  11. isLogImage = false;
  12. }
  13. ClassFlowImage::ClassFlowImage(std::vector<ClassFlow*> * lfc, const char* logTag) : ClassFlow((std::vector<ClassFlow*>*)lfc)
  14. {
  15. this->logTag = logTag;
  16. isLogImage = false;
  17. }
  18. string ClassFlowImage::CreateLogFolder(string time) {
  19. if (!isLogImage)
  20. return "";
  21. string logPath = LogImageLocation + "/" + time.LOGFILE_TIME_FORMAT_DATE_EXTR + "/" + time.LOGFILE_TIME_FORMAT_HOUR_EXTR;
  22. isLogImage = mkdir_r(logPath.c_str(), S_IRWXU) == 0;
  23. if (!isLogImage) {
  24. ESP_LOGW(logTag, "Can't create log foolder for analog images. Path %s", logPath.c_str());
  25. LogFile.WriteToFile("Can't create log foolder for analog images. Path " + logPath);
  26. }
  27. return logPath;
  28. }
  29. void ClassFlowImage::LogImage(string logPath, string name, float *resultFloat, int *resultInt, string time) {
  30. if (!isLogImage)
  31. return;
  32. char buf[10];
  33. if (resultFloat != NULL) {
  34. sprintf(buf, "%.1f_", *resultFloat);
  35. } else if (resultInt != NULL) {
  36. sprintf(buf, "%d_", *resultInt);
  37. } else {
  38. buf[0] = '\0';
  39. }
  40. string nm = logPath + "/" + buf + name + "_" + time + ".jpg";
  41. nm = FormatFileName(nm);
  42. string output = "/sdcard/img_tmp/" + name + ".jpg";
  43. output = FormatFileName(output);
  44. printf("save to file: %s\n", nm.c_str());
  45. CopyFile(output, nm);
  46. }
  47. void ClassFlowImage::RemoveOldLogs()
  48. {
  49. if (!isLogImage)
  50. return;
  51. ESP_LOGI(logTag, "remove old log images");
  52. if (logfileRetentionInDays == 0) {
  53. return;
  54. }
  55. time_t rawtime;
  56. struct tm* timeinfo;
  57. char cmpfilename[30];
  58. time(&rawtime);
  59. rawtime = addDays(rawtime, -logfileRetentionInDays);
  60. timeinfo = localtime(&rawtime);
  61. strftime(cmpfilename, 30, LOGFILE_TIME_FORMAT, timeinfo);
  62. //ESP_LOGE(TAG, "log file name to compare: %s", cmpfilename);
  63. string folderName = string(cmpfilename).LOGFILE_TIME_FORMAT_DATE_EXTR;
  64. DIR *dir = opendir(LogImageLocation.c_str());
  65. if (!dir) {
  66. ESP_LOGI(logTag, "Failed to stat dir : %s", LogImageLocation.c_str());
  67. return;
  68. }
  69. struct dirent *entry;
  70. int deleted = 0;
  71. int notDeleted = 0;
  72. while ((entry = readdir(dir)) != NULL) {
  73. string folderPath = LogImageLocation + "/" + entry->d_name;
  74. if (entry->d_type == DT_DIR) {
  75. //ESP_LOGI(logTag, "Compare %s %s", entry->d_name, folderName.c_str());
  76. if ((strlen(entry->d_name) == folderName.length()) && (strcmp(entry->d_name, folderName.c_str()) < 0)) {
  77. deleted += removeFolder(folderPath.c_str(), logTag);
  78. } else {
  79. notDeleted ++;
  80. }
  81. }
  82. }
  83. ESP_LOGI(logTag, "%d older log files deleted. %d current log files not deleted.", deleted, notDeleted);
  84. closedir(dir);
  85. }