Replaced the previous http header sends by the new way of doing it : ie using the sendHTTPHeader helper function

This commit is contained in:
anschrammh 2022-04-22 08:10:02 +02:00
parent 1c1992820d
commit db1873b714

View File

@ -21,17 +21,19 @@ boolean viewByUIDApi(WEBServer<WEBClient>::HttpRequestData &HRD, WiFiClient *wc,
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\"}");
strcpy(buffer, "{\"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());
sprintf(buffer, "{\"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());
sprintf(buffer, "{\"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\"}");
strcpy(buffer, "{\"status\":\"failed\",\"message\":\"UID parameter empty\"}");
WEBServer<WEBClient>::sendHTTPHeader(wc, HttpConstants::httpMIMETypeToString(HttpConstants::APPLICATION_JSON), strlen(buffer));
wc->print(buffer);
return true;
}
@ -42,12 +44,12 @@ boolean nextViewApi(WEBServer<WEBClient>::HttpRequestData &HRD, WiFiClient *wc,
SAB *p = (SAB *)pData;
char buffer[200];
p->getScreenManager().displayNextView();
if(p->getScreenManager().getError() == ScreenManager::OK)
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, "{\"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());
sprintf(buffer, "{\"status\":\"failed\",\"message\":\"%s\"}", p->getScreenManager().getErrorMessage());
WEBServer<WEBClient>::sendHTTPHeader(wc, HttpConstants::httpMIMETypeToString(HttpConstants::APPLICATION_JSON), strlen(buffer));
wc->print(buffer);
return true;
}
@ -59,10 +61,11 @@ boolean reloadViewApi(WEBServer<WEBClient>::HttpRequestData &HRD, WiFiClient *wc
char buffer[200];
if(p->getScreenManager().init())
strcpy(buffer, "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{\"status\":\"ok\",\"message\":\"config reloaded\"}");
strcpy(buffer, "{\"status\":\"ok\",\"message\":\"config reloaded\"}");
else
strcpy(buffer, "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{\"status\":\"failed\",\"message\":\"unable to reload config\"}");
strcpy(buffer, "{\"status\":\"failed\",\"message\":\"unable to reload config\"}");
WEBServer<WEBClient>::sendHTTPHeader(wc, HttpConstants::httpMIMETypeToString(HttpConstants::APPLICATION_JSON), strlen(buffer));
wc->print(buffer);
return true;
}
@ -88,22 +91,20 @@ boolean rtcSetTimeApi(WEBServer<WEBClient>::HttpRequestData &HRD, WiFiClient *wc
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\"}");
strcpy(buffer,"{\"status\":\"failed\",\"message\":\"expected datetime parameter\"}");
}
else if(strlen(pSE->getString()) > 0)
{
Dictionary<DictionaryHelper::StringEntity> *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;
strcpy(buffer,"{\"status\":\"failed\",\"message\":\"datetime format error\"}");
}
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\"}");
strcpy(buffer,"{\"status\":\"error\",\"message\":\"datetime format error\"}");
}
else
{
@ -116,15 +117,16 @@ boolean rtcSetTimeApi(WEBServer<WEBClient>::HttpRequestData &HRD, WiFiClient *wc
atoi(dictio->getAt(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());
sprintf(buffer,"{\"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;
}
}
else
{
strcpy(buffer,"{\"status\":\"failed\",\"message\":\"datetime parameter is empty\"}");
}
WEBServer<WEBClient>::sendHTTPHeader(wc, HttpConstants::httpMIMETypeToString(HttpConstants::APPLICATION_JSON), strlen(buffer));
wc->print(buffer);
return true;
}
@ -183,7 +185,8 @@ boolean espRestartApi(WEBServer<WEBClient>::HttpRequestData &HRD, WiFiClient *wc
SAB *sab = (SAB*)pData;
char buffer[200];
sprintf(buffer,"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{\"status\":\"ok\",\"message\":\"module %s in 10 seconds\"}",strstr(HRD.httpResource,"reset") == NULL ? "restarting" : "resetting");
sprintf(buffer,"{\"status\":\"ok\",\"message\":\"module %s in 10 seconds\"}",strstr(HRD.httpResource,"reset") == NULL ? "restarting" : "resetting");
WEBServer<WEBClient>::sendHTTPHeader(wc, HttpConstants::httpMIMETypeToString(HttpConstants::APPLICATION_JSON), strlen(buffer));
wc->print(buffer);
sab->getTaskSchedulerManager().addTask(sab->getTaskSchedulerManager().findFreeTaskId(), TaskSchedulerManagerHelper::Schedule::scheduleBuilder()
@ -206,10 +209,11 @@ boolean sdCardSizeApi(WEBServer<WEBClient>::HttpRequestData &HRD, WiFiClient *wc
char buffer[200];
double size = p->getSdCardManager().getSize(GBYTE);
if(size == 0.0)
sprintf(buffer,"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{\"status\":\"ok\",\"card\":\"not present\",\"size\":\"0\"}");
strcpy(buffer,"{\"status\":\"ok\",\"card\":\"not present\",\"size\":\"0\"}");
else
sprintf(buffer,"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{\"status\":\"ok\",\"card\":\"present\",\"size\":\"%.2f\",\"unit\":\"GByte\"}",size);
sprintf(buffer,"{\"status\":\"ok\",\"card\":\"present\",\"size\":\"%.2f\",\"unit\":\"GByte\"}",size);
WEBServer<WEBClient>::sendHTTPHeader(wc, HttpConstants::httpMIMETypeToString(HttpConstants::APPLICATION_JSON), strlen(buffer));
wc->print(buffer);
return true;
}
@ -222,8 +226,8 @@ boolean staWifiInfoApi(WEBServer<WEBClient>::HttpRequestData &HRD, WiFiClient *w
IPAddress IP = p->getConnectivityManager().localIP();
sprintf(buffer,"{\"status\":\"ok\",\"RSSI\":%d,\"RSSI2\":%d,\"local IP\":\"%u.%u.%u.%u\",\"mac\":\"%s\"}", p->getConnectivityManager().RSSI(), p->getConnectivityManager().RSSIPercent(), IP[0], IP[1], IP[2], IP[3], p->getConnectivityManager().macAddress().c_str());
WEBServer<WEBClient>::sendHTTPHeader(wc, HttpConstants::httpMIMETypeToString(HttpConstants::APPLICATION_JSON), strlen(buffer));
WEBServer<WEBClient>::sendHTTPHeader(wc, HttpConstants::httpMIMETypeToString(HttpConstants::APPLICATION_JSON), strlen(buffer));
wc->print(buffer);
return true;
}
@ -247,7 +251,8 @@ boolean apScannerApi(WEBServer<WEBClient>::HttpRequestData &HRD, WiFiClient *wc,
(void)HRD;
(void)pData;
uint8_t number = WiFi.scanNetworks();
wc->print("HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n[");
WEBServer<WEBClient>::sendHTTPHeader(wc, HttpConstants::httpMIMETypeToString(HttpConstants::APPLICATION_JSON));
wc->print("[");
for(uint8_t i(0); i < number; i++)
{
@ -283,12 +288,13 @@ boolean powerInfoApi(WEBServer<WEBClient>::HttpRequestData &HRD, WiFiClient *wc,
char buffer[300];
PowerManager::PowerInfo pi = p->getPowerManager().getPowerInfo();
sprintf(buffer,"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{\"status\":\"ok\",\"power type\":\"%s\",\"level\":\"%u\",\"unit\":\"%%\"}", pi.powerType == PowerManager::USB ? "USB" : "BAT", pi.level);
sprintf(buffer,"{\"status\":\"ok\",\"power type\":\"%s\",\"level\":\"%u\",\"unit\":\"%%\"}", pi.powerType == PowerManager::USB ? "USB" : "BAT", pi.level);
WEBServer<WEBClient>::sendHTTPHeader(wc, HttpConstants::httpMIMETypeToString(HttpConstants::APPLICATION_JSON), strlen(buffer));
wc->print(buffer);
return true;
}
/* Changing the MCU frequency on the fly isn"t supported anymore by the SDK.
boolean powerSettingsApi(WEBServer<WEBClient>::HttpRequestData &HRD, WiFiClient *wc, void *pData)
{
SAB *p = (SAB *)pData;
@ -321,6 +327,7 @@ boolean powerSettingsApi(WEBServer<WEBClient>::HttpRequestData &HRD, WiFiClient
wc->print(buffer);
return true;
}
*/
boolean ioGetLevelApi(WEBServer<WEBClient>::HttpRequestData &HRD, WiFiClient *wc, void *pData)
{
@ -330,11 +337,11 @@ boolean ioGetLevelApi(WEBServer<WEBClient>::HttpRequestData &HRD, WiFiClient *wc
if(HRD.getParams.count() == 0)//We send every IO state
{
sprintf(buffer,"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{\"status\":\"ok\",\"P0\":\"%d\",\"P1\":\"%d\",\"P2\":\"%d\",\"P3\":\"%d\",\"P4\":\"%d\",\"P5\":\"%d\",\"P6\":\"%d\",\"P7\":\"%d\"}",ioState[0],ioState[1],ioState[2],ioState[3],ioState[4],ioState[5],ioState[6],ioState[7]);
sprintf(buffer,"{\"status\":\"ok\",\"P0\":\"%d\",\"P1\":\"%d\",\"P2\":\"%d\",\"P3\":\"%d\",\"P4\":\"%d\",\"P5\":\"%d\",\"P6\":\"%d\",\"P7\":\"%d\"}",ioState[0],ioState[1],ioState[2],ioState[3],ioState[4],ioState[5],ioState[6],ioState[7]);
}
else
{
strcpy(buffer,"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{\"status\":\"ok\"");
strcpy(buffer,"{\"status\":\"ok\"");
if(HRD.getParams("P0") != NULL)
{
sprintf(helperBuffer,",\"P0\":\"%d\"",ioState[0]);
@ -378,6 +385,7 @@ boolean ioGetLevelApi(WEBServer<WEBClient>::HttpRequestData &HRD, WiFiClient *wc
strcat(buffer,"}");
}
WEBServer<WEBClient>::sendHTTPHeader(wc, HttpConstants::httpMIMETypeToString(HttpConstants::APPLICATION_JSON), strlen(buffer));
wc->print(buffer);
return true;
}
@ -422,8 +430,9 @@ boolean ioSetLevelApi(WEBServer<WEBClient>::HttpRequestData &HRD, WiFiClient *wc
}
p->getIoManager().getPcf().digitalReadAll(ioState);//We retrieve the IO state
sprintf(buffer,"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{\"status\":\"ok\",\"P0\":\"%d\",\"P1\":\"%d\",\"P2\":\"%d\",\"P3\":\"%d\",\"P4\":\"%d\",\"P5\":\"%d\",\"P6\":\"%d\",\"P7\":\"%d\"}",ioState[0],ioState[1],ioState[2],ioState[3],ioState[4],ioState[5],ioState[6],ioState[7]);
sprintf(buffer,"{\"status\":\"ok\",\"P0\":\"%d\",\"P1\":\"%d\",\"P2\":\"%d\",\"P3\":\"%d\",\"P4\":\"%d\",\"P5\":\"%d\",\"P6\":\"%d\",\"P7\":\"%d\"}",ioState[0],ioState[1],ioState[2],ioState[3],ioState[4],ioState[5],ioState[6],ioState[7]);
WEBServer<WEBClient>::sendHTTPHeader(wc, HttpConstants::httpMIMETypeToString(HttpConstants::APPLICATION_JSON), strlen(buffer));
wc->print(buffer);
return true;
}
@ -440,11 +449,19 @@ boolean ioGetModeApi(WEBServer<WEBClient>::HttpRequestData &HRD, WiFiClient *wc,
if(HRD.getParams.count() == 0)//We send every IO mode
{
sprintf(buffer,"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{\"status\":\"ok\",\"P0\":\"%s\",\"P1\":\"%s\",\"P2\":\"%s\",\"P3\":\"%s\",\"P4\":\"%s\",\"P5\":\"%s\",\"P6\":\"%s\",\"P7\":\"%s\"}",ioMode[0] ? OUT:IN,ioMode[1] ? OUT:IN,ioMode[2] ? OUT:IN,ioMode[3] ? OUT:IN,ioMode[4] ? OUT:IN,ioMode[5] ? OUT:IN,ioMode[6] ? OUT:IN,ioMode[7] ? OUT:IN);
sprintf(buffer,"{\"status\":\"ok\",\"P0\":\"%s\",\"P1\":\"%s\",\"P2\":\"%s\",\"P3\":\"%s\",\"P4\":\"%s\",\"P5\":\"%s\",\"P6\":\"%s\",\"P7\":\"%s\"}",
ioMode[0] ? OUT:IN,
ioMode[1] ? OUT:IN,
ioMode[2] ? OUT:IN,
ioMode[3] ? OUT:IN,
ioMode[4] ? OUT:IN,
ioMode[5] ? OUT:IN,
ioMode[6] ? OUT:IN,
ioMode[7] ? OUT:IN);
}
else
{
strcpy(buffer,"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{\"status\":\"ok\"");
strcpy(buffer,"{\"status\":\"ok\"");
if(HRD.getParams("P0") != NULL)
{
sprintf(helperBuffer,",\"P0\":\"%s\"",ioMode[0] ? OUT:IN);
@ -488,6 +505,7 @@ boolean ioGetModeApi(WEBServer<WEBClient>::HttpRequestData &HRD, WiFiClient *wc,
strcat(buffer," }");
}
WEBServer<WEBClient>::sendHTTPHeader(wc, HttpConstants::httpMIMETypeToString(HttpConstants::APPLICATION_JSON), strlen(buffer));
wc->print(buffer);
return true;
}
@ -533,8 +551,9 @@ boolean ioSetModeApi(WEBServer<WEBClient>::HttpRequestData &HRD, WiFiClient *wc,
}
p->getIoManager().getPcf().getPinModeAll(ioMode);//We retrieve the IO modes aka INPUT or OUTPUT
sprintf(buffer,"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{\"status\":\"ok\",\"P0\":\"%s\",\"P1\":\"%s\",\"P2\":\"%s\",\"P3\":\"%s\",\"P4\":\"%s\",\"P5\":\"%s\",\"P6\":\"%s\",\"P7\":\"%s\"}",ioMode[0] ? OUT:IN,ioMode[1] ? OUT:IN,ioMode[2] ? OUT:IN,ioMode[3] ? OUT:IN,ioMode[4] ? OUT:IN,ioMode[5] ? OUT:IN,ioMode[6] ? OUT:IN,ioMode[7] ? OUT:IN);
sprintf(buffer,"{\"status\":\"ok\",\"P0\":\"%s\",\"P1\":\"%s\",\"P2\":\"%s\",\"P3\":\"%s\",\"P4\":\"%s\",\"P5\":\"%s\",\"P6\":\"%s\",\"P7\":\"%s\"}",ioMode[0] ? OUT:IN,ioMode[1] ? OUT:IN,ioMode[2] ? OUT:IN,ioMode[3] ? OUT:IN,ioMode[4] ? OUT:IN,ioMode[5] ? OUT:IN,ioMode[6] ? OUT:IN,ioMode[7] ? OUT:IN);
WEBServer<WEBClient>::sendHTTPHeader(wc, HttpConstants::httpMIMETypeToString(HttpConstants::APPLICATION_JSON), strlen(buffer));
wc->print(buffer);
return true;
}