refactor(server): ♻️ rewrite server config to c++ header

This commit is contained in:
Julien Oculi 2024-06-26 14:00:43 +02:00
parent 60cf7da8d7
commit 44699dba7c
2 changed files with 79 additions and 79 deletions

View file

@ -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

View file

@ -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);
@ -50,71 +118,3 @@ void connectWiFi()
// 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);
}