Skip to content

Commit

Permalink
Rest handler: Use none chunked transfer (#1797)
Browse files Browse the repository at this point in the history
* Resthandler info,sysinfo,starttime: no chunk trans

* flowstart,statusflow,cputemp,rssi,uptime,prevalue

* Renamed error messages
  • Loading branch information
Slider0007 committed Jan 10, 2023
1 parent 4b38c1e commit eefdc74
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 97 deletions.
10 changes: 5 additions & 5 deletions code/components/jomjol_controlcamera/server_camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ esp_err_t handler_lightOn(httpd_req_t *req)
}
else
{
httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera Light On API not yet initialized. Please retry later...");
httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera not initialized: REST API /lighton not available!");
return ESP_ERR_NOT_FOUND;
}

Expand All @@ -75,7 +75,7 @@ esp_err_t handler_lightOff(httpd_req_t *req)
}
else
{
httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera Light Off API not yet initialized. Please retry later...");
httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera not initialized: REST API /lightoff not available!");
return ESP_ERR_NOT_FOUND;
}

Expand Down Expand Up @@ -117,7 +117,7 @@ esp_err_t handler_capture(httpd_req_t *req)
}
else
{
httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera Capture API not yet initialized. Please retry later...");
httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera not initialized: REST API /capture not available!");
return ESP_ERR_NOT_FOUND;
}
}
Expand Down Expand Up @@ -177,7 +177,7 @@ esp_err_t handler_capture_with_ligth(httpd_req_t *req)
}
else
{
httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera Capture + flashlight API not yet initialized. Please retry later...");
httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera not initialized: REST API /capture_with_flashlight not available!");
return ESP_ERR_NOT_FOUND;
}
}
Expand Down Expand Up @@ -248,7 +248,7 @@ esp_err_t handler_capture_save_to_file(httpd_req_t *req)
}
else
{
httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera Capture + save API not yet initialized. Please retry later...");
httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera not initialized: REST API /save not available!");
return ESP_ERR_NOT_FOUND;
}
}
Expand Down
74 changes: 29 additions & 45 deletions code/components/jomjol_tfliteclass/server_tflite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ bool isPlannedReboot = false;

static const char *TAG = "TFLITE SERVER";

//#define DEBUG_DETAIL_ON


void CheckIsPlannedReboot()
{
Expand Down Expand Up @@ -156,6 +158,8 @@ esp_err_t handler_get_heap(httpd_req_t *req)
}
#endif

httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");

if (zw.length() > 0)
{
httpd_resp_send(req, zw.c_str(), zw.length());
Expand All @@ -180,16 +184,14 @@ esp_err_t handler_init(httpd_req_t *req)
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, strlen(resp_str));
httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);

doInit();

resp_str = "Init done<br>";
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
httpd_resp_send(req, resp_str, strlen(resp_str));
/* Respond with an empty chunk to signal HTTP response completion */
httpd_resp_send_chunk(req, NULL, 0);
httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);

#ifdef DEBUG_DETAIL_ON
LogFile.WriteHeapInfo("handler_init - Done");
Expand All @@ -207,23 +209,20 @@ esp_err_t handler_flow_start(httpd_req_t *req) {

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, strlen(resp_str));
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, strlen(resp_str));
httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
}

httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");

/* Respond with an empty chunk to signal HTTP response completion */
httpd_resp_send_chunk(req, NULL, 0);

#ifdef DEBUG_DETAIL_ON
LogFile.WriteHeapInfo("handler_flow_start - Done");
#endif
Expand Down Expand Up @@ -285,7 +284,7 @@ esp_err_t handler_json(httpd_req_t *req)
}
else
{
httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "JSON API not yet initialized. Please retry later...");
httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flow not (yet) started: REST API /json not yet available!");
return ESP_ERR_NOT_FOUND;
}

Expand Down Expand Up @@ -369,8 +368,8 @@ esp_err_t handler_wasserzaehler(httpd_req_t *req)
zw = tfliteflow.getReadoutAll(_intype);
ESP_LOGD(TAG, "ZW: %s", zw.c_str());
if (zw.length() > 0)
httpd_resp_sendstr_chunk(req, zw.c_str());
httpd_resp_sendstr_chunk(req, NULL);
httpd_resp_send(req, zw.c_str(), zw.length());

return ESP_OK;
}

Expand Down Expand Up @@ -439,7 +438,7 @@ esp_err_t handler_wasserzaehler(httpd_req_t *req)
}
else
{
httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Value API not yet initialized. Please retry later...");
httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flow not (yet) started: REST API /value not available!");
return ESP_ERR_NOT_FOUND;
}

