refactor(server): ♻️ extract server logic from main file
This commit is contained in:
parent
1e704fa356
commit
8d7c46f9c7
|
@ -7,15 +7,10 @@
|
||||||
#include "config.h" // Configuration des entrées/sorties et delais.
|
#include "config.h" // Configuration des entrées/sorties et delais.
|
||||||
#include "controls.h" // Fonctions pour se déplacer.
|
#include "controls.h" // Fonctions pour se déplacer.
|
||||||
#include "convert.h" // Utilitaires pour convertir des mesures en steps.
|
#include "convert.h" // Utilitaires pour convertir des mesures en steps.
|
||||||
|
#include "server.h" // Configure et demarre le server HTTP.
|
||||||
|
|
||||||
// Variables de commande :
|
// État global de la commande en cours
|
||||||
struct command_t
|
command_t command = {.running = false, .stopped = false};
|
||||||
{
|
|
||||||
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};
|
|
||||||
|
|
||||||
// Création du serveur asynchrone :
|
// Création du serveur asynchrone :
|
||||||
AsyncWebServer server(80);
|
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()
|
void chooseCommand()
|
||||||
{
|
{
|
||||||
if (command.name == "forward")
|
if (command.name == "forward")
|
||||||
|
|
120
server/src/server.h
Normal file
120
server/src/server.h
Normal 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);
|
||||||
|
}
|
Loading…
Reference in a new issue