diff --git a/src/app/webApi.cpp b/src/app/webApi.cpp index 1330dec..130cdba 100644 --- a/src/app/webApi.cpp +++ b/src/app/webApi.cpp @@ -1,9 +1,32 @@ #include "SAB.h" #include "webApi.h" -boolean helloServerApi(WEBServerManager::HttpRequestData &HRD, WiFiClient* wc, void* pData) +boolean apiTesterApi(WEBServerManager::HttpRequestData &HRD, WiFiClient* wc, void* pData) { - wc->print(F("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\n\r\n

Hello client !!!

\r\n")); + wc->print(F("HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{ \"status\" : \"ok\", \"API\" : \"available\" }")); + return true; +} + +boolean ViewByUIDApi(WEBServerManager::HttpRequestData &HRD, WiFiClient *wc, void *pData) +{ + SAB *p = (SAB *)pData; + char buffer[200]; + DictionaryHelper::StringEntity *pSE = HRD.getParams("UID"); + + if(pSE == NULL) + { + sprintf(buffer,"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{ \"status\" : \"failed\", \"message\" : \"expected UID parameter\" }"); + } + else if(strlen(pSE->getString()) > 0) + { + if(p->getScreenManager().displayView(atoi(pSE->getString()))) + sprintf(buffer,"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{ \"status\" : \"ok\", \"ViewUID\" : \"%d\" }", p->getScreenManager().getCurrentViewUID()); + else + sprintf(buffer,"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{ \"status\" : \"failed\", \"message\" : \"%s\" }", p->getScreenManager().getErrorMessage()); + } + else + sprintf(buffer,"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{ \"status\" : \"failed\", \"message\" : \"UID parameter empty\" }"); + wc->print(buffer); return true; } @@ -11,13 +34,15 @@ boolean nextViewApi(WEBServerManager::HttpRequestData &HRD, WiFiClient *wc, void { SAB *p = (SAB *)pData; char buffer[200]; - p->getScreenManager().displayNextView(); - sprintf(buffer,"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{ \"status\" : \"ok\", \"ViewUID\" : \"%d\" }", p->getScreenManager().getCurrentViewUID()); + if(p->getScreenManager().displayNextView()) + sprintf(buffer,"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{ \"status\" : \"ok\", \"ViewUID\" : \"%d\" }", p->getScreenManager().getCurrentViewUID()); + else + sprintf(buffer,"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{ \"status\" : \"failed\", \"message\" : \"%s\" }", p->getScreenManager().getErrorMessage()); wc->print(buffer); return true; } -boolean rtcTimeApi(WEBServerManager::HttpRequestData &HRD, WiFiClient *wc, void *pData) +boolean rtcGetTimeApi(WEBServerManager::HttpRequestData &HRD, WiFiClient *wc, void *pData) { SAB *p = (SAB *)pData; char buffer[200]; @@ -27,26 +52,89 @@ boolean rtcTimeApi(WEBServerManager::HttpRequestData &HRD, WiFiClient *wc, void return true; } -boolean sdCardEjectApi(WEBServerManager::HttpRequestData &HRD, WiFiClient *wc, void *pData) +boolean rtcSetTimeApi(WEBServerManager::HttpRequestData &HRD, WiFiClient *wc, void *pData) { SAB *p = (SAB *)pData; char buffer[200]; - p->getSdCardManager().end(); - sprintf(buffer,"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{ \"status\" : \"ok\", \"card\" : \"ejected\" }"); + DictionaryHelper::StringEntity *pSE = HRD.getParams("datetime"); + if(pSE == NULL) + { + sprintf(buffer,"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{ \"status\" : \"failed\", \"message\" : \"expected datetime parameter\" }"); + } + else if(strlen(pSE->getString()) > 0) + { + Dictionary *dictio = pSE->split('_'); + if(dictio == NULL) + { + sprintf(buffer,"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{ \"status\" : \"failed\", \"message\" : \"datetime format error\" }"); + wc->print(buffer); + return true; + } + else + { + if(dictio->count() != 6) + { + sprintf(buffer,"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{ \"status\" : \"error\", \"message\" : \"datetime format error\" }"); + } + else + { + p->getRtcManager().setDateTime(DateTime( + atoi(dictio->get(2)->getString()), + atoi(dictio->get(1)->getString()), + atoi(dictio->get((unsigned int)0)->getString()), + atoi(dictio->get(3)->getString()), + atoi(dictio->get(4)->getString()), + atoi(dictio->get(5)->getString()) + )); + DateTime d = p->getRtcManager().getDateTime(); + sprintf(buffer,"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{ \"status\" : \"ok\", \"date\" : \"%d/%d/%d\", \"time\" : \"%d:%d:%d\" }", d.day(), d.month(), d.year(), d.hour(), d.minute(), d.second()); + } + + wc->print(buffer); + delete dictio; + + return true; + } + } wc->print(buffer); return true; } -boolean sdCardInsertApi(WEBServerManager::HttpRequestData &HRD, WiFiClient *wc, void *pData) +boolean sdCardUnmountApi(WEBServerManager::HttpRequestData &HRD, WiFiClient *wc, void *pData) +{ + SAB *p = (SAB *)pData; + char buffer[200]; + p->getSdCardManager().end(); + 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(WEBServerManager::HttpRequestData &HRD, WiFiClient *wc, void *pData) { SAB *p = (SAB *)pData; char buffer[200]; if(p->getSdCardManager().begin(p->getSdCardConfig().getSPISpeed(), p->getPinConfig().getSPI_sdCard_cs())) - sprintf(buffer,"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{ \"status\" : \"ok\", \"card\" : \"inserted\" }"); + sprintf(buffer,"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{ \"status\" : \"ok\", \"card\" : \"mounted\" }"); else sprintf(buffer,"HTTP/1.1 500 OK\r\nContent-Type: application/json\r\n\r\n{ \"status\" : \"error\", \"message\" : \"begin failed\" }"); wc->print(buffer); return true; } + +boolean espRestartApi(WEBServerManager::HttpRequestData &HRD, WiFiClient *wc, void *pData) +{ + ESP.restart(); + + return true; +} + +boolean espResetApi(WEBServerManager::HttpRequestData &HRD, WiFiClient *wc, void *pData) +{ + ESP.restart(); + + return true; +} + diff --git a/src/app/webApi.h b/src/app/webApi.h index 0ff9434..932dfa9 100644 --- a/src/app/webApi.h +++ b/src/app/webApi.h @@ -2,10 +2,14 @@ #define WEBAPI_H #include "WebServerManager.h" -boolean helloServerApi(WEBServerManager::HttpRequestData&, WiFiClient*, void*); +boolean apiTesterApi(WEBServerManager::HttpRequestData&, WiFiClient*, void*); boolean nextViewApi(WEBServerManager::HttpRequestData&, WiFiClient*, void*); -boolean rtcTimeApi(WEBServerManager::HttpRequestData&, WiFiClient*, void*); -boolean sdCardEjectApi(WEBServerManager::HttpRequestData&, WiFiClient*, void*); -boolean sdCardInsertApi(WEBServerManager::HttpRequestData&, WiFiClient*, void*); +boolean ViewByUIDApi(WEBServerManager::HttpRequestData&, WiFiClient*, void*); +boolean rtcGetTimeApi(WEBServerManager::HttpRequestData&, WiFiClient*, void*); +boolean rtcSetTimeApi(WEBServerManager::HttpRequestData&, WiFiClient*, void*); +boolean sdCardUnmountApi(WEBServerManager::HttpRequestData&, WiFiClient*, void*); +boolean sdCardMountApi(WEBServerManager::HttpRequestData&, WiFiClient*, void*); +boolean espRestartApi(WEBServerManager::HttpRequestData&, WiFiClient*, void*); +boolean espResetApi(WEBServerManager::HttpRequestData&, WiFiClient*, void*); #endif