From 65d43ae1d14cd87ad8edcccc6f5e25c21e821bee Mon Sep 17 00:00:00 2001 From: anschrammh Date: Sun, 2 Feb 2020 20:59:08 +0100 Subject: [PATCH] Removed both sdCardMount and unMount api end points, they are now replaced by a single end point taking a parameter --- src/app/webApi.cpp | 54 ++++++++++++++++++++++++++++++---------------- src/app/webApi.h | 3 +-- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/app/webApi.cpp b/src/app/webApi.cpp index fa2fe39..bb68299 100644 --- a/src/app/webApi.cpp +++ b/src/app/webApi.cpp @@ -124,34 +124,50 @@ boolean rtcSetTimeApi(WEBServer::HttpRequestData &HRD, WiFiClient *wc return true; } -boolean sdCardUnmountApi(WEBServer::HttpRequestData &HRD, WiFiClient *wc, void *pData) +boolean sdCardActionApi(WEBServer::HttpRequestData &HRD, WiFiClient *wc, void *pData) { SdCardApiPacket *pV = (SdCardApiPacket*)pData; SAB *p = (SAB *)pV->pSab; float *sdCardSize = (float *) &pV->pView->sdCardSize; char buffer[200]; - p->getSdCardManager().unMountSD(); - *sdCardSize = 0.0; - sprintf(buffer,"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{ \"status\" : \"ok\", \"card\" : \"unmounted\" }"); - wc->print(buffer); - return true; -} -boolean sdCardMountApi(WEBServer::HttpRequestData &HRD, WiFiClient *wc, void *pData) -{ - SdCardApiPacket *pV = (SdCardApiPacket*)pData; - SAB *p = (SAB *)pV->pSab; - float *sdCardSize = (float *) &pV->pView->sdCardSize; - char buffer[200]; - - if(p->getSdCardManager().mountSD()) + //We first retrieve the parameter that indicates the action : + if(HRD.getParams.count() != 1) { - sprintf(buffer,"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{ \"status\" : \"ok\", \"card\" : \"mounted\" }"); - *sdCardSize = p->getSdCardManager().getSize(GBYTE); + strcpy_P(buffer, PSTR("{\"status\":\"error\",\"message\":\"One and only one parameter is expected\"}")); } else - sprintf(buffer,"HTTP/1.1 500 OK\r\nContent-Type: application/json\r\n\r\n{ \"status\" : \"error\", \"message\" : \"begin failed\" }"); - + { + if(HRD.getParams("action") != NULL) + { + if(strcmp_P(HRD.getParams("action")->getString(), PSTR("mount")) == 0) + { + if(p->getSdCardManager().mountSD()) + { + strcpy_P(buffer, PSTR("{\"status\":\"ok\",\"card\":\"mounted\"}")); + *sdCardSize = p->getSdCardManager().getSize(GBYTE); + } + else + strcpy_P(buffer, PSTR("{\"status\":\"error\",\"message\":\"begin failed\"}")); + } + else if(strcmp_P(HRD.getParams("action")->getString(), PSTR("unmount")) == 0) + { + p->getSdCardManager().unMountSD(); + *sdCardSize = 0.0; + strcpy_P(buffer, PSTR("{\"status\":\"ok\",\"card\":\"unmounted\"}")); + } + else //Error + { + strcpy_P(buffer, PSTR("{\"status\":\"error\",\"message\":\"Parameter 'action' should be mount or unmount\"}")); + } + } + else //Error + { + strcpy_P(buffer, PSTR("{\"status\":\"error\",\"message\":\"Parameter should be 'action'\"}")); + } + } + + WEBServer::injectApiHeader(buffer, "application/json", buffer); wc->print(buffer); return true; } diff --git a/src/app/webApi.h b/src/app/webApi.h index 22be93b..fdd0412 100644 --- a/src/app/webApi.h +++ b/src/app/webApi.h @@ -16,8 +16,7 @@ typedef struct sdCardApiPacket SAB *pSab; View1Packet *pView; } SdCardApiPacket; -boolean sdCardUnmountApi(WEBServer::HttpRequestData&, WiFiClient*, void*); -boolean sdCardMountApi(WEBServer::HttpRequestData&, WiFiClient*, void*); +boolean sdCardActionApi(WEBServer::HttpRequestData&, WiFiClient*, void*); boolean apScannerApi(WEBServer::HttpRequestData&, WiFiClient*, void*); boolean staWifiInfoApi(WEBServer::HttpRequestData&, WiFiClient*, void*);