ClassFlowImage.cpp 3.1 KB

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