ClassFlowImage.cpp 3.3 KB

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