ClassFlowControll.cpp 15 KB

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