| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- #include "HTTPServer.h"
- #include <Arduino.h>
- /// <summary>
- /// Constructs an HTTPServer instance with the specified port and shutter controller.
- /// Creates a new AsyncWebServer instance but does not start it yet.
- /// </summary>
- HTTPServer::HTTPServer(uint16_t port, IShutterController* controller)
- : shutterController(controller)
- {
- server = new AsyncWebServer(port);
- }
- /// <summary>
- /// Destructor that properly cleans up the AsyncWebServer instance.
- /// </summary>
- HTTPServer::~HTTPServer()
- {
- if (server)
- {
- delete server;
- }
- }
- /// <summary>
- /// Configures the ESP8266 as a WiFi access point with the provided credentials.
- /// Sets up the static IP configuration and starts the access point.
- /// </summary>
- void HTTPServer::setupWiFiAP(const char* ssid, const char* password,
- IPAddress local_IP, IPAddress gateway, IPAddress subnet)
- {
- if (!WiFi.softAPConfig(local_IP, gateway, subnet))
- {
- Serial.println("Failed to config IP");
- }
- WiFi.softAP(ssid, password);
- Serial.println(WiFi.softAPIP());
- Serial.println("AP Started - Waiting for connections");
- }
- /// <summary>
- /// Registers all HTTP API routes and their corresponding handlers.
- /// Associates each route with its handler method using lambda expressions.
- /// </summary>
- void HTTPServer::setupRoutes()
- {
- // Focus endpoint
- server->on("/api/focus", HTTP_GET, [this](AsyncWebServerRequest *request) {
- handleFocus(request);
- });
- // Take photo endpoint
- server->on("/api/takePhoto", HTTP_GET, [this](AsyncWebServerRequest *request) {
- handleTakePhoto(request);
- });
- // Reset endpoint
- server->on("/api/reset", HTTP_GET, [this](AsyncWebServerRequest *request) {
- handleReset(request);
- });
- // Multiple shoot endpoint
- server->on("/api/multiple", HTTP_GET, [this](AsyncWebServerRequest *request) {
- handleMultiple(request);
- });
- // Health check endpoint
- server->on("/get", HTTP_GET, [this](AsyncWebServerRequest *request) {
- handleHealthCheck(request);
- });
- }
- /// <summary>
- /// Handles GET /api/focus request to perform a half press (focus) operation.
- /// Logs the request, activates the camera focus, and responds with success.
- /// </summary>
- void HTTPServer::handleFocus(AsyncWebServerRequest *request)
- {
- onRequestStart("api/focus");
- shutterController->halfPress();
- onRequestEnd("api/focus");
- request->send(200, "application/json", "{\"result\":\"true\"}");
- }
- /// <summary>
- /// Handles GET /api/takePhoto request to take a photograph.
- /// Supports optional "msec" query parameter to control the shutter hold duration (default: 1000ms).
- /// </summary>
- void HTTPServer::handleTakePhoto(AsyncWebServerRequest *request)
- {
- onRequestStart("api/takePhoto");
- if (request->hasParam("msec"))
- {
- int ms = request->getParam("msec")->value().toInt();
- shutterController->pressFullWithDelay(ms);
- }
- else
- {
- shutterController->pressFullWithDelay(1000);
- }
- onRequestEnd("api/takePhoto");
- request->send(200, "application/json", "{\"result\":\"true\"}");
- }
- /// <summary>
- /// Handles GET /api/reset request to release the shutter and return to neutral position.
- /// Logs the request, releases all controls, and responds with success.
- /// </summary>
- void HTTPServer::handleReset(AsyncWebServerRequest *request)
- {
- onRequestStart("api/reset");
- shutterController->unPress();
- onRequestEnd("api/reset");
- request->send(200, "application/json", "{\"result\":\"true\"}");
- }
- /// <summary>
- /// Handles GET /api/multiple request to take multiple photos in sequence.
- /// Requires query parameters: "count" (number of photos) and "delay" (ms between shots).
- /// Supports optional "msec" parameter for individual shot duration (default: 1000ms).
- /// </summary>
- void HTTPServer::handleMultiple(AsyncWebServerRequest *request)
- {
- onRequestStart("api/multiple");
- if (request->hasParam("count") && request->hasParam("delay"))
- {
- int ms = 1000;
- int count = request->getParam("count")->value().toInt();
- int delayMs = request->getParam("delay")->value().toInt();
- if (request->hasParam("msec"))
- {
- ms = request->getParam("msec")->value().toInt();
- }
- shutterController->multipleShoot(ms, count, delayMs);
- }
- onRequestEnd("api/multiple");
- request->send(200, "application/json", "{\"result\":\"true\"}");
- }
- /// <summary>
- /// Handles GET /get request as a simple health check endpoint.
- /// Returns a plain text "Is working" response to verify server connectivity.
- /// </summary>
- void HTTPServer::handleHealthCheck(AsyncWebServerRequest *request)
- {
- onRequestStart("get");
- onRequestEnd("get");
- request->send(200, "text/plain", "Is working");
- }
- /// <summary>
- /// Logs the start of a request to the serial console and enables the status LED.
- /// Called at the beginning of each request handler.
- /// </summary>
- /// <param name="endpoint">The name of the API endpoint being processed</param>
- void HTTPServer::onRequestStart(const char* endpoint)
- {
- Serial.print("[REQUEST] ");
- Serial.println(endpoint);
- digitalWrite(LED_BUILTIN, HIGH);
- }
- /// <summary>
- /// Logs the completion of a request to the serial console and disables the status LED.
- /// Called at the end of each request handler.
- /// </summary>
- /// <param name="endpoint">The name of the API endpoint that was processed</param>
- void HTTPServer::onRequestEnd(const char* endpoint)
- {
- digitalWrite(LED_BUILTIN, LOW);
- Serial.print("[RESPONSE] ");
- Serial.println(endpoint);
- }
- /// <summary>
- /// Initializes the WiFi access point and starts the HTTP server.
- /// Must be called during application setup to enable the server.
- /// Calls setupWiFiAP() to configure WiFi, setupRoutes() to register endpoints, and starts the server.
- /// </summary>
- void HTTPServer::begin(const char* ssid, const char* password,
- IPAddress local_IP, IPAddress gateway, IPAddress subnet)
- {
- setupWiFiAP(ssid, password, local_IP, gateway, subnet);
- setupRoutes();
- server->begin();
- Serial.println("HTTP Server started");
- }
|