mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-31 20:39:14 +03:00
HttpGet
This commit is contained in:
100
src/modules/exec/HttpGet/HttpGet.cpp
Normal file
100
src/modules/exec/HttpGet/HttpGet.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
#include "Global.h"
|
||||
#include "classes/IoTItem.h"
|
||||
|
||||
class HttpGet : public IoTItem
|
||||
{
|
||||
public:
|
||||
HttpGet(String parameters) : IoTItem(parameters)
|
||||
{
|
||||
}
|
||||
|
||||
void sendHttpPOST(String url, String msg)
|
||||
{
|
||||
if (WiFi.status() == WL_CONNECTED)
|
||||
{
|
||||
|
||||
WiFiClient client;
|
||||
HTTPClient http;
|
||||
http.begin(client, url);
|
||||
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||
String httpRequestData = msg;
|
||||
int httpResponseCode = http.POST(httpRequestData);
|
||||
String payload = http.getString();
|
||||
SerialPrint("<-", F("HttpPOST"), "URL: " + url + ", msg: " + msg);
|
||||
SerialPrint("->", F("HttpPOST"), "URL: " + url + ", server: " + httpResponseCode);
|
||||
|
||||
if (httpResponseCode > 0)
|
||||
{
|
||||
value.valS = payload;
|
||||
SerialPrint("->", F("HttpPOST"), "msg from server: " + (String)payload.c_str());
|
||||
value.valS = payload;
|
||||
regEvent(value.valS, "HttpGet");
|
||||
}
|
||||
http.end();
|
||||
}
|
||||
}
|
||||
void sendHttpGET(String url)
|
||||
{
|
||||
WiFiClient client;
|
||||
HTTPClient http;
|
||||
#if defined ESP8266
|
||||
if (!http.begin(client, url))
|
||||
{
|
||||
#elif defined ESP32
|
||||
if (!http.begin(url))
|
||||
{
|
||||
#endif
|
||||
|
||||
SerialPrint("I", F("HttpGet"), "connection failed ");
|
||||
}
|
||||
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||
int httpResponseCode = http.GET();
|
||||
String payload = http.getString();
|
||||
SerialPrint("<-", F("HttpGET"), "URL: " + url);
|
||||
SerialPrint("->", F("HttpGET"), "URL: " + url + ", server: " + httpResponseCode);
|
||||
if (httpResponseCode > 0)
|
||||
{
|
||||
value.valS = payload;
|
||||
SerialPrint("->", F("HttpGET"), "msg from server: " + (String)payload.c_str());
|
||||
value.valS = payload;
|
||||
regEvent(value.valS, "HttpGet");
|
||||
}
|
||||
http.end();
|
||||
}
|
||||
|
||||
IoTValue execute(String command, std::vector<IoTValue> ¶m)
|
||||
{
|
||||
if (param.size() > 0)
|
||||
{
|
||||
if (command == "get")
|
||||
{
|
||||
if (param.size())
|
||||
{
|
||||
sendHttpGET(param[0].valS);
|
||||
}
|
||||
}
|
||||
else if (command == "post")
|
||||
{
|
||||
if (param.size())
|
||||
{
|
||||
sendHttpPOST(param[0].valS, param[1].valS);
|
||||
}
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
~HttpGet(){};
|
||||
};
|
||||
|
||||
void *getAPI_HttpGet(String subtype, String param)
|
||||
{
|
||||
if (subtype == F("HttpGet"))
|
||||
{
|
||||
return new HttpGet(param);
|
||||
}
|
||||
else
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user