исправление бага модуля http get

This commit is contained in:
IoT Manager
2023-10-08 19:01:51 +02:00
parent e82df3f575
commit 8efd1a1578

View File

@@ -1,18 +1,12 @@
#include "Global.h" #include "Global.h"
#include "classes/IoTItem.h" #include "classes/IoTItem.h"
class HttpGet : public IoTItem class HttpGet : public IoTItem {
{
public: public:
HttpGet(String parameters) : IoTItem(parameters) HttpGet(String parameters) : IoTItem(parameters) {}
{
}
void sendHttpPOST(String url, String msg)
{
if (isNetworkActive())
{
void sendHttpPOST(String url, String msg) {
if (isNetworkActive()) {
WiFiClient client; WiFiClient client;
HTTPClient http; HTTPClient http;
http.begin(client, url); http.begin(client, url);
@@ -23,26 +17,23 @@ public:
SerialPrint("<-", F("HttpPOST"), "URL: " + url + ", msg: " + msg); SerialPrint("<-", F("HttpPOST"), "URL: " + url + ", msg: " + msg);
SerialPrint("->", F("HttpPOST"), "URL: " + url + ", server: " + httpResponseCode); SerialPrint("->", F("HttpPOST"), "URL: " + url + ", server: " + httpResponseCode);
if (httpResponseCode > 0) if (httpResponseCode > 0) {
{
value.valS = payload; value.valS = payload;
value.isDecimal = false;
SerialPrint("->", F("HttpPOST"), "msg from server: " + (String)payload.c_str()); SerialPrint("->", F("HttpPOST"), "msg from server: " + (String)payload.c_str());
value.valS = payload;
regEvent(value.valS, "HttpGet"); regEvent(value.valS, "HttpGet");
} }
http.end(); http.end();
} }
} }
void sendHttpGET(String url)
{ void sendHttpGET(String url) {
WiFiClient client; WiFiClient client;
HTTPClient http; HTTPClient http;
#if defined ESP8266 #if defined ESP8266
if (!http.begin(client, url)) if (!http.begin(client, url)) {
{
#elif defined ESP32 #elif defined ESP32
if (!http.begin(url)) if (!http.begin(url)) {
{
#endif #endif
SerialPrint("I", F("HttpGet"), "connection failed "); SerialPrint("I", F("HttpGet"), "connection failed ");
@@ -52,31 +43,23 @@ public:
String payload = http.getString(); String payload = http.getString();
SerialPrint("<-", F("HttpGET"), "URL: " + url); SerialPrint("<-", F("HttpGET"), "URL: " + url);
SerialPrint("->", F("HttpGET"), "URL: " + url + ", server: " + httpResponseCode); SerialPrint("->", F("HttpGET"), "URL: " + url + ", server: " + httpResponseCode);
if (httpResponseCode > 0) if (httpResponseCode > 0) {
{
value.valS = payload; value.valS = payload;
value.isDecimal = false;
SerialPrint("->", F("HttpGET"), "msg from server: " + (String)payload.c_str()); SerialPrint("->", F("HttpGET"), "msg from server: " + (String)payload.c_str());
value.valS = payload;
regEvent(value.valS, "HttpGet"); regEvent(value.valS, "HttpGet");
} }
http.end(); http.end();
} }
IoTValue execute(String command, std::vector<IoTValue> &param) IoTValue execute(String command, std::vector<IoTValue> &param) {
{ if (param.size() > 0) {
if (param.size() > 0) if (command == "get") {
{ if (param.size()) {
if (command == "get")
{
if (param.size())
{
sendHttpGET(param[0].valS); sendHttpGET(param[0].valS);
} }
} } else if (command == "post") {
else if (command == "post") if (param.size()) {
{
if (param.size())
{
sendHttpPOST(param[0].valS, param[1].valS); sendHttpPOST(param[0].valS, param[1].valS);
} }
} }
@@ -87,14 +70,10 @@ public:
~HttpGet(){}; ~HttpGet(){};
}; };
void *getAPI_HttpGet(String subtype, String param) void *getAPI_HttpGet(String subtype, String param) {
{ if (subtype == F("HttpGet")) {
if (subtype == F("HttpGet"))
{
return new HttpGet(param); return new HttpGet(param);
} } else {
else
{
return nullptr; return nullptr;
} }
} }