diff --git a/server/src/commande_telephone.ino b/server/src/commande_telephone.ino index 201b06d..0293bb5 100644 --- a/server/src/commande_telephone.ino +++ b/server/src/commande_telephone.ino @@ -7,7 +7,7 @@ #include "config.h" // Configuration des entrées/sorties et delais. #include "controls.h" // Fonctions pour se déplacer. #include "convert.h" // Utilitaires pour convertir des mesures en steps. -#include "server.h" // Configure et demarre le server HTTP. +#include "server.hpp" // Configure et demarre le server HTTP. // État global de la commande en cours command_t command = {.running = false, .stopped = false}; @@ -18,11 +18,11 @@ AsyncWebServer server(80); // Code exécuté au démarrage (paramétrage) : void setup() { - setupPins(); // Configure les pins de l'ESP32 - Serial.begin(115200); // Démarrage d'une communication Série avec l'ordinateur s'il est connecté - connectWiFi(); // Connexion au wifi - serverConfig(); // Configuration du serveur - server.begin(); // Démarrage du serveur + setupPins(); // Configure les pins de l'ESP32 + Serial.begin(115200); // Démarrage d'une communication Série avec l'ordinateur s'il est connecté + connectWiFi(WiFi); // Connexion au wifi + serverConfig(server, command); // Configuration du serveur + server.begin(); // Démarrage du serveur } // Code exécuté en boucle jusqu'à l'extinction du robot diff --git a/server/src/server.h b/server/src/server.hpp similarity index 73% rename from server/src/server.h rename to server/src/server.hpp index 2f74acb..6a38fb7 100644 --- a/server/src/server.h +++ b/server/src/server.hpp @@ -11,16 +11,84 @@ struct command_t int value; // Valeur (nombre de pas à effectuer) }; -// Configuration du serveur asynchrone. -void serverConfig() +// Met à jour la command globale. +void setGlobals(String commandName, float commandValue, command_t &globalCommand) { - server.on("/get", HTTP_GET, [](AsyncWebServerRequest *request) + // Assert command before updating globals + if (commandName != "forward" && commandName != "backward" && commandName != "left" && commandName != "right") + { + return; + } + + // Update globals + globalCommand.name = commandName; + globalCommand.running = true; + globalCommand.stopped = false; + globalCommand.value = convertLengthToSteps(commandValue); +} + +/** + * @brief Lorsqu'une commande arrive au serveur, cette fonction permet. + * @internal + * + * @param commandName Nom de la commande. + * @param commandValue Valeur de la command (si 0 assigné à 474). + */ +void requestCheck(String commandName, float commandValue, command_t &globalCommand) +{ + if (commandName == "stop") + { + globalCommand.name = "stop"; + globalCommand.running = true; + globalCommand.stopped = true; + } + else + { + setGlobals(commandName, commandValue, globalCommand); + } + + if (commandValue == 0) + { + globalCommand.value = 474; + } +} + +/** + * Parse la requête GET (vers "/get") du serveur et résout le nom de la commande et sa valeur. + * + * Example + * String commandName; + * float commandValue; + * parseRequestParams(request, commandName, commandValue); + */ +void parseRequestParams(AsyncWebServerRequest *request, String &commandName, float &commandValue, command_t &globalCommand) +{ + // Stop here with error message if wrong command + if (!request->hasParam("command")) + { + commandName = "No command provided"; + return; + } + + // Update command from params + commandName = request->getParam("command")->value(); + + // Update value from params and use "0" as default + commandValue = request->hasParam("value") ? request->getParam("value")->value().toFloat() : 0; + + requestCheck(commandName, commandValue, globalCommand); +} + +// Configuration du serveur asynchrone. +void serverConfig(AsyncWebServer &server, command_t &globalCommand) +{ + server.on("/get", HTTP_GET, [&globalCommand](AsyncWebServerRequest *request) { String commandName; float commandValue; // Parse request params and update command and value - parseRequestParams(request, commandName, commandValue); + parseRequestParams(request, commandName, commandValue, globalCommand); Serial.print(commandName); Serial.print(": "); @@ -29,7 +97,7 @@ void serverConfig() } // Démarre le point d'accès WiFi. -void connectWiFi() +void connectWiFi(WiFiClass &WiFi) { // For simulator only WiFi.mode(WIFI_STA); @@ -49,72 +117,4 @@ void connectWiFi() // Serial.print("[+] AP Created with IP Gateway "); // Serial.println(WiFi.softAPIP()); // Serial.println(""); -} - -/** - * Parse la requête GET (vers "/get") du serveur et résout le nom de la commande et sa valeur. - * - * Example - * String commandName; - * float commandValue; - * parseRequestParams(request, commandName, commandValue); - */ -void parseRequestParams(AsyncWebServerRequest *request, String &commandName, float &commandValue) -{ - // Stop here with error message if wrong command - if (!request->hasParam("command")) - { - commandName = "No command provided"; - return; - } - - // Update command from params - commandName = request->getParam("command")->value(); - - // Update value from params and use "0" as default - commandValue = request->hasParam("value") ? request->getParam("value")->value().toFloat() : 0; - - requestCheck(commandName, commandValue); -} - -/** - * @brief Lorsqu'une commande arrive au serveur, cette fonction permet. - * @internal - * - * @param commandName Nom de la commande. - * @param commandValue Valeur de la command (si 0 assigné à 474). - */ -void requestCheck(String commandName, float commandValue) -{ - if (commandName == "stop") - { - command.name = "stop"; - command.running = true; - command.stopped = true; - } - else - { - setGlobals(commandName, commandValue); - } - - if (commandValue == 0) - { - command.value = 474; - } -} - -// Met à jour la command globale. -void setGlobals(String commandName, float commandValue) -{ - // Assert command before updating globals - if (commandName != "forward" && commandName != "backward" && commandName != "left" && commandName != "right") - { - return; - } - - // Update globals - command.name = commandName; - command.running = true; - command.stopped = false; - command.value = convertLengthToSteps(commandValue); } \ No newline at end of file