ClassFlowImage.cpp 3.6 KB

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