ClassFlowImage.cpp 3.5 KB

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