| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073 |
- #include "server_tflite.h"
- #include <string>
- #include <vector>
- #include "string.h"
- #include "esp_log.h"
- #include <iomanip>
- #include <sstream>
- #include "../../include/defines.h"
- #include "Helper.h"
- #include "statusled.h"
- #include "esp_camera.h"
- #include "time_sntp.h"
- #include "ClassControllCamera.h"
- #include "ClassFlowControll.h"
- #include "ClassLogFile.h"
- #include "server_GPIO.h"
- #include "server_file.h"
- #include "read_wlanini.h"
- #include "connect_wlan.h"
- ClassFlowControll tfliteflow;
- TaskHandle_t xHandletask_autodoFlow = NULL;
- bool bTaskAutoFlowCreated = false;
- bool flowisrunning = false;
- long auto_interval = 0;
- bool auto_isrunning = false;
- int countRounds = 0;
- bool isPlannedReboot = false;
- static const char *TAG = "TFLITE SERVER";
- //#define DEBUG_DETAIL_ON
- void CheckIsPlannedReboot()
- {
- FILE *pfile;
- if ((pfile = fopen("/sdcard/reboot.txt", "r")) == NULL) {
- //LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Initial boot or not a planned reboot");
- isPlannedReboot = false;
- }
- else {
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Planned reboot");
- DeleteFile("/sdcard/reboot.txt"); // Prevent Boot Loop!!!
- isPlannedReboot = true;
- }
- }
- bool getIsPlannedReboot()
- {
- return isPlannedReboot;
- }
- int getCountFlowRounds()
- {
- return countRounds;
- }
- esp_err_t GetJPG(std::string _filename, httpd_req_t *req)
- {
- return tfliteflow.GetJPGStream(_filename, req);
- }
- esp_err_t GetRawJPG(httpd_req_t *req)
- {
- return tfliteflow.SendRawJPG(req);
- }
- bool isSetupModusActive() {
- return tfliteflow.getStatusSetupModus();
- return false;
- }
- void KillTFliteTasks()
- {
- #ifdef DEBUG_DETAIL_ON
- ESP_LOGD(TAG, "KillTFliteTasks: xHandletask_autodoFlow: %ld", (long) xHandletask_autodoFlow);
- #endif
- if( xHandletask_autodoFlow != NULL )
- {
- vTaskDelete(xHandletask_autodoFlow);
- xHandletask_autodoFlow = NULL;
- }
- #ifdef DEBUG_DETAIL_ON
- ESP_LOGD(TAG, "Killed: xHandletask_autodoFlow");
- #endif
- }
- void doInit(void)
- {
- #ifdef DEBUG_DETAIL_ON
- ESP_LOGD(TAG, "Start tfliteflow.InitFlow(config);");
- #endif
- tfliteflow.InitFlow(CONFIG_FILE);
- #ifdef DEBUG_DETAIL_ON
- ESP_LOGD(TAG, "Finished tfliteflow.InitFlow(config);");
- #endif
-
- /* GPIO handler has to be initialized before MQTT init to ensure proper topic subscription */
- gpio_handler_init();
- #ifdef ENABLE_MQTT
- tfliteflow.StartMQTTService();
- #endif //ENABLE_MQTT
- }
- bool doflow(void)
- {
- std::string zw_time = getCurrentTimeString(LOGFILE_TIME_FORMAT);
- ESP_LOGD(TAG, "doflow - start %s", zw_time.c_str());
- flowisrunning = true;
- tfliteflow.doFlow(zw_time);
- flowisrunning = false;
- #ifdef DEBUG_DETAIL_ON
- ESP_LOGD(TAG, "doflow - end %s", zw_time.c_str());
- #endif
- return true;
- }
- esp_err_t handler_get_heap(httpd_req_t *req)
- {
- #ifdef DEBUG_DETAIL_ON
- LogFile.WriteHeapInfo("handler_get_heap - Start");
- ESP_LOGD(TAG, "handler_get_heap uri: %s", req->uri);
- #endif
- std::string zw = "Heap info:<br>" + getESPHeapInfo();
- #ifdef TASK_ANALYSIS_ON
- char* pcTaskList = (char*) heap_caps_calloc(1, sizeof(char) * 768, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
- if (pcTaskList) {
- vTaskList(pcTaskList);
- zw = zw + "<br><br>Task info:<br><pre>Name | State | Prio | Lowest stacksize | Creation order | CPU (-1=NoAffinity)<br>"
- + std::string(pcTaskList) + "</pre>";
- heap_caps_free(pcTaskList);
- }
- else {
- zw = zw + "<br><br>Task info:<br>ERROR - Allocation of TaskList buffer in PSRAM failed";
- }
- #endif
- httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
- if (zw.length() > 0)
- {
- httpd_resp_send(req, zw.c_str(), zw.length());
- }
- else
- {
- httpd_resp_send(req, NULL, 0);
- }
- #ifdef DEBUG_DETAIL_ON
- LogFile.WriteHeapInfo("handler_get_heap - Done");
- #endif
- return ESP_OK;
- }
- esp_err_t handler_init(httpd_req_t *req)
- {
- #ifdef DEBUG_DETAIL_ON
- LogFile.WriteHeapInfo("handler_init - Start");
- ESP_LOGD(TAG, "handler_doinit uri: %s", req->uri);
- #endif
- httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
- const char* resp_str = "Init started<br>";
- httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
- doInit();
- resp_str = "Init done<br>";
- httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
- #ifdef DEBUG_DETAIL_ON
- LogFile.WriteHeapInfo("handler_init - Done");
- #endif
- return ESP_OK;
- }
- esp_err_t handler_flow_start(httpd_req_t *req) {
- #ifdef DEBUG_DETAIL_ON
- LogFile.WriteHeapInfo("handler_flow_start - Start");
- #endif
- ESP_LOGD(TAG, "handler_flow_start uri: %s", req->uri);
- httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
- if (auto_isrunning) {
- xTaskAbortDelay(xHandletask_autodoFlow); // Delay will be aborted if task is in blocked (waiting) state. If task is already running, no action
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Flow start triggered by REST API /flow_start");
- const char* resp_str = "The flow is going to be started immediately or is already running";
- httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
- }
- else {
- LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Flow start triggered by REST API, but flow is not active!");
- const char* resp_str = "WARNING: Flow start triggered by REST API, but flow is not active";
- httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
- }
- #ifdef DEBUG_DETAIL_ON
- LogFile.WriteHeapInfo("handler_flow_start - Done");
- #endif
- return ESP_OK;
- }
- #ifdef ENABLE_MQTT
- esp_err_t MQTTCtrlFlowStart(std::string _topic) {
- #ifdef DEBUG_DETAIL_ON
- LogFile.WriteHeapInfo("MQTTCtrlFlowStart - Start");
- #endif
- ESP_LOGD(TAG, "MQTTCtrlFlowStart: topic %s", _topic.c_str());
- if (auto_isrunning)
- {
- xTaskAbortDelay(xHandletask_autodoFlow); // Delay will be aborted if task is in blocked (waiting) state. If task is already running, no action
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Flow start triggered by MQTT topic " + _topic);
- }
- else
- {
- LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Flow start triggered by MQTT topic " + _topic + ", but flow is not active!");
- }
- #ifdef DEBUG_DETAIL_ON
- LogFile.WriteHeapInfo("MQTTCtrlFlowStart - Done");
- #endif
- return ESP_OK;
- }
- #endif //ENABLE_MQTT
- esp_err_t handler_json(httpd_req_t *req)
- {
- #ifdef DEBUG_DETAIL_ON
- LogFile.WriteHeapInfo("handler_json - Start");
- #endif
- ESP_LOGD(TAG, "handler_JSON uri: %s", req->uri);
-
- if (bTaskAutoFlowCreated)
- {
- httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
- httpd_resp_set_type(req, "application/json");
- std::string zw = tfliteflow.getJSON();
- if (zw.length() > 0)
- {
- httpd_resp_send(req, zw.c_str(), zw.length());
- }
- else
- {
- httpd_resp_send(req, NULL, 0);
- }
- }
- else
- {
- httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flow not (yet) started: REST API /json not yet available!");
- return ESP_ERR_NOT_FOUND;
- }
- #ifdef DEBUG_DETAIL_ON
- LogFile.WriteHeapInfo("handler_JSON - Done");
- #endif
- return ESP_OK;
- }
- esp_err_t handler_wasserzaehler(httpd_req_t *req)
- {
- #ifdef DEBUG_DETAIL_ON
- LogFile.WriteHeapInfo("handler water counter - Start");
- #endif
- if (bTaskAutoFlowCreated)
- {
- bool _rawValue = false;
- bool _noerror = false;
- bool _all = false;
- std::string _type = "value";
- string zw;
- ESP_LOGD(TAG, "handler water counter uri: %s", req->uri);
- char _query[100];
- char _size[10];
- if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
- {
- // ESP_LOGD(TAG, "Query: %s", _query);
- if (httpd_query_key_value(_query, "all", _size, 10) == ESP_OK)
- {
- #ifdef DEBUG_DETAIL_ON
- ESP_LOGD(TAG, "all is found%s", _size);
- #endif
- _all = true;
- }
- if (httpd_query_key_value(_query, "type", _size, 10) == ESP_OK)
- {
- #ifdef DEBUG_DETAIL_ON
- ESP_LOGD(TAG, "all is found: %s", _size);
- #endif
- _type = std::string(_size);
- }
- if (httpd_query_key_value(_query, "rawvalue", _size, 10) == ESP_OK)
- {
- #ifdef DEBUG_DETAIL_ON
- ESP_LOGD(TAG, "rawvalue is found: %s", _size);
- #endif
- _rawValue = true;
- }
- if (httpd_query_key_value(_query, "noerror", _size, 10) == ESP_OK)
- {
- #ifdef DEBUG_DETAIL_ON
- ESP_LOGD(TAG, "noerror is found: %s", _size);
- #endif
- _noerror = true;
- }
- }
- httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
- if (_all)
- {
- httpd_resp_set_type(req, "text/plain");
- ESP_LOGD(TAG, "TYPE: %s", _type.c_str());
- int _intype = READOUT_TYPE_VALUE;
- if (_type == "prevalue")
- _intype = READOUT_TYPE_PREVALUE;
- if (_type == "raw")
- _intype = READOUT_TYPE_RAWVALUE;
- if (_type == "error")
- _intype = READOUT_TYPE_ERROR;
- zw = tfliteflow.getReadoutAll(_intype);
- ESP_LOGD(TAG, "ZW: %s", zw.c_str());
- if (zw.length() > 0)
- httpd_resp_send(req, zw.c_str(), zw.length());
-
- return ESP_OK;
- }
- std::string *status = tfliteflow.getActStatus();
- string query = std::string(_query);
- // ESP_LOGD(TAG, "Query: %s, query.c_str());
- if (query.find("full") != std::string::npos)
- {
- string txt;
- txt = "<body style=\"font-family: arial\">";
- if ((countRounds <= 1) && (*status != std::string("Flow finished"))) { // First round not completed yet
- txt += "<h3>Please wait for the first round to complete!</h3><h3>Current state: " + *status + "</h3>\n";
- }
- else {
- txt += "<h3>Value</h3>";
- }
- httpd_resp_sendstr_chunk(req, txt.c_str());
- }
- zw = tfliteflow.getReadout(_rawValue, _noerror);
- if (zw.length() > 0)
- httpd_resp_sendstr_chunk(req, zw.c_str());
- if (query.find("full") != std::string::npos)
- {
- string txt, zw;
- if ((countRounds <= 1) && (*status != std::string("Flow finished"))) { // First round not completed yet
- // Nothing to do
- }
- else {
- /* Digital ROIs */
- txt = "<body style=\"font-family: arial\">";
- txt += "<h3>Recognized Digit ROIs (previous round)</h3>\n";
- txt += "<table style=\"border-spacing: 5px\"><tr style=\"text-align: center; vertical-align: top;\">\n";
- std::vector<HTMLInfo*> htmlinfodig;
- htmlinfodig = tfliteflow.GetAllDigital();
- for (int i = 0; i < htmlinfodig.size(); ++i)
- {
- if (tfliteflow.GetTypeDigital() == Digital)
- {
- if (htmlinfodig[i]->val == 10)
- zw = "NaN";
- else
- zw = to_string((int) htmlinfodig[i]->val);
- txt += "<td style=\"width: 100px\"><h4>" + zw + "</h4><p><img src=\"/img_tmp/" + htmlinfodig[i]->filename + "\"></p></td>\n";
- }
- else
- {
- std::stringstream stream;
- stream << std::fixed << std::setprecision(1) << htmlinfodig[i]->val;
- zw = stream.str();
- txt += "<td style=\"width: 100px\"><h4>" + zw + "</h4><p><img src=\"/img_tmp/" + htmlinfodig[i]->filename + "\"></p></td>\n";
- }
- delete htmlinfodig[i];
- }
- htmlinfodig.clear();
-
- txt += "</tr></table>\n";
- httpd_resp_sendstr_chunk(req, txt.c_str());
- /* Analog ROIs */
- txt = "<h3>Recognized Analog ROIs (previous round)</h3>\n";
- txt += "<table style=\"border-spacing: 5px\"><tr style=\"text-align: center; vertical-align: top;\">\n";
-
- std::vector<HTMLInfo*> htmlinfoana;
- htmlinfoana = tfliteflow.GetAllAnalog();
- for (int i = 0; i < htmlinfoana.size(); ++i)
- {
- std::stringstream stream;
- stream << std::fixed << std::setprecision(1) << htmlinfoana[i]->val;
- zw = stream.str();
- txt += "<td style=\"width: 150px;\"><h4>" + zw + "</h4><p><img src=\"/img_tmp/" + htmlinfoana[i]->filename + "\"></p></td>\n";
- delete htmlinfoana[i];
- }
- htmlinfoana.clear();
- txt += "</tr>\n</table>\n";
- httpd_resp_sendstr_chunk(req, txt.c_str());
- /* Full Image
- * Only show it after the image got taken and aligned */
- txt = "<h3>Aligned Image (current round)</h3>\n";
- if ((*status == std::string("Initialization")) ||
- (*status == std::string("Initialization (delayed)")) ||
- (*status == std::string("Take Image"))) {
- txt += "<p>Current state: " + *status + "</p>\n";
- }
- else {
- txt += "<img src=\"/img_tmp/alg_roi.jpg\">\n";
- }
- httpd_resp_sendstr_chunk(req, txt.c_str());
- }
- }
- /* Respond with an empty chunk to signal HTTP response completion */
- httpd_resp_sendstr_chunk(req, NULL);
- }
- else
- {
- httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flow not (yet) started: REST API /value not available!");
- return ESP_ERR_NOT_FOUND;
- }
- #ifdef DEBUG_DETAIL_ON
- LogFile.WriteHeapInfo("handler_wasserzaehler - Done");
- #endif
- return ESP_OK;
- }
- esp_err_t handler_editflow(httpd_req_t *req)
- {
- #ifdef DEBUG_DETAIL_ON
- LogFile.WriteHeapInfo("handler_editflow - Start");
- #endif
- ESP_LOGD(TAG, "handler_editflow uri: %s", req->uri);
- char _query[200];
- char _valuechar[30];
- string _task;
- if (httpd_req_get_url_query_str(req, _query, 200) == ESP_OK)
- {
- if (httpd_query_key_value(_query, "task", _valuechar, 30) == ESP_OK)
- {
- #ifdef DEBUG_DETAIL_ON
- ESP_LOGD(TAG, "task is found: %s", _valuechar);
- #endif
- _task = string(_valuechar);
- }
- }
- if (_task.compare("namenumbers") == 0)
- {
- ESP_LOGD(TAG, "Get NUMBER list");
- return get_numbers_file_handler(req);
- }
- if (_task.compare("data") == 0)
- {
- ESP_LOGD(TAG, "Get data list");
- return get_data_file_handler(req);
- }
- if (_task.compare("tflite") == 0)
- {
- ESP_LOGD(TAG, "Get tflite list");
- return get_tflite_file_handler(req);
- }
- if (_task.compare("copy") == 0)
- {
- string in, out, zw;
- httpd_query_key_value(_query, "in", _valuechar, 30);
- in = string(_valuechar);
- httpd_query_key_value(_query, "out", _valuechar, 30);
- out = string(_valuechar);
- #ifdef DEBUG_DETAIL_ON
- ESP_LOGD(TAG, "in: %s", in.c_str());
- ESP_LOGD(TAG, "out: %s", out.c_str());
- #endif
- in = "/sdcard" + in;
- out = "/sdcard" + out;
- CopyFile(in, out);
- zw = "Copy Done";
- httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
- httpd_resp_send(req, zw.c_str(), zw.length());
- }
- if (_task.compare("cutref") == 0)
- {
- string in, out, zw;
- int x, y, dx, dy;
- bool enhance = false;
- httpd_query_key_value(_query, "in", _valuechar, 30);
- in = string(_valuechar);
- httpd_query_key_value(_query, "out", _valuechar, 30);
- out = string(_valuechar);
- httpd_query_key_value(_query, "x", _valuechar, 30);
- zw = string(_valuechar);
- x = stoi(zw);
- httpd_query_key_value(_query, "y", _valuechar, 30);
- zw = string(_valuechar);
- y = stoi(zw);
- httpd_query_key_value(_query, "dx", _valuechar, 30);
- zw = string(_valuechar);
- dx = stoi(zw);
- httpd_query_key_value(_query, "dy", _valuechar, 30);
- zw = string(_valuechar);
- dy = stoi(zw);
- #ifdef DEBUG_DETAIL_ON
- ESP_LOGD(TAG, "in: %s", in.c_str());
- ESP_LOGD(TAG, "out: %s", out.c_str());
- ESP_LOGD(TAG, "x: %s", zw.c_str());
- ESP_LOGD(TAG, "y: %s", zw.c_str());
- ESP_LOGD(TAG, "dx: %s", zw.c_str());
- ESP_LOGD(TAG, "dy: %s", zw.c_str());
- #endif
- if (httpd_query_key_value(_query, "enhance", _valuechar, 10) == ESP_OK)
- {
- zw = string(_valuechar);
- if (zw.compare("true") == 0)
- {
- enhance = true;
- }
- }
- in = "/sdcard" + in;
- out = "/sdcard" + out;
- string out2 = out.substr(0, out.length() - 4) + "_org.jpg";
- CAlignAndCutImage *caic = new CAlignAndCutImage(in);
- caic->CutAndSave(out2, x, y, dx, dy);
- delete caic;
- CImageBasis *cim = new CImageBasis(out2);
- if (enhance)
- {
- cim->Contrast(90);
- }
- cim->SaveToFile(out);
- delete cim;
- zw = "CutImage Done";
- httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
- httpd_resp_send(req, zw.c_str(), zw.length());
-
- }
- if (_task.compare("test_take") == 0)
- {
- std::string _host = "";
- std::string _bri = "";
- std::string _con = "";
- std::string _sat = "";
- std::string _int = "";
- int bri = -100;
- int sat = -100;
- int con = -100;
- int intens = -100;
- if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK) {
- _host = std::string(_valuechar);
- }
- if (httpd_query_key_value(_query, "int", _valuechar, 30) == ESP_OK) {
- _int = std::string(_valuechar);
- intens = stoi(_int);
- }
- if (httpd_query_key_value(_query, "bri", _valuechar, 30) == ESP_OK) {
- _bri = std::string(_valuechar);
- bri = stoi(_bri);
- }
- if (httpd_query_key_value(_query, "con", _valuechar, 30) == ESP_OK) {
- _con = std::string(_valuechar);
- con = stoi(_con);
- }
- if (httpd_query_key_value(_query, "sat", _valuechar, 30) == ESP_OK) {
- _sat = std::string(_valuechar);
- sat = stoi(_sat);
- }
- // ESP_LOGD(TAG, "Parameter host: %s", _host.c_str());
- // string zwzw = "Do " + _task + " start\n"; ESP_LOGD(TAG, zwzw.c_str());
- Camera.SetBrightnessContrastSaturation(bri, con, sat);
- Camera.SetLEDIntensity(intens);
- ESP_LOGD(TAG, "test_take - vor TakeImage");
- std::string zw = tfliteflow.doSingleStep("[TakeImage]", _host);
- httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
- httpd_resp_send(req, zw.c_str(), zw.length());
- }
- if (_task.compare("test_align") == 0)
- {
- std::string _host = "";
- if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK) {
- _host = std::string(_valuechar);
- }
- // ESP_LOGD(TAG, "Parameter host: %s", _host.c_str());
- // string zwzw = "Do " + _task + " start\n"; ESP_LOGD(TAG, zwzw.c_str());
- std::string zw = tfliteflow.doSingleStep("[Alignment]", _host);
- httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
- httpd_resp_send(req, zw.c_str(), zw.length());
- }
- #ifdef DEBUG_DETAIL_ON
- LogFile.WriteHeapInfo("handler_editflow - Done");
- #endif
- return ESP_OK;
- }
- esp_err_t handler_statusflow(httpd_req_t *req)
- {
- #ifdef DEBUG_DETAIL_ON
- LogFile.WriteHeapInfo("handler_prevalue - Start");
- #endif
- const char* resp_str;
- httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
- if (bTaskAutoFlowCreated)
- {
- #ifdef DEBUG_DETAIL_ON
- ESP_LOGD(TAG, "handler_prevalue: %s", req->uri);
- #endif
- string* zw = tfliteflow.getActStatusWithTime();
- resp_str = zw->c_str();
- httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
- }
- else
- {
- resp_str = "Flow task not yet created";
- httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
- }
- #ifdef DEBUG_DETAIL_ON
- LogFile.WriteHeapInfo("handler_prevalue - Done");
- #endif
- return ESP_OK;
- }
- esp_err_t handler_cputemp(httpd_req_t *req)
- {
- #ifdef DEBUG_DETAIL_ON
- LogFile.WriteHeapInfo("handler_cputemp - Start");
- #endif
- httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
- httpd_resp_send(req, std::to_string((int)temperatureRead()).c_str(), HTTPD_RESP_USE_STRLEN);
- #ifdef DEBUG_DETAIL_ON
- LogFile.WriteHeapInfo("handler_cputemp - End");
- #endif
- return ESP_OK;
- }
- esp_err_t handler_rssi(httpd_req_t *req)
- {
- #ifdef DEBUG_DETAIL_ON
- LogFile.WriteHeapInfo("handler_rssi - Start");
- #endif
- if (getWIFIisConnected())
- {
- httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
- httpd_resp_send(req, std::to_string(get_WIFI_RSSI()).c_str(), HTTPD_RESP_USE_STRLEN);
- }
- else
- {
- httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "WIFI not (yet) connected: REST API /rssi not available!");
- return ESP_ERR_NOT_FOUND;
- }
- #ifdef DEBUG_DETAIL_ON
- LogFile.WriteHeapInfo("handler_rssi - End");
- #endif
- return ESP_OK;
- }
- esp_err_t handler_uptime(httpd_req_t *req)
- {
- #ifdef DEBUG_DETAIL_ON
- LogFile.WriteHeapInfo("handler_uptime - Start");
- #endif
-
- std::string formatedUptime = getFormatedUptime(false);
- httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
- httpd_resp_send(req, formatedUptime.c_str(), formatedUptime.length());
- #ifdef DEBUG_DETAIL_ON
- LogFile.WriteHeapInfo("handler_uptime - End");
- #endif
- return ESP_OK;
- }
- esp_err_t handler_prevalue(httpd_req_t *req)
- {
- #ifdef DEBUG_DETAIL_ON
- LogFile.WriteHeapInfo("handler_prevalue - Start");
- #endif
- const char* resp_str;
- string zw;
- #ifdef DEBUG_DETAIL_ON
- ESP_LOGD(TAG, "handler_prevalue: %s", req->uri);
- #endif
- char _query[100];
- char _size[10] = "";
- char _numbers[50] = "default";
- if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
- {
- #ifdef DEBUG_DETAIL_ON
- ESP_LOGD(TAG, "Query: %s", _query);
- #endif
- if (httpd_query_key_value(_query, "value", _size, 10) == ESP_OK)
- {
- #ifdef DEBUG_DETAIL_ON
- ESP_LOGD(TAG, "Value: %s", _size);
- #endif
- }
- httpd_query_key_value(_query, "numbers", _numbers, 50);
- }
- if (strlen(_size) == 0)
- {
- zw = tfliteflow.GetPrevalue(std::string(_numbers));
- }
- else
- {
- zw = "SetPrevalue to " + tfliteflow.UpdatePrevalue(_size, _numbers, true);
- }
-
- resp_str = zw.c_str();
- httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
-
- httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
- #ifdef DEBUG_DETAIL_ON
- LogFile.WriteHeapInfo("handler_prevalue - End");
- #endif
- return ESP_OK;
- }
- void task_autodoFlow(void *pvParameter)
- {
- int64_t fr_start, fr_delta_ms;
- bTaskAutoFlowCreated = true;
- if (!isPlannedReboot && (esp_reset_reason() == ESP_RST_PANIC))
- {
- tfliteflow.setActStatus("Initialization (delayed)");
- //#ifdef ENABLE_MQTT
- //MQTTPublish(mqttServer_getMainTopic() + "/" + "status", "Initialization (delayed)", false); // Right now, not possible -> MQTT Service is going to be started later
- //#endif //ENABLE_MQTT
- vTaskDelay(60*5000 / portTICK_PERIOD_MS); // Wait 5 minutes to give time to do an OTA update or fetch the log
- }
- ESP_LOGD(TAG, "task_autodoFlow: start");
- doInit();
- auto_isrunning = tfliteflow.isAutoStart(auto_interval);
- if (isSetupModusActive())
- {
- auto_isrunning = false;
- std::string zw_time = getCurrentTimeString(LOGFILE_TIME_FORMAT);
- tfliteflow.doFlowTakeImageOnly(zw_time);
- }
-
- while (auto_isrunning)
- {
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "----------------------------------------------------------------"); // Clear separation between runs
- std::string _zw = "Round #" + std::to_string(++countRounds) + " started";
- time_t roundStartTime = getUpTime();
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
- fr_start = esp_timer_get_time();
- if (flowisrunning)
- {
- #ifdef DEBUG_DETAIL_ON
- ESP_LOGD(TAG, "Autoflow: doFlow is already running!");
- #endif
- }
- else
- {
- #ifdef DEBUG_DETAIL_ON
- ESP_LOGD(TAG, "Autoflow: doFlow is started");
- #endif
- flowisrunning = true;
- doflow();
- #ifdef DEBUG_DETAIL_ON
- ESP_LOGD(TAG, "Remove older log files");
- #endif
- LogFile.RemoveOldLogFile();
- LogFile.RemoveOldDataLog();
- }
- // Round finished -> Logfile
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Round #" + std::to_string(countRounds) +
- " completed (" + std::to_string(getUpTime() - roundStartTime) + " seconds)");
-
- // CPU Temp -> Logfile
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "CPU Temperature: " + std::to_string((int)temperatureRead()) + "°C");
-
- // WIFI Signal Strength (RSSI) -> Logfile
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "WIFI Signal (RSSI): " + std::to_string(get_WIFI_RSSI()) + "dBm");
- // Check if time is synchronized (if NTP is configured)
- if (getUseNtp() && !getTimeIsSet()) {
- LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Time server is configured, but time is not yet set!");
- StatusLED(TIME_CHECK, 1, false);
- }
- #if (defined WLAN_USE_MESH_ROAMING && defined WLAN_USE_MESH_ROAMING_ACTIVATE_CLIENT_TRIGGERED_QUERIES)
- wifiRoamingQuery();
- #endif
-
- // Scan channels and check if an AP with better RSSI is available, then disconnect and try to reconnect to AP with better RSSI
- // NOTE: Keep this direct before the following task delay, because scan is done in blocking mode and this takes ca. 1,5 - 2s.
- #ifdef WLAN_USE_ROAMING_BY_SCANNING
- wifiRoamByScanning();
- #endif
-
- fr_delta_ms = (esp_timer_get_time() - fr_start) / 1000;
- if (auto_interval > fr_delta_ms)
- {
- const TickType_t xDelay = (auto_interval - fr_delta_ms) / portTICK_PERIOD_MS;
- ESP_LOGD(TAG, "Autoflow: sleep for: %ldms", (long) xDelay);
- vTaskDelay( xDelay );
- }
- }
- vTaskDelete(NULL); //Delete this task if it exits from the loop above
- xHandletask_autodoFlow = NULL;
- ESP_LOGD(TAG, "task_autodoFlow: end");
- }
- void TFliteDoAutoStart()
- {
- BaseType_t xReturned;
- ESP_LOGD(TAG, "getESPHeapInfo: %s", getESPHeapInfo().c_str());
- uint32_t stackSize = 16 * 1024;
- xReturned = xTaskCreatePinnedToCore(&task_autodoFlow, "task_autodoFlow", stackSize, NULL, tskIDLE_PRIORITY+2, &xHandletask_autodoFlow, 0);
- if( xReturned != pdPASS )
- {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Creation task_autodoFlow failed. Requested stack size:" + std::to_string(stackSize));
- LogFile.WriteHeapInfo("Creation task_autodoFlow failed");
- }
- ESP_LOGD(TAG, "getESPHeapInfo: %s", getESPHeapInfo().c_str());
- }
- #ifdef ENABLE_MQTT
- std::string GetMQTTMainTopic()
- {
- return tfliteflow.GetMQTTMainTopic();
- }
- #endif//ENABLE_MQTT
- void register_server_tflite_uri(httpd_handle_t server)
- {
- ESP_LOGI(TAG, "server_part_camera - Registering URI handlers");
-
- httpd_uri_t camuri = { };
- camuri.method = HTTP_GET;
- camuri.uri = "/doinit";
- camuri.handler = handler_init;
- camuri.user_ctx = (void*) "Light On";
- httpd_register_uri_handler(server, &camuri);
- // Legacy API => New: "/setPreValue"
- camuri.uri = "/setPreValue.html";
- camuri.handler = handler_prevalue;
- camuri.user_ctx = (void*) "Prevalue";
- httpd_register_uri_handler(server, &camuri);
- camuri.uri = "/setPreValue";
- camuri.handler = handler_prevalue;
- camuri.user_ctx = (void*) "Prevalue";
- httpd_register_uri_handler(server, &camuri);
- camuri.uri = "/flow_start";
- camuri.handler = handler_flow_start;
- camuri.user_ctx = (void*) "Flow Start";
- httpd_register_uri_handler(server, &camuri);
- camuri.uri = "/statusflow.html";
- camuri.handler = handler_statusflow;
- camuri.user_ctx = (void*) "Light Off";
- httpd_register_uri_handler(server, &camuri);
- camuri.uri = "/statusflow";
- camuri.handler = handler_statusflow;
- camuri.user_ctx = (void*) "Light Off";
- httpd_register_uri_handler(server, &camuri);
- // Legacy API => New: "/cpu_temperature"
- camuri.uri = "/cputemp.html";
- camuri.handler = handler_cputemp;
- camuri.user_ctx = (void*) "Light Off";
- httpd_register_uri_handler(server, &camuri);
- camuri.uri = "/cpu_temperature";
- camuri.handler = handler_cputemp;
- camuri.user_ctx = (void*) "Light Off";
- httpd_register_uri_handler(server, &camuri);
- // Legacy API => New: "/rssi"
- camuri.uri = "/rssi.html";
- camuri.handler = handler_rssi;
- camuri.user_ctx = (void*) "Light Off";
- httpd_register_uri_handler(server, &camuri);
- camuri.uri = "/rssi";
- camuri.handler = handler_rssi;
- camuri.user_ctx = (void*) "Light Off";
- httpd_register_uri_handler(server, &camuri);
- camuri.uri = "/uptime";
- camuri.handler = handler_uptime;
- camuri.user_ctx = (void*) "Light Off";
- httpd_register_uri_handler(server, &camuri);
- camuri.uri = "/editflow";
- camuri.handler = handler_editflow;
- camuri.user_ctx = (void*) "EditFlow";
- httpd_register_uri_handler(server, &camuri);
- // Legacy API => New: "/value"
- camuri.uri = "/value.html";
- camuri.handler = handler_wasserzaehler;
- camuri.user_ctx = (void*) "Value";
- httpd_register_uri_handler(server, &camuri);
- camuri.uri = "/value";
- camuri.handler = handler_wasserzaehler;
- camuri.user_ctx = (void*) "Value";
- httpd_register_uri_handler(server, &camuri);
- // Legacy API => New: "/value"
- camuri.uri = "/wasserzaehler.html";
- camuri.handler = handler_wasserzaehler;
- camuri.user_ctx = (void*) "Wasserzaehler";
- httpd_register_uri_handler(server, &camuri);
- camuri.uri = "/json";
- camuri.handler = handler_json;
- camuri.user_ctx = (void*) "JSON";
- httpd_register_uri_handler(server, &camuri);
- camuri.uri = "/heap";
- camuri.handler = handler_get_heap;
- camuri.user_ctx = (void*) "Heap";
- httpd_register_uri_handler(server, &camuri);
- }
|