refactor(server): ♻️ extract server logic from main file

This commit is contained in:
Julien Oculi 2024-06-26 13:11:33 +02:00
parent 1e704fa356
commit 8d7c46f9c7
2 changed files with 123 additions and 100 deletions

View file

@ -7,15 +7,10 @@
#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.
// Variables de commande :
struct command_t
{
bool running; // Une commande doit être exécutée
bool stopped; // La commande doit être stoppée
String name; // Nom de la commande
int value; // Valeur (nombre de pas à effectuer)
} command = {false, false};
// État global de la commande en cours
command_t command = {.running = false, .stopped = false};
// Création du serveur asynchrone :
AsyncWebServer server(80);
@ -59,98 +54,6 @@ void loop()
}
}
// Configuration du serveur asynchrone
void serverConfig()
{
server.on("/get", HTTP_GET, [](AsyncWebServerRequest *request)
{
String commandName;
float commandValue;
// Parse request params and update command and value
parseRequestParams(request, commandName, commandValue);
Serial.print(commandName);
Serial.print(": ");
Serial.println(commandValue);
request->send(200, "text/plain", commandName); });
}
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);
}
// Lorsqu'une commande arrive au serveur, cette fonction permet
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;
}
}
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);
}
void connectWiFi()
{
// For simulator only
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
if (WiFi.waitForConnectResult() != WL_CONNECTED)
{
Serial.printf("WiFi Failed!\n");
return;
}
Serial.println(" Connected!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// WiFi.mode(WIFI_AP);
// WiFi.softAP(WIFI_SSID, WIFI_PASSWORD);
// Serial.print("[+] AP Created with IP Gateway ");
// Serial.println(WiFi.softAPIP());
// Serial.println("");
}
void chooseCommand()
{
if (command.name == "forward")

120
server/src/server.h Normal file
View file

@ -0,0 +1,120 @@
/************************************
Configuration et handlers du server
************************************/
// Type de la variable globale command
struct command_t
{
bool running; // Une commande doit être exécutée
bool stopped; // La commande doit être stoppée
String name; // Nom de la commande
int value; // Valeur (nombre de pas à effectuer)
};
// Configuration du serveur asynchrone.
void serverConfig()
{
server.on("/get", HTTP_GET, [](AsyncWebServerRequest *request)
{
String commandName;
float commandValue;
// Parse request params and update command and value
parseRequestParams(request, commandName, commandValue);
Serial.print(commandName);
Serial.print(": ");
Serial.println(commandValue);
request->send(200, "text/plain", commandName); });
}
// Démarre le point d'accès WiFi.
void connectWiFi()
{
// For simulator only
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
if (WiFi.waitForConnectResult() != WL_CONNECTED)
{
Serial.printf("WiFi Failed!\n");
return;
}
Serial.println(" Connected!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// WiFi.mode(WIFI_AP);
// WiFi.softAP(WIFI_SSID, WIFI_PASSWORD);
// 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);
}