server_camera.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include "server_camera.h"
  2. #include <string>
  3. #include <string.h>
  4. #include <esp_log.h>
  5. #include "defines.h"
  6. #include "esp_camera.h"
  7. #include "ClassControllCamera.h"
  8. #include "MainFlowControl.h"
  9. #include "ClassLogFile.h"
  10. #include "basic_auth.h"
  11. static const char *TAG = "server_cam";
  12. esp_err_t handler_lightOn(httpd_req_t *req)
  13. {
  14. if (Camera.get_camera_init_successful())
  15. {
  16. Camera.set_flash_light_on_off(true);
  17. const char *resp_str = (const char *)req->user_ctx;
  18. httpd_resp_send(req, resp_str, strlen(resp_str));
  19. }
  20. else
  21. {
  22. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera not initialized: REST API /lighton not available!");
  23. return ESP_ERR_NOT_FOUND;
  24. }
  25. return ESP_OK;
  26. }
  27. esp_err_t handler_lightOff(httpd_req_t *req)
  28. {
  29. if (Camera.get_camera_init_successful())
  30. {
  31. Camera.set_flash_light_on_off(false);
  32. const char *resp_str = (const char *)req->user_ctx;
  33. httpd_resp_send(req, resp_str, strlen(resp_str));
  34. }
  35. else
  36. {
  37. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera not initialized: REST API /lightoff not available!");
  38. return ESP_ERR_NOT_FOUND;
  39. }
  40. return ESP_OK;
  41. }
  42. esp_err_t handler_capture(httpd_req_t *req)
  43. {
  44. if (Camera.get_camera_init_successful())
  45. {
  46. return Camera.capture_to_http(req);
  47. }
  48. else
  49. {
  50. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera not initialized: REST API /capture not available!");
  51. return ESP_ERR_NOT_FOUND;
  52. }
  53. }
  54. esp_err_t handler_capture_with_light(httpd_req_t *req)
  55. {
  56. if (Camera.get_camera_init_successful())
  57. {
  58. char _query[100];
  59. char _delay[10];
  60. int delay = 2500;
  61. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  62. {
  63. ESP_LOGD(TAG, "Query: %s", _query);
  64. if (httpd_query_key_value(_query, "delay", _delay, 10) == ESP_OK)
  65. {
  66. std::string _delay_ = std::string(_delay);
  67. if (is_string_numeric(_delay_))
  68. {
  69. delay = std::atoi(_delay);
  70. if (delay < 0)
  71. {
  72. delay = 0;
  73. }
  74. }
  75. }
  76. }
  77. return Camera.capture_to_http(req, delay);
  78. }
  79. else
  80. {
  81. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera not initialized: REST API /capture_with_flashlight not available!");
  82. return ESP_ERR_NOT_FOUND;
  83. }
  84. }
  85. esp_err_t handler_capture_save_to_file(httpd_req_t *req)
  86. {
  87. if (Camera.get_camera_init_successful())
  88. {
  89. char _query[100];
  90. char _delay[10];
  91. int delay = 0;
  92. char filename[100];
  93. std::string fn = "/sdcard/";
  94. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  95. {
  96. ESP_LOGD(TAG, "Query: %s", _query);
  97. if (httpd_query_key_value(_query, "filename", filename, 100) == ESP_OK)
  98. {
  99. fn.append(filename);
  100. }
  101. else
  102. {
  103. fn.append("noname.jpg");
  104. }
  105. if (httpd_query_key_value(_query, "delay", _delay, 10) == ESP_OK)
  106. {
  107. std::string _delay_ = std::string(_delay);
  108. if (is_string_numeric(_delay_))
  109. {
  110. delay = std::atoi(_delay);
  111. if (delay < 0)
  112. {
  113. delay = 0;
  114. }
  115. }
  116. }
  117. }
  118. else
  119. {
  120. fn.append("noname.jpg");
  121. }
  122. esp_err_t result = Camera.capture_to_file(fn, delay);
  123. const char *resp_str = (const char *)fn.c_str();
  124. httpd_resp_send(req, resp_str, strlen(resp_str));
  125. return result;
  126. }
  127. else
  128. {
  129. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera not initialized: REST API /save not available!");
  130. return ESP_ERR_NOT_FOUND;
  131. }
  132. }
  133. void camera_register_uri(httpd_handle_t server)
  134. {
  135. httpd_uri_t camuri = {};
  136. camuri.method = HTTP_GET;
  137. camuri.uri = "/lighton";
  138. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_lightOn);
  139. camuri.user_ctx = (void *)"Light On";
  140. httpd_register_uri_handler(server, &camuri);
  141. camuri.uri = "/lightoff";
  142. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_lightOff);
  143. camuri.user_ctx = (void *)"Light Off";
  144. httpd_register_uri_handler(server, &camuri);
  145. camuri.uri = "/capture";
  146. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_capture);
  147. camuri.user_ctx = NULL;
  148. httpd_register_uri_handler(server, &camuri);
  149. camuri.uri = "/capture_with_flashlight";
  150. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_capture_with_light);
  151. camuri.user_ctx = NULL;
  152. httpd_register_uri_handler(server, &camuri);
  153. camuri.uri = "/save";
  154. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_capture_save_to_file);
  155. camuri.user_ctx = NULL;
  156. httpd_register_uri_handler(server, &camuri);
  157. }