refactor(server): ♻️ reduce code complexity

This commit is contained in:
Julien Oculi 2024-06-25 17:57:54 +02:00
parent 1e5fc119ad
commit 455d6d6bce

View file

@ -87,25 +87,34 @@ void serverConfig()
{ {
String command; String command;
float value; float value;
if (request->hasParam("command")) {
command = request->getParam("command")->value(); // Parse request params and update command and value
if (request->hasParam("value")){ parseRequestParams(request, command, value);
value = request->getParam("value")->value().toFloat();
}
else{
value = 0;
}
requestCheck(command,value);
}
else {
command = "Not the good command";
}
Serial.print(command); Serial.print(command);
Serial.print(": "); Serial.print(": ");
Serial.println(value); Serial.println(value);
request->send(200, "text/plain", command); }); request->send(200, "text/plain", command); });
} }
void parseRequestParams(AsyncWebServerRequest &request, String &command, int &value)
{
// Stop here with error message if wrong command
if (!request->hasParam("command"))
{
command = "Not the good command";
return;
}
// Update command from params
command = request->getParam("command")->value();
// Update value from params and use "0" as default
value = request->hasParam("value") ? request->getParam("value")->value().toFloat() : 0;
requestCheck(command, value);
}
// Fonction pour convertir une longueur en cm vers un nombre de pas moteur. // Fonction pour convertir une longueur en cm vers un nombre de pas moteur.
int convertLengthToSteps(float length) int convertLengthToSteps(float length)
{ {