ClassFlowImage.cpp 3.5 KB

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