Block REST API calls till resource is ready (#1609)

* Block REST API call till ressource is ready

* Update

* Update

* Update
This commit is contained in:
Slider0007
2022-12-17 20:38:24 +01:00
committed by GitHub
parent 4088e9ee75
commit 6083fe6151
4 changed files with 496 additions and 407 deletions

View File

@@ -164,6 +164,7 @@ static size_t jpg_encode_stream(void * arg, size_t index, const void* data, size
return len;
}
bool CCamera::SetBrightnessContrastSaturation(int _brightness, int _contrast, int _saturation)
{
bool result = false;
@@ -281,7 +282,6 @@ void CCamera::EnableAutoExposure(int flashdauer)
}
esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
{
string ftype;
@@ -543,6 +543,7 @@ esp_err_t CCamera::CaptureToHTTP(httpd_req_t *req, int delay)
return res;
}
void CCamera::LightOnOff(bool status)
{
GpioHandler* gpioHandler = gpio_handler_get();
@@ -578,6 +579,7 @@ void CCamera::LightOnOff(bool status)
}
}
void CCamera::LEDOnOff(bool status)
{
// Init the GPIO
@@ -638,6 +640,7 @@ void CCamera::GetCameraParameter(httpd_req_t *req, int &qual, framesize_t &resol
};
}
framesize_t CCamera::TextToFramesize(const char * _size)
{
if (strcmp(_size, "QVGA") == 0)
@@ -669,6 +672,7 @@ CCamera::CCamera()
ledc_init();
}
esp_err_t CCamera::InitCam()
{
ESP_LOGD(TAG, "Init Camera");
@@ -682,9 +686,11 @@ esp_err_t CCamera::InitCam()
return err;
}
CameraInitSuccessful = true;
return ESP_OK;
}
void CCamera::SetLEDIntensity(float _intrel)
{
_intrel = min(_intrel, (float) 100);
@@ -694,3 +700,9 @@ void CCamera::SetLEDIntensity(float _intrel)
ESP_LOGD(TAG, "Set led_intensity to %d of 8191", led_intensity);
}
bool CCamera::getCameraInitSuccessful()
{
return CameraInitSuccessful;
}

View File

