ClassFlowImage.cpp 3.4 KB

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