ClassFlowImage.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. if (*resultFloat < 0)
  44. sprintf(buf, "N.N_");
  45. else
  46. sprintf(buf, "%.1f_", *resultFloat);
  47. } else if (resultInt != NULL) {
  48. sprintf(buf, "%d_", *resultInt);
  49. } else {
  50. buf[0] = '\0';
  51. }
  52. string nm = logPath + "/" + buf + name + "_" + time + ".jpg";
  53. nm = FormatFileName(nm);
  54. string output = "/sdcard/img_tmp/" + name + ".jpg";
  55. output = FormatFileName(output);
  56. printf("save to file: %s\n", nm.c_str());
  57. _img->SaveToFile(nm);
  58. // CopyFile(output, nm);
  59. }
  60. void ClassFlowImage::RemoveOldLogs()
  61. {
  62. if (!isLogImage)
  63. return;
  64. ESP_LOGI(logTag, "remove old log images");
  65. if (logfileRetentionInDays == 0) {
  66. return;
  67. }
  68. time_t rawtime;
  69. struct tm* timeinfo;
  70. char cmpfilename[30];
  71. time(&rawtime);
  72. rawtime = addDays(rawtime, -logfileRetentionInDays);
  73. timeinfo = localtime(&rawtime);
  74. strftime(cmpfilename, 30, LOGFILE_TIME_FORMAT, timeinfo);
  75. //ESP_LOGE(TAG, "log file name to compare: %s", cmpfilename);
  76. string folderName = string(cmpfilename).LOGFILE_TIME_FORMAT_DATE_EXTR;
  77. DIR *dir = opendir(LogImageLocation.c_str());
  78. if (!dir) {
  79. ESP_LOGI(logTag, "Failed to stat dir : %s", LogImageLocation.c_str());
  80. return;
  81. }
  82. struct dirent *entry;
  83. int deleted = 0;
  84. int notDeleted = 0;
  85. while ((entry = readdir(dir)) != NULL) {
  86. string folderPath = LogImageLocation + "/" + entry->d_name;
  87. if (entry->d_type == DT_DIR) {
  88. //ESP_LOGI(logTag, "Compare %s %s", entry->d_name, folderName.c_str());
  89. if ((strlen(entry->d_name) == folderName.length()) && (strcmp(entry->d_name, folderName.c_str()) < 0)) {
  90. deleted += removeFolder(folderPath.c_str(), logTag);
  91. } else {
  92. notDeleted ++;
  93. }
  94. }
  95. }
  96. ESP_LOGI(logTag, "%d older log files deleted. %d current log files not deleted.", deleted, notDeleted);
  97. closedir(dir);
  98. }