@@ -26,6 +26,7 @@ class CCamera {
int led_intensity = 4095;
void ledc_init(void);
bool CameraInitSuccessful = false;
public:
int image_height, image_width;
@@ -42,6 +43,7 @@ class CCamera {
void SetLEDIntensity(float _intrel);
void EnableAutoExposure(int flashdauer);
bool getCameraInitSuccessful();
framesize_t TextToFramesize(const char * text);

View File

@@ -17,7 +17,8 @@ char scratch2[SCRATCH_BUFSIZE2];
//#define DEBUG_DETAIL_ON
void PowerResetCamera(){
void PowerResetCamera()
{
ESP_LOGD(TAG, "Resetting camera by power down line");
gpio_config_t conf;
conf.intr_type = GPIO_INTR_DISABLE;
@@ -42,16 +43,25 @@ esp_err_t handler_lightOn(httpd_req_t *req)
ESP_LOGD(TAG, "handler_lightOn uri: %s", req->uri);
#endif
if (Camera.getCameraInitSuccessful())
{
Camera.LightOnOff(true);
const char* resp_str = (const char*) req->user_ctx;
httpd_resp_send(req, resp_str, strlen(resp_str));
}
else
{
httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera Light On API not yet initialized. Please retry later...");
return ESP_ERR_NOT_FOUND;
}
#ifdef DEBUG_DETAIL_ON
LogFile.WriteHeapInfo("handler_lightOn - Done");
#endif
return ESP_OK;
};
}
esp_err_t handler_lightOff(httpd_req_t *req)
{
@@ -59,16 +69,26 @@ esp_err_t handler_lightOff(httpd_req_t *req)
LogFile.WriteHeapInfo("handler_lightOff - Start");
ESP_LOGD(TAG, "handler_lightOff uri: %s", req->uri);
#endif
if (Camera.getCameraInitSuccessful())
{
Camera.LightOnOff(false);
const char* resp_str = (const char*) req->user_ctx;
httpd_resp_send(req, resp_str, strlen(resp_str));
}
else
{
httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera Light Off API not yet initialized. Please retry later...");
return ESP_ERR_NOT_FOUND;
}
#ifdef DEBUG_DETAIL_ON
LogFile.WriteHeapInfo("handler_lightOff - Done");
#endif
return ESP_OK;
};
}
esp_err_t handler_capture(httpd_req_t *req)
{
@@ -76,6 +96,8 @@ esp_err_t handler_capture(httpd_req_t *req)
LogFile.WriteHeapInfo("handler_capture - Start");
#endif
if (Camera.getCameraInitSuccessful())
{
int quality;
framesize_t res;
@@ -87,15 +109,21 @@ esp_err_t handler_capture(httpd_req_t *req)
Camera.SetQualitySize(quality, res);
esp_err_t ressult;
ressult = Camera.CaptureToHTTP(req);
esp_err_t result;
result = Camera.CaptureToHTTP(req);
#ifdef DEBUG_DETAIL_ON
LogFile.WriteHeapInfo("handler_capture - Done");
#endif
return ressult;
};
return result;
}
else
{
httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera Capture API not yet initialized. Please retry later...");
return ESP_ERR_NOT_FOUND;
}
}
esp_err_t handler_capture_with_ligth(httpd_req_t *req)
@@ -103,6 +131,9 @@ esp_err_t handler_capture_with_ligth(httpd_req_t *req)
#ifdef DEBUG_DETAIL_ON
LogFile.WriteHeapInfo("handler_capture_with_ligth - Start");
#endif
if (Camera.getCameraInitSuccessful())
{
char _query[100];
char _delay[10];
@@ -123,7 +154,7 @@ esp_err_t handler_capture_with_ligth(httpd_req_t *req)
if (delay < 0)
delay = 0;
}
};
}
Camera.GetCameraParameter(req, quality, res);
@@ -136,8 +167,8 @@ esp_err_t handler_capture_with_ligth(httpd_req_t *req)
const TickType_t xDelay = delay / portTICK_PERIOD_MS;
vTaskDelay( xDelay );
esp_err_t ressult;
ressult = Camera.CaptureToHTTP(req);
esp_err_t result;
result = Camera.CaptureToHTTP(req);
Camera.LightOnOff(false);
@@ -145,9 +176,14 @@ esp_err_t handler_capture_with_ligth(httpd_req_t *req)
LogFile.WriteHeapInfo("handler_capture_with_ligth - Done");
#endif
return ressult;
};
return result;
}
else
{
httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera Capture + flashlight API not yet initialized. Please retry later...");
return ESP_ERR_NOT_FOUND;
}
}
esp_err_t handler_capture_save_to_file(httpd_req_t *req)
@@ -156,6 +192,8 @@ esp_err_t handler_capture_save_to_file(httpd_req_t *req)
LogFile.WriteHeapInfo("handler_capture_save_to_file - Start");
#endif
if (Camera.getCameraInitSuccessful())
{
char _query[100];
char _delay[10];
int delay = 0;
@@ -189,7 +227,6 @@ esp_err_t handler_capture_save_to_file(httpd_req_t *req)
if (delay < 0)
delay = 0;
}
}
else
fn.append("noname.jpg");
@@ -200,8 +237,8 @@ esp_err_t handler_capture_save_to_file(httpd_req_t *req)
#endif
Camera.SetQualitySize(quality, res);
esp_err_t ressult;
ressult = Camera.CaptureToFile(fn, delay);
esp_err_t result;
result = Camera.CaptureToFile(fn, delay);
const char* resp_str = (const char*) fn.c_str();
httpd_resp_send(req, resp_str, strlen(resp_str));
@@ -210,9 +247,14 @@ esp_err_t handler_capture_save_to_file(httpd_req_t *req)
LogFile.WriteHeapInfo("handler_capture_save_to_file - Done");
#endif
return ressult;
};
return result;
}
else
{
httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera Capture + save API not yet initialized. Please retry later...");
return ESP_ERR_NOT_FOUND;
}
}
void register_server_camera_uri(httpd_handle_t server)

View File

