ClassFlowImage.cpp 3.1 KB

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