From 455d6d6bce073b6cf16e5328fd8347c5a4cf06e6 Mon Sep 17 00:00:00 2001 From: Julien Oculi Date: Tue, 25 Jun 2024 17:57:54 +0200 Subject: [PATCH] refactor(server): :recycle: reduce code complexity --- server/commande_telephone.ino | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/server/commande_telephone.ino b/server/commande_telephone.ino index 2820cde..590eea3 100644 --- a/server/commande_telephone.ino +++ b/server/commande_telephone.ino @@ -87,25 +87,34 @@ void serverConfig() { String command; float value; - if (request->hasParam("command")) { - command = request->getParam("command")->value(); - if (request->hasParam("value")){ - value = request->getParam("value")->value().toFloat(); - } - else{ - value = 0; - } - requestCheck(command,value); - } - else { - command = "Not the good command"; - } + + // Parse request params and update command and value + parseRequestParams(request, command, value); + Serial.print(command); Serial.print(": "); Serial.println(value); 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. int convertLengthToSteps(float length) {