Expand Down Expand Up @@ -514,7 +513,7 @@ esp_err_t handler_editflow(httpd_req_t *req)

CopyFile(in, out);
zw = "Copy Done";
httpd_resp_sendstr_chunk(req, zw.c_str());
httpd_resp_send(req, zw.c_str(), zw.length());
}


Expand Down Expand Up @@ -583,7 +582,7 @@ esp_err_t handler_editflow(httpd_req_t *req)
delete cim;

zw = "CutImage Done";
httpd_resp_sendstr_chunk(req, zw.c_str());
httpd_resp_send(req, zw.c_str(), zw.length());

}

Expand Down Expand Up @@ -626,8 +625,7 @@ esp_err_t handler_editflow(httpd_req_t *req)
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_sendstr_chunk(req, zw.c_str());
httpd_resp_send(req, zw.c_str(), zw.length());
}


Expand All @@ -641,12 +639,9 @@ esp_err_t handler_editflow(httpd_req_t *req)

// string zwzw = "Do " + _task + " start\n"; ESP_LOGD(TAG, zwzw.c_str());
std::string zw = tfliteflow.doSingleStep("[Alignment]", _host);
httpd_resp_sendstr_chunk(req, zw.c_str());
httpd_resp_send(req, zw.c_str(), zw.length());
}

/* Respond with an empty chunk to signal HTTP response completion */
httpd_resp_sendstr_chunk(req, NULL);

#ifdef DEBUG_DETAIL_ON
LogFile.WriteHeapInfo("handler_editflow - Done");
#endif
Expand All @@ -673,13 +668,11 @@ esp_err_t handler_statusflow(httpd_req_t *req)
resp_str = zw->c_str();

httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
httpd_resp_send(req, resp_str, strlen(resp_str));
/* Respond with an empty chunk to signal HTTP response completion */
httpd_resp_send_chunk(req, NULL, 0);
httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
}
else
{
httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flowstatus API not yet initialized. Please retry later...");
httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flow not (yet) started: REST API /flowstatus not available!");
return ESP_ERR_NOT_FOUND;
}

Expand All @@ -705,9 +698,7 @@ esp_err_t handler_cputemp(httpd_req_t *req)
resp_str = cputemp;

httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
httpd_resp_send(req, resp_str, strlen(resp_str));
/* Respond with an empty chunk to signal HTTP response completion */
httpd_resp_send_chunk(req, NULL, 0);
httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);

#ifdef DEBUG_DETAIL_ON
LogFile.WriteHeapInfo("handler_cputemp - End");
Expand All @@ -733,13 +724,11 @@ esp_err_t handler_rssi(httpd_req_t *req)
resp_str = rssi;

httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
httpd_resp_send(req, resp_str, strlen(resp_str));
/* Respond with an empty chunk to signal HTTP response completion */
httpd_resp_send_chunk(req, NULL, 0);
httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
}
else
{
httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "RSSI API not yet initialized. Please retry later...");
httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "WIFI not (yet) connected: REST API /rssi not available!");
return ESP_ERR_NOT_FOUND;
}

Expand All @@ -761,9 +750,7 @@ esp_err_t handler_uptime(httpd_req_t *req)
std::string formatedUptime = getFormatedUptime(false);

httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
httpd_resp_send(req, formatedUptime.c_str(), strlen(formatedUptime.c_str()));
/* Respond with an empty chunk to signal HTTP response completion */
httpd_resp_send_chunk(req, NULL, 0);
httpd_resp_send(req, formatedUptime.c_str(), formatedUptime.length());

#ifdef DEBUG_DETAIL_ON
LogFile.WriteHeapInfo("handler_uptime - End");
Expand Down Expand Up @@ -792,9 +779,9 @@ esp_err_t handler_prevalue(httpd_req_t *req)

if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
{
#ifdef DEBUG_DETAIL_ON
#ifdef DEBUG_DETAIL_ON
ESP_LOGD(TAG, "Query: %s", _query);
#endif
#endif

if (httpd_query_key_value(_query, "value", _size, 10) == ESP_OK)
{
Expand All @@ -818,11 +805,8 @@ esp_err_t handler_prevalue(httpd_req_t *req)
resp_str = zw.c_str();

httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");


httpd_resp_send(req, resp_str, strlen(resp_str));
/* Respond with an empty chunk to signal HTTP response completion */
httpd_resp_send_chunk(req, NULL, 0);

httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);

#ifdef DEBUG_DETAIL_ON
LogFile.WriteHeapInfo("handler_prevalue - End");
Expand Down

0 comments on commit eefdc74

Please sign in to comment.