ClassFlowControll.cpp 16 KB

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