@@ -31,6 +31,7 @@ ClassFlowControll tfliteflow;
TaskHandle_t xHandleblink_task_doFlow = NULL;
TaskHandle_t xHandletask_autodoFlow = NULL;
bool FlowInitDone = false;
bool flowisrunning = false;
long auto_intervall = 0;
@@ -47,17 +48,18 @@ int getCountFlowRounds() {
}
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;
@@ -94,12 +96,14 @@ void KillTFliteTasks()
}
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
@@ -112,7 +116,6 @@ void doInit(void)
bool doflow(void)
{
std::string zw_time = gettimestring(LOGFILE_TIME_FORMAT);
ESP_LOGD(TAG, "doflow - start %s", zw_time.c_str());
flowisrunning = true;
@@ -125,6 +128,7 @@ bool doflow(void)
return true;
}
void blink_task_doFlow(void *pvParameter)
{
#ifdef DEBUG_DETAIL_ON
@@ -163,7 +167,8 @@ esp_err_t handler_init(httpd_req_t *req)
#endif
return ESP_OK;
};
}
esp_err_t handler_doflow(httpd_req_t *req)
{
@@ -193,7 +198,7 @@ esp_err_t handler_doflow(httpd_req_t *req)
#endif
return ESP_OK;
};
}
esp_err_t handler_json(httpd_req_t *req)
@@ -202,9 +207,10 @@ esp_err_t handler_json(httpd_req_t *req)
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");
@@ -217,13 +223,18 @@ esp_err_t handler_json(httpd_req_t *req)
{
httpd_resp_send(req, NULL, 0);
}
}
else
{
httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "JSON API not yet initialized. Please retry later...");
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)
@@ -232,6 +243,8 @@ esp_err_t handler_wasserzaehler(httpd_req_t *req)
LogFile.WriteHeapInfo("handler_wasserzaehler - Start");
#endif
if (FlowInitDone)
{
bool _rawValue = false;
bool _noerror = false;
bool _all = false;
@@ -361,19 +374,20 @@ esp_err_t handler_wasserzaehler(httpd_req_t *req)
}
/* 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, "Value API not yet initialized. Please retry later...");
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)
@@ -575,7 +589,7 @@ esp_err_t handler_editflow(httpd_req_t *req)
#endif
return ESP_OK;
};
}
esp_err_t handler_statusflow(httpd_req_t *req)
@@ -584,6 +598,8 @@ esp_err_t handler_statusflow(httpd_req_t *req)
LogFile.WriteHeapInfo("handler_prevalue - Start");
#endif
if (FlowInitDone)
{
const char* resp_str;
#ifdef DEBUG_DETAIL_ON
@@ -597,13 +613,20 @@ esp_err_t handler_statusflow(httpd_req_t *req)
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);
}
else
{
httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flowstatus API not yet initialized. Please retry later...");
return ESP_ERR_NOT_FOUND;
}
#ifdef DEBUG_DETAIL_ON
LogFile.WriteHeapInfo("handler_prevalue - Start");
LogFile.WriteHeapInfo("handler_prevalue - Done");
#endif
return ESP_OK;
};
}
esp_err_t handler_cputemp(httpd_req_t *req)
{
@@ -628,7 +651,8 @@ esp_err_t handler_cputemp(httpd_req_t *req)
#endif
return ESP_OK;
};
}
esp_err_t handler_rssi(httpd_req_t *req)
{
@@ -636,6 +660,8 @@ esp_err_t handler_rssi(httpd_req_t *req)
LogFile.WriteHeapInfo("handler_rssi - Start");
#endif
if (getWIFIisConnected())
{
const char* resp_str;
char rssi[20];
@@ -647,13 +673,19 @@ esp_err_t handler_rssi(httpd_req_t *req)
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);
}
else
{
httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "RSSI API not yet initialized. Please retry later...");
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)
@@ -734,7 +766,8 @@ esp_err_t handler_prevalue(httpd_req_t *req)
#endif
return ESP_OK;
};
}
void task_autodoFlow(void *pvParameter)
{
@@ -808,6 +841,7 @@ void task_autodoFlow(void *pvParameter)
ESP_LOGD(TAG, "task_autodoFlow: end");
}
void TFliteDoAutoStart()
{
BaseType_t xReturned;
@@ -825,10 +859,9 @@ void TFliteDoAutoStart()
ESP_LOGD(TAG, "ERROR task_autodoFlow konnte nicht erzeugt werden!");
}
ESP_LOGD(TAG, "getESPHeapInfo: %s", getESPHeapInfo().c_str());
}
#ifdef ENABLE_MQTT
std::string GetMQTTMainTopic()
{