4#include "base_detection_module.h"
9#include <esp_http_server.h>
12static const char* TAG_WEBSOCKETS =
"WEBSOCKETS";
18 : _server_handle(server_handle), _uri(uri), _is_registered(
false)
24 void register_endpoint()
26 ESP_LOGI(TAG_WEBSOCKETS,
"WebSocket: Register endpoint %s", this->_uri);
27 httpd_uri_t uri_obj = {0};
28 uri_obj.uri = this->_uri;
29 uri_obj.method = HTTP_GET;
30 uri_obj.user_ctx =
this;
31 uri_obj.handler = WebsocketHandler::websocket_handler;
32 uri_obj.is_websocket =
true;
34 httpd_register_uri_handler(this->_server_handle, &uri_obj);
37 void unregister_endpoint()
39 ESP_LOGI(TAG_WEBSOCKETS,
"WebSocket: Unregister endpoint %s", this->_uri);
40 httpd_unregister_uri_handler(this->_server_handle, this->_uri, HTTP_GET);
43 static esp_err_t websocket_handler(httpd_req_t* req)
47 if (req->method == HTTP_GET)
50 ESP_LOGI(TAG_WEBSOCKETS,
"WebSocket: New connection established to %s", _this->_uri);
55 httpd_ws_frame_t ws_pkt;
56 memset(&ws_pkt, 0,
sizeof(httpd_ws_frame_t));
59 esp_err_t ret = httpd_ws_recv_frame(req, &ws_pkt, 0);
63 ESP_LOGW(TAG_WEBSOCKETS,
"WebSocket: Frame receive error: %s", esp_err_to_name(ret));
72 uint8_t* buf = (uint8_t*)malloc(ws_pkt.len);
79 ret = httpd_ws_recv_frame(req, &ws_pkt, ws_pkt.len);
86 httpd_ws_frame_t response_pkt = {0};
89 void* resource = _this->handler(ws_pkt.payload, ws_pkt.len, response_pkt.payload, response_pkt.len);
92 ESP_LOGE(TAG_WEBSOCKETS,
"Handler returned NULL for %s", _this->_uri);
96 ESP_LOGD(TAG_WEBSOCKETS,
"Sending response on %s, type=%d, len=%d", _this->_uri, response_pkt.type,
99 ret = httpd_ws_send_frame(req, &response_pkt);
106 _this->release_resource(resource);
113 virtual void* handler(
const uint8_t* recv_buf,
size_t recv_len, uint8_t*& out_buf,
size_t& out_len) = 0;
115 virtual void release_resource(
void* resource) {}
124 httpd_handle_t _server_handle;
137 virtual void* handler(
const uint8_t* recv_buf,
size_t recv_len, uint8_t*& out_buf,
size_t& out_len)
143 ESP_LOGE(TAG_WEBSOCKETS,
"Frame buffer is invalid");
147 out_buf = (uint8_t*)fb.buffer;
153 virtual httpd_ws_type_t
get_frame_type()
const override {
return HTTPD_WS_TYPE_BINARY; }
155 virtual void release_resource(
void* resource)
173 virtual void* handler(
const uint8_t* recv_buf,
size_t recv_len, uint8_t*& out_buf,
size_t& out_len)
176 uint8_t* json_buf = (uint8_t*)malloc(256);
179 ESP_LOGE(TAG_WEBSOCKETS,
"Failed to allocate JSON buffer");
180 out_buf = (uint8_t*)
"{}";
186 if (_detection_instance !=
nullptr)
191 int len = snprintf((
char*)json_buf, 256,
192 "{\"detected\":%d,\"x\":%d,\"y\":%d,\"width\":%d,\"height\":%d,\"pixels\":%d}",
198 ESP_LOGD(TAG_WEBSOCKETS,
"Metrics: detected=%d, x=%d, y=%d, pixels=%d", motion.
is_detected(),
203 int len = snprintf((
char*)json_buf, 256,
204 "{\"detected\":0,\"x\":0,\"y\":0,\"width\":320,\"height\":240,\"pixels\":0}");
207 ESP_LOGW(TAG_WEBSOCKETS,
"No detection module connected, sending fallback metrics");
210 return (
void*)json_buf;
213 virtual void release_resource(
void* resource)
215 if (resource !=
nullptr)
An abstract base class (Interface) that defines the contract for detection algorithms....
Definition base_detection_module.h:22
virtual MotionData get_motion_data() const
Get the latest motion data from the detection module.
Definition base_detection_module.h:44
Singleton-style manager for camera lifecycle and frame acquisition.
Definition camera.h:63
Definition websockets.h:166
Clean abstraction for motion detection results.
Definition motion_data.h:20
int get_centroid_x() const
Get motion centroid X coordinate.
Definition motion_data.h:73
int get_frame_height() const
Get the height of the frame processed.
Definition motion_data.h:91
int get_pixel_count() const
Get the volume of detected motion.
Definition motion_data.h:97
int get_centroid_y() const
Get motion centroid Y coordinate.
Definition motion_data.h:79
int get_frame_width() const
Get the width of the frame processed.
Definition motion_data.h:85
bool is_detected() const
Check if valid motion was detected in this snapshot.
Definition motion_data.h:67
Definition websockets.h:130
virtual httpd_ws_type_t get_frame_type() const override
Get the WebSocket frame type for this handler.
Definition websockets.h:153
Definition websockets.h:15
virtual httpd_ws_type_t get_frame_type() const
Get the WebSocket frame type for this handler.
Definition websockets.h:121