ClassFlowControll.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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 <new>
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #include <dirent.h>
  11. #ifdef __cplusplus
  12. }
  13. #endif
  14. #include "ClassLogFile.h"
  15. #include "time_sntp.h"
  16. #include "Helper.h"
  17. #include "server_ota.h"
  18. #ifdef ENABLE_MQTT
  19. #include "interface_mqtt.h"
  20. #include "server_mqtt.h"
  21. #endif //ENABLE_MQTT
  22. #include "server_help.h"
  23. #include "MainFlowControl.h"
  24. #include "basic_auth.h"
  25. #include "../../include/defines.h"
  26. static const char* TAG = "FLOWCTRL";
  27. //#define DEBUG_DETAIL_ON
  28. std::string ClassFlowControll::doSingleStep(std::string _stepname, std::string _host)
  29. {
  30. std::string _classname = "";
  31. std::string result = "";
  32. ESP_LOGD(TAG, "Step %s start", _stepname.c_str());
  33. if ((_stepname.compare("[TakeImage]") == 0) || (_stepname.compare(";[TakeImage]") == 0)) {
  34. _classname = "ClassFlowTakeImage";
  35. }
  36. if ((_stepname.compare("[Alignment]") == 0) || (_stepname.compare(";[Alignment]") == 0)) {
  37. _classname = "ClassFlowAlignment";
  38. }
  39. if ((_stepname.compare(0, 7, "[Digits") == 0) || (_stepname.compare(0, 8, ";[Digits") == 0)) {
  40. _classname = "ClassFlowCNNGeneral";
  41. }
  42. if ((_stepname.compare("[Analog]") == 0) || (_stepname.compare(";[Analog]") == 0)) {
  43. _classname = "ClassFlowCNNGeneral";
  44. }
  45. #ifdef ENABLE_MQTT
  46. if ((_stepname.compare("[MQTT]") == 0) || (_stepname.compare(";[MQTT]") == 0)) {
  47. _classname = "ClassFlowMQTT";
  48. }
  49. #endif //ENABLE_MQTT
  50. #ifdef ENABLE_INFLUXDB
  51. if ((_stepname.compare("[InfluxDB]") == 0) || (_stepname.compare(";[InfluxDB]") == 0)) {
  52. _classname = "ClassFlowInfluxDB";
  53. }
  54. if ((_stepname.compare("[InfluxDBv2]") == 0) || (_stepname.compare(";[InfluxDBv2]") == 0)) {
  55. _classname = "ClassFlowInfluxDBv2";
  56. }
  57. #endif //ENABLE_INFLUXDB
  58. #ifdef ENABLE_WEBHOOK
  59. if ((_stepname.compare("[Webhook]") == 0) || (_stepname.compare(";[Webhook]") == 0)) {
  60. _classname = "ClassFlowWebhook";
  61. }
  62. #endif //ENABLE_WEBHOOK
  63. for (int i = 0; i < FlowControll.size(); ++i) {
  64. if (FlowControll[i]->name().compare(_classname) == 0) {
  65. if (!(FlowControll[i]->name().compare("ClassFlowTakeImage") == 0)) {
  66. // if it is a TakeImage, the image does not need to be included, this happens automatically with the html query.
  67. FlowControll[i]->doFlow("");
  68. }
  69. result = FlowControll[i]->getHTMLSingleStep(_host);
  70. }
  71. }
  72. ESP_LOGD(TAG, "Step %s end", _stepname.c_str());
  73. return result;
  74. }
  75. std::string ClassFlowControll::TranslateAktstatus(std::string _input)
  76. {
  77. if (_input.compare("ClassFlowTakeImage") == 0) {
  78. return ("Take Image");
  79. }
  80. if (_input.compare("ClassFlowAlignment") == 0) {
  81. return ("Aligning");
  82. }
  83. if (_input.compare("ClassFlowCNNGeneral") == 0) {
  84. return ("Digitization of ROIs");
  85. }
  86. #ifdef ENABLE_MQTT
  87. if (_input.compare("ClassFlowMQTT") == 0) {
  88. return ("Sending MQTT");
  89. }
  90. #endif //ENABLE_MQTT
  91. #ifdef ENABLE_INFLUXDB
  92. if (_input.compare("ClassFlowInfluxDB") == 0) {
  93. return ("Sending InfluxDB");
  94. }
  95. if (_input.compare("ClassFlowInfluxDBv2") == 0) {
  96. return ("Sending InfluxDBv2");
  97. }
  98. #endif //ENABLE_INFLUXDB
  99. #ifdef ENABLE_WEBHOOK
  100. if (_input.compare("ClassFlowWebhook") == 0) {
  101. return ("Sending Webhook");
  102. }
  103. #endif //ENABLE_WEBHOOK
  104. if (_input.compare("ClassFlowPostProcessing") == 0) {
  105. return ("Post-Processing");
  106. }
  107. return "Unkown Status";
  108. }
  109. std::vector<HTMLInfo*> ClassFlowControll::GetAllDigit()
  110. {
  111. if (flowdigit) {
  112. ESP_LOGD(TAG, "ClassFlowControll::GetAllDigit - flowdigit != NULL");
  113. return flowdigit->GetHTMLInfo();
  114. }
  115. std::vector<HTMLInfo*> empty;
  116. return empty;
  117. }
  118. std::vector<HTMLInfo*> ClassFlowControll::GetAllAnalog()
  119. {
  120. if (flowanalog) {
  121. return flowanalog->GetHTMLInfo();
  122. }
  123. std::vector<HTMLInfo*> empty;
  124. return empty;
  125. }
  126. t_CNNType ClassFlowControll::GetTypeDigit()
  127. {
  128. if (flowdigit) {
  129. return flowdigit->getCNNType();
  130. }
  131. return t_CNNType::None;
  132. }
  133. t_CNNType ClassFlowControll::GetTypeAnalog()
  134. {
  135. if (flowanalog) {
  136. return flowanalog->getCNNType();
  137. }
  138. return t_CNNType::None;
  139. }
  140. #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
  141. void ClassFlowControll::DigitDrawROI(CImageBasis *_zw)
  142. {
  143. if (flowdigit) {
  144. flowdigit->DrawROI(_zw);
  145. }
  146. }
  147. void ClassFlowControll::AnalogDrawROI(CImageBasis *_zw)
  148. {
  149. if (flowanalog) {
  150. flowanalog->DrawROI(_zw);
  151. }
  152. }
  153. #endif
  154. #ifdef ENABLE_MQTT
  155. bool ClassFlowControll::StartMQTTService()
  156. {
  157. /* Start the MQTT service */
  158. for (int i = 0; i < FlowControll.size(); ++i) {
  159. if (FlowControll[i]->name().compare("ClassFlowMQTT") == 0) {
  160. return ((ClassFlowMQTT*) (FlowControll[i]))->Start(AutoInterval);
  161. }
  162. }
  163. return false;
  164. }
  165. #endif //ENABLE_MQTT
  166. ClassFlowControll::ClassFlowControll()
  167. {
  168. SetInitialParameter();
  169. }
  170. ClassFlowControll::~ClassFlowControll()
  171. {
  172. // Destroy dependants before the flows they reference (PostProcessing, CNN,
  173. // Alignment, then TakeImage), so borrowed image pointers remain valid.
  174. for (auto it = FlowControll.rbegin(); it != FlowControll.rend(); ++it) {
  175. delete *it;
  176. }
  177. FlowControll.clear();
  178. flowpostprocessing = NULL;
  179. flowdigit = NULL;
  180. flowanalog = NULL;
  181. flowalignment = NULL;
  182. flowtakeimage = NULL;
  183. }
  184. void ClassFlowControll::SetInitialParameter(void)
  185. {
  186. AutoStart = true;
  187. SetupModeActive = false;
  188. AutoInterval = 10;
  189. flowdigit = NULL;
  190. flowanalog = NULL;
  191. flowpostprocessing = NULL;
  192. flowalignment = NULL;
  193. flowtakeimage = NULL;
  194. disabled = false;
  195. aktRunNr = 0;
  196. setStatus("Flow task not yet created", "Flow task not yet created");
  197. }
  198. bool ClassFlowControll::getIsAutoStart(void)
  199. {
  200. //return AutoStart;
  201. return true; // Flow must always be enabled, else the manual trigger (REST, MQTT) will not work!
  202. }
  203. void ClassFlowControll::setAutoStartInterval(long &_interval)
  204. {
  205. _interval = AutoInterval * 60 * 1000; // AutoInterval: minutes -> ms
  206. }
  207. ClassFlow* ClassFlowControll::CreateClassFlow(std::string _type)
  208. {
  209. ClassFlow* cfc = NULL;
  210. _type = trim(_type);
  211. if (toUpper(_type).compare("[TAKEIMAGE]") == 0) {
  212. cfc = new (std::nothrow) ClassFlowTakeImage(&FlowControll);
  213. flowtakeimage = (ClassFlowTakeImage*) cfc;
  214. }
  215. if (toUpper(_type).compare("[ALIGNMENT]") == 0) {
  216. cfc = new (std::nothrow) ClassFlowAlignment(&FlowControll);
  217. flowalignment = (ClassFlowAlignment*) cfc;
  218. }
  219. if (toUpper(_type).compare("[ANALOG]") == 0) {
  220. cfc = new (std::nothrow) ClassFlowCNNGeneral(flowalignment);
  221. flowanalog = (ClassFlowCNNGeneral*) cfc;
  222. }
  223. if (toUpper(_type).compare(0, 7, "[DIGITS") == 0) {
  224. cfc = new (std::nothrow) ClassFlowCNNGeneral(flowalignment);
  225. flowdigit = (ClassFlowCNNGeneral*) cfc;
  226. }
  227. #ifdef ENABLE_MQTT
  228. if (toUpper(_type).compare("[MQTT]") == 0) {
  229. cfc = new (std::nothrow) ClassFlowMQTT(&FlowControll);
  230. }
  231. #endif //ENABLE_MQTT
  232. #ifdef ENABLE_INFLUXDB
  233. if (toUpper(_type).compare("[INFLUXDB]") == 0) {
  234. cfc = new (std::nothrow) ClassFlowInfluxDB(&FlowControll);
  235. }
  236. if (toUpper(_type).compare("[INFLUXDBV2]") == 0) {
  237. cfc = new (std::nothrow) ClassFlowInfluxDBv2(&FlowControll);
  238. }
  239. #endif //ENABLE_INFLUXDB
  240. #ifdef ENABLE_WEBHOOK
  241. if (toUpper(_type).compare("[WEBHOOK]") == 0) {
  242. cfc = new (std::nothrow) ClassFlowWebhook(&FlowControll);
  243. }
  244. #endif //ENABLE_WEBHOOK
  245. if (toUpper(_type).compare("[POSTPROCESSING]") == 0) {
  246. cfc = new (std::nothrow) ClassFlowPostProcessing(&FlowControll, flowanalog, flowdigit);
  247. flowpostprocessing = (ClassFlowPostProcessing*) cfc;
  248. }
  249. const std::string normalizedType = toUpper(_type);
  250. const bool disabledSection = !_type.empty() && (_type[0] == ';');
  251. if (cfc) {
  252. FlowControll.push_back(cfc);
  253. } else if (!disabledSection &&
  254. normalizedType != "[AUTOTIMER]" && normalizedType != "[DATALOGGING]" &&
  255. normalizedType != "[DEBUG]" && normalizedType != "[SYSTEM]") {
  256. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to allocate flow component " + _type);
  257. }
  258. if (toUpper(_type).compare("[AUTOTIMER]") == 0) {
  259. cfc = this;
  260. }
  261. if (toUpper(_type).compare("[DATALOGGING]") == 0) {
  262. cfc = this;
  263. }
  264. if (toUpper(_type).compare("[DEBUG]") == 0) {
  265. cfc = this;
  266. }
  267. if (toUpper(_type).compare("[SYSTEM]") == 0) {
  268. cfc = this;
  269. }
  270. return cfc;
  271. }
  272. void ClassFlowControll::InitFlow(std::string config)
  273. {
  274. setStatus("Initialization", "Initialization");
  275. string line;
  276. flowpostprocessing = NULL;
  277. ClassFlow* cfc;
  278. FILE* pFile;
  279. config = FormatFileName(config);
  280. pFile = fopen(config.c_str(), "r");
  281. line = "";
  282. char zw[1024];
  283. if (pFile != NULL) {
  284. fgets(zw, 1024, pFile);
  285. ESP_LOGD(TAG, "%s", zw);
  286. line = std::string(zw);
  287. }
  288. while ((line.size() > 0) && !(feof(pFile))) {
  289. cfc = CreateClassFlow(line);
  290. // printf("Name: %s\n", cfc->name().c_str());
  291. if (cfc) {
  292. ESP_LOGI(TAG, "Start ReadParameter (%s)", line.c_str());
  293. cfc->ReadParameter(pFile, line);
  294. }
  295. else {
  296. // This controller only instantiates flow steps. Other or disabled
  297. // config sections (for example GPIO and commented-out integrations)
  298. // are handled elsewhere or intentionally inactive. Skip their whole
  299. // paragraph instead of treating each key/value line as a flow name.
  300. if (!this->GetNextParagraph(pFile, line)) {
  301. line.clear();
  302. }
  303. }
  304. }
  305. fclose(pFile);
  306. }
  307. std::string ClassFlowControll::getActStatusWithTime()
  308. {
  309. std::lock_guard<std::mutex> lock(statusMutex);
  310. return aktstatusWithTime;
  311. }
  312. std::string ClassFlowControll::getActStatus()
  313. {
  314. std::lock_guard<std::mutex> lock(statusMutex);
  315. return aktstatus;
  316. }
  317. void ClassFlowControll::setStatus(const std::string &_status, const std::string &_statusWithTime)
  318. {
  319. std::lock_guard<std::mutex> lock(statusMutex);
  320. aktstatus = _status;
  321. aktstatusWithTime = _statusWithTime;
  322. }
  323. void ClassFlowControll::setActStatus(std::string _aktstatus)
  324. {
  325. setStatus(_aktstatus, _aktstatus);
  326. }
  327. void ClassFlowControll::doFlowTakeImageOnly(string time)
  328. {
  329. std::string zw_time;
  330. for (int i = 0; i < FlowControll.size(); ++i) {
  331. if (FlowControll[i]->name() == "ClassFlowTakeImage") {
  332. zw_time = getCurrentTimeString("%H:%M:%S");
  333. std::string status = TranslateAktstatus(FlowControll[i]->name());
  334. setStatus(status, status + " (" + zw_time + ")");
  335. #ifdef ENABLE_MQTT
  336. MQTTPublish(mqttServer_getMainTopic() + "/" + "status", aktstatus, 1, false);
  337. #endif //ENABLE_MQTT
  338. FlowControll[i]->doFlow(time);
  339. }
  340. }
  341. }
  342. bool ClassFlowControll::doFlow(string time)
  343. {
  344. bool result = true;
  345. std::string zw_time;
  346. int repeat = 0;
  347. int qos = 1;
  348. #ifdef DEBUG_DETAIL_ON
  349. LogFile.WriteHeapInfo("ClassFlowControll::doFlow - Start");
  350. #endif
  351. for (int i = 0; i < FlowControll.size(); ++i) {
  352. zw_time = getCurrentTimeString("%H:%M:%S");
  353. std::string status = TranslateAktstatus(FlowControll[i]->name());
  354. setStatus(status, status + " (" + zw_time + ")");
  355. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Status: " + aktstatusWithTime);
  356. #ifdef ENABLE_MQTT
  357. MQTTPublish(mqttServer_getMainTopic() + "/" + "status", aktstatus, qos, false);
  358. #endif //ENABLE_MQTT
  359. #ifdef DEBUG_DETAIL_ON
  360. string zw = "FlowControll.doFlow - " + FlowControll[i]->name();
  361. LogFile.WriteHeapInfo(zw);
  362. #endif
  363. if (!FlowControll[i]->doFlow(time)) {
  364. repeat++;
  365. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Fehler im vorheriger Schritt - wird zum " + to_string(repeat) + ". Mal wiederholt");
  366. if (i) { i -= 1; } // vPrevious step must be repeated (probably take pictures)
  367. result = false;
  368. if (repeat > 5) {
  369. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Wiederholung 5x nicht erfolgreich --> reboot");
  370. doReboot();
  371. //Step was repeated 5x --> reboot
  372. }
  373. }
  374. else {
  375. result = true;
  376. }
  377. #ifdef DEBUG_DETAIL_ON
  378. LogFile.WriteHeapInfo("ClassFlowControll::doFlow");
  379. #endif
  380. }
  381. zw_time = getCurrentTimeString("%H:%M:%S");
  382. setStatus("Flow finished", std::string("Flow finished (") + zw_time + ")");
  383. //LogFile.WriteToFile(ESP_LOG_INFO, TAG, aktstatusWithTime);
  384. #ifdef ENABLE_MQTT
  385. MQTTPublish(mqttServer_getMainTopic() + "/" + "status", aktstatus, qos, false);
  386. #endif //ENABLE_MQTT
  387. return result;
  388. }
  389. string ClassFlowControll::getReadoutAll(int _type)
  390. {
  391. std::string out = "";
  392. if (flowpostprocessing) {
  393. std::vector<NumberPost*> *numbers = flowpostprocessing->GetNumbers();
  394. for (int i = 0; i < (*numbers).size(); ++i) {
  395. out = out + (*numbers)[i]->name + "\t";
  396. switch (_type) {
  397. case READOUT_TYPE_VALUE:
  398. out = out + (*numbers)[i]->ReturnValue;
  399. break;
  400. case READOUT_TYPE_PREVALUE:
  401. if (flowpostprocessing->PreValueUse) {
  402. if ((*numbers)[i]->PreValueOkay) {
  403. out = out + (*numbers)[i]->ReturnPreValue;
  404. }
  405. else {
  406. out = out + "PreValue too old";
  407. }
  408. }
  409. else {
  410. out = out + "PreValue deactivated";
  411. }
  412. break;
  413. case READOUT_TYPE_RAWVALUE:
  414. out = out + (*numbers)[i]->ReturnRawValue;
  415. break;
  416. case READOUT_TYPE_ERROR:
  417. out = out + (*numbers)[i]->ErrorMessageText;
  418. break;
  419. }
  420. if (i < (*numbers).size()-1) {
  421. out = out + "\r\n";
  422. }
  423. }
  424. // ESP_LOGD(TAG, "OUT: %s", out.c_str());
  425. }
  426. return out;
  427. }
  428. string ClassFlowControll::getReadout(bool _rawvalue = false, bool _noerror = false, int _number = 0)
  429. {
  430. if (flowpostprocessing) {
  431. return flowpostprocessing->getReadoutParam(_rawvalue, _noerror, _number);
  432. }
  433. return std::string("");
  434. }
  435. string ClassFlowControll::GetPrevalue(std::string _number)
  436. {
  437. if (flowpostprocessing) {
  438. return flowpostprocessing->GetPreValue(_number);
  439. }
  440. return std::string("");
  441. }
  442. bool ClassFlowControll::UpdatePrevalue(std::string _newvalue, std::string _numbers, bool _extern)
  443. {
  444. double newvalueAsDouble;
  445. char* p;
  446. _newvalue = trim(_newvalue);
  447. // ESP_LOGD(TAG, "Input UpdatePreValue: %s", _newvalue.c_str());
  448. if (_newvalue.substr(0,8).compare("0.000000") == 0 || _newvalue.compare("0.0") == 0 || _newvalue.compare("0") == 0) {
  449. newvalueAsDouble = 0; // preset to value = 0
  450. }
  451. else {
  452. newvalueAsDouble = strtod(_newvalue.c_str(), &p);
  453. if (newvalueAsDouble == 0) {
  454. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "UpdatePrevalue: No valid value for processing: " + _newvalue);
  455. return false;
  456. }
  457. }
  458. if (flowpostprocessing) {
  459. if (flowpostprocessing->SetPreValue(newvalueAsDouble, _numbers, _extern)) {
  460. return true;
  461. }
  462. else {
  463. return false;
  464. }
  465. }
  466. else {
  467. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "UpdatePrevalue: ERROR - Class Post-Processing not initialized");
  468. return false;
  469. }
  470. }
  471. bool ClassFlowControll::ReadParameter(FILE* pfile, string& aktparamgraph)
  472. {
  473. std::vector<string> splitted;
  474. aktparamgraph = trim(aktparamgraph);
  475. if (aktparamgraph.size() == 0) {
  476. if (!this->GetNextParagraph(pfile, aktparamgraph)) {
  477. return false;
  478. }
  479. }
  480. if ((toUpper(aktparamgraph).compare("[AUTOTIMER]") != 0) && (toUpper(aktparamgraph).compare("[DEBUG]") != 0) &&
  481. (toUpper(aktparamgraph).compare("[SYSTEM]") != 0 && (toUpper(aktparamgraph).compare("[DATALOGGING]") != 0))) {
  482. // Paragraph passt nicht zu Debug oder DataLogging
  483. return false;
  484. }
  485. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph)) {
  486. splitted = ZerlegeZeile(aktparamgraph, " =");
  487. if ((toUpper(splitted[0]) == "INTERVAL") && (splitted.size() > 1)) {
  488. if (isStringNumeric(splitted[1])) {
  489. AutoInterval = std::stof(splitted[1]);
  490. }
  491. }
  492. if ((toUpper(splitted[0]) == "DATALOGACTIVE") && (splitted.size() > 1)) {
  493. LogFile.SetDataLogToSD(alphanumericToBoolean(splitted[1]));
  494. }
  495. if ((toUpper(splitted[0]) == "DATAFILESRETENTION") && (splitted.size() > 1)) {
  496. if (isStringNumeric(splitted[1])) {
  497. LogFile.SetDataLogRetention(std::stoi(splitted[1]));
  498. }
  499. }
  500. if ((toUpper(splitted[0]) == "LOGLEVEL") && (splitted.size() > 1)) {
  501. /* matches esp_log_level_t */
  502. if ((toUpper(splitted[1]) == "TRUE") || (toUpper(splitted[1]) == "2")) {
  503. LogFile.setLogLevel(ESP_LOG_WARN);
  504. }
  505. else if ((toUpper(splitted[1]) == "FALSE") || (toUpper(splitted[1]) == "0") || (toUpper(splitted[1]) == "1")) {
  506. LogFile.setLogLevel(ESP_LOG_ERROR);
  507. }
  508. else if (toUpper(splitted[1]) == "3") {
  509. LogFile.setLogLevel(ESP_LOG_INFO);
  510. }
  511. else if (toUpper(splitted[1]) == "4") {
  512. LogFile.setLogLevel(ESP_LOG_DEBUG);
  513. }
  514. /* If system reboot was not triggered by user and reboot was caused by execption -> keep log level to DEBUG */
  515. if (!getIsPlannedReboot() && (esp_reset_reason() == ESP_RST_PANIC)) {
  516. LogFile.setLogLevel(ESP_LOG_DEBUG);
  517. }
  518. }
  519. if ((toUpper(splitted[0]) == "LOGFILESRETENTION") && (splitted.size() > 1)) {
  520. if (isStringNumeric(splitted[1])) {
  521. LogFile.SetLogFileRetention(std::stoi(splitted[1]));
  522. }
  523. }
  524. /* TimeServer and TimeZone got already read from the config, see setupTime () */
  525. #if (defined WLAN_USE_ROAMING_BY_SCANNING || (defined WLAN_USE_MESH_ROAMING && defined WLAN_USE_MESH_ROAMING_ACTIVATE_CLIENT_TRIGGERED_QUERIES))
  526. if ((toUpper(splitted[0]) == "RSSITHRESHOLD") && (splitted.size() > 1)) {
  527. int RSSIThresholdTMP = atoi(splitted[1].c_str());
  528. RSSIThresholdTMP = min(0, max(-100, RSSIThresholdTMP)); // Verify input limits (-100 - 0)
  529. if (ChangeRSSIThreshold(WLAN_CONFIG_FILE, RSSIThresholdTMP)) {
  530. // reboot necessary so that the new wlan.ini is also used !!!
  531. fclose(pfile);
  532. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Rebooting to activate new RSSITHRESHOLD ...");
  533. doReboot();
  534. }
  535. }
  536. #endif
  537. if ((toUpper(splitted[0]) == "HOSTNAME") && (splitted.size() > 1)) {
  538. if (ChangeHostName(WLAN_CONFIG_FILE, splitted[1])) {
  539. // reboot necessary so that the new wlan.ini is also used !!!
  540. fclose(pfile);
  541. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Rebooting to activate new HOSTNAME...");
  542. doReboot();
  543. }
  544. }
  545. if ((toUpper(splitted[0]) == "SETUPMODE") && (splitted.size() > 1)) {
  546. SetupModeActive = alphanumericToBoolean(splitted[1]);
  547. }
  548. }
  549. return true;
  550. }
  551. int ClassFlowControll::CleanTempFolder() {
  552. const char* folderPath = "/sdcard/img_tmp";
  553. ESP_LOGD(TAG, "Clean up temporary folder to avoid damage of sdcard sectors: %s", folderPath);
  554. DIR *dir = opendir(folderPath);
  555. if (!dir) {
  556. ESP_LOGE(TAG, "Failed to stat dir: %s", folderPath);
  557. return -1;
  558. }
  559. struct dirent *entry;
  560. int deleted = 0;
  561. while ((entry = readdir(dir)) != NULL) {
  562. std::string path = string(folderPath) + "/" + entry->d_name;
  563. if (entry->d_type == DT_REG) {
  564. if (unlink(path.c_str()) == 0) {
  565. deleted ++;
  566. }
  567. else {
  568. ESP_LOGE(TAG, "can't delete file: %s", path.c_str());
  569. }
  570. }
  571. else if (entry->d_type == DT_DIR) {
  572. deleted += removeFolder(path.c_str(), TAG);
  573. }
  574. }
  575. closedir(dir);
  576. ESP_LOGD(TAG, "%d files deleted", deleted);
  577. return 0;
  578. }
  579. esp_err_t ClassFlowControll::SendRawJPG(httpd_req_t *req, bool previewCapture)
  580. {
  581. return flowtakeimage != NULL ? flowtakeimage->SendRawJPG(req, previewCapture) : ESP_FAIL;
  582. }
  583. esp_err_t ClassFlowControll::GetJPGStream(std::string _fn, httpd_req_t *req)
  584. {
  585. ESP_LOGD(TAG, "ClassFlowControll::GetJPGStream %s", _fn.c_str());
  586. #ifdef DEBUG_DETAIL_ON
  587. LogFile.WriteHeapInfo("ClassFlowControll::GetJPGStream - Start");
  588. #endif
  589. CImageBasis *_send = NULL;
  590. esp_err_t result = ESP_FAIL;
  591. bool _sendDelete = false;
  592. if (_fn == "alg.jpg") {
  593. if (flowalignment && flowalignment->ImageBasis && flowalignment->ImageBasis->ImageOkay()) {
  594. _send = flowalignment->ImageBasis;
  595. }
  596. else {
  597. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ClassFlowControll::GetJPGStream: alg.jpg cannot be served");
  598. return ESP_FAIL;
  599. }
  600. }
  601. else if (_fn == "alg_roi.jpg") {
  602. std::string status = getActStatus(); // copy: the flow task changes it while this handler runs
  603. #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG // no CImageBasis needed to create alg_roi.jpg (ca. 790kB less RAM)
  604. if (status.find("Initialization (delayed)") != -1) {
  605. std::string filename = "/sdcard/html/Flowstate_initialization_delayed.jpg";
  606. result = send_file(req, filename);
  607. }
  608. else if (status.find("Initialization") != -1) {
  609. std::string filename = "/sdcard/html/Flowstate_initialization.jpg";
  610. result = send_file(req, filename);
  611. }
  612. else if (status.find("Take Image") != -1) {
  613. if (flowalignment != NULL) {
  614. std::string filename = "/sdcard/html/Flowstate_take_image.jpg";
  615. result = send_file(req, filename);
  616. }
  617. else {
  618. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ClassFlowControll::GetJPGStream: alg_roi.jpg cannot be served -> alg.jpg is going to be served!");
  619. if (flowalignment && flowalignment->ImageBasis && flowalignment->ImageBasis->ImageOkay()) {
  620. _send = flowalignment->ImageBasis;
  621. }
  622. else {
  623. std::string filename = "/sdcard/html/Flowstate_at_work.jpg";
  624. result = send_file(req, filename);
  625. }
  626. }
  627. }
  628. else {
  629. if (flowalignment != NULL) {
  630. result = flowalignment->SendAlgROIToHTTP(req);
  631. if (result != ESP_ERR_NOT_FOUND && result != ESP_ERR_TIMEOUT) {
  632. return result;
  633. }
  634. }
  635. // A missing preview must not fall back to ImageBasis: that would
  636. // make the single HTTP server task wait on the global image lock.
  637. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "ClassFlowControll::GetJPGStream: ROI preview is not ready");
  638. httpd_resp_set_status(req, "503 Service Unavailable");
  639. httpd_resp_set_type(req, "text/plain");
  640. return httpd_resp_send(req, "ROI preview not ready", HTTPD_RESP_USE_STRLEN);
  641. }
  642. #else
  643. if (!flowalignment) {
  644. ESP_LOGD(TAG, "ClassFloDControll::GetJPGStream: FlowAlignment is not (yet) initialized. Interrupt serving!");
  645. httpd_resp_send(req, NULL, 0);
  646. return ESP_FAIL;
  647. }
  648. if (flowalignment->ImageBasis == NULL || !flowalignment->ImageBasis->ImageOkay()) {
  649. httpd_resp_send(req, NULL, 0);
  650. return ESP_FAIL;
  651. }
  652. _send = new (std::nothrow) CImageBasis("alg_roi", flowalignment->ImageBasis);
  653. if (_send != NULL && _send->ImageOkay()) {
  654. flowalignment->DrawRef(_send);
  655. if (flowdigit) flowdigit->DrawROI(_send);
  656. if (flowanalog) flowanalog->DrawROI(_send);
  657. _sendDelete = true;
  658. } else {
  659. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "ClassFlowControll::GetJPGStream: Not enough memory to create alg_roi.jpg -> alg.jpg is going to be served!");
  660. delete _send;
  661. _send = flowalignment->ImageBasis;
  662. _sendDelete = false;
  663. }
  664. #endif
  665. }
  666. else {
  667. std::vector<HTMLInfo*> htmlinfo;
  668. htmlinfo = GetAllDigit();
  669. ESP_LOGD(TAG, "After getClassFlowControll::GetAllDigit");
  670. for (int i = 0; i < htmlinfo.size(); ++i) {
  671. if (_fn == htmlinfo[i]->filename) {
  672. if (htmlinfo[i]->image) {
  673. _send = htmlinfo[i]->image;
  674. }
  675. }
  676. if (_fn == htmlinfo[i]->filename_org) {
  677. if (htmlinfo[i]->image_org) {
  678. _send = htmlinfo[i]->image_org;
  679. }
  680. }
  681. delete htmlinfo[i];
  682. }
  683. htmlinfo.clear();
  684. if (!_send) {
  685. htmlinfo = GetAllAnalog();
  686. ESP_LOGD(TAG, "After getClassFlowControll::GetAllAnalog");
  687. for (int i = 0; i < htmlinfo.size(); ++i) {
  688. if (_fn == htmlinfo[i]->filename) {
  689. if (htmlinfo[i]->image) {
  690. _send = htmlinfo[i]->image;
  691. }
  692. }
  693. if (_fn == htmlinfo[i]->filename_org) {
  694. if (htmlinfo[i]->image_org) {
  695. _send = htmlinfo[i]->image_org;
  696. }
  697. }
  698. delete htmlinfo[i];
  699. }
  700. htmlinfo.clear();
  701. }
  702. }
  703. #ifdef DEBUG_DETAIL_ON
  704. LogFile.WriteHeapInfo("ClassFlowControll::GetJPGStream - before send");
  705. #endif
  706. if (_send) {
  707. ESP_LOGD(TAG, "Sending file: %s ...", _fn.c_str());
  708. set_content_type_from_file(req, _fn.c_str());
  709. result = _send->SendJPGtoHTTP(req);
  710. /* Respond with an empty chunk to signal HTTP response completion */
  711. httpd_resp_send_chunk(req, NULL, 0);
  712. ESP_LOGD(TAG, "File sending complete");
  713. if (_sendDelete) {
  714. delete _send;
  715. }
  716. _send = NULL;
  717. }
  718. #ifdef DEBUG_DETAIL_ON
  719. LogFile.WriteHeapInfo("ClassFlowControll::GetJPGStream - done");
  720. #endif
  721. return result;
  722. }
  723. // flowpostprocessing is NULL until InitFlow() has parsed the config, and InitFlow() resets it to NULL
  724. // whenever the flow is rebuilt; REST handlers can call these at any time.
  725. string ClassFlowControll::getNumbersName()
  726. {
  727. if (flowpostprocessing) {
  728. return flowpostprocessing->getNumbersName();
  729. }
  730. return "";
  731. }
  732. string ClassFlowControll::getJSON()
  733. {
  734. if (flowpostprocessing) {
  735. return flowpostprocessing->GetJSON();
  736. }
  737. return "";
  738. }
  739. /**
  740. * @returns a vector of all current sequences (empty while the flow is not initialized)
  741. **/
  742. const std::vector<NumberPost*> &ClassFlowControll::getNumbers()
  743. {
  744. static const std::vector<NumberPost*> noNumbers;
  745. if (flowpostprocessing) {
  746. return *flowpostprocessing->GetNumbers();
  747. }
  748. return noNumbers;
  749. }