| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017 |
- #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 "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 "connect_wlan.h"
- ClassFlowControll tfliteflow;
- TaskHandle_t xHandletask_autodoFlow = NULL;
- bool FlowInitDone = false;
- bool flowisrunning = false;
- long auto_intervall = 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, "Not a planned reboot.");
- isPlannedReboot = false;
- }
- else
- {
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Planned reboot.");
- DeleteFile("/sdcard/reboot.txt"); // Prevent Boot Loop!!!
- isPlannedReboot = true;
- }
- }
- 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);
- FlowInitDone = true;
- #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 (FlowInitDone)
- {
- 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 (FlowInitDone)
- {
- 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;
- }
- zw = tfliteflow.getReadout(_rawValue, _noerror);
- if (zw.length() > 0)
- httpd_resp_sendstr_chunk(req, zw.c_str());
- string query = std::string(_query);
- // ESP_LOGD(TAG, "Query: %s, query.c_str());
- if (query.find("full") != std::string::npos)
- {
- string txt, zw;
-
- txt = "<p>Aligned Image: <p><img src=\"/img_tmp/alg_roi.jpg\"> <p>\n";
- txt = txt + "Digital Counter: <p> ";
- httpd_resp_sendstr_chunk(req, txt.c_str());
-
- 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 = "<img src=\"/img_tmp/" + htmlinfodig[i]->filename + "\"> " + zw;
- }
- else
- {
- std::stringstream stream;
- stream << std::fixed << std::setprecision(1) << htmlinfodig[i]->val;
- zw = stream.str();
- txt = "<img src=\"/img_tmp/" + htmlinfodig[i]->filename + "\"> " + zw;
- }
- httpd_resp_sendstr_chunk(req, txt.c_str());
- delete htmlinfodig[i];
- }
- htmlinfodig.clear();
-
- txt = " <p> Analog Meter: <p> ";
- httpd_resp_sendstr_chunk(req, txt.c_str());
-
- 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 = "<img src=\"/img_tmp/" + htmlinfoana[i]->filename + "\"> " + zw;
- httpd_resp_sendstr_chunk(req, txt.c_str());
- delete htmlinfoana[i];
- }
- htmlinfoana.clear();
- }
- /* 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 MakeImage");
- std::string zw = tfliteflow.doSingleStep("[MakeImage]", _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
- if (FlowInitDone)
- {
- const char* resp_str;
- #ifdef DEBUG_DETAIL_ON
- ESP_LOGD(TAG, "handler_prevalue: %s", req->uri);
- #endif
- string* zw = tfliteflow.getActStatus();
- resp_str = zw->c_str();
- httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
- httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
- }
- else
- {
- httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flow not (yet) started: REST API /flowstatus not available!");
- return ESP_ERR_NOT_FOUND;
- }
- #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
- const char* resp_str;
- char cputemp[20];
-
- sprintf(cputemp, "%4.1f°C", temperatureRead());
- resp_str = cputemp;
- 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_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())
- {
- const char* resp_str;
- char rssi[20];
- sprintf(rssi, "%idBm", get_WIFI_RSSI());
- resp_str = rssi;
- httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
- httpd_resp_send(req, resp_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;
- if (!isPlannedReboot)
- {
- if (esp_reset_reason() == ESP_RST_PANIC) {
- LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Restarted due to an Exception/panic! Postponing first round start by 5 minutes to allow for an OTA Update or to fetch the log!");
- LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Setting logfile level to DEBUG until the next reboot!");
- LogFile.setLogLevel(ESP_LOG_DEBUG);
- //MQTTPublish(GetMQTTMainTopic() + "/" + "status", "Postponing first round", false);
- vTaskDelay(60*5000 / portTICK_RATE_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_intervall);
- if (isSetupModusActive()) {
- auto_isrunning = false;
- std::string zw_time = getCurrentTimeString(LOGFILE_TIME_FORMAT);
- tfliteflow.doFlowMakeImageOnly(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();
- }
-
- //CPU Temp -> Logfile
- std::stringstream stream;
- stream << std::fixed << std::setprecision(1) << temperatureRead();
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "CPU Temperature: " + stream.str() + "°C");
- // WIFI Signal Strength (RSSI) -> Logfile
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "WIFI Signal (RSSI): " + std::to_string(get_WIFI_RSSI()) + "dBm");
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Round #" + std::to_string(countRounds) +
- " completed (" + std::to_string(getUpTime() - roundStartTime) + " seconds)");
- fr_delta_ms = (esp_timer_get_time() - fr_start) / 1000;
- if (auto_intervall > fr_delta_ms)
- {
- const TickType_t xDelay = (auto_intervall - 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());
- xReturned = xTaskCreatePinnedToCore(&task_autodoFlow, "task_autodoFlow", 16 * 1024, NULL, tskIDLE_PRIORITY+2, &xHandletask_autodoFlow, 0);
- //xReturned = xTaskCreate(&task_autodoFlow, "task_autodoFlow", 16 * 1024, NULL, tskIDLE_PRIORITY+2, &xHandletask_autodoFlow);
- if( xReturned != pdPASS )
- {
- ESP_LOGD(TAG, "ERROR task_autodoFlow konnte nicht erzeugt werden!");
- }
- 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);
- }
|