Helper.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. //#pragma warning(disable : 4996)
  2. #include "freertos/FreeRTOS.h"
  3. #include "freertos/task.h"
  4. #include "Helper.h"
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <dirent.h>
  8. #include <string.h>
  9. #include <esp_log.h>
  10. #include "ClassLogFile.h"
  11. //#include "ClassLogFile.h"
  12. //#define ISWINDOWS_TRUE
  13. #define PATH_MAX_STRING_SIZE 256
  14. using namespace std;
  15. FILE* OpenFileAndWait(const char* nm, char* _mode, int _waitsec)
  16. {
  17. FILE *pfile = fopen(nm, _mode);
  18. if (pfile == NULL)
  19. {
  20. TickType_t xDelay;
  21. xDelay = _waitsec * 1000 / portTICK_PERIOD_MS;
  22. std::string zw = "File is locked: " + std::string(nm) + " - wait for " + std::to_string(_waitsec);
  23. printf(zw.c_str());
  24. printf("\n");
  25. LogFile.WriteToFile(zw);
  26. vTaskDelay( xDelay );
  27. pfile = fopen(nm, _mode);
  28. }
  29. return pfile;
  30. }
  31. std::string FormatFileName(std::string input)
  32. {
  33. #ifdef ISWINDOWS_TRUE
  34. input.erase(0, 1);
  35. std::string os = "/";
  36. std::string ns = "\\";
  37. FindReplace(input, os, ns);
  38. #endif
  39. return input;
  40. }
  41. void FindReplace(std::string& line, std::string& oldString, std::string& newString) {
  42. const size_t oldSize = oldString.length();
  43. // do nothing if line is shorter than the string to find
  44. if (oldSize > line.length()) return;
  45. const size_t newSize = newString.length();
  46. for (size_t pos = 0; ; pos += newSize) {
  47. // Locate the substring to replace
  48. pos = line.find(oldString, pos);
  49. if (pos == std::string::npos) return;
  50. if (oldSize == newSize) {
  51. // if they're same size, use std::string::replace
  52. line.replace(pos, oldSize, newString);
  53. }
  54. else {
  55. // if not same size, replace by erasing and inserting
  56. line.erase(pos, oldSize);
  57. line.insert(pos, newString);
  58. }
  59. }
  60. }
  61. bool ctype_space(const char c, string adddelimiter)
  62. {
  63. if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == 11)
  64. {
  65. return true;
  66. }
  67. if (adddelimiter.find(c) != string::npos)
  68. return true;
  69. return false;
  70. }
  71. string trim(string istring, string adddelimiter)
  72. {
  73. bool trimmed = false;
  74. if (ctype_space(istring[istring.length() - 1], adddelimiter))
  75. {
  76. istring.erase(istring.length() - 1);
  77. trimmed = true;
  78. }
  79. if (ctype_space(istring[0], adddelimiter))
  80. {
  81. istring.erase(0, 1);
  82. trimmed = true;
  83. }
  84. if ((trimmed == false) || (istring.size() == 0))
  85. {
  86. return istring;
  87. }
  88. else
  89. {
  90. return trim(istring, adddelimiter);
  91. }
  92. }
  93. size_t findDelimiterPos(string input, string delimiter)
  94. {
  95. size_t pos = std::string::npos;
  96. size_t zw;
  97. string akt_del;
  98. for (int anz = 0; anz < delimiter.length(); ++anz)
  99. {
  100. akt_del = delimiter[anz];
  101. if ((zw = input.find(akt_del)) != std::string::npos)
  102. {
  103. if (pos != std::string::npos)
  104. {
  105. if (zw < pos)
  106. pos = zw;
  107. }
  108. else
  109. pos = zw;
  110. }
  111. }
  112. return pos;
  113. }
  114. void CopyFile(string input, string output)
  115. {
  116. input = FormatFileName(input);
  117. output = FormatFileName(output);
  118. if (toUpper(input).compare("/SDCARD/WLAN.INI") == 0)
  119. {
  120. printf("wlan.ini kann nicht kopiert werden!\n");
  121. return;
  122. }
  123. char cTemp;
  124. FILE* fpSourceFile = OpenFileAndWait(input.c_str(), "rb");
  125. if (!fpSourceFile) // Sourcefile existiert nicht sonst gibt es einen Fehler beim Kopierversuch!
  126. {
  127. printf("File %s existiert nicht!\n", input.c_str());
  128. return;
  129. }
  130. FILE* fpTargetFile = OpenFileAndWait(output.c_str(), "wb");
  131. // Code Section
  132. // Read From The Source File - "Copy"
  133. while (fread(&cTemp, 1, 1, fpSourceFile) == 1)
  134. {
  135. // Write To The Target File - "Paste"
  136. fwrite(&cTemp, 1, 1, fpTargetFile);
  137. }
  138. // Close The Files
  139. fclose(fpSourceFile);
  140. fclose(fpTargetFile);
  141. }
  142. string getFileType(string filename)
  143. {
  144. int lastpos = filename.find(".", 0);
  145. int neu_pos;
  146. while ((neu_pos = filename.find(".", lastpos + 1)) > -1)
  147. {
  148. lastpos = neu_pos;
  149. }
  150. string zw = filename.substr(lastpos + 1, filename.size() - lastpos);
  151. zw = toUpper(zw);
  152. return zw;
  153. }
  154. /* recursive mkdir */
  155. int mkdir_r(const char *dir, const mode_t mode) {
  156. char tmp[PATH_MAX_STRING_SIZE];
  157. char *p = NULL;
  158. struct stat sb;
  159. size_t len;
  160. /* copy path */
  161. len = strnlen (dir, PATH_MAX_STRING_SIZE);
  162. if (len == 0 || len == PATH_MAX_STRING_SIZE) {
  163. return -1;
  164. }
  165. memcpy (tmp, dir, len);
  166. tmp[len] = '\0';
  167. /* remove trailing slash */
  168. if(tmp[len - 1] == '/') {
  169. tmp[len - 1] = '\0';
  170. }
  171. /* check if path exists and is a directory */
  172. if (stat (tmp, &sb) == 0) {
  173. if (S_ISDIR (sb.st_mode)) {
  174. return 0;
  175. }
  176. }
  177. /* recursive mkdir */
  178. for(p = tmp + 1; *p; p++) {
  179. if(*p == '/') {
  180. *p = 0;
  181. /* test path */
  182. if (stat(tmp, &sb) != 0) {
  183. /* path does not exist - create directory */
  184. if (mkdir(tmp, mode) < 0) {
  185. return -1;
  186. }
  187. } else if (!S_ISDIR(sb.st_mode)) {
  188. /* not a directory */
  189. return -1;
  190. }
  191. *p = '/';
  192. }
  193. }
  194. /* test path */
  195. if (stat(tmp, &sb) != 0) {
  196. /* path does not exist - create directory */
  197. if (mkdir(tmp, mode) < 0) {
  198. return -1;
  199. }
  200. } else if (!S_ISDIR(sb.st_mode)) {
  201. /* not a directory */
  202. return -1;
  203. }
  204. return 0;
  205. }
  206. string toUpper(string in)
  207. {
  208. for (int i = 0; i < in.length(); ++i)
  209. in[i] = toupper(in[i]);
  210. return in;
  211. }
  212. // CPU Temp
  213. extern "C" uint8_t temprature_sens_read();
  214. float temperatureRead()
  215. {
  216. return (temprature_sens_read() - 32) / 1.8;
  217. }
  218. time_t addDays(time_t startTime, int days) {
  219. struct tm* tm = localtime(&startTime);
  220. tm->tm_mday += days;
  221. return mktime(tm);
  222. }
  223. int removeFolder(const char* folderPath, const char* logTag) {
  224. ESP_LOGI(logTag, "Delete folder %s", folderPath);
  225. DIR *dir = opendir(folderPath);
  226. if (!dir) {
  227. ESP_LOGI(logTag, "Failed to stat dir : %s", folderPath);
  228. return -1;
  229. }
  230. struct dirent *entry;
  231. int deleted = 0;
  232. while ((entry = readdir(dir)) != NULL) {
  233. std::string path = string(folderPath) + "/" + entry->d_name;
  234. if (entry->d_type == DT_REG) {
  235. if (unlink(path.c_str()) == 0) {
  236. deleted ++;
  237. } else {
  238. ESP_LOGE(logTag, "can't delete file : %s", path.c_str());
  239. }
  240. } else if (entry->d_type == DT_DIR) {
  241. deleted += removeFolder(path.c_str(), logTag);
  242. }
  243. }
  244. closedir(dir);
  245. if (rmdir(folderPath) != 0) {
  246. ESP_LOGE(logTag, "can't delete file : %s", folderPath);
  247. }
  248. ESP_LOGI(logTag, "%d older log files in folder %s deleted.", deleted, folderPath);
  249. return deleted;
  250. }