ClassFlowControll.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. #include "ClassFlowControll.h"
  2. #include "connect_wlan.h"
  3. #include "freertos/task.h"
  4. #include <sys/stat.h>
  5. #include <dirent.h>
  6. #include "ClassLogFile.h"
  7. #include "time_sntp.h"
  8. #include "Helper.h"
  9. #include "server_ota.h"
  10. #include "server_help.h"
  11. //#define DEBUG_DETAIL_ON
  12. static const char* TAG = "flow_controll";
  13. std::string ClassFlowControll::doSingleStep(std::string _stepname, std::string _host){
  14. std::string _classname = "";
  15. std::string result = "";
  16. if ((_stepname.compare("[MakeImage]") == 0) || (_stepname.compare(";[MakeImage]") == 0)){
  17. _classname = "ClassFlowMakeImage";
  18. }
  19. if ((_stepname.compare("[Alignment]") == 0) || (_stepname.compare(";[Alignment]") == 0)){
  20. _classname = "ClassFlowAlignment";
  21. }
  22. if ((_stepname.compare("[Digits]") == 0) || (_stepname.compare(";[Digits]") == 0)){
  23. _classname = "ClassFlowDigit";
  24. }
  25. if ((_stepname.compare("[Analog]") == 0) || (_stepname.compare(";[Analog]") == 0)){
  26. _classname = "ClassFlowAnalog";
  27. }
  28. if ((_stepname.compare("[MQTT]") == 0) || (_stepname.compare(";[MQTT]") == 0)){
  29. _classname = "ClassFlowMQTT";
  30. }
  31. for (int i = 0; i < FlowControll.size(); ++i)
  32. if (FlowControll[i]->name().compare(_classname) == 0){
  33. FlowControll[i]->doFlow("");
  34. result = FlowControll[i]->getHTMLSingleStep(_host);
  35. }
  36. return result;
  37. }
  38. std::vector<HTMLInfo*> ClassFlowControll::GetAllDigital()
  39. {
  40. for (int i = 0; i < FlowControll.size(); ++i)
  41. if (FlowControll[i]->name().compare("ClassFlowDigit") == 0)
  42. return ((ClassFlowDigit*) (FlowControll[i]))->GetHTMLInfo();
  43. std::vector<HTMLInfo*> empty;
  44. return empty;
  45. }
  46. std::vector<HTMLInfo*> ClassFlowControll::GetAllAnalog()
  47. {
  48. for (int i = 0; i < FlowControll.size(); ++i)
  49. if (FlowControll[i]->name().compare("ClassFlowAnalog") == 0)
  50. return ((ClassFlowAnalog*) (FlowControll[i]))->GetHTMLInfo();
  51. std::vector<HTMLInfo*> empty;
  52. return empty;
  53. }
  54. void ClassFlowControll::SetInitialParameter(void)
  55. {
  56. AutoStart = false;
  57. SetupModeActive = false;
  58. AutoIntervall = 10;
  59. flowdigit = NULL;
  60. flowanalog = NULL;
  61. flowpostprocessing = NULL;
  62. disabled = false;
  63. }
  64. bool ClassFlowControll::isAutoStart(long &_intervall)
  65. {
  66. _intervall = AutoIntervall * 60 * 1000; // AutoIntervall: Minuten -> ms
  67. return AutoStart;
  68. }
  69. ClassFlow* ClassFlowControll::CreateClassFlow(std::string _type)
  70. {
  71. ClassFlow* cfc = NULL;
  72. _type = trim(_type);
  73. if (toUpper(_type).compare("[MAKEIMAGE]") == 0)
  74. {
  75. cfc = new ClassFlowMakeImage(&FlowControll);
  76. flowmakeimage = (ClassFlowMakeImage*) cfc;
  77. }
  78. if (toUpper(_type).compare("[ALIGNMENT]") == 0)
  79. {
  80. cfc = new ClassFlowAlignment(&FlowControll);
  81. flowalignment = (ClassFlowAlignment*) cfc;
  82. }
  83. if (toUpper(_type).compare("[ANALOG]") == 0)
  84. {
  85. cfc = new ClassFlowAnalog(&FlowControll);
  86. flowanalog = (ClassFlowAnalog*) cfc;
  87. }
  88. if (toUpper(_type).compare("[DIGITS]") == 0)
  89. {
  90. cfc = new ClassFlowDigit(&FlowControll);
  91. flowdigit = (ClassFlowDigit*) cfc;
  92. }
  93. if (toUpper(_type).compare("[MQTT]") == 0)
  94. cfc = new ClassFlowMQTT(&FlowControll);
  95. if (toUpper(_type).compare("[POSTPROCESSING]") == 0)
  96. {
  97. cfc = new ClassFlowPostProcessing(&FlowControll);
  98. flowpostprocessing = (ClassFlowPostProcessing*) cfc;
  99. }
  100. if (cfc) // Wird nur angehangen, falls es nicht [AutoTimer] ist, denn dieses ist für FlowControll
  101. FlowControll.push_back(cfc);
  102. if (toUpper(_type).compare("[AUTOTIMER]") == 0)
  103. cfc = this;
  104. if (toUpper(_type).compare("[DEBUG]") == 0)
  105. cfc = this;
  106. if (toUpper(_type).compare("[SYSTEM]") == 0)
  107. cfc = this;
  108. return cfc;
  109. }
  110. void ClassFlowControll::InitFlow(std::string config)
  111. {
  112. string line;
  113. flowpostprocessing = NULL;
  114. ClassFlow* cfc;
  115. FILE* pFile;
  116. config = FormatFileName(config);
  117. pFile = OpenFileAndWait(config.c_str(), "r");
  118. line = "";
  119. char zw[1024];
  120. if (pFile != NULL)
  121. {
  122. fgets(zw, 1024, pFile);
  123. printf("%s", zw);
  124. line = std::string(zw);
  125. }
  126. while ((line.size() > 0) && !(feof(pFile)))
  127. {
  128. cfc = CreateClassFlow(line);
  129. if (cfc)
  130. {
  131. printf("Start ReadParameter\n");
  132. cfc->ReadParameter(pFile, line);
  133. }
  134. else
  135. {
  136. fgets(zw, 1024, pFile);
  137. printf("%s", zw);
  138. line = std::string(zw);
  139. }
  140. }
  141. fclose(pFile);
  142. }
  143. std::string ClassFlowControll::getActStatus(){
  144. return aktstatus;
  145. }
  146. void ClassFlowControll::doFlowMakeImageOnly(string time){
  147. std::string zw_time;
  148. for (int i = 0; i < FlowControll.size(); ++i)
  149. {
  150. if (FlowControll[i]->name() == "ClassFlowMakeImage") {
  151. zw_time = gettimestring("%Y%m%d-%H%M%S");
  152. aktstatus = zw_time + ": " + FlowControll[i]->name();
  153. string zw = "FlowControll.doFlowMakeImageOnly - " + FlowControll[i]->name();
  154. FlowControll[i]->doFlow(time);
  155. }
  156. }
  157. }
  158. bool ClassFlowControll::doFlow(string time)
  159. {
  160. // CleanTempFolder(); // dazu muss man noch eine Rolling einführen
  161. bool result = true;
  162. std::string zw_time;
  163. int repeat = 0;
  164. #ifdef DEBUG_DETAIL_ON
  165. LogFile.WriteHeapInfo("ClassFlowAnalog::doFlow - Start");
  166. #endif
  167. for (int i = 0; i < FlowControll.size(); ++i)
  168. {
  169. zw_time = gettimestring("%Y%m%d-%H%M%S");
  170. aktstatus = zw_time + ": " + FlowControll[i]->name();
  171. // #ifdef DEBUG_DETAIL_ON
  172. string zw = "FlowControll.doFlow - " + FlowControll[i]->name();
  173. LogFile.WriteHeapInfo(zw);
  174. // #endif
  175. if (!FlowControll[i]->doFlow(time)){
  176. repeat++;
  177. LogFile.WriteToFile("Fehler im vorheriger Schritt - wird zum " + to_string(repeat) + ". Mal wiederholt");
  178. i = -1; // vorheriger Schritt muss wiederholt werden (vermutlich Bilder aufnehmen)
  179. result = false;
  180. if (repeat > 5) {
  181. LogFile.WriteToFile("Wiederholung 5x nicht erfolgreich --> reboot");
  182. doReboot();
  183. // Schritt wurde 5x wiederholt --> reboot
  184. }
  185. }
  186. else
  187. {
  188. result = true;
  189. }
  190. #ifdef DEBUG_DETAIL_ON
  191. LogFile.WriteHeapInfo("ClassFlowAnalog::doFlow");
  192. #endif
  193. }
  194. zw_time = gettimestring("%Y%m%d-%H%M%S");
  195. aktstatus = zw_time + ": Flow is done";
  196. return result;
  197. }
  198. string ClassFlowControll::getReadout(bool _rawvalue = false, bool _noerror = false)
  199. {
  200. if (flowpostprocessing)
  201. return flowpostprocessing->getReadoutParam(_rawvalue, _noerror);
  202. string zw = "";
  203. string result = "";
  204. for (int i = 0; i < FlowControll.size(); ++i)
  205. {
  206. zw = FlowControll[i]->getReadout();
  207. if (zw.length() > 0)
  208. {
  209. if (result.length() == 0)
  210. result = zw;
  211. else
  212. result = result + "\t" + zw;
  213. }
  214. }
  215. return result;
  216. }
  217. string ClassFlowControll::GetPrevalue()
  218. {
  219. if (flowpostprocessing)
  220. {
  221. return flowpostprocessing->GetPreValue();
  222. }
  223. return std::string();
  224. }
  225. std::string ClassFlowControll::UpdatePrevalue(std::string _newvalue)
  226. {
  227. float zw;
  228. char* p;
  229. _newvalue = trim(_newvalue);
  230. // printf("Input UpdatePreValue: %s\n", _newvalue.c_str());
  231. if (_newvalue.compare("0.0") == 0)
  232. {
  233. zw = 0;
  234. }
  235. else
  236. {
  237. zw = strtof(_newvalue.c_str(), &p);
  238. if (zw == 0)
  239. return "- Error in String to Value Conversion!!! Must be of format value=123.456";
  240. }
  241. if (flowpostprocessing)
  242. {
  243. flowpostprocessing->SavePreValue(zw);
  244. return _newvalue;
  245. }
  246. return std::string();
  247. }
  248. bool ClassFlowControll::ReadParameter(FILE* pfile, string& aktparamgraph)
  249. {
  250. std::vector<string> zerlegt;
  251. aktparamgraph = trim(aktparamgraph);
  252. if (aktparamgraph.size() == 0)
  253. if (!this->GetNextParagraph(pfile, aktparamgraph))
  254. return false;
  255. if ((toUpper(aktparamgraph).compare("[AUTOTIMER]") != 0) && (toUpper(aktparamgraph).compare("[DEBUG]") != 0) && (toUpper(aktparamgraph).compare("[SYSTEM]") != 0)) // Paragraph passt nicht zu MakeImage
  256. return false;
  257. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  258. {
  259. zerlegt = this->ZerlegeZeile(aktparamgraph, " =");
  260. if ((toUpper(zerlegt[0]) == "AUTOSTART") && (zerlegt.size() > 1))
  261. {
  262. if (toUpper(zerlegt[1]) == "TRUE")
  263. {
  264. AutoStart = true;
  265. }
  266. }
  267. if ((toUpper(zerlegt[0]) == "INTERVALL") && (zerlegt.size() > 1))
  268. {
  269. AutoIntervall = std::stof(zerlegt[1]);
  270. }
  271. if ((toUpper(zerlegt[0]) == "LOGFILE") && (zerlegt.size() > 1))
  272. {
  273. if (toUpper(zerlegt[1]) == "TRUE")
  274. {
  275. LogFile.SwitchOnOff(true);
  276. }
  277. if (toUpper(zerlegt[1]) == "FALSE")
  278. {
  279. LogFile.SwitchOnOff(false);
  280. }
  281. }
  282. if ((toUpper(zerlegt[0]) == "LOGFILERETENTIONINDAYS") && (zerlegt.size() > 1))
  283. {
  284. LogFile.SetRetention(std::stoi(zerlegt[1]));
  285. }
  286. if ((toUpper(zerlegt[0]) == "TIMEZONE") && (zerlegt.size() > 1))
  287. {
  288. string zw = "Set TimeZone: " + zerlegt[1];
  289. setTimeZone(zerlegt[1]);
  290. }
  291. if ((toUpper(zerlegt[0]) == "TIMESERVER") && (zerlegt.size() > 1))
  292. {
  293. string zw = "Set TimeZone: " + zerlegt[1];
  294. reset_servername(zerlegt[1]);
  295. }
  296. if ((toUpper(zerlegt[0]) == "HOSTNAME") && (zerlegt.size() > 1))
  297. {
  298. if (ChangeHostName("/sdcard/wlan.ini", zerlegt[1]))
  299. {
  300. // reboot notwendig damit die neue wlan.ini auch benutzt wird !!!
  301. fclose(pfile);
  302. doReboot();
  303. }
  304. }
  305. if ((toUpper(zerlegt[0]) == "SETUPMODE") && (zerlegt.size() > 1))
  306. {
  307. if (toUpper(zerlegt[1]) == "TRUE")
  308. {
  309. SetupModeActive = true;
  310. }
  311. }
  312. }
  313. return true;
  314. }
  315. int ClassFlowControll::CleanTempFolder() {
  316. const char* folderPath = "/sdcard/img_tmp";
  317. ESP_LOGI(TAG, "Clean up temporary folder to avoid damage of sdcard sectors : %s", folderPath);
  318. DIR *dir = opendir(folderPath);
  319. if (!dir) {
  320. ESP_LOGE(TAG, "Failed to stat dir : %s", folderPath);
  321. return -1;
  322. }
  323. struct dirent *entry;
  324. int deleted = 0;
  325. while ((entry = readdir(dir)) != NULL) {
  326. std::string path = string(folderPath) + "/" + entry->d_name;
  327. if (entry->d_type == DT_REG) {
  328. if (unlink(path.c_str()) == 0) {
  329. deleted ++;
  330. } else {
  331. ESP_LOGE(TAG, "can't delete file : %s", path.c_str());
  332. }
  333. } else if (entry->d_type == DT_DIR) {
  334. deleted += removeFolder(path.c_str(), TAG);
  335. }
  336. }
  337. closedir(dir);
  338. ESP_LOGI(TAG, "%d files deleted", deleted);
  339. return 0;
  340. }
  341. esp_err_t ClassFlowControll::SendRawJPG(httpd_req_t *req)
  342. {
  343. return flowmakeimage->SendRawJPG(req);
  344. }
  345. esp_err_t ClassFlowControll::GetJPGStream(std::string _fn, httpd_req_t *req)
  346. {
  347. printf("ClassFlowControll::GetJPGStream %s\n", _fn.c_str());
  348. CImageBasis *_send = NULL;
  349. esp_err_t result = ESP_FAIL;
  350. bool Dodelete = false;
  351. if (_fn == "alg.jpg")
  352. {
  353. _send = flowalignment->ImageBasis;
  354. }
  355. if (_fn == "alg_roi.jpg")
  356. {
  357. CImageBasis* _imgzw = new CImageBasis(flowalignment->ImageBasis);
  358. flowalignment->DrawRef(_imgzw);
  359. if (flowdigit) flowdigit->DrawROI(_imgzw);
  360. if (flowanalog) flowanalog->DrawROI(_imgzw);
  361. _send = _imgzw;
  362. Dodelete = true;
  363. }
  364. std::vector<HTMLInfo*> htmlinfo;
  365. htmlinfo = GetAllDigital();
  366. for (int i = 0; i < htmlinfo.size(); ++i)
  367. {
  368. if (_fn == htmlinfo[i]->filename)
  369. {
  370. if (htmlinfo[i]->image)
  371. _send = htmlinfo[i]->image;
  372. }
  373. if (_fn == htmlinfo[i]->filename_org)
  374. {
  375. if (htmlinfo[i]->image_org)
  376. _send = htmlinfo[i]->image_org;
  377. }
  378. }
  379. htmlinfo = GetAllAnalog();
  380. for (int i = 0; i < htmlinfo.size(); ++i)
  381. {
  382. if (_fn == htmlinfo[i]->filename)
  383. {
  384. if (htmlinfo[i]->image)
  385. _send = htmlinfo[i]->image;
  386. }
  387. if (_fn == htmlinfo[i]->filename_org)
  388. {
  389. if (htmlinfo[i]->image_org)
  390. _send = htmlinfo[i]->image_org;
  391. }
  392. }
  393. if (_send)
  394. {
  395. ESP_LOGI(TAG, "Sending file : %s ...", _fn.c_str());
  396. set_content_type_from_file(req, _fn.c_str());
  397. result = _send->SendJPGtoHTTP(req);
  398. ESP_LOGI(TAG, "File sending complete");
  399. /* Respond with an empty chunk to signal HTTP response completion */
  400. httpd_resp_send_chunk(req, NULL, 0);
  401. }
  402. if (Dodelete)
  403. {
  404. delete _send;
  405. }
  406. return result;
  407. }