ClassFlowControll.cpp 31 KB

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