| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- #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()
- {
- // Root endpoint - serves home page
- server->on("/", HTTP_GET, [this](AsyncWebServerRequest *request) {
- handleRoot(request);
- });
- // 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("/api/healthcheck", HTTP_GET, [this](AsyncWebServerRequest *request) {
- handleHealthCheck(request);
- });
- }
- /// <summary>
- /// Handles GET / request to serve the home page.
- /// Returns a simple HTML interface for controlling the camera remote.
- /// Provides buttons for focus, photo capture, reset, and multiple shot controls.
- /// </summary>
- void HTTPServer::handleRoot(AsyncWebServerRequest *request)
- {
- const char* html = R"rawliteral(
- <!DOCTYPE html>
- <html>
- <head>
- <title>Minolta Remote Control</title>
- <style>
- * { margin: 0; padding: 0; box-sizing: border-box; }
- body {
- font-family: Arial, sans-serif;
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
- min-height: 100vh;
- display: flex;
- justify-content: center;
- align-items: center;
- padding: 20px;
- }
- .container {
- background: white;
- border-radius: 15px;
- box-shadow: 0 20px 60px rgba(0,0,0,0.3);
- padding: 30px;
- max-width: 500px;
- width: 100%;
- }
- h1 {
- text-align: center;
- color: #333;
- margin-bottom: 30px;
- font-size: 28px;
- }
- .section {
- margin-bottom: 25px;
- padding-bottom: 25px;
- border-bottom: 1px solid #eee;
- }
- .section:last-child { border-bottom: none; }
- .section h2 {
- color: #667eea;
- font-size: 16px;
- margin-bottom: 15px;
- }
- button {
- width: 100%;
- padding: 12px 20px;
- margin-bottom: 10px;
- font-size: 16px;
- font-weight: bold;
- border: none;
- border-radius: 8px;
- cursor: pointer;
- transition: all 0.3s ease;
- }
- .btn-focus {
- background: #4CAF50;
- color: white;
- }
- .btn-focus:hover { background: #45a049; }
- .btn-photo {
- background: #2196F3;
- color: white;
- }
- .btn-photo:hover { background: #0b7dda; }
- .btn-reset {
- background: #f44336;
- color: white;
- }
- .btn-reset:hover { background: #da190b; }
- .input-group {
- display: flex;
- gap: 10px;
- margin-bottom: 10px;
- }
- input {
- flex: 1;
- padding: 10px;
- border: 1px solid #ddd;
- border-radius: 4px;
- font-size: 14px;
- }
- .btn-multiple {
- background: #FF9800;
- color: white;
- }
- .btn-multiple:hover { background: #e68900; }
- .status {
- margin-top: 20px;
- padding: 15px;
- border-radius: 8px;
- text-align: center;
- display: none;
- }
- .status.success {
- background: #d4edda;
- color: #155724;
- display: block;
- }
- .status.error {
- background: #f8d7da;
- color: #721c24;
- display: block;
- }
- </style>
- </head>
- <body>
- <div class="container">
- <h1>📷 Minolta Remote</h1>
-
- <div class="section">
- <h2>Basic Controls</h2>
- <button class="btn-focus" onclick="sendRequest('/api/focus')">🎯 Focus</button>
- <button class="btn-photo" onclick="sendRequest('/api/takePhoto')">📸 Take Photo</button>
- <button class="btn-reset" onclick="sendRequest('/api/reset')">🔄 Reset</button>
- </div>
-
- <div class="section">
- <h2>Photo Hold Duration</h2>
- <input type="number" id="msec" min="100" max="5000" value="1000" placeholder="Duration (ms)">
- <button class="btn-photo" onclick="takePhotoWithDelay()">📸 Take with Delay</button>
- </div>
-
- <div class="section">
- <h2>Multiple Shots</h2>
- <div class="input-group">
- <input type="number" id="count" min="1" max="100" value="3" placeholder="Count">
- <input type="number" id="delay" min="100" max="5000" value="500" placeholder="Delay (ms)">
- </div>
- <div class="input-group">
- <input type="number" id="msecMulti" min="100" max="5000" value="1000" placeholder="Duration (ms)">
- </div>
- <button class="btn-multiple" onclick="multipleShoot()">🎬 Multiple Shots</button>
- </div>
-
- <div id="status" class="status"></div>
- </div>
- <script>
- function sendRequest(endpoint) {
- fetch(endpoint)
- .then(response => {
- if (response.ok) {
- showStatus('✓ Success: ' + endpoint, 'success');
- } else {
- showStatus('✗ Error: ' + response.statusText, 'error');
- }
- })
- .catch(error => showStatus('✗ Connection Error', 'error'));
- }
- function takePhotoWithDelay() {
- const msec = document.getElementById('msec').value;
- fetch('/api/takePhoto?msec=' + msec)
- .then(response => {
- if (response.ok) {
- showStatus('✓ Photo taken (' + msec + 'ms)', 'success');
- } else {
- showStatus('✗ Error taking photo', 'error');
- }
- })
- .catch(error => showStatus('✗ Connection Error', 'error'));
- }
- function multipleShoot() {
- const count = document.getElementById('count').value;
- const delay = document.getElementById('delay').value;
- const msec = document.getElementById('msecMulti').value;
-
- if (count < 1 || count > 100) {
- showStatus('✗ Count must be between 1 and 100', 'error');
- return;
- }
-
- fetch('/api/multiple?count=' + count + '&delay=' + delay + '&msec=' + msec)
- .then(response => {
- if (response.ok) {
- showStatus('✓ ' + count + ' shots queued', 'success');
- } else {
- showStatus('✗ Error taking photos', 'error');
- }
- })
- .catch(error => showStatus('✗ Connection Error', 'error'));
- }
- function showStatus(message, type) {
- const status = document.getElementById('status');
- status.textContent = message;
- status.className = 'status ' + type;
-
- setTimeout(() => {
- status.className = 'status';
- }, 3000);
- }
- </script>
- </body>
- </html>
- )rawliteral";
- request->send(200, "text/html", html);
- }
- /// <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 /api/healthcheck 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("api/healthcheck");
- onRequestEnd("api/healthcheck");
- 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");
- }
|